| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block patterns functions. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( '_load_remote_featured_patterns' ) ) { |
| 9 |
/** |
| 10 |
* Loads featured patterns from patterns directory. |
| 11 |
*/ |
| 12 |
function _load_remote_featured_patterns() { |
| 13 |
/** |
| 14 |
* Filter to disable remote block patterns. |
| 15 |
* |
| 16 |
* @since 5.8.0 |
| 17 |
* |
| 18 |
* @param bool $should_load_remote |
| 19 |
*/ |
| 20 |
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); |
| 21 |
|
| 22 |
if ( ! $should_load_remote ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) { |
| 27 |
register_block_pattern_category( 'featured', array( 'label' => __( 'Featured', 'gutenberg' ) ) ); |
| 28 |
} |
| 29 |
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); |
| 30 |
$request['category'] = 26; // This is the `Featured` category id from pattern directory. |
| 31 |
$response = rest_do_request( $request ); |
| 32 |
if ( $response->is_error() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
$patterns = $response->get_data(); |
| 36 |
foreach ( $patterns as $pattern ) { |
| 37 |
$pattern_name = sanitize_title( $pattern['title'] ); |
| 38 |
$registry = WP_Block_Patterns_Registry::get_instance(); |
| 39 |
// Some patterns might be already registered as core patterns with the `core` prefix. |
| 40 |
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" ); |
| 41 |
if ( ! $is_registered ) { |
| 42 |
register_block_pattern( $pattern_name, (array) $pattern ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
add_action( |
| 48 |
'current_screen', |
| 49 |
function( $current_screen ) { |
| 50 |
if ( ! get_theme_support( 'core-block-patterns' ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) ); |
| 55 |
if ( $current_screen->is_block_editor || $is_site_editor ) { |
| 56 |
_load_remote_featured_patterns(); |
| 57 |
} |
| 58 |
} |
| 59 |
); |
| 60 |
} |
| 61 |
|