Skip to content

Commit

Permalink
Navigation link: prime caches for all posts in menu items (#40752)
Browse files Browse the repository at this point in the history
* Prime posts in navigation menus.

* Improve logic again.

* Change variable name and function names.

* Feedback and tweaks.

* Add a unit test.

* Fix lints.

* Apply suggestions from code review

Co-authored-by: Colin Stewart <[email protected]>

* Move functions

* Improve comment.

* Add another unit test.

Co-authored-by: Colin Stewart <[email protected]>
  • Loading branch information
2 people authored and adamziel committed May 10, 2022
1 parent a2be55c commit 57e4435
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,41 @@ function block_core_navigation_get_fallback_blocks() {
return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks );
}

/**
* Iterate through all inner blocks recursively and get navigation link block's post IDs.
*
* @param WP_Block_List $inner_blocks Block list class instance.
*
* @return array Array of post IDs.
*/
function block_core_navigation_get_post_ids( $inner_blocks ) {
$post_ids = array_map( 'block_core_navigation_from_block_get_post_ids', iterator_to_array( $inner_blocks ) );
return array_unique( array_merge( ...$post_ids ) );
}

/**
* Get post IDs from a navigation link block instance.
*
* @param WP_Block $block Instance of a block.
*
* @return array Array of post IDs.
*/
function block_core_navigation_from_block_get_post_ids( $block ) {
$post_ids = array();

if ( $block->inner_blocks ) {
$post_ids = block_core_navigation_get_post_ids( $block->inner_blocks );
}

if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) {
if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] ) {
$post_ids[] = $block->attributes['id'];
}
}

return $post_ids;
}

/**
* Renders the `core/navigation` block on server.
*
Expand Down Expand Up @@ -501,6 +536,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$text_decoration ? array( $text_decoration_class ) : array()
);

$post_ids = block_core_navigation_get_post_ids( $inner_blocks );
if ( $post_ids ) {
_prime_post_caches( $post_ids, false, false );
}

$inner_blocks_html = '';
$is_list_open = false;
foreach ( $inner_blocks as $inner_block ) {
Expand Down
69 changes: 69 additions & 0 deletions phpunit/blocks/render-block-navigation-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Navigation block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Navigation block.
*
* @group blocks
*/
class Render_Block_Navigation_Test extends WP_UnitTestCase {
/**
* @covers ::gutenberg_block_core_navigation_from_block_get_post_ids
*/
public function test_block_core_navigation_get_post_ids_from_block() {
$parsed_blocks = parse_blocks(
'<!-- wp:navigation-link {"label":"Sample Page","type":"page","kind":"post-type","id":755,"url":"http:https://localhost:8888/?page_id=755"} /-->'
);
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );

$post_ids = gutenberg_block_core_navigation_from_block_get_post_ids( $block );
$this->assertSameSets( array( 755 ), $post_ids );
}

/**
* @covers ::gutenberg_block_core_navigation_from_block_get_post_ids
*/
public function test_block_core_navigation_get_post_ids_from_block_nested() {
$parsed_blocks = parse_blocks(
'<!-- wp:group -->
<!-- wp:navigation-link {"label":"Sample Page","type":"page","id":20,"url":"http:https://localhost:8888/?page_id=20","kind":"post-type","isTopLevelLink":true} /-->
<!-- wp:navigation-link {"label":"Hello world!","type":"post","id":10,"url":"http:https://localhost:8888/?p=10","kind":"post-type","isTopLevelLink":true} /-->
<!-- wp:navigation-submenu {"label":"Uncategorized","type":"category","id":1,"url":"http:https://localhost:8888/?cat=1","kind":"taxonomy","isTopLevelItem":true} -->
<!-- wp:navigation-link {"label":"Sample Page","type":"page","id":30,"url":"http:https://localhost:8888/?page_id=30","kind":"post-type","isTopLevelLink":false} /-->
<!-- wp:navigation-submenu {"label":"Hello world!","type":"post","id":40,"url":"http:https://localhost:8888/?p=40","kind":"post-type","isTopLevelItem":false} -->
<!-- wp:navigation-link {"label":"Uncategorized","type":"category","id":5,"url":"http:https://localhost:8888/?cat=5","kind":"taxonomy","isTopLevelLink":false} /-->
<!-- wp:navigation-link {"label":"Hello world!","type":"post","id":60,"url":"http:https://localhost:8888/?p=60","kind":"post-type","isTopLevelLink":false} /-->
<!-- /wp:navigation-submenu -->
<!-- /wp:navigation-submenu -->
<!-- /wp:group -->'
);
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );

$post_ids = gutenberg_block_core_navigation_from_block_get_post_ids( $block );
$this->assertSameSets( array( 40, 60, 10, 20, 30 ), $post_ids );
}

/**
* @covers ::gutenberg_block_core_navigation_from_block_get_post_ids
*/
public function test_block_core_navigation_get_post_ids_from_block_with_submenu() {
$parsed_blocks = parse_blocks( '<!-- wp:navigation-submenu {"label":"Test","type":"post","id":789,"url":"http:https://localhost/blog/test-3","kind":"post-type","isTopLevelItem":true} -->\n<!-- wp:navigation-link {"label":"(no title)","type":"post","id":755,"url":"http:https://localhost/blog/755","kind":"post-type","isTopLevelLink":false} /-->\n<!-- /wp:navigation-submenu -->' );
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );

$post_ids = gutenberg_block_core_navigation_from_block_get_post_ids( $block );
$this->assertSameSetsWithIndex( array( 755, 789 ), $post_ids );
}


}

0 comments on commit 57e4435

Please sign in to comment.