| | 86 | |
| | 87 | /** |
| | 88 | * Register `Featured` (category) patterns from wordpress.org/patterns. |
| | 89 | * |
| | 90 | * @since 5.9.0 |
| | 91 | * |
| | 92 | * @param WP_Screen $current_screen The screen that the current request was triggered from. |
| | 93 | */ |
| | 94 | function _load_remote_featured_patterns( $current_screen ) { |
| | 95 | if ( ! $current_screen->is_block_editor ) { |
| | 96 | return; |
| | 97 | } |
| | 98 | |
| | 99 | $supports_core_patterns = get_theme_support( 'core-block-patterns' ); |
| | 100 | |
| | 101 | /** This filter is documented in wp-includes/block-patterns.php */ |
| | 102 | $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); |
| | 103 | |
| | 104 | if ( ! $should_load_remote || ! $supports_core_patterns ) { |
| | 105 | return; |
| | 106 | } |
| | 107 | |
| | 108 | if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) { |
| | 109 | register_block_pattern_category( 'featured', array( 'label' => __( 'Featured' ) ) ); |
| | 110 | } |
| | 111 | |
| | 112 | $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); |
| | 113 | $featured_cat_id = 26; // This is the `Featured` category id from pattern directory. |
| | 114 | $request->set_param( 'category', $featured_cat_id ); |
| | 115 | $response = rest_do_request( $request ); |
| | 116 | if ( $response->is_error() ) { |
| | 117 | return; |
| | 118 | } |
| | 119 | $patterns = $response->get_data(); |
| | 120 | |
| | 121 | foreach ( $patterns as $pattern ) { |
| | 122 | $pattern_name = sanitize_title( $pattern['title'] ); |
| | 123 | $registry = WP_Block_Patterns_Registry::get_instance(); |
| | 124 | // Some patterns might be already registerd as `core patterns with the `core` prefix. |
| | 125 | $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" ); |
| | 126 | if ( ! $is_registered ) { |
| | 127 | register_block_pattern( $pattern_name, (array) $pattern ); |
| | 128 | } |
| | 129 | } |
| | 130 | } |