Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 58 additions & 35 deletions src/features/class-block-audit-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public function boot(): void {
*
* ## OPTIONS
*
* [--<field>=<value>]
* [--<field>=<value>]
* : One or more args to pass to WP_Query except for 'order', 'orderby', or 'paged'.
*
* [--block_name=<block_name>...]
* : One or more block names to report on (comma separated). (Default: all block types).
*
* [--format=<format>]
* : Render output in a particular format.
* ---
Expand All @@ -63,30 +66,40 @@ 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<string> $args
* @phpstan-param array<string, string> $assoc_args
Expand Down Expand Up @@ -128,23 +141,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', '' ) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the Mantle package providing this function be included in composer.json?

->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;
}

Expand Down
Loading