Make WordPress Core


Ignore:
Timestamp:
05/26/2021 01:10:57 AM (8 months 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.

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