[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Utilities used to fetch and create templates and template parts. 4 * 5 * @package WordPress 6 * @since 5.8.0 7 */ 8 9 // Define constants for supported wp_template_part_area taxonomy. 10 if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) { 11 define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' ); 12 } 13 if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) { 14 define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' ); 15 } 16 if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) { 17 define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' ); 18 } 19 if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { 20 define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); 21 } 22 23 /** 24 * For backward compatibility reasons, 25 * block themes might be using block-templates or block-template-parts, 26 * this function ensures we fallback to these folders properly. 27 * 28 * @since 5.9.0 29 * 30 * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root. 31 * 32 * @return string[] { 33 * Folder names used by block themes. 34 * 35 * @type string $wp_template Theme-relative directory name for block templates. 36 * @type string $wp_template_part Theme-relative directory name for block template parts. 37 * } 38 */ 39 function get_block_theme_folders( $theme_stylesheet = null ) { 40 $theme = wp_get_theme( (string) $theme_stylesheet ); 41 if ( ! $theme->exists() ) { 42 // Return the default folders if the theme doesn't exist. 43 return array( 44 'wp_template' => 'templates', 45 'wp_template_part' => 'parts', 46 ); 47 } 48 return $theme->get_block_template_folders(); 49 } 50 51 /** 52 * Returns a filtered list of allowed area values for template parts. 53 * 54 * @since 5.9.0 55 * 56 * @return array[] { 57 * The allowed template part area values. 58 * 59 * @type array ...$0 { 60 * Data for the allowed template part area. 61 * 62 * @type string $area Template part area name. 63 * @type string $label Template part area label. 64 * @type string $description Template part area description. 65 * @type string $icon Template part area icon. 66 * @type string $area_tag Template part area tag. 67 * } 68 * } 69 */ 70 function get_allowed_block_template_part_areas() { 71 $default_area_definitions = array( 72 array( 73 'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED, 74 'label' => _x( 'General', 'template part area' ), 75 'description' => __( 76 'General templates often perform a specific role like displaying post content, and are not tied to any particular area.' 77 ), 78 'icon' => 'layout', 79 'area_tag' => 'div', 80 ), 81 array( 82 'area' => WP_TEMPLATE_PART_AREA_HEADER, 83 'label' => _x( 'Header', 'template part area' ), 84 'description' => __( 85 'The Header template defines a page area that typically contains a title, logo, and main navigation.' 86 ), 87 'icon' => 'header', 88 'area_tag' => 'header', 89 ), 90 array( 91 'area' => WP_TEMPLATE_PART_AREA_FOOTER, 92 'label' => _x( 'Footer', 'template part area' ), 93 'description' => __( 94 'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.' 95 ), 96 'icon' => 'footer', 97 'area_tag' => 'footer', 98 ), 99 ); 100 101 /** 102 * Filters the list of allowed template part area values. 103 * 104 * @since 5.9.0 105 * 106 * @param array[] $default_area_definitions { 107 * The allowed template part area values. 108 * 109 * @type array ...$0 { 110 * Data for the template part area. 111 * 112 * @type string $area Template part area name. 113 * @type string $label Template part area label. 114 * @type string $description Template part area description. 115 * @type string $icon Template part area icon. 116 * @type string $area_tag Template part area tag. 117 * } 118 * } 119 */ 120 return apply_filters( 'default_wp_template_part_areas', $default_area_definitions ); 121 } 122 123 124 /** 125 * Returns a filtered list of default template types, containing their 126 * localized titles and descriptions. 127 * 128 * @since 5.9.0 129 * 130 * @return array[] { 131 * The default template types. 132 * 133 * @type array ...$0 { 134 * Data for the template type. 135 * 136 * @type string $title Template type title. 137 * @type string $description Template type description. 138 * } 139 * } 140 */ 141 function get_default_block_template_types() { 142 $default_template_types = array( 143 'index' => array( 144 'title' => _x( 'Index', 'Template name' ), 145 'description' => __( 'Used as a fallback template for all pages when a more specific template is not defined.' ), 146 ), 147 'home' => array( 148 'title' => _x( 'Blog Home', 'Template name' ), 149 'description' => __( 'Displays the latest posts as either the site homepage or as the "Posts page" as defined under reading settings. If it exists, the Front Page template overrides this template when posts are shown on the homepage.' ), 150 ), 151 'front-page' => array( 152 'title' => _x( 'Front Page', 'Template name' ), 153 'description' => __( 'Displays your site\'s homepage, whether it is set to display latest posts or a static page. The Front Page template takes precedence over all templates.' ), 154 ), 155 'singular' => array( 156 'title' => _x( 'Single Entries', 'Template name' ), 157 'description' => __( 'Displays any single entry, such as a post or a page. This template will serve as a fallback when a more specific template (e.g. Single Post, Page, or Attachment) cannot be found.' ), 158 ), 159 'single' => array( 160 'title' => _x( 'Single Posts', 'Template name' ), 161 'description' => __( 'Displays a single post on your website unless a custom template has been applied to that post or a dedicated template exists.' ), 162 ), 163 'page' => array( 164 'title' => _x( 'Pages', 'Template name' ), 165 'description' => __( 'Displays a static page unless a custom template has been applied to that page or a dedicated template exists.' ), 166 ), 167 'archive' => array( 168 'title' => _x( 'All Archives', 'Template name' ), 169 'description' => __( 'Displays any archive, including posts by a single author, category, tag, taxonomy, custom post type, and date. This template will serve as a fallback when more specific templates (e.g. Category or Tag) cannot be found.' ), 170 ), 171 'author' => array( 172 'title' => _x( 'Author Archives', 'Template name' ), 173 'description' => __( 'Displays a single author\'s post archive. This template will serve as a fallback when a more specific template (e.g. Author: Admin) cannot be found.' ), 174 ), 175 'category' => array( 176 'title' => _x( 'Category Archives', 'Template name' ), 177 'description' => __( 'Displays a post category archive. This template will serve as a fallback when a more specific template (e.g. Category: Recipes) cannot be found.' ), 178 ), 179 'taxonomy' => array( 180 'title' => _x( 'Taxonomy', 'Template name' ), 181 'description' => __( 'Displays a custom taxonomy archive. Like categories and tags, taxonomies have terms which you use to classify things. For example: a taxonomy named "Art" can have multiple terms, such as "Modern" and "18th Century." This template will serve as a fallback when a more specific template (e.g. Taxonomy: Art) cannot be found.' ), 182 ), 183 'date' => array( 184 'title' => _x( 'Date Archives', 'Template name' ), 185 'description' => __( 'Displays a post archive when a specific date is visited (e.g., example.com/2023/).' ), 186 ), 187 'tag' => array( 188 'title' => _x( 'Tag Archives', 'Template name' ), 189 'description' => __( 'Displays a post tag archive. This template will serve as a fallback when a more specific template (e.g. Tag: Pizza) cannot be found.' ), 190 ), 191 'attachment' => array( 192 'title' => __( 'Attachment Pages' ), 193 'description' => __( 'Displays when a visitor views the dedicated page that exists for any media attachment.' ), 194 ), 195 'search' => array( 196 'title' => _x( 'Search Results', 'Template name' ), 197 'description' => __( 'Displays when a visitor performs a search on your website.' ), 198 ), 199 'privacy-policy' => array( 200 'title' => __( 'Privacy Policy' ), 201 'description' => __( 'Displays your site\'s Privacy Policy page.' ), 202 ), 203 '404' => array( 204 'title' => _x( 'Page: 404', 'Template name' ), 205 'description' => __( 'Displays when a visitor views a non-existent page, such as a dead link or a mistyped URL.' ), 206 ), 207 ); 208 209 // Add a title and description to post format templates. 210 $post_formats = get_post_format_strings(); 211 foreach ( $post_formats as $post_format_slug => $post_format_name ) { 212 $default_template_types[ 'taxonomy-post_format-post-format-' . $post_format_slug ] = array( 213 'title' => sprintf( 214 /* translators: %s: Post format name. */ 215 _x( 'Post Format: %s', 'Template name' ), 216 $post_format_name 217 ), 218 'description' => sprintf( 219 /* translators: %s: Post format name. */ 220 __( 'Displays the %s post format archive.' ), 221 $post_format_name 222 ), 223 ); 224 } 225 226 /** 227 * Filters the list of default template types. 228 * 229 * @since 5.9.0 230 * 231 * @param array[] $default_template_types { 232 * The default template types. 233 * 234 * @type array ...$0 { 235 * Data for the template type. 236 * 237 * @type string $title Template type title. 238 * @type string $description Template type description. 239 * } 240 * } 241 */ 242 return apply_filters( 'default_template_types', $default_template_types ); 243 } 244 245 /** 246 * Checks whether the input 'area' is a supported value. 247 * Returns the input if supported, otherwise returns the 'uncategorized' value. 248 * 249 * @since 5.9.0 250 * @access private 251 * 252 * @param string $type Template part area name. 253 * @return string Input if supported, else the uncategorized value. 254 */ 255 function _filter_block_template_part_area( $type ) { 256 $allowed_areas = array_map( 257 static function ( $item ) { 258 return $item['area']; 259 }, 260 get_allowed_block_template_part_areas() 261 ); 262 if ( in_array( $type, $allowed_areas, true ) ) { 263 return $type; 264 } 265 266 $warning_message = sprintf( 267 /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */ 268 __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ), 269 $type, 270 WP_TEMPLATE_PART_AREA_UNCATEGORIZED 271 ); 272 wp_trigger_error( __FUNCTION__, $warning_message ); 273 return WP_TEMPLATE_PART_AREA_UNCATEGORIZED; 274 } 275 276 /** 277 * Finds all nested template part file paths in a theme's directory. 278 * 279 * @since 5.9.0 280 * @access private 281 * 282 * @param string $base_directory The theme's file path. 283 * @return string[] A list of paths to all template part files. 284 */ 285 function _get_block_templates_paths( $base_directory ) { 286 static $template_path_list = array(); 287 if ( isset( $template_path_list[ $base_directory ] ) ) { 288 return $template_path_list[ $base_directory ]; 289 } 290 $path_list = array(); 291 if ( is_dir( $base_directory ) ) { 292 $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); 293 $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); 294 foreach ( $nested_html_files as $path => $file ) { 295 $path_list[] = $path; 296 } 297 } 298 $template_path_list[ $base_directory ] = $path_list; 299 return $path_list; 300 } 301 302 /** 303 * Retrieves the template file from the theme for a given slug. 304 * 305 * @since 5.9.0 306 * @access private 307 * 308 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 309 * @param string $slug Template slug. 310 * @return array|null { 311 * Array with template metadata if $template_type is one of 'wp_template' or 'wp_template_part', 312 * null otherwise. 313 * 314 * @type string $slug Template slug. 315 * @type string $path Template file path. 316 * @type string $theme Theme slug. 317 * @type string $type Template type. 318 * @type string $area Template area. Only for 'wp_template_part'. 319 * @type string $title Optional. Template title. 320 * @type string[] $postTypes Optional. List of post types that the template supports. Only for 'wp_template'. 321 * } 322 */ 323 function _get_block_template_file( $template_type, $slug ) { 324 if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { 325 return null; 326 } 327 328 $themes = array( 329 get_stylesheet() => get_stylesheet_directory(), 330 get_template() => get_template_directory(), 331 ); 332 foreach ( $themes as $theme_slug => $theme_dir ) { 333 $template_base_paths = get_block_theme_folders( $theme_slug ); 334 $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; 335 if ( file_exists( $file_path ) ) { 336 $new_template_item = array( 337 'slug' => $slug, 338 'path' => $file_path, 339 'theme' => $theme_slug, 340 'type' => $template_type, 341 ); 342 343 if ( 'wp_template_part' === $template_type ) { 344 return _add_block_template_part_area_info( $new_template_item ); 345 } 346 347 if ( 'wp_template' === $template_type ) { 348 return _add_block_template_info( $new_template_item ); 349 } 350 351 return $new_template_item; 352 } 353 } 354 355 return null; 356 } 357 358 /** 359 * Retrieves the template files from the theme. 360 * 361 * @since 5.9.0 362 * @since 6.3.0 Added the `$query` parameter. 363 * @access private 364 * 365 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 366 * @param array $query { 367 * Arguments to retrieve templates. Optional, empty by default. 368 * 369 * @type string[] $slug__in List of slugs to include. 370 * @type string[] $slug__not_in List of slugs to skip. 371 * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). 372 * @type string $post_type Post type to get the templates for. 373 * } 374 * 375 * @return array|null Template files on success, null if `$template_type` is not matched. 376 */ 377 function _get_block_templates_files( $template_type, $query = array() ) { 378 if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { 379 return null; 380 } 381 382 $default_template_types = array(); 383 if ( 'wp_template' === $template_type ) { 384 $default_template_types = get_default_block_template_types(); 385 } 386 387 // Prepare metadata from $query. 388 $slugs_to_include = isset( $query['slug__in'] ) ? $query['slug__in'] : array(); 389 $slugs_to_skip = isset( $query['slug__not_in'] ) ? $query['slug__not_in'] : array(); 390 $area = isset( $query['area'] ) ? $query['area'] : null; 391 $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; 392 393 $stylesheet = get_stylesheet(); 394 $template = get_template(); 395 $themes = array( 396 $stylesheet => get_stylesheet_directory(), 397 ); 398 // Add the parent theme if it's not the same as the current theme. 399 if ( $stylesheet !== $template ) { 400 $themes[ $template ] = get_template_directory(); 401 } 402 $template_files = array(); 403 foreach ( $themes as $theme_slug => $theme_dir ) { 404 $template_base_paths = get_block_theme_folders( $theme_slug ); 405 $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); 406 foreach ( $theme_template_files as $template_file ) { 407 $template_base_path = $template_base_paths[ $template_type ]; 408 $template_slug = substr( 409 $template_file, 410 // Starting position of slug. 411 strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ), 412 // Subtract ending '.html'. 413 -5 414 ); 415 416 // Skip this item if its slug doesn't match any of the slugs to include. 417 if ( ! empty( $slugs_to_include ) && ! in_array( $template_slug, $slugs_to_include, true ) ) { 418 continue; 419 } 420 421 // Skip this item if its slug matches any of the slugs to skip. 422 if ( ! empty( $slugs_to_skip ) && in_array( $template_slug, $slugs_to_skip, true ) ) { 423 continue; 424 } 425 426 /* 427 * The child theme items (stylesheet) are processed before the parent theme's (template). 428 * If a child theme defines a template, prevent the parent template from being added to the list as well. 429 */ 430 if ( isset( $template_files[ $template_slug ] ) ) { 431 continue; 432 } 433 434 $new_template_item = array( 435 'slug' => $template_slug, 436 'path' => $template_file, 437 'theme' => $theme_slug, 438 'type' => $template_type, 439 ); 440 441 if ( 'wp_template_part' === $template_type ) { 442 $candidate = _add_block_template_part_area_info( $new_template_item ); 443 if ( ! isset( $area ) || ( isset( $area ) && $area === $candidate['area'] ) ) { 444 $template_files[ $template_slug ] = $candidate; 445 } 446 } 447 448 if ( 'wp_template' === $template_type ) { 449 $candidate = _add_block_template_info( $new_template_item ); 450 $is_custom = ! isset( $default_template_types[ $candidate['slug'] ] ); 451 452 if ( 453 ! $post_type || 454 ( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) ) 455 ) { 456 $template_files[ $template_slug ] = $candidate; 457 } 458 459 // The custom templates with no associated post types are available for all post types. 460 if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) { 461 $template_files[ $template_slug ] = $candidate; 462 } 463 } 464 } 465 } 466 467 return array_values( $template_files ); 468 } 469 470 /** 471 * Attempts to add custom template information to the template item. 472 * 473 * @since 5.9.0 474 * @access private 475 * 476 * @param array $template_item Template to add information to (requires 'slug' field). 477 * @return array Template item. 478 */ 479 function _add_block_template_info( $template_item ) { 480 if ( ! wp_theme_has_theme_json() ) { 481 return $template_item; 482 } 483 484 $theme_data = wp_get_theme_data_custom_templates(); 485 if ( isset( $theme_data[ $template_item['slug'] ] ) ) { 486 $template_item['title'] = $theme_data[ $template_item['slug'] ]['title']; 487 $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes']; 488 } 489 490 return $template_item; 491 } 492 493 /** 494 * Attempts to add the template part's area information to the input template. 495 * 496 * @since 5.9.0 497 * @access private 498 * 499 * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). 500 * @return array Template info. 501 */ 502 function _add_block_template_part_area_info( $template_info ) { 503 if ( wp_theme_has_theme_json() ) { 504 $theme_data = wp_get_theme_data_template_parts(); 505 } 506 507 if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { 508 $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; 509 $template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); 510 } else { 511 $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; 512 } 513 514 return $template_info; 515 } 516 517 /** 518 * Returns an array containing the references of 519 * the passed blocks and their inner blocks. 520 * 521 * @since 5.9.0 522 * @access private 523 * 524 * @param array $blocks array of blocks. 525 * @return array block references to the passed blocks and their inner blocks. 526 */ 527 function _flatten_blocks( &$blocks ) { 528 $all_blocks = array(); 529 $queue = array(); 530 foreach ( $blocks as &$block ) { 531 $queue[] = &$block; 532 } 533 534 while ( count( $queue ) > 0 ) { 535 $block = &$queue[0]; 536 array_shift( $queue ); 537 $all_blocks[] = &$block; 538 539 if ( ! empty( $block['innerBlocks'] ) ) { 540 foreach ( $block['innerBlocks'] as &$inner_block ) { 541 $queue[] = &$inner_block; 542 } 543 } 544 } 545 546 return $all_blocks; 547 } 548 549 /** 550 * Injects the active theme's stylesheet as a `theme` attribute 551 * into a given template part block. 552 * 553 * @since 6.4.0 554 * @access private 555 * 556 * @param array $block a parsed block. 557 */ 558 function _inject_theme_attribute_in_template_part_block( &$block ) { 559 if ( 560 'core/template-part' === $block['blockName'] && 561 ! isset( $block['attrs']['theme'] ) 562 ) { 563 $block['attrs']['theme'] = get_stylesheet(); 564 } 565 } 566 567 /** 568 * Removes the `theme` attribute from a given template part block. 569 * 570 * @since 6.4.0 571 * @access private 572 * 573 * @param array $block a parsed block. 574 */ 575 function _remove_theme_attribute_from_template_part_block( &$block ) { 576 if ( 577 'core/template-part' === $block['blockName'] && 578 isset( $block['attrs']['theme'] ) 579 ) { 580 unset( $block['attrs']['theme'] ); 581 } 582 } 583 584 /** 585 * Builds a unified template object based on a theme file. 586 * 587 * @since 5.9.0 588 * @since 6.3.0 Added `modified` property to template objects. 589 * @access private 590 * 591 * @param array $template_file Theme file. 592 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 593 * @return WP_Block_Template Template. 594 */ 595 function _build_block_template_result_from_file( $template_file, $template_type ) { 596 $default_template_types = get_default_block_template_types(); 597 $theme = get_stylesheet(); 598 599 $template = new WP_Block_Template(); 600 $template->id = $theme . '//' . $template_file['slug']; 601 $template->theme = $theme; 602 $template->content = file_get_contents( $template_file['path'] ); 603 $template->slug = $template_file['slug']; 604 $template->source = 'theme'; 605 $template->type = $template_type; 606 $template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug']; 607 $template->status = 'publish'; 608 $template->has_theme_file = true; 609 $template->is_custom = true; 610 $template->modified = null; 611 612 if ( 'wp_template' === $template_type ) { 613 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] ); 614 if ( $registered_template ) { 615 $template->plugin = $registered_template->plugin; 616 $template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title; 617 $template->description = empty( $template->description ) ? $registered_template->description : $template->description; 618 } 619 } 620 621 if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { 622 $template->description = $default_template_types[ $template_file['slug'] ]['description']; 623 $template->title = $default_template_types[ $template_file['slug'] ]['title']; 624 $template->is_custom = false; 625 } 626 627 if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) { 628 $template->post_types = $template_file['postTypes']; 629 } 630 631 if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { 632 $template->area = $template_file['area']; 633 } 634 635 if ( 'wp_template_part' === $template->type ) { 636 /* 637 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part, 638 * we need to wrap its content a mock template part block and traverse it. 639 */ 640 $content = get_comment_delimited_block_content( 641 'core/template-part', 642 array(), 643 $template->content 644 ); 645 $content = apply_block_hooks_to_content( 646 $content, 647 $template, 648 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' 649 ); 650 $template->content = remove_serialized_parent_block( $content ); 651 } else { 652 $template->content = apply_block_hooks_to_content( 653 $template->content, 654 $template, 655 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' 656 ); 657 } 658 659 return $template; 660 } 661 662 /** 663 * Builds the title and description of a post-specific template based on the underlying referenced post. 664 * 665 * Mutates the underlying template object. 666 * 667 * @since 6.1.0 668 * @access private 669 * 670 * @param string $post_type Post type, e.g. page, post, product. 671 * @param string $slug Slug of the post, e.g. a-story-about-shoes. 672 * @param WP_Block_Template $template Template to mutate adding the description and title computed. 673 * @return bool Returns true if the referenced post was found and false otherwise. 674 */ 675 function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) { 676 $post_type_object = get_post_type_object( $post_type ); 677 678 $default_args = array( 679 'post_type' => $post_type, 680 'post_status' => 'publish', 681 'posts_per_page' => 1, 682 'update_post_meta_cache' => false, 683 'update_post_term_cache' => false, 684 'ignore_sticky_posts' => true, 685 'no_found_rows' => true, 686 ); 687 688 $args = array( 689 'name' => $slug, 690 ); 691 $args = wp_parse_args( $args, $default_args ); 692 693 $posts_query = new WP_Query( $args ); 694 695 if ( empty( $posts_query->posts ) ) { 696 $template->title = sprintf( 697 /* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */ 698 __( 'Not found: %1$s (%2$s)' ), 699 $post_type_object->labels->singular_name, 700 $slug 701 ); 702 703 return false; 704 } 705 706 $post_title = $posts_query->posts[0]->post_title; 707 708 $template->title = sprintf( 709 /* translators: Custom template title in the Site Editor. 1: Post type singular name, 2: Post title. */ 710 __( '%1$s: %2$s' ), 711 $post_type_object->labels->singular_name, 712 $post_title 713 ); 714 715 $template->description = sprintf( 716 /* translators: Custom template description in the Site Editor. %s: Post title. */ 717 __( 'Template for %s' ), 718 $post_title 719 ); 720 721 $args = array( 722 'title' => $post_title, 723 ); 724 $args = wp_parse_args( $args, $default_args ); 725 726 $posts_with_same_title_query = new WP_Query( $args ); 727 728 if ( count( $posts_with_same_title_query->posts ) > 1 ) { 729 $template->title = sprintf( 730 /* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */ 731 __( '%1$s (%2$s)' ), 732 $template->title, 733 $slug 734 ); 735 } 736 737 return true; 738 } 739 740 /** 741 * Builds the title and description of a taxonomy-specific template based on the underlying entity referenced. 742 * 743 * Mutates the underlying template object. 744 * 745 * @since 6.1.0 746 * @access private 747 * 748 * @param string $taxonomy Identifier of the taxonomy, e.g. category. 749 * @param string $slug Slug of the term, e.g. shoes. 750 * @param WP_Block_Template $template Template to mutate adding the description and title computed. 751 * @return bool True if the term referenced was found and false otherwise. 752 */ 753 function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) { 754 $taxonomy_object = get_taxonomy( $taxonomy ); 755 756 $default_args = array( 757 'taxonomy' => $taxonomy, 758 'hide_empty' => false, 759 'update_term_meta_cache' => false, 760 ); 761 762 $term_query = new WP_Term_Query(); 763 764 $args = array( 765 'number' => 1, 766 'slug' => $slug, 767 ); 768 $args = wp_parse_args( $args, $default_args ); 769 770 $terms_query = $term_query->query( $args ); 771 772 if ( empty( $terms_query ) ) { 773 $template->title = sprintf( 774 /* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */ 775 __( 'Not found: %1$s (%2$s)' ), 776 $taxonomy_object->labels->singular_name, 777 $slug 778 ); 779 return false; 780 } 781 782 $term_title = $terms_query[0]->name; 783 784 $template->title = sprintf( 785 /* translators: Custom template title in the Site Editor. 1: Taxonomy singular name, 2: Term title. */ 786 __( '%1$s: %2$s' ), 787 $taxonomy_object->labels->singular_name, 788 $term_title 789 ); 790 791 $template->description = sprintf( 792 /* translators: Custom template description in the Site Editor. %s: Term title. */ 793 __( 'Template for %s' ), 794 $term_title 795 ); 796 797 $term_query = new WP_Term_Query(); 798 799 $args = array( 800 'number' => 2, 801 'name' => $term_title, 802 ); 803 $args = wp_parse_args( $args, $default_args ); 804 805 $terms_with_same_title_query = $term_query->query( $args ); 806 807 if ( count( $terms_with_same_title_query ) > 1 ) { 808 $template->title = sprintf( 809 /* translators: Custom template title in the Site Editor. 1: Template title, 2: Term slug. */ 810 __( '%1$s (%2$s)' ), 811 $template->title, 812 $slug 813 ); 814 } 815 816 return true; 817 } 818 819 /** 820 * Builds a block template object from a post object. 821 * 822 * This is a helper function that creates a block template object from a given post object. 823 * It is self-sufficient in that it only uses information passed as arguments; it does not 824 * query the database for additional information. 825 * 826 * @since 6.5.3 827 * @access private 828 * 829 * @param WP_Post $post Template post. 830 * @param array $terms Additional terms to inform the template object. 831 * @param array $meta Additional meta fields to inform the template object. 832 * @return WP_Block_Template|WP_Error Template or error object. 833 */ 834 function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) { 835 if ( empty( $terms['wp_theme'] ) ) { 836 return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); 837 } 838 $theme = $terms['wp_theme']; 839 840 $default_template_types = get_default_block_template_types(); 841 842 $template_file = _get_block_template_file( $post->post_type, $post->post_name ); 843 $has_theme_file = get_stylesheet() === $theme && null !== $template_file; 844 845 $template = new WP_Block_Template(); 846 $template->wp_id = $post->ID; 847 $template->id = $theme . '//' . $post->post_name; 848 $template->theme = $theme; 849 $template->content = $post->post_content; 850 $template->slug = $post->post_name; 851 $template->source = 'custom'; 852 $template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null; 853 $template->type = $post->post_type; 854 $template->description = $post->post_excerpt; 855 $template->title = $post->post_title; 856 $template->status = $post->post_status; 857 $template->has_theme_file = $has_theme_file; 858 $template->is_custom = empty( $meta['is_wp_suggestion'] ); 859 $template->author = $post->post_author; 860 $template->modified = $post->post_modified; 861 862 if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { 863 $template->post_types = $template_file['postTypes']; 864 } 865 866 if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { 867 $template->is_custom = false; 868 } 869 870 if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) { 871 $template->area = $terms['wp_template_part_area']; 872 } 873 874 return $template; 875 } 876 877 /** 878 * Builds a unified template object based a post Object. 879 * 880 * @since 5.9.0 881 * @since 6.3.0 Added `modified` property to template objects. 882 * @since 6.4.0 Added support for a revision post to be passed to this function. 883 * @access private 884 * 885 * @param WP_Post $post Template post. 886 * @return WP_Block_Template|WP_Error Template or error object. 887 */ 888 function _build_block_template_result_from_post( $post ) { 889 $post_id = wp_is_post_revision( $post ); 890 if ( ! $post_id ) { 891 $post_id = $post; 892 } 893 $parent_post = get_post( $post_id ); 894 $post->post_name = $parent_post->post_name; 895 $post->post_type = $parent_post->post_type; 896 897 $terms = get_the_terms( $parent_post, 'wp_theme' ); 898 899 if ( is_wp_error( $terms ) ) { 900 return $terms; 901 } 902 903 if ( ! $terms ) { 904 return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); 905 } 906 907 $terms = array( 908 'wp_theme' => $terms[0]->name, 909 ); 910 911 if ( 'wp_template_part' === $parent_post->post_type ) { 912 $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); 913 if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { 914 $terms['wp_template_part_area'] = $type_terms[0]->name; 915 } 916 } 917 918 $meta = array( 919 'origin' => get_post_meta( $parent_post->ID, 'origin', true ), 920 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), 921 ); 922 923 $template = _build_block_template_object_from_post_object( $post, $terms, $meta ); 924 925 if ( is_wp_error( $template ) ) { 926 return $template; 927 } 928 929 // Check for a block template without a description and title or with a title equal to the slug. 930 if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) { 931 $matches = array(); 932 933 // Check for a block template for a single author, page, post, tag, category, custom post type, or custom taxonomy. 934 if ( preg_match( '/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches ) ) { 935 $type = $matches[1]; 936 $slug_remaining = $matches[2]; 937 938 switch ( $type ) { 939 case 'author': 940 $nice_name = $slug_remaining; 941 $users = get_users( 942 array( 943 'capability' => 'edit_posts', 944 'search' => $nice_name, 945 'search_columns' => array( 'user_nicename' ), 946 'fields' => 'display_name', 947 ) 948 ); 949 950 if ( empty( $users ) ) { 951 $template->title = sprintf( 952 /* translators: Custom template title in the Site Editor, referencing a deleted author. %s: Author nicename. */ 953 __( 'Deleted author: %s' ), 954 $nice_name 955 ); 956 } else { 957 $author_name = $users[0]; 958 959 $template->title = sprintf( 960 /* translators: Custom template title in the Site Editor. %s: Author name. */ 961 __( 'Author: %s' ), 962 $author_name 963 ); 964 965 $template->description = sprintf( 966 /* translators: Custom template description in the Site Editor. %s: Author name. */ 967 __( 'Template for %s' ), 968 $author_name 969 ); 970 971 $users_with_same_name = get_users( 972 array( 973 'capability' => 'edit_posts', 974 'search' => $author_name, 975 'search_columns' => array( 'display_name' ), 976 'fields' => 'display_name', 977 ) 978 ); 979 980 if ( count( $users_with_same_name ) > 1 ) { 981 $template->title = sprintf( 982 /* translators: Custom template title in the Site Editor. 1: Template title of an author template, 2: Author nicename. */ 983 __( '%1$s (%2$s)' ), 984 $template->title, 985 $nice_name 986 ); 987 } 988 } 989 break; 990 case 'page': 991 _wp_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template ); 992 break; 993 case 'single': 994 $post_types = get_post_types(); 995 996 foreach ( $post_types as $post_type ) { 997 $post_type_length = strlen( $post_type ) + 1; 998 999 // If $slug_remaining starts with $post_type followed by a hyphen. 1000 if ( 0 === strncmp( $slug_remaining, $post_type . '-', $post_type_length ) ) { 1001 $slug = substr( $slug_remaining, $post_type_length, strlen( $slug_remaining ) ); 1002 $found = _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, $template ); 1003 1004 if ( $found ) { 1005 break; 1006 } 1007 } 1008 } 1009 break; 1010 case 'tag': 1011 _wp_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template ); 1012 break; 1013 case 'category': 1014 _wp_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template ); 1015 break; 1016 case 'taxonomy': 1017 $taxonomies = get_taxonomies(); 1018 1019 foreach ( $taxonomies as $taxonomy ) { 1020 $taxonomy_length = strlen( $taxonomy ) + 1; 1021 1022 // If $slug_remaining starts with $taxonomy followed by a hyphen. 1023 if ( 0 === strncmp( $slug_remaining, $taxonomy . '-', $taxonomy_length ) ) { 1024 $slug = substr( $slug_remaining, $taxonomy_length, strlen( $slug_remaining ) ); 1025 $found = _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, $template ); 1026 1027 if ( $found ) { 1028 break; 1029 } 1030 } 1031 } 1032 break; 1033 } 1034 } 1035 } 1036 1037 if ( 'wp_template' === $post->post_type ) { 1038 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug ); 1039 if ( $registered_template ) { 1040 $template->plugin = $registered_template->plugin; 1041 $template->origin = 1042 'theme' !== $template->origin && 'theme' !== $template->source ? 1043 'plugin' : 1044 $template->origin; 1045 $template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title; 1046 $template->description = empty( $template->description ) ? $registered_template->description : $template->description; 1047 } 1048 } 1049 1050 if ( 'wp_template_part' === $template->type ) { 1051 $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ); 1052 $attributes = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array(); 1053 1054 /* 1055 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part, 1056 * we need to wrap its content a mock template part block and traverse it. 1057 */ 1058 $content = get_comment_delimited_block_content( 1059 'core/template-part', 1060 $attributes, 1061 $template->content 1062 ); 1063 $content = apply_block_hooks_to_content( 1064 $content, 1065 $template, 1066 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' 1067 ); 1068 $template->content = remove_serialized_parent_block( $content ); 1069 } else { 1070 $template->content = apply_block_hooks_to_content( 1071 $template->content, 1072 $template, 1073 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' 1074 ); 1075 } 1076 1077 return $template; 1078 } 1079 1080 /** 1081 * Retrieves a list of unified template objects based on a query. 1082 * 1083 * @since 5.8.0 1084 * 1085 * @param array $query { 1086 * Optional. Arguments to retrieve templates. 1087 * 1088 * @type string[] $slug__in List of slugs to include. 1089 * @type int $wp_id Post ID of customized template. 1090 * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). 1091 * @type string $post_type Post type to get the templates for. 1092 * } 1093 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1094 * @return WP_Block_Template[] Array of block templates. 1095 */ 1096 function get_block_templates( $query = array(), $template_type = 'wp_template' ) { 1097 /** 1098 * Filters the block templates array before the query takes place. 1099 * 1100 * Return a non-null value to bypass the WordPress queries. 1101 * 1102 * @since 5.9.0 1103 * 1104 * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query, 1105 * or null to allow WP to run its normal queries. 1106 * @param array $query { 1107 * Arguments to retrieve templates. All arguments are optional. 1108 * 1109 * @type string[] $slug__in List of slugs to include. 1110 * @type int $wp_id Post ID of customized template. 1111 * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). 1112 * @type string $post_type Post type to get the templates for. 1113 * } 1114 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1115 */ 1116 $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); 1117 if ( ! is_null( $templates ) ) { 1118 return $templates; 1119 } 1120 1121 $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; 1122 $wp_query_args = array( 1123 'post_status' => array( 'auto-draft', 'draft', 'publish' ), 1124 'post_type' => $template_type, 1125 'posts_per_page' => -1, 1126 'no_found_rows' => true, 1127 'lazy_load_term_meta' => false, 1128 'tax_query' => array( 1129 array( 1130 'taxonomy' => 'wp_theme', 1131 'field' => 'name', 1132 'terms' => get_stylesheet(), 1133 ), 1134 ), 1135 ); 1136 1137 if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { 1138 $wp_query_args['tax_query'][] = array( 1139 'taxonomy' => 'wp_template_part_area', 1140 'field' => 'name', 1141 'terms' => $query['area'], 1142 ); 1143 $wp_query_args['tax_query']['relation'] = 'AND'; 1144 } 1145 1146 if ( ! empty( $query['slug__in'] ) ) { 1147 $wp_query_args['post_name__in'] = $query['slug__in']; 1148 $wp_query_args['posts_per_page'] = count( array_unique( $query['slug__in'] ) ); 1149 } 1150 1151 // This is only needed for the regular templates/template parts post type listing and editor. 1152 if ( isset( $query['wp_id'] ) ) { 1153 $wp_query_args['p'] = $query['wp_id']; 1154 } else { 1155 $wp_query_args['post_status'] = 'publish'; 1156 } 1157 1158 $template_query = new WP_Query( $wp_query_args ); 1159 $query_result = array(); 1160 foreach ( $template_query->posts as $post ) { 1161 $template = _build_block_template_result_from_post( $post ); 1162 1163 if ( is_wp_error( $template ) ) { 1164 continue; 1165 } 1166 1167 if ( $post_type && ! $template->is_custom ) { 1168 continue; 1169 } 1170 1171 if ( 1172 $post_type && 1173 isset( $template->post_types ) && 1174 ! in_array( $post_type, $template->post_types, true ) 1175 ) { 1176 continue; 1177 } 1178 1179 $query_result[] = $template; 1180 } 1181 1182 if ( ! isset( $query['wp_id'] ) ) { 1183 /* 1184 * If the query has found some user templates, those have priority 1185 * over the theme-provided ones, so we skip querying and building them. 1186 */ 1187 $query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); 1188 $template_files = _get_block_templates_files( $template_type, $query ); 1189 foreach ( $template_files as $template_file ) { 1190 $query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); 1191 } 1192 1193 if ( 'wp_template' === $template_type ) { 1194 // Add templates registered in the template registry. Filtering out the ones which have a theme file. 1195 $registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query ); 1196 $matching_registered_templates = array_filter( 1197 $registered_templates, 1198 function ( $registered_template ) use ( $template_files ) { 1199 foreach ( $template_files as $template_file ) { 1200 if ( $template_file['slug'] === $registered_template->slug ) { 1201 return false; 1202 } 1203 } 1204 return true; 1205 } 1206 ); 1207 $query_result = array_merge( $query_result, $matching_registered_templates ); 1208 } 1209 } 1210 1211 /** 1212 * Filters the array of queried block templates array after they've been fetched. 1213 * 1214 * @since 5.9.0 1215 * 1216 * @param WP_Block_Template[] $query_result Array of found block templates. 1217 * @param array $query { 1218 * Arguments to retrieve templates. All arguments are optional. 1219 * 1220 * @type string[] $slug__in List of slugs to include. 1221 * @type int $wp_id Post ID of customized template. 1222 * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). 1223 * @type string $post_type Post type to get the templates for. 1224 * } 1225 * @param string $template_type wp_template or wp_template_part. 1226 */ 1227 return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); 1228 } 1229 1230 /** 1231 * Retrieves a single unified template object using its id. 1232 * 1233 * @since 5.8.0 1234 * 1235 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1236 * @param string $template_type Optional. Template type. Either 'wp_template' or 'wp_template_part'. 1237 * Default 'wp_template'. 1238 * @return WP_Block_Template|null Template. 1239 */ 1240 function get_block_template( $id, $template_type = 'wp_template' ) { 1241 /** 1242 * Filters the block template object before the query takes place. 1243 * 1244 * Return a non-null value to bypass the WordPress queries. 1245 * 1246 * @since 5.9.0 1247 * 1248 * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, 1249 * or null to allow WP to run its normal queries. 1250 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1251 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1252 */ 1253 $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); 1254 if ( ! is_null( $block_template ) ) { 1255 return $block_template; 1256 } 1257 1258 $parts = explode( '//', $id, 2 ); 1259 if ( count( $parts ) < 2 ) { 1260 return null; 1261 } 1262 list( $theme, $slug ) = $parts; 1263 $wp_query_args = array( 1264 'post_name__in' => array( $slug ), 1265 'post_type' => $template_type, 1266 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), 1267 'posts_per_page' => 1, 1268 'no_found_rows' => true, 1269 'tax_query' => array( 1270 array( 1271 'taxonomy' => 'wp_theme', 1272 'field' => 'name', 1273 'terms' => $theme, 1274 ), 1275 ), 1276 ); 1277 $template_query = new WP_Query( $wp_query_args ); 1278 $posts = $template_query->posts; 1279 1280 if ( count( $posts ) > 0 ) { 1281 $template = _build_block_template_result_from_post( $posts[0] ); 1282 1283 if ( ! is_wp_error( $template ) ) { 1284 return $template; 1285 } 1286 } 1287 1288 $block_template = get_block_file_template( $id, $template_type ); 1289 1290 /** 1291 * Filters the queried block template object after it's been fetched. 1292 * 1293 * @since 5.9.0 1294 * 1295 * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one. 1296 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1297 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1298 */ 1299 return apply_filters( 'get_block_template', $block_template, $id, $template_type ); 1300 } 1301 1302 /** 1303 * Retrieves a unified template object based on a theme file. 1304 * 1305 * This is a fallback of get_block_template(), used when no templates are found in the database. 1306 * 1307 * @since 5.9.0 1308 * 1309 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1310 * @param string $template_type Optional. Template type. Either 'wp_template' or 'wp_template_part'. 1311 * Default 'wp_template'. 1312 * @return WP_Block_Template|null The found block template, or null if there isn't one. 1313 */ 1314 function get_block_file_template( $id, $template_type = 'wp_template' ) { 1315 /** 1316 * Filters the block template object before the theme file discovery takes place. 1317 * 1318 * Return a non-null value to bypass the WordPress theme file discovery. 1319 * 1320 * @since 5.9.0 1321 * 1322 * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, 1323 * or null to allow WP to run its normal queries. 1324 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1325 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1326 */ 1327 $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); 1328 if ( ! is_null( $block_template ) ) { 1329 return $block_template; 1330 } 1331 1332 $parts = explode( '//', $id, 2 ); 1333 if ( count( $parts ) < 2 ) { 1334 /** This filter is documented in wp-includes/block-template-utils.php */ 1335 return apply_filters( 'get_block_file_template', null, $id, $template_type ); 1336 } 1337 list( $theme, $slug ) = $parts; 1338 1339 if ( get_stylesheet() === $theme ) { 1340 $template_file = _get_block_template_file( $template_type, $slug ); 1341 if ( null !== $template_file ) { 1342 $block_template = _build_block_template_result_from_file( $template_file, $template_type ); 1343 1344 /** This filter is documented in wp-includes/block-template-utils.php */ 1345 return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); 1346 } 1347 } 1348 1349 $block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug ); 1350 1351 /** 1352 * Filters the block template object after it has been (potentially) fetched from the theme file. 1353 * 1354 * @since 5.9.0 1355 * 1356 * @param WP_Block_Template|null $block_template The found block template, or null if there is none. 1357 * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). 1358 * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. 1359 */ 1360 return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); 1361 } 1362 1363 /** 1364 * Prints a block template part. 1365 * 1366 * @since 5.9.0 1367 * 1368 * @param string $part The block template part to print, for example 'header' or 'footer'. 1369 */ 1370 function block_template_part( $part ) { 1371 $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); 1372 if ( ! $template_part || empty( $template_part->content ) ) { 1373 return; 1374 } 1375 echo do_blocks( $template_part->content ); 1376 } 1377 1378 /** 1379 * Prints the header block template part. 1380 * 1381 * @since 5.9.0 1382 */ 1383 function block_header_area() { 1384 block_template_part( 'header' ); 1385 } 1386 1387 /** 1388 * Prints the footer block template part. 1389 * 1390 * @since 5.9.0 1391 */ 1392 function block_footer_area() { 1393 block_template_part( 'footer' ); 1394 } 1395 1396 /** 1397 * Determines whether a theme directory should be ignored during export. 1398 * 1399 * @since 6.0.0 1400 * 1401 * @param string $path The path of the file in the theme. 1402 * @return bool Whether this file is in an ignored directory. 1403 */ 1404 function wp_is_theme_directory_ignored( $path ) { 1405 $directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' ); 1406 1407 foreach ( $directories_to_ignore as $directory ) { 1408 if ( str_starts_with( $path, $directory ) ) { 1409 return true; 1410 } 1411 } 1412 1413 return false; 1414 } 1415 1416 /** 1417 * Creates an export of the current templates and 1418 * template parts from the site editor at the 1419 * specified path in a ZIP file. 1420 * 1421 * @since 5.9.0 1422 * @since 6.0.0 Adds the whole theme to the export archive. 1423 * 1424 * @return WP_Error|string Path of the ZIP file or error on failure. 1425 */ 1426 function wp_generate_block_templates_export_file() { 1427 $wp_version = wp_get_wp_version(); 1428 1429 if ( ! class_exists( 'ZipArchive' ) ) { 1430 return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) ); 1431 } 1432 1433 $obscura = wp_generate_password( 12, false, false ); 1434 $theme_name = basename( get_stylesheet() ); 1435 $filename = get_temp_dir() . $theme_name . $obscura . '.zip'; 1436 1437 $zip = new ZipArchive(); 1438 if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { 1439 return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) ); 1440 } 1441 1442 $zip->addEmptyDir( 'templates' ); 1443 $zip->addEmptyDir( 'parts' ); 1444 1445 // Get path of the theme. 1446 $theme_path = wp_normalize_path( get_stylesheet_directory() ); 1447 1448 // Create recursive directory iterator. 1449 $theme_files = new RecursiveIteratorIterator( 1450 new RecursiveDirectoryIterator( $theme_path ), 1451 RecursiveIteratorIterator::LEAVES_ONLY 1452 ); 1453 1454 // Make a copy of the current theme. 1455 foreach ( $theme_files as $file ) { 1456 // Skip directories as they are added automatically. 1457 if ( ! $file->isDir() ) { 1458 // Get real and relative path for current file. 1459 $file_path = wp_normalize_path( $file ); 1460 $relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); 1461 1462 if ( ! wp_is_theme_directory_ignored( $relative_path ) ) { 1463 $zip->addFile( $file_path, $relative_path ); 1464 } 1465 } 1466 } 1467 1468 // Load templates into the zip file. 1469 $templates = get_block_templates(); 1470 foreach ( $templates as $template ) { 1471 $template->content = traverse_and_serialize_blocks( 1472 parse_blocks( $template->content ), 1473 '_remove_theme_attribute_from_template_part_block' 1474 ); 1475 1476 $zip->addFromString( 1477 'templates/' . $template->slug . '.html', 1478 $template->content 1479 ); 1480 } 1481 1482 // Load template parts into the zip file. 1483 $template_parts = get_block_templates( array(), 'wp_template_part' ); 1484 foreach ( $template_parts as $template_part ) { 1485 $zip->addFromString( 1486 'parts/' . $template_part->slug . '.html', 1487 $template_part->content 1488 ); 1489 } 1490 1491 // Load theme.json into the zip file. 1492 $tree = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) ); 1493 // Merge with user data. 1494 $tree->merge( WP_Theme_JSON_Resolver::get_user_data() ); 1495 1496 $theme_json_raw = $tree->get_data(); 1497 // If a version is defined, add a schema. 1498 if ( $theme_json_raw['version'] ) { 1499 $theme_json_version = 'wp/' . substr( $wp_version, 0, 3 ); 1500 $schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' ); 1501 $theme_json_raw = array_merge( $schema, $theme_json_raw ); 1502 } 1503 1504 // Convert to a string. 1505 $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); 1506 1507 // Replace 4 spaces with a tab. 1508 $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); 1509 1510 // Add the theme.json file to the zip. 1511 $zip->addFromString( 1512 'theme.json', 1513 $theme_json_tabbed 1514 ); 1515 1516 // Save changes to the zip file. 1517 $zip->close(); 1518 1519 return $filename; 1520 } 1521 1522 /** 1523 * Gets the template hierarchy for the given template slug to be created. 1524 * 1525 * Note: Always add `index` as the last fallback template. 1526 * 1527 * @since 6.1.0 1528 * 1529 * @param string $slug The template slug to be created. 1530 * @param bool $is_custom Optional. Indicates if a template is custom or 1531 * part of the template hierarchy. Default false. 1532 * @param string $template_prefix Optional. The template prefix for the created template. 1533 * Used to extract the main template type, e.g. 1534 * in `taxonomy-books` the `taxonomy` is extracted. 1535 * Default empty string. 1536 * @return string[] The template hierarchy. 1537 */ 1538 function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) { 1539 if ( 'index' === $slug ) { 1540 /** This filter is documented in wp-includes/template.php */ 1541 return apply_filters( 'index_template_hierarchy', array( 'index' ) ); 1542 } 1543 if ( $is_custom ) { 1544 /** This filter is documented in wp-includes/template.php */ 1545 return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) ); 1546 } 1547 if ( 'front-page' === $slug ) { 1548 /** This filter is documented in wp-includes/template.php */ 1549 return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) ); 1550 } 1551 1552 $matches = array(); 1553 1554 $template_hierarchy = array( $slug ); 1555 // Most default templates don't have `$template_prefix` assigned. 1556 if ( ! empty( $template_prefix ) ) { 1557 list( $type ) = explode( '-', $template_prefix ); 1558 // We need these checks because we always add the `$slug` above. 1559 if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { 1560 $template_hierarchy[] = $template_prefix; 1561 } 1562 if ( $slug !== $type ) { 1563 $template_hierarchy[] = $type; 1564 } 1565 } elseif ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) { 1566 $template_hierarchy[] = $matches[1]; 1567 } elseif ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) { 1568 $type = $matches[1]; 1569 $slug_remaining = $matches[2]; 1570 1571 $items = 'single' === $type ? get_post_types() : get_taxonomies(); 1572 foreach ( $items as $item ) { 1573 if ( ! str_starts_with( $slug_remaining, $item ) ) { 1574 continue; 1575 } 1576 1577 // If $slug_remaining is equal to $post_type or $taxonomy we have 1578 // the single-$post_type template or the taxonomy-$taxonomy template. 1579 if ( $slug_remaining === $item ) { 1580 $template_hierarchy[] = $type; 1581 break; 1582 } 1583 1584 // If $slug_remaining is single-$post_type-$slug template. 1585 if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) { 1586 $template_hierarchy[] = "$type-$item"; 1587 $template_hierarchy[] = $type; 1588 break; 1589 } 1590 } 1591 } 1592 // Handle `archive` template. 1593 if ( 1594 str_starts_with( $slug, 'author' ) || 1595 str_starts_with( $slug, 'taxonomy' ) || 1596 str_starts_with( $slug, 'category' ) || 1597 str_starts_with( $slug, 'tag' ) || 1598 'date' === $slug 1599 ) { 1600 $template_hierarchy[] = 'archive'; 1601 } 1602 // Handle `single` template. 1603 if ( 'attachment' === $slug ) { 1604 $template_hierarchy[] = 'single'; 1605 } 1606 // Handle `singular` template. 1607 if ( 1608 str_starts_with( $slug, 'single' ) || 1609 str_starts_with( $slug, 'page' ) || 1610 'attachment' === $slug 1611 ) { 1612 $template_hierarchy[] = 'singular'; 1613 } 1614 $template_hierarchy[] = 'index'; 1615 1616 $template_type = ''; 1617 if ( ! empty( $template_prefix ) ) { 1618 list( $template_type ) = explode( '-', $template_prefix ); 1619 } else { 1620 list( $template_type ) = explode( '-', $slug ); 1621 } 1622 $valid_template_types = array( '404', 'archive', 'attachment', 'author', 'category', 'date', 'embed', 'frontpage', 'home', 'index', 'page', 'paged', 'privacypolicy', 'search', 'single', 'singular', 'tag', 'taxonomy' ); 1623 if ( in_array( $template_type, $valid_template_types, true ) ) { 1624 /** This filter is documented in wp-includes/template.php */ 1625 return apply_filters( "{$template_type}_template_hierarchy", $template_hierarchy ); 1626 } 1627 return $template_hierarchy; 1628 } 1629 1630 /** 1631 * Inject ignoredHookedBlocks metadata attributes into a template or template part. 1632 * 1633 * Given an object that represents a `wp_template` or `wp_template_part` post object 1634 * prepared for inserting or updating the database, locate all blocks that have 1635 * hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor 1636 * blocks to reflect the latter. 1637 * 1638 * @since 6.5.0 1639 * @access private 1640 * 1641 * @param stdClass $changes An object representing a template or template part 1642 * prepared for inserting or updating the database. 1643 * @param WP_REST_Request $deprecated Deprecated. Not used. 1644 * @return stdClass|WP_Error The updated object representing a template or template part. 1645 */ 1646 function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) { 1647 if ( null !== $deprecated ) { 1648 _deprecated_argument( __FUNCTION__, '6.5.3' ); 1649 } 1650 1651 if ( ! isset( $changes->post_content ) ) { 1652 return $changes; 1653 } 1654 1655 $hooked_blocks = get_hooked_blocks(); 1656 if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { 1657 return $changes; 1658 } 1659 1660 $meta = isset( $changes->meta_input ) ? $changes->meta_input : array(); 1661 $terms = isset( $changes->tax_input ) ? $changes->tax_input : array(); 1662 1663 if ( empty( $changes->ID ) ) { 1664 // There's no post object for this template in the database for this template yet. 1665 $post = $changes; 1666 } else { 1667 // Find the existing post object. 1668 $post = get_post( $changes->ID ); 1669 1670 // If the post is a revision, use the parent post's post_name and post_type. 1671 $post_id = wp_is_post_revision( $post ); 1672 if ( $post_id ) { 1673 $parent_post = get_post( $post_id ); 1674 $post->post_name = $parent_post->post_name; 1675 $post->post_type = $parent_post->post_type; 1676 } 1677 1678 // Apply the changes to the existing post object. 1679 $post = (object) array_merge( (array) $post, (array) $changes ); 1680 1681 $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); 1682 $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; 1683 } 1684 1685 // Required for the WP_Block_Template. Update the post object with the current time. 1686 $post->post_modified = current_time( 'mysql' ); 1687 1688 // If the post_author is empty, set it to the current user. 1689 if ( empty( $post->post_author ) ) { 1690 $post->post_author = get_current_user_id(); 1691 } 1692 1693 if ( 'wp_template_part' === $post->post_type && ! isset( $terms['wp_template_part_area'] ) ) { 1694 $area_terms = get_the_terms( $changes->ID, 'wp_template_part_area' ); 1695 $terms['wp_template_part_area'] = ! is_wp_error( $area_terms ) && ! empty( $area_terms ) ? $area_terms[0]->name : null; 1696 } 1697 1698 $template = _build_block_template_object_from_post_object( new WP_Post( $post ), $terms, $meta ); 1699 1700 if ( is_wp_error( $template ) ) { 1701 return $template; 1702 } 1703 1704 if ( 'wp_template_part' === $post->post_type ) { 1705 $attributes = array(); 1706 $existing_ignored_hooked_blocks = isset( $post->ID ) ? get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ) : ''; 1707 1708 if ( ! empty( $existing_ignored_hooked_blocks ) ) { 1709 $attributes['metadata'] = array( 1710 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ), 1711 ); 1712 } 1713 1714 $content = get_comment_delimited_block_content( 1715 'core/template-part', 1716 $attributes, 1717 $changes->post_content 1718 ); 1719 $content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' ); 1720 $changes->post_content = remove_serialized_parent_block( $content ); 1721 1722 $wrapper_block_markup = extract_serialized_parent_block( $content ); 1723 $wrapper_block = parse_blocks( $wrapper_block_markup )[0]; 1724 $ignored_hooked_blocks = $wrapper_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array(); 1725 if ( ! empty( $ignored_hooked_blocks ) ) { 1726 if ( ! isset( $changes->meta_input ) ) { 1727 $changes->meta_input = array(); 1728 } 1729 $changes->meta_input['_wp_ignored_hooked_blocks'] = wp_json_encode( $ignored_hooked_blocks ); 1730 } 1731 } else { 1732 $changes->post_content = apply_block_hooks_to_content( $changes->post_content, $template, 'set_ignored_hooked_blocks_metadata' ); 1733 } 1734 1735 return $changes; 1736 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Feb 21 08:20:01 2025 | Cross-referenced by PHPXref |