Make WordPress Core

Ticket #54623: 54623.diff

File 54623.diff, 2.6 KB (added by ryelle, 4 years ago)
  • src/wp-includes/block-patterns.php

     
    8383                }
    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}
  • src/wp-includes/default-filters.php

     
    333333add_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
    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 );
    338339add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );