wp/wp-includes/block-patterns.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    17 function _register_core_block_patterns_and_categories() {
    17 function _register_core_block_patterns_and_categories() {
    18 	$should_register_core_patterns = get_theme_support( 'core-block-patterns' );
    18 	$should_register_core_patterns = get_theme_support( 'core-block-patterns' );
    19 
    19 
    20 	if ( $should_register_core_patterns ) {
    20 	if ( $should_register_core_patterns ) {
    21 		$core_block_patterns = array(
    21 		$core_block_patterns = array(
    22 			'text-two-columns',
    22 			'query-standard-posts',
    23 			'two-buttons',
    23 			'query-medium-posts',
    24 			'two-images',
    24 			'query-small-posts',
    25 			'text-two-columns-with-images',
    25 			'query-grid-posts',
    26 			'text-three-columns-buttons',
    26 			'query-large-title-posts',
    27 			'large-header',
    27 			'query-offset-posts',
    28 			'large-header-button',
    28 			'social-links-shared-background-color',
    29 			'three-buttons',
       
    30 			'heading-paragraph',
       
    31 			'quote',
       
    32 		);
    29 		);
    33 
    30 
    34 		foreach ( $core_block_patterns as $core_block_pattern ) {
    31 		foreach ( $core_block_patterns as $core_block_pattern ) {
    35 			register_block_pattern(
    32 			register_block_pattern(
    36 				'core/' . $core_block_pattern,
    33 				'core/' . $core_block_pattern,
    42 	register_block_pattern_category( 'buttons', array( 'label' => _x( 'Buttons', 'Block pattern category' ) ) );
    39 	register_block_pattern_category( 'buttons', array( 'label' => _x( 'Buttons', 'Block pattern category' ) ) );
    43 	register_block_pattern_category( 'columns', array( 'label' => _x( 'Columns', 'Block pattern category' ) ) );
    40 	register_block_pattern_category( 'columns', array( 'label' => _x( 'Columns', 'Block pattern category' ) ) );
    44 	register_block_pattern_category( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category' ) ) );
    41 	register_block_pattern_category( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category' ) ) );
    45 	register_block_pattern_category( 'header', array( 'label' => _x( 'Headers', 'Block pattern category' ) ) );
    42 	register_block_pattern_category( 'header', array( 'label' => _x( 'Headers', 'Block pattern category' ) ) );
    46 	register_block_pattern_category( 'text', array( 'label' => _x( 'Text', 'Block pattern category' ) ) );
    43 	register_block_pattern_category( 'text', array( 'label' => _x( 'Text', 'Block pattern category' ) ) );
       
    44 	register_block_pattern_category( 'query', array( 'label' => _x( 'Query', 'Block pattern category' ) ) );
    47 }
    45 }
       
    46 
       
    47 /**
       
    48  * Register Core's official patterns from wordpress.org/patterns.
       
    49  *
       
    50  * @since 5.8.0
       
    51  *
       
    52  * @param WP_Screen $current_screen The screen that the current request was triggered from.
       
    53  */
       
    54 function _load_remote_block_patterns( $current_screen ) {
       
    55 	if ( ! $current_screen->is_block_editor ) {
       
    56 		return;
       
    57 	}
       
    58 
       
    59 	$supports_core_patterns = get_theme_support( 'core-block-patterns' );
       
    60 
       
    61 	/**
       
    62 	 * Filter to disable remote block patterns.
       
    63 	 *
       
    64 	 * @since 5.8.0
       
    65 	 *
       
    66 	 * @param bool $should_load_remote
       
    67 	 */
       
    68 	$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
       
    69 
       
    70 	if ( $supports_core_patterns && $should_load_remote ) {
       
    71 		$request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
       
    72 		$core_keyword_id = 11; // 11 is the ID for "core".
       
    73 		$request->set_param( 'keyword', $core_keyword_id );
       
    74 		$response = rest_do_request( $request );
       
    75 		if ( $response->is_error() ) {
       
    76 			return;
       
    77 		}
       
    78 		$patterns = $response->get_data();
       
    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 }