Make WordPress Core

Changeset 52377


Ignore:
Timestamp:
12/14/2021 07:37:44 PM (5 years ago)
Author:
hellofromTonya
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-patterns.php

    r51307 r52377  
    8484        }
    8585}
     86
     87/**
     88 * Register `Featured` (category) patterns from wordpress.org/patterns.
     89 *
     90 * @since 5.9.0
     91 *
     92 * @param WP_Screen $current_screen The screen that the current request was triggered from.
     93 */
     94function _load_remote_featured_patterns( $current_screen ) {
     95        if ( ! $current_screen->is_block_editor ) {
     96                return;
     97        }
     98
     99        $supports_core_patterns = get_theme_support( 'core-block-patterns' );
     100
     101        /** This filter is documented in wp-includes/block-patterns.php */
     102        $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
     103
     104        if ( ! $should_load_remote || ! $supports_core_patterns ) {
     105                return;
     106        }
     107
     108        if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) {
     109                register_block_pattern_category( 'featured', array( 'label' => __( 'Featured' ) ) );
     110        }
     111
     112        $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
     113        $featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
     114        $request->set_param( 'category', $featured_cat_id );
     115        $response = rest_do_request( $request );
     116        if ( $response->is_error() ) {
     117                return;
     118        }
     119        $patterns = $response->get_data();
     120
     121        foreach ( $patterns as $pattern ) {
     122                $pattern_name = sanitize_title( $pattern['title'] );
     123                $registry     = WP_Block_Patterns_Registry::get_instance();
     124                // Some patterns might be already registerd as `core patterns with the `core` prefix.
     125                $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
     126                if ( ! $is_registered ) {
     127                        register_block_pattern( $pattern_name, (array) $pattern );
     128                }
     129        }
     130}
  • trunk/src/wp-includes/default-filters.php

    r52369 r52377  
    334334add_action( 'init', '_register_core_block_patterns_and_categories' );
    335335add_action( 'current_screen', '_load_remote_block_patterns' );
     336add_action( 'current_screen', '_load_remote_featured_patterns' );
    336337add_action( 'init', 'check_theme_switched', 99 );
    337338add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
Note: See TracChangeset for help on using the changeset viewer.