[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * @since 6.3.0 Added source to core block patterns. 16 * @access private 17 */ 18 function _register_core_block_patterns_and_categories() { 19 $should_register_core_patterns = get_theme_support( 'core-block-patterns' ); 20 21 if ( $should_register_core_patterns ) { 22 $core_block_patterns = array( 23 'query-standard-posts', 24 'query-medium-posts', 25 'query-small-posts', 26 'query-grid-posts', 27 'query-large-title-posts', 28 'query-offset-posts', 29 'social-links-shared-background-color', 30 ); 31 32 foreach ( $core_block_patterns as $core_block_pattern ) { 33 $pattern = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'; 34 $pattern['source'] = 'core'; 35 register_block_pattern( 'core/' . $core_block_pattern, $pattern ); 36 } 37 } 38 39 register_block_pattern_category( 'banner', array( 'label' => _x( 'Banners', 'Block pattern category' ) ) ); 40 register_block_pattern_category( 41 'buttons', 42 array( 43 'label' => _x( 'Buttons', 'Block pattern category' ), 44 'description' => __( 'Patterns that contain buttons and call to actions.' ), 45 ) 46 ); 47 register_block_pattern_category( 48 'columns', 49 array( 50 'label' => _x( 'Columns', 'Block pattern category' ), 51 'description' => __( 'Multi-column patterns with more complex layouts.' ), 52 ) 53 ); 54 register_block_pattern_category( 55 'text', 56 array( 57 'label' => _x( 'Text', 'Block pattern category' ), 58 'description' => __( 'Patterns containing mostly text.' ), 59 ) 60 ); 61 register_block_pattern_category( 62 'query', 63 array( 64 'label' => _x( 'Posts', 'Block pattern category' ), 65 'description' => __( 'Display your latest posts in lists, grids or other layouts.' ), 66 ) 67 ); 68 register_block_pattern_category( 69 'featured', 70 array( 71 'label' => _x( 'Featured', 'Block pattern category' ), 72 'description' => __( 'A set of high quality curated patterns.' ), 73 ) 74 ); 75 register_block_pattern_category( 76 'call-to-action', 77 array( 78 'label' => _x( 'Call to Action', 'Block pattern category' ), 79 'description' => __( 'Sections whose purpose is to trigger a specific action.' ), 80 ) 81 ); 82 register_block_pattern_category( 83 'team', 84 array( 85 'label' => _x( 'Team', 'Block pattern category' ), 86 'description' => __( 'A variety of designs to display your team members.' ), 87 ) 88 ); 89 register_block_pattern_category( 90 'testimonials', 91 array( 92 'label' => _x( 'Testimonials', 'Block pattern category' ), 93 'description' => __( 'Share reviews and feedback about your brand/business.' ), 94 ) 95 ); 96 register_block_pattern_category( 97 'services', 98 array( 99 'label' => _x( 'Services', 'Block pattern category' ), 100 'description' => __( 'Briefly describe what your business does and how you can help.' ), 101 ) 102 ); 103 register_block_pattern_category( 104 'contact', 105 array( 106 'label' => _x( 'Contact', 'Block pattern category' ), 107 'description' => __( 'Display your contact information.' ), 108 ) 109 ); 110 register_block_pattern_category( 111 'about', 112 array( 113 'label' => _x( 'About', 'Block pattern category' ), 114 'description' => __( 'Introduce yourself.' ), 115 ) 116 ); 117 register_block_pattern_category( 118 'portfolio', 119 array( 120 'label' => _x( 'Portfolio', 'Block pattern category' ), 121 'description' => __( 'Showcase your latest work.' ), 122 ) 123 ); 124 register_block_pattern_category( 125 'gallery', 126 array( 127 'label' => _x( 'Gallery', 'Block pattern category' ), 128 'description' => __( 'Different layouts for displaying images.' ), 129 ) 130 ); 131 register_block_pattern_category( 132 'media', 133 array( 134 'label' => _x( 'Media', 'Block pattern category' ), 135 'description' => __( 'Different layouts containing video or audio.' ), 136 ) 137 ); 138 register_block_pattern_category( 139 'videos', 140 array( 141 'label' => _x( 'Videos', 'Block pattern category' ), 142 'description' => __( 'Different layouts containing videos.' ), 143 ) 144 ); 145 register_block_pattern_category( 146 'audio', 147 array( 148 'label' => _x( 'Audio', 'Block pattern category' ), 149 'description' => __( 'Different layouts containing audio.' ), 150 ) 151 ); 152 register_block_pattern_category( 153 'posts', 154 array( 155 'label' => _x( 'Posts', 'Block pattern category' ), 156 'description' => __( 'Display your latest posts in lists, grids or other layouts.' ), 157 ) 158 ); 159 register_block_pattern_category( 160 'footer', 161 array( 162 'label' => _x( 'Footers', 'Block pattern category' ), 163 'description' => __( 'A variety of footer designs displaying information and site navigation.' ), 164 ) 165 ); 166 register_block_pattern_category( 167 'header', 168 array( 169 'label' => _x( 'Headers', 'Block pattern category' ), 170 'description' => __( 'A variety of header designs displaying your site title and navigation.' ), 171 ) 172 ); 173 } 174 175 /** 176 * Normalize the pattern properties to camelCase. 177 * 178 * The API's format is snake_case, `register_block_pattern()` expects camelCase. 179 * 180 * @since 6.2.0 181 * @access private 182 * 183 * @param array $pattern Pattern as returned from the Pattern Directory API. 184 * @return array Normalized pattern. 185 */ 186 function wp_normalize_remote_block_pattern( $pattern ) { 187 if ( isset( $pattern['block_types'] ) ) { 188 $pattern['blockTypes'] = $pattern['block_types']; 189 unset( $pattern['block_types'] ); 190 } 191 192 if ( isset( $pattern['viewport_width'] ) ) { 193 $pattern['viewportWidth'] = $pattern['viewport_width']; 194 unset( $pattern['viewport_width'] ); 195 } 196 197 return (array) $pattern; 198 } 199 200 /** 201 * Register Core's official patterns from wordpress.org/patterns. 202 * 203 * @since 5.8.0 204 * @since 5.9.0 The $current_screen argument was removed. 205 * @since 6.2.0 Normalize the pattern from the API (snake_case) to the 206 * format expected by `register_block_pattern` (camelCase). 207 * @since 6.3.0 Add 'pattern-directory/core' to the pattern's 'source'. 208 * 209 * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. 210 */ 211 function _load_remote_block_patterns( $deprecated = null ) { 212 if ( ! empty( $deprecated ) ) { 213 _deprecated_argument( __FUNCTION__, '5.9.0' ); 214 $current_screen = $deprecated; 215 if ( ! $current_screen->is_block_editor ) { 216 return; 217 } 218 } 219 220 $supports_core_patterns = get_theme_support( 'core-block-patterns' ); 221 222 /** 223 * Filter to disable remote block patterns. 224 * 225 * @since 5.8.0 226 * 227 * @param bool $should_load_remote 228 */ 229 $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); 230 231 if ( $supports_core_patterns && $should_load_remote ) { 232 $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); 233 $core_keyword_id = 11; // 11 is the ID for "core". 234 $request->set_param( 'keyword', $core_keyword_id ); 235 $response = rest_do_request( $request ); 236 if ( $response->is_error() ) { 237 return; 238 } 239 $patterns = $response->get_data(); 240 241 foreach ( $patterns as $pattern ) { 242 $pattern['source'] = 'pattern-directory/core'; 243 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 244 $pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] ); 245 register_block_pattern( $pattern_name, $normalized_pattern ); 246 } 247 } 248 } 249 250 /** 251 * Register `Featured` (category) patterns from wordpress.org/patterns. 252 * 253 * @since 5.9.0 254 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the 255 * format expected by `register_block_pattern()` (camelCase). 256 * @since 6.3.0 Add 'pattern-directory/featured' to the pattern's 'source'. 257 */ 258 function _load_remote_featured_patterns() { 259 $supports_core_patterns = get_theme_support( 'core-block-patterns' ); 260 261 /** This filter is documented in wp-includes/block-patterns.php */ 262 $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); 263 264 if ( ! $should_load_remote || ! $supports_core_patterns ) { 265 return; 266 } 267 268 $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); 269 $featured_cat_id = 26; // This is the `Featured` category id from pattern directory. 270 $request->set_param( 'category', $featured_cat_id ); 271 $response = rest_do_request( $request ); 272 if ( $response->is_error() ) { 273 return; 274 } 275 $patterns = $response->get_data(); 276 $registry = WP_Block_Patterns_Registry::get_instance(); 277 foreach ( $patterns as $pattern ) { 278 $pattern['source'] = 'pattern-directory/featured'; 279 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 280 $pattern_name = sanitize_title( $normalized_pattern['title'] ); 281 // Some patterns might be already registered as core patterns with the `core` prefix. 282 $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" ); 283 if ( ! $is_registered ) { 284 register_block_pattern( $pattern_name, $normalized_pattern ); 285 } 286 } 287 } 288 289 /** 290 * Registers patterns from Pattern Directory provided by a theme's 291 * `theme.json` file. 292 * 293 * @since 6.0.0 294 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the 295 * format expected by `register_block_pattern()` (camelCase). 296 * @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'. 297 * @access private 298 */ 299 function _register_remote_theme_patterns() { 300 /** This filter is documented in wp-includes/block-patterns.php */ 301 if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) { 302 return; 303 } 304 305 if ( ! wp_theme_has_theme_json() ) { 306 return; 307 } 308 309 $pattern_settings = wp_get_theme_directory_pattern_slugs(); 310 if ( empty( $pattern_settings ) ) { 311 return; 312 } 313 314 $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); 315 $request['slug'] = $pattern_settings; 316 $response = rest_do_request( $request ); 317 if ( $response->is_error() ) { 318 return; 319 } 320 $patterns = $response->get_data(); 321 $patterns_registry = WP_Block_Patterns_Registry::get_instance(); 322 foreach ( $patterns as $pattern ) { 323 $pattern['source'] = 'pattern-directory/theme'; 324 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 325 $pattern_name = sanitize_title( $normalized_pattern['title'] ); 326 // Some patterns might be already registered as core patterns with the `core` prefix. 327 $is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" ); 328 if ( ! $is_registered ) { 329 register_block_pattern( $pattern_name, $normalized_pattern ); 330 } 331 } 332 } 333 334 /** 335 * Register any patterns that the active theme may provide under its 336 * `./patterns/` directory. 337 * 338 * @since 6.0.0 339 * @since 6.1.0 The `postTypes` property was added. 340 * @since 6.2.0 The `templateTypes` property was added. 341 * @since 6.4.0 Uses the `WP_Theme::get_block_patterns` method. 342 * @access private 343 */ 344 function _register_theme_block_patterns() { 345 346 /* 347 * During the bootstrap process, a check for active and valid themes is run. 348 * If no themes are returned, the theme's functions.php file will not be loaded, 349 * which can lead to errors if patterns expect some variables or constants to 350 * already be set at this point, so bail early if that is the case. 351 */ 352 if ( empty( wp_get_active_and_valid_themes() ) ) { 353 return; 354 } 355 356 /* 357 * Register patterns for the active theme. If the theme is a child theme, 358 * let it override any patterns from the parent theme that shares the same slug. 359 */ 360 $themes = array(); 361 $theme = wp_get_theme(); 362 $themes[] = $theme; 363 if ( $theme->parent() ) { 364 $themes[] = $theme->parent(); 365 } 366 $registry = WP_Block_Patterns_Registry::get_instance(); 367 368 foreach ( $themes as $theme ) { 369 $patterns = $theme->get_block_patterns(); 370 $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; 371 $text_domain = $theme->get( 'TextDomain' ); 372 373 foreach ( $patterns as $file => $pattern_data ) { 374 if ( $registry->is_registered( $pattern_data['slug'] ) ) { 375 continue; 376 } 377 378 $file_path = $dirpath . $file; 379 380 if ( ! file_exists( $file_path ) ) { 381 _doing_it_wrong( 382 __FUNCTION__, 383 sprintf( 384 /* translators: %s: file name. */ 385 __( 'Could not register file "%s" as a block pattern as the file does not exist.' ), 386 $file 387 ), 388 '6.4.0' 389 ); 390 $theme->delete_pattern_cache(); 391 continue; 392 } 393 394 $pattern_data['filePath'] = $file_path; 395 396 // Translate the pattern metadata. 397 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction 398 $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain ); 399 if ( ! empty( $pattern_data['description'] ) ) { 400 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction 401 $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain ); 402 } 403 404 register_block_pattern( $pattern_data['slug'], $pattern_data ); 405 } 406 } 407 } 408 add_action( 'init', '_register_theme_block_patterns' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Apr 19 08:20:01 2024 | Cross-referenced by PHPXref |