*/ protected function ability(): array { return array( 'name' => 'blocks/remove-block', 'args' => array( 'label' => __( 'Remove Block', 'acrossai-abilities-manager' ), 'description' => __( 'Remove the Gutenberg block at the given canonical path from a post.', 'acrossai-abilities-manager' ), 'category' => 'acrossai-block', 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => static function (): bool { return current_user_can( 'manage_options' ) && current_user_can( 'edit_posts' ); }, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer', 'minimum' => 1, ), 'path' => array( 'type' => 'array', 'minItems' => 1, 'items' => array( 'type' => 'integer', 'minimum' => 0, ), ), ), 'required' => array( 'post_id', 'path' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'success' => array( 'type' => 'boolean' ), 'post_id' => array( 'type' => 'integer' ), 'removed' => array( 'type' => 'object' ), 'message' => array( 'type' => 'string' ), ), 'required' => array( 'success' ), 'additionalProperties' => false, ), 'meta' => array( 'acrossai' => array( 'tab_group' => 'blocks', 'sub_group' => 'post-blocks', 'sub_group_label' => __( 'Posts', 'acrossai-abilities-manager' ), ), 'show_in_rest' => true, 'mcp' => array( 'public' => false, 'type' => 'tool', ), 'annotations' => array( 'readonly' => false, 'destructive' => true, 'idempotent' => false, ), ), ), ); } /** * Execute the ability. * * @param array $input Ability input payload. * @return array */ /** * Where a caller should look before running this. * * @since 0.0.34 * @return array> */ protected function suggested_abilities(): array { return array( array( 'slug' => 'content/inspect-post-builder', 'reason' => __( 'Run first on an existing post. A page built with Elementor or another page builder keeps its layout outside post_content, so this write can report success, change nothing a visitor sees, and be reverted the next time the page is saved in the builder.', 'acrossai-abilities-manager' ), ), ); } public function execute( array $input = array() ): array { $post_id = absint( $input['post_id'] ?? 0 ); $path = self::sanitize_path( $input['path'] ?? array() ); if ( array() === $path ) { return self::fail( $post_id, 'invalid_path', __( 'A non-empty path is required.', 'acrossai-abilities-manager' ) ); } $blocks = Block_Tree::parse_post_blocks( $post_id, 'edit' ); if ( is_wp_error( $blocks ) ) { return self::fail( $post_id, (string) $blocks->get_error_code(), (string) $blocks->get_error_message() ); } $removed = Block_Tree::remove_at_path( $blocks, $path ); if ( null === $removed ) { return self::fail( $post_id, 'invalid_path', __( 'Path does not resolve.', 'acrossai-abilities-manager' ) ); } $saved = wp_update_post( array( 'ID' => $post_id, 'post_content' => serialize_blocks( $blocks ), ), true ); if ( is_wp_error( $saved ) ) { return self::fail( $post_id, (string) $saved->get_error_code(), (string) $saved->get_error_message() ); } return array( 'success' => true, 'post_id' => $post_id, 'removed' => $removed, /* translators: %d: post ID */ 'message' => sprintf( __( 'Removed block from post #%d.', 'acrossai-abilities-manager' ), $post_id ), ); } /** * Coerce a raw path input to int[]. * * @param mixed $raw * @return int[] */ private static function sanitize_path( $raw ): array { if ( ! is_array( $raw ) ) { return array(); } $out = array(); foreach ( $raw as $item ) { if ( is_int( $item ) && $item >= 0 ) { $out[] = $item; } elseif ( is_string( $item ) && ctype_digit( $item ) ) { $out[] = (int) $item; } else { return array(); } } return $out; } /** * Build a failure envelope. * * @param int $post_id * @param string $code * @param string $message * @return array */ private static function fail( int $post_id, string $code, string $message ): array { return array( 'success' => false, 'post_id' => $post_id, 'message' => $message, 'error_code' => $code, ); } }