From 979199ff0de484b8cb17de7516dba3f929e7cb07 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 14 Dec 2021 19:37:44 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@52377 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-patterns.php | 45 +++++++++++++++++++++++++++++ src/wp-includes/default-filters.php | 1 + 2 files changed, 46 insertions(+) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index b01869f8b6..b1dfaeb403 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -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 ); + } + } +} diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 6e98a069fd..94c9436ee9 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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' ) );