Skip to content

Commit

Permalink
Categories Block: Add iAPI directive for client-side routing (#64907)
Browse files Browse the repository at this point in the history
Enable client-side navigation for the Categories block, when used within the Query Loop block.

Co-authored-by: ockham <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: michalczaplinski <[email protected]>
Co-authored-by: luisherranz <[email protected]>
Co-authored-by: dmsnell <[email protected]>
  • Loading branch information
6 people authored Sep 4, 2024
1 parent 6accdd3 commit 04cf579
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"default": true
}
},
"usesContext": [ "enhancedPagination" ],
"supports": {
"align": true,
"html": false,
Expand Down
15 changes: 13 additions & 2 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,6 +57,14 @@ function render_block_core_categories( $attributes ) {
$wrapper_markup = '<ul %1$s>%2$s</ul>';
$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}" ) );
Expand Down

0 comments on commit 04cf579

Please sign in to comment.