'object', 'additionalProperties' => false, 'required' => array( 'id' ), 'properties' => array( 'id' => array( 'type' => 'integer', 'description' => __( 'The post id to delete.', 'gripmcp' ), 'minimum' => 1, ), 'force' => array( 'type' => 'boolean', 'description' => __( 'If true, permanently delete instead of trashing. Irreversible.', 'gripmcp' ), 'default' => false, ), ), ); } protected function required_capability(): string { return 'delete_posts'; } protected function is_destructive(): bool { return true; } /** * @param array $arguments Validated arguments. * @param Request_Context $context Context. * @return true|\WP_Error */ public function preflight( array $arguments, Request_Context $context ) { $id = (int) ( $arguments['id'] ?? 0 ); $post = get_post( $id ); if ( ! $post instanceof \WP_Post ) { return new \WP_Error( 'gripmcp_not_found', sprintf( /* translators: %d: post id. */ __( 'No post found with id %d.', 'gripmcp' ), $id ) ); } if ( ! $context->user_can( 'delete_post', $id ) ) { return new \WP_Error( 'gripmcp_forbidden', __( 'You do not have permission to delete this post.', 'gripmcp' ) ); } // An order is trashed or deleted by WooCommerce, with its refunds, // items and customer records. $change = Post_Access::change_error( $post, Post_Access::CHANGE_DELETE ); if ( '' !== $change ) { return new \WP_Error( 'gripmcp_forbidden', $change ); } // Without force: the trash or nothing, as the REST API does. if ( empty( $arguments['force'] ) ) { $problem = Post_Trash::problem( $post ); if ( '' !== $problem ) { return new \WP_Error( 'gripmcp_no_trash', $problem ); } } return true; } protected function run( array $arguments, Request_Context $context ): Tool_Result { $check = $this->preflight( $arguments, $context ); if ( is_wp_error( $check ) ) { return $this->fail( $check->get_error_message() ); } $id = (int) $arguments['id']; $force = ! empty( $arguments['force'] ); $result = $force ? wp_delete_post( $id, true ) : wp_trash_post( $id ); if ( ! $result ) { return $this->fail( sprintf( /* translators: %d: post id. */ __( 'Failed to delete post %d.', 'gripmcp' ), $id ) ); } return $this->data( array( 'id' => $id, 'deleted' => true, 'force' => $force, ), $force ? sprintf( /* translators: %d: post id. */ __( 'Permanently deleted post %d.', 'gripmcp' ), $id ) : sprintf( /* translators: %d: post id. */ __( 'Moved post %d to trash.', 'gripmcp' ), $id ) ); } }