Make WordPress Core

Changeset 51021


Ignore:
Timestamp:
05/26/2021 01:10:57 AM (4 years ago)
Author:
ryelle
Message:

Block Editor: Add support for the pattern directory.

Add an endpoint for fetching block patterns from WordPress.org, and load the block patterns from this new API. Remove the block patterns that have already been moved to WordPress.org/patterns.

Props ryelle, iandunn, youknowriad, timothyblynjacobs.
Fixes #53246.

Location:
trunk
Files:
7 added
15 deleted
6 edited

Legend:

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

    r50948 r51021  
    2020    if ( $should_register_core_patterns ) {
    2121        $core_block_patterns = array(
    22             'media-text-nature',
    23             'two-images-gallery',
    24             'three-columns-media-text',
    25             'quote',
    26             'large-header-left',
    27             'large-header-text-button',
    28             'media-text-art',
    29             'text-two-columns-title',
    30             'three-columns-text',
    31             'text-two-columns-title-offset',
    32             'heading',
    33             'three-images-gallery',
    34             'text-two-columns',
    35             'media-text-arquitecture',
    36             'two-buttons',
    3722            'query-standard-posts',
    3823            'query-medium-posts',
     
    5944    register_block_pattern_category( 'query', array( 'label' => __( 'Query', 'Block pattern category' ) ) );
    6045}
     46
     47/**
     48 * Import patterns from wordpress.org/patterns.
     49 */
     50function _load_remote_block_patterns( $current_screen ) {
     51    if ( ! $current_screen->is_block_editor ) {
     52        return;
     53    }
     54
     55    $supports_core_patterns = get_theme_support( 'core-block-patterns' );
     56
     57    /**
     58     * Filter to disable remote block patterns.
     59     *
     60     * @since 5.8.0
     61     *
     62     * @param bool $should_load_remote
     63     */
     64    $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
     65
     66    if ( $supports_core_patterns && $should_load_remote ) {
     67        $patterns = get_transient( 'wp_remote_block_patterns' );
     68        if ( ! $patterns ) {
     69            $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
     70            $core_keyword_id = 11; // 11 is the ID for "core".
     71            $request->set_param( 'keyword', $core_keyword_id );
     72            $response = rest_do_request( $request );
     73            if ( $response->is_error() ) {
     74                return;
     75            }
     76            $patterns = $response->get_data();
     77            set_transient( 'wp_remote_block_patterns', $patterns, HOUR_IN_SECONDS );
     78        }
     79
     80        foreach ( $patterns as $settings ) {
     81            $pattern_name = 'core/' . sanitize_title( $settings['title'] );
     82            register_block_pattern( $pattern_name, (array) $settings );
     83        }
     84    }
     85}
  • trunk/src/wp-includes/default-filters.php

    r51019 r51021  
    316316add_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
    317317add_action( 'init', '_register_core_block_patterns_and_categories' );
     318add_action( 'current_screen', '_load_remote_block_patterns' );
    318319add_action( 'init', 'check_theme_switched', 99 );
    319320add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
  • trunk/src/wp-includes/rest-api.php

    r50995 r51021  
    328328    // Block Directory.
    329329    $controller = new WP_REST_Block_Directory_Controller();
     330    $controller->register_routes();
     331
     332    // Pattern Directory.
     333    $controller = new WP_REST_Pattern_Directory_Controller();
    330334    $controller->register_routes();
    331335
  • trunk/src/wp-settings.php

    r51013 r51021  
    269269require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-plugins-controller.php';
    270270require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-controller.php';
     271require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
    271272require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
    272273require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-controller.php';
  • trunk/tests/phpunit/tests/blocks/block-editor.php

    r50996 r51021  
    3030
    3131        $post = $this->factory()->post->create_and_get( $args );
     32
     33        global $wp_rest_server;
     34        $wp_rest_server = new Spy_REST_Server;
     35        do_action( 'rest_api_init', $wp_rest_server );
     36    }
     37
     38    public function tearDown() {
     39        /** @var WP_REST_Server $wp_rest_server */
     40        global $wp_rest_server;
     41        $wp_rest_server = null;
     42        parent::tearDown();
    3243    }
    3344
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r51003 r51021  
    9696            '/wp/v2/pages/(?P<id>[\\d]+)/autosaves',
    9797            '/wp/v2/pages/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)',
     98            '/wp/v2/pattern-directory/patterns',
    9899            '/wp/v2/media',
    99100            '/wp/v2/media/(?P<id>[\\d]+)',
Note: See TracChangeset for help on using the changeset viewer.