Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
File Manager
/
wp-includes
/
js
/
jquery
/
ui
:
block-template-utils.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php /** * Utilities used to fetch and create templates and template parts. * * @package WordPress * @since 5.8.0 */ // Define constants for supported wp_template_part_area taxonomy. if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) { define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' ); } if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) { define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' ); } if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) { define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' ); } if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); } /** * For backward compatibility reasons, * block themes might be using block-templates or block-template-parts, * this function ensures we fallback to these folders properly. * * @since 5.9.0 * * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root. * * @return string[] { * Folder names used by block themes. * * @type string $wp_template Theme-relative directory name for block templates. * @type string $wp_template_part Theme-relative directory name for block template parts. * } */ function get_block_theme_folders( $theme_stylesheet = null ) { $theme = wp_get_theme( (string) $theme_stylesheet ); if ( ! $theme->exists() ) { // Return the default folders if the theme doesn't exist. return array( 'wp_template' => 'templates', 'wp_template_part' => 'parts', ); } return $theme->get_block_template_folders(); } /** * Returns a filtered list of allowed area values for template parts. * * @since 5.9.0 * * @return array[] The supported template part area values. */ function get_allowed_block_template_part_areas() { $default_area_definitions = array( array( 'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED, 'label' => _x( 'General', 'template part area' ), 'description' => __( 'General templates often perform a specific role like displaying post content, and are not tied to any particular area.' ), 'icon' => 'layout', 'area_tag' => 'div', ), array( 'area' => WP_TEMPLATE_PART_AREA_HEADER, 'label' => _x( 'Header', 'template part area' ), 'description' => __( 'The Header template defines a page area that typically contains a title, logo, and main navigation.' ), 'icon' => 'header', 'area_tag' => 'header', ), array( 'area' => WP_TEMPLATE_PART_AREA_FOOTER, 'label' => _x( 'Footer', 'template part area' ), 'description' => __( 'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.' ), 'icon' => 'footer', 'area_tag' => 'footer', ), ); /** * Filters the list of allowed template part area values. * * @since 5.9.0 * * @param array[] $default_area_definitions An array of supported area objects. */ return apply_filters( 'default_wp_template_part_areas', $default_area_definitions ); } /** * Returns a filtered list of default template types, containing their * localized titles and descriptions. * * @since 5.9.0 * * @return array[] The default template types. */ function get_default_block_template_types() { $default_template_types = array( 'index' => array( 'title' => _x( 'Index', 'Template name' ), 'description' => __( 'Used as a fallback template for all pages when a more specific template is not defined.' ), ), 'home' => array( 'title' => _x( 'Blog Home', 'Template name' ), '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.' ), ), 'front-page' => array( 'title' => _x( 'Front Page', 'Template name' ), '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.' ), ), 'singular' => array( 'title' => _x( 'Single Entries', 'Template name' ), '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.' ), ), 'single' => array( 'title' => _x( 'Single Posts', 'Template name' ), 'description' => __( 'Displays a single post on your website unless a custom template has been applied to that post or a dedicated template exists.' ), ), 'page' => array( 'title' => _x( 'Pages', 'Template name' ), 'description' => __( 'Displays a static page unless a custom template has been applied to that page or a dedicated template exists.' ), ), 'archive' => array( 'title' => _x( 'All Archives', 'Template name' ), '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.' ), ), 'author' => array( 'title' => _x( 'Author Archives', 'Template name' ), '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.' ), ), 'category' => array( 'title' => _x( 'Category Archives', 'Template name' ), '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.' ), ), 'taxonomy' => array( 'title' => _x( 'Taxonomy', 'Template name' ), '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.' ), ), 'date' => array( 'title' => _x( 'Date Archives', 'Template name' ), 'description' => __( 'Displays a post archive when a specific date is visited (e.g., example.com/2023/).' ), ), 'tag' => array( 'title' => _x( 'Tag Archives', 'Template name' ), '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.' ), ), 'attachment' => array( 'title' => __( 'Attachment Pages' ), 'description' => __( 'Displays when a visitor views the dedicated page that exists for any media attachment.' ), ), 'search' => array( 'title' => _x( 'Search Results', 'Template name' ), 'description' => __( 'Displays when a visitor performs a search on your website.' ), ), 'privacy-policy' => array( 'title' => __( 'Privacy Policy' ), 'description' => __( 'Displays your site\'s Privacy Policy page.' ), ), '404' => array( 'title' => _x( 'Page: 404', 'Template name' ), 'description' => __( 'Displays when a visitor views a non-existent page, such as a dead link or a mistyped URL.' ), ), ); /** * Filters the list of default template types. * * @since 5.9.0 * * @param array[] $default_template_types An array of template types, formatted as [ slug => [ title, description ] ]. */ return apply_filters( 'default_template_types', $default_template_types ); } /** * Checks whether the input 'area' is a supported value. * Returns the input if supported, otherwise returns the 'uncategorized' value. * * @since 5.9.0 * @access private * * @param string $type Template part area name. * @return string Input if supported, else the uncategorized value. */ function _filter_block_template_part_area( $type ) { $allowed_areas = array_map( static function ( $item ) { return $item['area']; }, get_allowed_block_template_part_areas() ); if ( in_array( $type, $allowed_areas, true ) ) { return $type; } $warning_message = sprintf( /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */ __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ), $type, WP_TEMPLATE_PART_AREA_UNCATEGORIZED ); trigger_error( $warning_message, E_USER_NOTICE ); return WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } /** * Finds all nested template part file paths in a theme's directory. * * @since 5.9.0 * @access private * * @param string $base_directory The theme's file path. * @return string[] A list of paths to all template part files. */ function _get_block_templates_paths( $base_directory ) { static $template_path_list = array(); if ( isset( $template_path_list[ $base_directory ] ) ) { return $template_path_list[ $base_directory ]; } $path_list = array(); if ( is_dir( $base_directory ) ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); foreach ( $nested_html_files as $path => $file ) { $path_list[] = $path; } } $template_path_list[ $base_directory ] = $path_list; return $path_list; } /** * Retrieves the template file from the theme for a given slug. * * @since 5.9.0 * @access private * * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. * @param string $slug Template slug. * @return array|null { * Array with template metadata if $template_type is one of 'wp_template' or 'wp_template_part', * null otherwise. * * @type string $slug Template slug. * @type string $path Template file path. * @type string $theme Theme slug. * @type string $type Template type. * @type string $area Template area. Only for 'wp_template_part'. * @type string $title Optional. Template title. * @type string[] $postTypes Optional. List of post types that the template supports. Only for 'wp_template'. * } */ function _get_block_template_file( $template_type, $slug ) { if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } $themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); foreach ( $themes as $theme_slug => $theme_dir ) { $template_base_paths = get_block_theme_folders( $theme_slug ); $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { $new_template_item = array( 'slug' => $slug, 'path' => $file_path, 'theme' => $theme_slug, 'type' => $template_type, ); if ( 'wp_template_part' === $template_type ) { return _add_block_template_part_area_info( $new_template_item ); } if ( 'wp_template' === $template_type ) { return _add_block_template_info( $new_template_item ); } return $new_template_item; } } return null; } /** * Retrieves the template files from the theme. * * @since 5.9.0 * @since 6.3.0 Added the `$query` parameter. * @access private * * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. * @param array $query { * Arguments to retrieve templates. Optional, empty by default. * * @type string[] $slug__in List of slugs to include. * @type string[] $slug__not_in List of slugs to skip. * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). * @type string $post_type Post type to get the templates for. * } * * @return array Template */ function _get_block_templates_files( $template_type, $query = array() ) { if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } // Prepare metadata from $query. $slugs_t