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