block-patterns.php (2663B)
1 <?php 2 /** 3 * Register the block patterns and block patterns categories 4 * 5 * @package WordPress 6 * @since 5.5.0 7 */ 8 9 add_theme_support( 'core-block-patterns' ); 10 11 /** 12 * Registers the core block patterns and categories. 13 * 14 * @since 5.5.0 15 * @private 16 */ 17 function _register_core_block_patterns_and_categories() { 18 $should_register_core_patterns = get_theme_support( 'core-block-patterns' ); 19 20 if ( $should_register_core_patterns ) { 21 $core_block_patterns = array( 22 'query-standard-posts', 23 'query-medium-posts', 24 'query-small-posts', 25 'query-grid-posts', 26 'query-large-title-posts', 27 'query-offset-posts', 28 'social-links-shared-background-color', 29 ); 30 31 foreach ( $core_block_patterns as $core_block_pattern ) { 32 register_block_pattern( 33 'core/' . $core_block_pattern, 34 require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php' 35 ); 36 } 37 } 38 39 register_block_pattern_category( 'buttons', array( 'label' => _x( 'Buttons', 'Block pattern category' ) ) ); 40 register_block_pattern_category( 'columns', array( 'label' => _x( 'Columns', 'Block pattern category' ) ) ); 41 register_block_pattern_category( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category' ) ) ); 42 register_block_pattern_category( 'header', array( 'label' => _x( 'Headers', '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' ) ) ); 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 }