Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions v3/lib/endpoints/class-acf-to-rest-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function register_routes() {
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
),
) );

register_rest_route( $this->namespace, '/' . $this->rest_base, array(
Expand Down Expand Up @@ -109,6 +114,29 @@ public function update_item_permissions_check( $request ) {
return apply_filters( 'acf/rest_api/item_permissions/update', current_user_can( 'edit_posts' ), $request, $this->type );
}

public function delete_item_permissions_check( $request ) {
return apply_filters( 'acf/rest_api/item_permissions/delete', current_user_can( 'delete_posts' ), $request, $this->type );
}

public function delete_item( $request ) {
$fields = $this->acf->get_fields( $request );
if ( $fields && isset( $fields['acf'] ) ) {
$fields_acf = $fields['acf'];
$id = $this->acf->get_id( $request );
if ( is_array( $fields_acf ) && count( $fields_acf ) > 0 ) {
foreach ( $fields_acf as $key => $value ) {
if ( function_exists( 'delete_field' ) ) {
delete_field( $key, $id );
}
}

return new WP_REST_Response( $fields, 200 );
}
}

return new WP_Error( 'cant_delete_item', __( 'Cannot delete item', 'acf-to-rest-api' ), array( 'status' => 500, 'fields' => $fields ) );
}

public function update_item( $request ) {
$item = $this->prepare_item_for_database( $request );
if ( is_array( $item ) && count( $item ) > 0 ) {
Expand All @@ -123,7 +151,7 @@ public function update_item( $request ) {
update_field( $field['key'], $value, $item['id'] );
} else {
do_action( 'acf/update_value', $value, $item['id'], $field );
}
}
}
}
}
Expand All @@ -136,13 +164,13 @@ public function update_item( $request ) {

public function rest_insert( $object, $request, $creating ) {
if ( $request instanceof WP_REST_Request ) {
$id = $this->acf->get_id( $object );
$id = $this->acf->get_id( $object );
if ( ! $id ) {
$id = $this->acf->get_id( $request );
}
$request->set_param( 'id', $id );
}

return $this->update_item( $request );
}

Expand Down
5 changes: 5 additions & 0 deletions v3/lib/endpoints/class-acf-to-rest-api-options-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function register_routes() {
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
),
) );
}

Expand Down