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