From 1c38cea91ee4693fbd08e0f44fe8af0405f6b2bf Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Thu, 2 Oct 2025 23:50:31 -0300 Subject: [PATCH 1/2] Allow command runner to choose specific blocks to report --- src/features/class-block-audit-command.php | 100 +++++++++++++-------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/src/features/class-block-audit-command.php b/src/features/class-block-audit-command.php index 5234c48..312be8c 100644 --- a/src/features/class-block-audit-command.php +++ b/src/features/class-block-audit-command.php @@ -40,9 +40,12 @@ public function boot(): void { * * ## OPTIONS * - * [--=] + * [--=] * : One or more args to pass to WP_Query except for 'order', 'orderby', or 'paged'. * + * [--block_name=...] + * : One or more block names to report on (comma separated). (Default: all block types). + * * [--format=] * : Render output in a particular format. * --- @@ -63,36 +66,45 @@ public function boot(): void { * * ## EXAMPLES * - * $ wp block-audit run --post_type=post,page - * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ - * | Block Name | Count | Example URL | Post Types | Details | - * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ - * | core/archives | 3 | https://www.example.com/2023/01/13/widgets-block-category/ | ["post"] | | - * | core/button | 12 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | {"align":{"left":2,"center":1,"right":1}} | - * | core/code | 2 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | - * | core/column | 40 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | - * | core/columns | 13 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | {"align":{"wide":2,"full":1}} | - * | core/cover | 21 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"left":1,"center":2,"full":1,"wide":2}} | - * | core/file | 3 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | | - * | core/gallery | 10 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | | - * | core/group | 25 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | - * | core/heading | 23 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post","page"] | {"H1":2,"H2":11,"H3":4,"H4":2,"H5":2,"H6":2} | - * | core/html | 2 | https://www.example.com/2023/01/13/widgets-block-category/ | ["post"] | | - * | core/image | 19 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"center":2,"left":2,"right":3,"none":1,"wide":1,"full":1}} | - * | core/list | 9 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | - * | core/list-item | 6 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | - * | core/media-text | 6 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"full":1}} | - * | core/paragraph | 262 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post","page"] | {"align":{"center":16,"right":1,"left":1}} | - * | core/pullquote | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | - * | core/spacer | 4 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | - * | core/table | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | - * +-----------------------------------+-------+---------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * # Audit specific blocks in posts and pages. + * $ wp block-audit run --post_type=post,page --block_name=core/paragraph,core/table + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * | Block Name | Count | Example URL | Post Types | Details | + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * | core/paragraph | 262 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post","page"] | {"align":{"center":16,"right":1,"left":1}} | + * | core/table | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * + * # Audit all blocks in posts and pages. + * $ wp block-audit run --post_type=post,page + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * | Block Name | Count | Example URL | Post Types | Details | + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ + * | core/archives | 3 | https://www.example.com/2023/01/13/widgets-block-category/ | ["post"] | | + * | core/button | 12 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | {"align":{"left":2,"center":1,"right":1}} | + * | core/code | 2 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * | core/column | 40 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | + * | core/columns | 13 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | {"align":{"wide":2,"full":1}} | + * | core/cover | 21 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"left":1,"center":2,"full":1,"wide":2}} | + * | core/file | 3 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | | + * | core/gallery | 10 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | | + * | core/group | 25 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | + * | core/heading | 23 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post","page"] | {"H1":2,"H2":11,"H3":4,"H4":2,"H5":2,"H6":2} | + * | core/html | 2 | https://www.example.com/2023/01/13/widgets-block-category/ | ["post"] | | + * | core/image | 19 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"center":2,"left":2,"right":3,"none":1,"wide":1,"full":1}} | + * | core/list | 9 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * | core/list-item | 6 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * | core/media-text | 6 | https://www.example.com/2023/01/13/media-category-blocks/ | ["post"] | {"align":{"full":1}} | + * | core/paragraph | 262 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post","page"] | {"align":{"center":16,"right":1,"left":1}} | + * | core/pullquote | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * | core/spacer | 4 | https://www.example.com/2023/01/13/design-category-blocks/ | ["post"] | | + * | core/table | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | + * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ * - * @phpstan-param array $args - * @phpstan-param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. + * @throws \Exception If invalid method callback passed to bulk_task. */ public function run( array $args, array $assoc_args = [] ): void { global $wpdb; @@ -128,23 +140,33 @@ public function run( array $args, array $assoc_args = [] ): void { ], $user_query_args, ); + $query_args = self::process_csv_arguments_to_arrays( $query_args ); if ( isset( $query_args['post_type'] ) && is_string( $query_args['post_type'] ) && 'any' !== $query_args['post_type'] ) { $query_args['post_type'] = explode( ',', $query_args['post_type'] ); } + $block_names = \Mantle\Support\Helpers\mixed( get_flag_value( $assoc_args, 'block_name', '' ) ) + ->stringable() + ->explode( ',' ) + ->filter() + ->all(); + + $block_query_args = [ + 'flatten' => true, + 'skip_empty_blocks' => false, // For counting classic blocks. + ]; + + if ( ! empty( $block_names ) ) { + $block_query_args['name'] = $block_names; + } + $bulk_task->run( $query_args, - function ( \WP_Post $post ) use ( &$out ) { - $blocks = match_blocks( - $post, - [ - 'flatten' => true, - 'skip_empty_blocks' => false, // For counting classic blocks. - ], - ); - - if ( ! is_iterable( $blocks ) ) { + function ( WP_Post $post ) use ( &$out, $block_query_args ) { + $blocks = match_blocks( $post, $block_query_args ); + + if ( ! is_iterable( $blocks ) || empty( $blocks ) ) { return; } From f94f67e5aa5cee8e89f3f90a386ced0a6dcbdb6c Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Fri, 3 Oct 2025 08:33:26 -0300 Subject: [PATCH 2/2] Making linting happy --- src/features/class-block-audit-command.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/features/class-block-audit-command.php b/src/features/class-block-audit-command.php index 312be8c..ace9bd0 100644 --- a/src/features/class-block-audit-command.php +++ b/src/features/class-block-audit-command.php @@ -101,10 +101,11 @@ public function boot(): void { * | core/table | 4 | https://www.example.com/2023/01/13/text-category-blocks/ | ["post"] | | * +-----------------------------------+-------+------------------------------------------------------------+-----------------+----------------------------------------------------------------------+ * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. + * @phpstan-param array $args + * @phpstan-param array $assoc_args * - * @throws \Exception If invalid method callback passed to bulk_task. + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. */ public function run( array $args, array $assoc_args = [] ): void { global $wpdb;