Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation link: prime caches for all posts in menu items #40752

Merged
merged 11 commits into from
May 9, 2022
40 changes: 40 additions & 0 deletions lib/compat/wordpress-6.1/navigation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Temporary compatibility shims for navigation block.
*
* @package gutenberg
*/

/**
* Iterate through all inner blocks recursively and get navigation link block's post ids..
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*
* @param WP_Block_List $inner_blocks Block list class instance.
*
* @return array Array of post ids.
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
function block_core_navigation_get_post_ids( $inner_blocks ) {
$post_ids = array_map( 'block_core_navigation_get_post_ids_from_block', iterator_to_array( $inner_blocks ) );
return array_unique( array_merge( ...$post_ids ) );
}

/**
* Get post ids from a navigation link block instance.
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*
* @param WP_Block $block Instance of a block.
*
* @return array Array of post ids.
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
function block_core_navigation_get_post_ids_from_block( $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 ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] ) {
$post_ids[] = $block->attributes['id'];
}
}

return $post_ids;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// WordPress 6.1 compat.
require __DIR__ . '/compat/wordpress-6.1/blocks.php';
require __DIR__ . '/compat/wordpress-6.1/navigation.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';

// Experimental features.
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,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
34 changes: 34 additions & 0 deletions phpunit/blocks/render-block-navigation-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class Render_Block_Navigation_Test extends WP_UnitTestCase {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved

/**
* @covers ::block_core_navigation_get_post_ids_from_block
*/
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://localhost:8888/?page_id=755"} /-->'
);
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );

$post_ids = block_core_navigation_get_post_ids_from_block( $block );
$this->assertEqualSets( array( 755 ), $post_ids );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @covers ::block_core_navigation_get_post_ids_from_block
*/
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://localhost/blog/test-3","kind":"post-type","isTopLevelItem":true} -->\n<!-- wp:navigation-link {"label":"(no title)","type":"post","id":755,"url":"http://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 = block_core_navigation_get_post_ids_from_block( $block );
$this->assertEqualSets( array( 755, 789 ), $post_ids );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}


}