Editor: Add "Featured" patterns from pattern directory to Patterns in block inserter.

This commit backports the remote "Featured" category request and loads it into the Featured Patterns in the block inserter.

Props ryelle.
Fixes #54623.
Built from https://develop.svn.wordpress.org/trunk@52377


git-svn-id: http://core.svn.wordpress.org/trunk@51969 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-12-14 19:39:06 +00:00
parent 84d67f77f6
commit 4132ac0f48
3 changed files with 47 additions and 1 deletions

View File

@ -83,3 +83,48 @@ function _load_remote_block_patterns( $current_screen ) {
}
}
}
/**
* Register `Featured` (category) patterns from wordpress.org/patterns.
*
* @since 5.9.0
*
* @param WP_Screen $current_screen The screen that the current request was triggered from.
*/
function _load_remote_featured_patterns( $current_screen ) {
if ( ! $current_screen->is_block_editor ) {
return;
}
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
/** This filter is documented in wp-includes/block-patterns.php */
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
if ( ! $should_load_remote || ! $supports_core_patterns ) {
return;
}
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) {
register_block_pattern_category( 'featured', array( 'label' => __( 'Featured' ) ) );
}
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
$request->set_param( 'category', $featured_cat_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
foreach ( $patterns as $pattern ) {
$pattern_name = sanitize_title( $pattern['title'] );
$registry = WP_Block_Patterns_Registry::get_instance();
// Some patterns might be already registerd as `core patterns with the `core` prefix.
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
if ( ! $is_registered ) {
register_block_pattern( $pattern_name, (array) $pattern );
}
}
}

View File

@ -333,6 +333,7 @@ add_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
add_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
add_action( 'init', '_register_core_block_patterns_and_categories' );
add_action( 'current_screen', '_load_remote_block_patterns' );
add_action( 'current_screen', '_load_remote_featured_patterns' );
add_action( 'init', 'check_theme_switched', 99 );
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-beta2-52376';
$wp_version = '5.9-beta2-52377';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.