WP_REST_Posts_Controller::delete_item PHP Method

delete_item() public method

Deletes a single post.
Since: 4.7.0
public delete_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.
    public function delete_item($request)
    {
        $id = (int) $request['id'];
        $force = (bool) $request['force'];
        $post = get_post($id);
        if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) {
            return new WP_Error('rest_post_invalid_id', __('Invalid post ID.'), array('status' => 404));
        }
        $supports_trash = EMPTY_TRASH_DAYS > 0;
        if ('attachment' === $post->post_type) {
            $supports_trash = $supports_trash && MEDIA_TRASH;
        }
        /**
         * Filters whether a post is trashable.
         *
         * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
         *
         * Pass false to disable trash support for the post.
         *
         * @since 4.7.0
         *
         * @param bool    $supports_trash Whether the post type support trashing.
         * @param WP_Post $post           The Post object being considered for trashing support.
         */
        $supports_trash = apply_filters("rest_{$this->post_type}_trashable", $supports_trash, $post);
        if (!$this->check_delete_permission($post)) {
            return new WP_Error('rest_user_cannot_delete_post', __('Sorry, you are not allowed to delete this post.'), array('status' => rest_authorization_required_code()));
        }
        $request->set_param('context', 'edit');
        // If we're forcing, then delete permanently.
        if ($force) {
            $previous = $this->prepare_item_for_response($post, $request);
            $result = wp_delete_post($id, true);
            $response = new WP_REST_Response();
            $response->set_data(array('deleted' => true, 'previous' => $previous->get_data()));
        } else {
            // If we don't support trashing for this type, error out.
            if (!$supports_trash) {
                return new WP_Error('rest_trash_not_supported', __('The post does not support trashing. Set force=true to delete.'), array('status' => 501));
            }
            // Otherwise, only trash if we haven't already.
            if ('trash' === $post->post_status) {
                return new WP_Error('rest_already_trashed', __('The post has already been deleted.'), array('status' => 410));
            }
            // (Note that internally this falls through to `wp_delete_post` if
            // the trash is disabled.)
            $result = wp_trash_post($id);
            $post = get_post($id);
            $response = $this->prepare_item_for_response($post, $request);
        }
        if (!$result) {
            return new WP_Error('rest_cannot_delete', __('The post cannot be deleted.'), array('status' => 500));
        }
        /**
         * Fires immediately after a single post is deleted or trashed via the REST API.
         *
         * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
         *
         * @since 4.7.0
         *
         * @param object           $post     The deleted or trashed post.
         * @param WP_REST_Response $response The response data.
         * @param WP_REST_Request  $request  The request sent to the API.
         */
        do_action("rest_delete_{$this->post_type}", $post, $response, $request);
        return $response;
    }