From 04cf5797631e746cfd4bcaae11aa687ad6cb33be Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Wed, 4 Sep 2024 13:54:12 +0200 Subject: [PATCH] Categories Block: Add iAPI directive for client-side routing (#64907) Enable client-side navigation for the Categories block, when used within the Query Loop block. Co-authored-by: ockham Co-authored-by: gziolo Co-authored-by: michalczaplinski Co-authored-by: luisherranz Co-authored-by: dmsnell --- packages/block-library/src/categories/block.json | 1 + packages/block-library/src/categories/index.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index f192087dfb4617..460fbef92fdab2 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 062130e85010a5..5c0cc63415cb78 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; @@ -54,6 +57,14 @@ function render_block_core_categories( $attributes ) { $wrapper_markup = '
    %2$s
'; $items_markup = wp_list_categories( $args ); $type = 'list'; + + 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(); + } } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );