From f2afb10163e951020d7f17dca20c87aca1277df7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 29 Aug 2024 16:45:34 +0200 Subject: [PATCH 1/3] Categories Block: Add iAPI directive for client-side routing --- packages/block-library/src/categories/index.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 062130e85010a..26b55d1c2280d 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -54,6 +54,12 @@ function render_block_core_categories( $attributes ) { $wrapper_markup = ''; $items_markup = wp_list_categories( $args ); $type = 'list'; + + $p = new WP_HTML_Tag_Processor( $items_markup ); + while ( $p->next_tag( 'a' ) ) { + $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); + } + $items_markup = $p->get_updated_html(); } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); From d9dc1b7941fbac39320017625df5d9fb8d5eaa73 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 29 Aug 2024 21:28:21 +0200 Subject: [PATCH 2/3] Respect enhancedPagination context --- .../block-library/src/categories/block.json | 1 + packages/block-library/src/categories/index.php | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index f192087dfb461..460fbef92fdab 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -36,6 +36,7 @@ "default": true } }, + "usesContext": [ "enhancedPagination" ], "supports": { "align": true, "html": false, diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 26b55d1c2280d..5ec43a4940d0c 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -9,12 +9,15 @@ * Renders the `core/categories` block on server. * * @since 5.0.0 + * @since 6.7.0 Enable client-side rendering if enhancedPagination context is true. * - * @param array $attributes The block attributes. + * @param array $attributes The block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. * * @return string Returns the categories list/dropdown markup. */ -function render_block_core_categories( $attributes ) { +function render_block_core_categories( $attributes, $content, $block ) { static $block_id = 0; ++$block_id; @@ -55,11 +58,13 @@ function render_block_core_categories( $attributes ) { $items_markup = wp_list_categories( $args ); $type = 'list'; - $p = new WP_HTML_Tag_Processor( $items_markup ); - while ( $p->next_tag( 'a' ) ) { - $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); + if ( ! empty ( $block->context['enhancedPagination'] ) ) { + $p = new WP_HTML_Tag_Processor( $items_markup ); + while ( $p->next_tag( 'a' ) ) { + $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); + } + $items_markup = $p->get_updated_html(); } - $items_markup = $p->get_updated_html(); } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); From 766f88815504c383935b3c60abf42010e00f9989 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Sep 2024 12:01:42 +0200 Subject: [PATCH 3/3] WPCS --- packages/block-library/src/categories/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 5ec43a4940d0c..5c0cc63415cb7 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -58,7 +58,7 @@ function render_block_core_categories( $attributes, $content, $block ) { $items_markup = wp_list_categories( $args ); $type = 'list'; - if ( ! empty ( $block->context['enhancedPagination'] ) ) { + if ( ! empty( $block->context['enhancedPagination'] ) ) { $p = new WP_HTML_Tag_Processor( $items_markup ); while ( $p->next_tag( 'a' ) ) { $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );