PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-5.9 / block-patterns.php

block-patterns.php in Gutenberg 12.6.0, at lib/compat/wordpress-5.9/block-patterns.php

61 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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