diff --git a/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php b/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php index fe816e4ea..9d9724649 100644 --- a/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php +++ b/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php @@ -56,6 +56,33 @@ class REST_Bulkedit_Controller extends REST_Controller { ), ) ); + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[0-9a-f]+)/trash', + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array($this, 'trash_items'), + 'permission_callback' => array($this, 'bulk_edit_permissions_check'), + ), + ) + ); + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[0-9a-f]+)/untrash', + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array($this, 'untrash_items'), + 'permission_callback' => array($this, 'bulk_edit_permissions_check'), + ), + ) + ); + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[0-9a-f]+)/delete_items', + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array($this, 'delete_items'), + 'permission_callback' => array($this, 'bulk_edit_permissions_check'), + ), + ) + ); register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[0-9a-f]+)/set', array( array( @@ -196,6 +223,63 @@ class REST_Bulkedit_Controller extends REST_Controller { } + public function trash_items($request) { + $group_id = $request['group_id']; + + $args = ['id' => $group_id]; + + $bulk = new \Tainacan\Bulk_Edit($args); + + $action = $bulk->trash_items(); + + if ( is_wp_error($action) ) { + return new \WP_REST_Response([ + 'error_message' => $action->get_error_message(), + ], 400); + } else { + return new \WP_REST_Response($action, 200); + } + + } + + public function untrash_items($request) { + $group_id = $request['group_id']; + + $args = ['id' => $group_id]; + + $bulk = new \Tainacan\Bulk_Edit($args); + + $action = $bulk->untrash_items(); + + if ( is_wp_error($action) ) { + return new \WP_REST_Response([ + 'error_message' => $action->get_error_message(), + ], 400); + } else { + return new \WP_REST_Response($action, 200); + } + + } + + public function delete_items($request) { + $group_id = $request['group_id']; + + $args = ['id' => $group_id]; + + $bulk = new \Tainacan\Bulk_Edit($args); + + $action = $bulk->delete_items(); + + if ( is_wp_error($action) ) { + return new \WP_REST_Response([ + 'error_message' => $action->get_error_message(), + ], 400); + } else { + return new \WP_REST_Response($action, 200); + } + + } + private function generic_action($method, $request, $keys = ['value']) { $body = json_decode($request->get_body(), true); diff --git a/src/classes/class-tainacan-bulk-edit.php b/src/classes/class-tainacan-bulk-edit.php index aac4cec9e..b6c0a074e 100644 --- a/src/classes/class-tainacan-bulk-edit.php +++ b/src/classes/class-tainacan-bulk-edit.php @@ -299,7 +299,7 @@ class Bulk_Edit { // restore status - $query_restore = "UPDATE $wpdb->posts SET post_status = (SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_status' AND post_id = ID) WHERE ID IN ($select_q)"; + $query_restore = "UPDATE $wpdb->posts SET post_status = (SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_status' AND post_id = ID) WHERE ID IN ($select_q) AND post_status = 'trash'"; $query_delete_meta1 = "DELETE FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_status' AND post_id IN ( SELECT implicitTemp.post_id FROM ($select_q) implicitTemp )"; $query_delete_meta2 = "DELETE FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND post_id IN ( SELECT implicitTemp.post_id FROM ($select_q) implicitTemp )";