Make WordPress Core


Ignore:
Timestamp:
12/14/2021 07:37:44 PM (3 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.