From 08b357234fe10fd0723a39b1b256fe5b381f9e5b Mon Sep 17 00:00:00 2001 From: Alin Pandichi Date: Thu, 13 Sep 2018 19:17:40 +0300 Subject: [PATCH 1/2] Add basic support for deleting acf fields --- .../class-acf-to-rest-api-controller.php | 31 +++++++++++++++++-- ...ass-acf-to-rest-api-options-controller.php | 5 +++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/v3/lib/endpoints/class-acf-to-rest-api-controller.php b/v3/lib/endpoints/class-acf-to-rest-api-controller.php index ff1e5f4..7972ed6 100644 --- a/v3/lib/endpoints/class-acf-to-rest-api-controller.php +++ b/v3/lib/endpoints/class-acf-to-rest-api-controller.php @@ -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( @@ -109,6 +114,26 @@ 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 )['acf']; + $id = $this->acf->get_id( $request ); + if ( is_array( $fields ) && count( $fields ) > 0 ) { + foreach ( $fields 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 ) { @@ -123,7 +148,7 @@ public function update_item( $request ) { update_field( $field['key'], $value, $item['id'] ); } else { do_action( 'acf/update_value', $value, $item['id'], $field ); - } + } } } } @@ -136,13 +161,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 ); } diff --git a/v3/lib/endpoints/class-acf-to-rest-api-options-controller.php b/v3/lib/endpoints/class-acf-to-rest-api-options-controller.php index a64bd85..c587eb2 100644 --- a/v3/lib/endpoints/class-acf-to-rest-api-options-controller.php +++ b/v3/lib/endpoints/class-acf-to-rest-api-options-controller.php @@ -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' ), + ), ) ); } From e2f5c9c1b65f0402fcd989bd7fd90a8e87da253b Mon Sep 17 00:00:00 2001 From: Alin Pandichi Date: Fri, 14 Sep 2018 11:40:43 +0300 Subject: [PATCH 2/2] Check if acf field is available before accessing it --- .../class-acf-to-rest-api-controller.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/v3/lib/endpoints/class-acf-to-rest-api-controller.php b/v3/lib/endpoints/class-acf-to-rest-api-controller.php index 7972ed6..4274700 100644 --- a/v3/lib/endpoints/class-acf-to-rest-api-controller.php +++ b/v3/lib/endpoints/class-acf-to-rest-api-controller.php @@ -119,16 +119,19 @@ public function delete_item_permissions_check( $request ) { } public function delete_item( $request ) { - $fields = $this->acf->get_fields( $request )['acf']; - $id = $this->acf->get_id( $request ); - if ( is_array( $fields ) && count( $fields ) > 0 ) { - foreach ( $fields as $key => $value ) { - if ( function_exists( 'delete_field' ) ) { - delete_field( $key, $id ); + $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_REST_Response( $fields, 200 ); + } } return new WP_Error( 'cant_delete_item', __( 'Cannot delete item', 'acf-to-rest-api' ), array( 'status' => 500, 'fields' => $fields ) );