Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Utils
File: BlockTemplateUtils.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Utils;
[1] Fix | Delete
[2] Fix | Delete
use WP_Block_Patterns_Registry;
[3] Fix | Delete
use Automattic\WooCommerce\Admin\Features\Features;
[4] Fix | Delete
use Automattic\WooCommerce\Blocks\Options;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Package;
[6] Fix | Delete
use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
[7] Fix | Delete
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Utility methods used for serving block templates from WooCommerce Blocks.
[11] Fix | Delete
* {@internal This class and its methods should only be used within the BlockTemplateController.php and is not intended for public use.}
[12] Fix | Delete
*/
[13] Fix | Delete
class BlockTemplateUtils {
[14] Fix | Delete
/**
[15] Fix | Delete
* Directory names for block templates
[16] Fix | Delete
*
[17] Fix | Delete
* Directory names conventions for block templates have changed with Gutenberg 12.1.0,
[18] Fix | Delete
* however, for backwards-compatibility, we also keep the older conventions, prefixed
[19] Fix | Delete
* with `DEPRECATED_`.
[20] Fix | Delete
*
[21] Fix | Delete
* @var array {
[22] Fix | Delete
* @var string DEPRECATED_TEMPLATES Old directory name of the block templates directory.
[23] Fix | Delete
* @var string DEPRECATED_TEMPLATE_PARTS Old directory name of the block template parts directory.
[24] Fix | Delete
* @var string TEMPLATES_DIR_NAME Directory name of the block templates directory.
[25] Fix | Delete
* @var string TEMPLATE_PARTS_DIR_NAME Directory name of the block template parts directory.
[26] Fix | Delete
* }
[27] Fix | Delete
*/
[28] Fix | Delete
const DIRECTORY_NAMES = array(
[29] Fix | Delete
'DEPRECATED_TEMPLATES' => 'block-templates',
[30] Fix | Delete
'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts',
[31] Fix | Delete
'TEMPLATES' => 'templates',
[32] Fix | Delete
'TEMPLATE_PARTS' => 'parts',
[33] Fix | Delete
);
[34] Fix | Delete
[35] Fix | Delete
const TEMPLATES_ROOT_DIR = 'templates';
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* WooCommerce plugin slug
[39] Fix | Delete
*
[40] Fix | Delete
* This is used to save templates to the DB which are stored against this value in the wp_terms table.
[41] Fix | Delete
*
[42] Fix | Delete
* @var string
[43] Fix | Delete
*/
[44] Fix | Delete
const PLUGIN_SLUG = 'woocommerce/woocommerce';
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Deprecated WooCommerce plugin slug
[48] Fix | Delete
*
[49] Fix | Delete
* For supporting users who have customized templates under the incorrect plugin slug during the first release.
[50] Fix | Delete
* More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
[51] Fix | Delete
*
[52] Fix | Delete
* @var string
[53] Fix | Delete
*/
[54] Fix | Delete
const DEPRECATED_PLUGIN_SLUG = 'woocommerce';
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* Returns the template matching the slug
[58] Fix | Delete
*
[59] Fix | Delete
* @param string $template_slug Slug of the template to retrieve.
[60] Fix | Delete
*
[61] Fix | Delete
* @return AbstractTemplate|AbstractTemplatePart|null
[62] Fix | Delete
*/
[63] Fix | Delete
public static function get_template( $template_slug ) {
[64] Fix | Delete
$block_templates_registry = Package::container()->get( BlockTemplatesRegistry::class );
[65] Fix | Delete
return $block_templates_registry->get_template( $template_slug );
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Returns an array containing the references of
[70] Fix | Delete
* the passed blocks and their inner blocks.
[71] Fix | Delete
*
[72] Fix | Delete
* @param array $blocks array of blocks.
[73] Fix | Delete
*
[74] Fix | Delete
* @return array block references to the passed blocks and their inner blocks.
[75] Fix | Delete
*/
[76] Fix | Delete
public static function flatten_blocks( &$blocks ) {
[77] Fix | Delete
$all_blocks = array();
[78] Fix | Delete
$queue = array();
[79] Fix | Delete
foreach ( $blocks as &$block ) {
[80] Fix | Delete
$queue[] = &$block;
[81] Fix | Delete
}
[82] Fix | Delete
$queue_count = count( $queue );
[83] Fix | Delete
[84] Fix | Delete
while ( $queue_count > 0 ) {
[85] Fix | Delete
$block = &$queue[0];
[86] Fix | Delete
array_shift( $queue );
[87] Fix | Delete
$all_blocks[] = &$block;
[88] Fix | Delete
[89] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[90] Fix | Delete
foreach ( $block['innerBlocks'] as &$inner_block ) {
[91] Fix | Delete
$queue[] = &$inner_block;
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$queue_count = count( $queue );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
return $all_blocks;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
/**
[102] Fix | Delete
* Parses wp_template content and injects the current theme's
[103] Fix | Delete
* stylesheet as a theme attribute into each wp_template_part
[104] Fix | Delete
*
[105] Fix | Delete
* @param string $template_content serialized wp_template content.
[106] Fix | Delete
*
[107] Fix | Delete
* @return string Updated wp_template content.
[108] Fix | Delete
*/
[109] Fix | Delete
public static function inject_theme_attribute_in_content( $template_content ) {
[110] Fix | Delete
$has_updated_content = false;
[111] Fix | Delete
$new_content = '';
[112] Fix | Delete
$template_blocks = parse_blocks( $template_content );
[113] Fix | Delete
[114] Fix | Delete
$blocks = self::flatten_blocks( $template_blocks );
[115] Fix | Delete
foreach ( $blocks as &$block ) {
[116] Fix | Delete
if (
[117] Fix | Delete
'core/template-part' === $block['blockName'] &&
[118] Fix | Delete
! isset( $block['attrs']['theme'] )
[119] Fix | Delete
) {
[120] Fix | Delete
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
[121] Fix | Delete
$has_updated_content = true;
[122] Fix | Delete
}
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
if ( $has_updated_content ) {
[126] Fix | Delete
foreach ( $template_blocks as &$block ) {
[127] Fix | Delete
$new_content .= serialize_block( $block );
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
return $new_content;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
return $template_content;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
/**
[137] Fix | Delete
* Build a unified template object based a post Object.
[138] Fix | Delete
* Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.
[139] Fix | Delete
*
[140] Fix | Delete
* @param \WP_Post $post Template post.
[141] Fix | Delete
*
[142] Fix | Delete
* @return \WP_Block_Template|\WP_Error Template.
[143] Fix | Delete
*/
[144] Fix | Delete
public static function build_template_result_from_post( $post ) {
[145] Fix | Delete
$terms = get_the_terms( $post, 'wp_theme' );
[146] Fix | Delete
[147] Fix | Delete
if ( is_wp_error( $terms ) ) {
[148] Fix | Delete
return $terms;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
if ( ! $terms ) {
[152] Fix | Delete
return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$theme = $terms[0]->name;
[156] Fix | Delete
$has_theme_file = true;
[157] Fix | Delete
[158] Fix | Delete
$template = new \WP_Block_Template();
[159] Fix | Delete
$template->wp_id = $post->ID;
[160] Fix | Delete
$template->id = $theme . '//' . $post->post_name;
[161] Fix | Delete
$template->theme = $theme;
[162] Fix | Delete
$template->content = $post->post_content;
[163] Fix | Delete
$template->slug = $post->post_name;
[164] Fix | Delete
$template->source = 'custom';
[165] Fix | Delete
$template->type = $post->post_type;
[166] Fix | Delete
$template->description = $post->post_excerpt;
[167] Fix | Delete
$template->title = $post->post_title;
[168] Fix | Delete
$template->status = $post->post_status;
[169] Fix | Delete
$template->has_theme_file = $has_theme_file;
[170] Fix | Delete
$template->is_custom = false;
[171] Fix | Delete
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
[172] Fix | Delete
[173] Fix | Delete
if ( 'wp_template_part' === $post->post_type ) {
[174] Fix | Delete
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
[175] Fix | Delete
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
[176] Fix | Delete
$template->area = $type_terms[0]->name;
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
// We are checking 'woocommerce' to maintain classic templates which are saved to the DB,
[181] Fix | Delete
// prior to updating to use the correct slug.
[182] Fix | Delete
// More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
[183] Fix | Delete
if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) {
[184] Fix | Delete
$template->origin = 'plugin';
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/*
[188] Fix | Delete
* Run the block hooks algorithm introduced in WP 6.4 on the template content.
[189] Fix | Delete
*/
[190] Fix | Delete
if ( function_exists( 'inject_ignored_hooked_blocks_metadata_attributes' ) ) {
[191] Fix | Delete
$hooked_blocks = get_hooked_blocks();
[192] Fix | Delete
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
[193] Fix | Delete
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
[194] Fix | Delete
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
[195] Fix | Delete
$blocks = parse_blocks( $template->content );
[196] Fix | Delete
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $template;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Build a unified template object based on a theme file.
[205] Fix | Delete
*
[206] Fix | Delete
* @internal Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.
[207] Fix | Delete
*
[208] Fix | Delete
* @param array|object $template_file Theme file.
[209] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[210] Fix | Delete
*
[211] Fix | Delete
* @return \WP_Block_Template Template.
[212] Fix | Delete
*/
[213] Fix | Delete
public static function build_template_result_from_file( $template_file, $template_type ) {
[214] Fix | Delete
$template_file = (object) $template_file;
[215] Fix | Delete
[216] Fix | Delete
// If the theme has an archive-products.html template but does not have product taxonomy templates
[217] Fix | Delete
// then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend.
[218] Fix | Delete
$template_is_from_theme = 'theme' === $template_file->source;
[219] Fix | Delete
$theme_name = wp_get_theme()->get( 'TextDomain' );
[220] Fix | Delete
[221] Fix | Delete
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
[222] Fix | Delete
$template_content = file_get_contents( $template_file->path );
[223] Fix | Delete
$template = new \WP_Block_Template();
[224] Fix | Delete
$template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
[225] Fix | Delete
$template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
[226] Fix | Delete
$template->content = self::inject_theme_attribute_in_content( $template_content );
[227] Fix | Delete
// Remove the term description block from the archive-product template
[228] Fix | Delete
// as the Product Catalog/Shop page doesn't have a description.
[229] Fix | Delete
if ( ProductCatalogTemplate::SLUG === $template_file->slug ) {
[230] Fix | Delete
$template->content = str_replace( '<!-- wp:term-description {"align":"wide"} /-->', '', $template->content );
[231] Fix | Delete
}
[232] Fix | Delete
// Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
[233] Fix | Delete
$template->source = $template_file->source ? $template_file->source : 'plugin';
[234] Fix | Delete
$template->slug = $template_file->slug;
[235] Fix | Delete
$template->type = $template_type;
[236] Fix | Delete
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
[237] Fix | Delete
$template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
[238] Fix | Delete
$template->status = 'publish';
[239] Fix | Delete
$template->has_theme_file = true;
[240] Fix | Delete
$template->origin = $template_file->source;
[241] Fix | Delete
$template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
[242] Fix | Delete
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
[243] Fix | Delete
$template->area = self::get_block_template_area( $template->slug, $template_type );
[244] Fix | Delete
[245] Fix | Delete
/*
[246] Fix | Delete
* Run the block hooks algorithm introduced in WP 6.4 on the template content.
[247] Fix | Delete
*/
[248] Fix | Delete
if ( function_exists( 'inject_ignored_hooked_blocks_metadata_attributes' ) ) {
[249] Fix | Delete
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
[250] Fix | Delete
$after_block_visitor = null;
[251] Fix | Delete
$hooked_blocks = get_hooked_blocks();
[252] Fix | Delete
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
[253] Fix | Delete
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
[254] Fix | Delete
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
[255] Fix | Delete
}
[256] Fix | Delete
$blocks = parse_blocks( $template->content );
[257] Fix | Delete
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
return $template;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any.
[265] Fix | Delete
*
[266] Fix | Delete
* @param string $template_file Block template file path.
[267] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[268] Fix | Delete
* @param string $template_slug Block template slug e.g. single-product.
[269] Fix | Delete
* @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks.
[270] Fix | Delete
*
[271] Fix | Delete
* @return object Block template object.
[272] Fix | Delete
*/
[273] Fix | Delete
public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) {
[274] Fix | Delete
$theme_name = wp_get_theme()->get( 'TextDomain' );
[275] Fix | Delete
[276] Fix | Delete
$new_template_item = array(
[277] Fix | Delete
'slug' => $template_slug,
[278] Fix | Delete
'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug,
[279] Fix | Delete
'path' => $template_file,
[280] Fix | Delete
'type' => $template_type,
[281] Fix | Delete
'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG,
[282] Fix | Delete
// Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
[283] Fix | Delete
'source' => $template_is_from_theme ? 'theme' : 'plugin',
[284] Fix | Delete
'title' => self::get_block_template_title( $template_slug ),
[285] Fix | Delete
'description' => self::get_block_template_description( $template_slug ),
[286] Fix | Delete
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
[287] Fix | Delete
);
[288] Fix | Delete
[289] Fix | Delete
return (object) $new_template_item;
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
/**
[293] Fix | Delete
* Finds all nested template part file paths in a theme's directory.
[294] Fix | Delete
*
[295] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[296] Fix | Delete
* @return array $path_list A list of paths to all template part files.
[297] Fix | Delete
*/
[298] Fix | Delete
public static function get_template_paths( $template_type ) {
[299] Fix | Delete
$wp_template_filenames = array(
[300] Fix | Delete
'archive-product.html',
[301] Fix | Delete
'order-confirmation.html',
[302] Fix | Delete
'page-cart.html',
[303] Fix | Delete
'page-checkout.html',
[304] Fix | Delete
'product-search-results.html',
[305] Fix | Delete
'single-product.html',
[306] Fix | Delete
'taxonomy-product_attribute.html',
[307] Fix | Delete
'taxonomy-product_brand.html',
[308] Fix | Delete
'taxonomy-product_cat.html',
[309] Fix | Delete
'taxonomy-product_tag.html',
[310] Fix | Delete
);
[311] Fix | Delete
[312] Fix | Delete
if ( Features::is_enabled( 'launch-your-store' ) ) {
[313] Fix | Delete
$wp_template_filenames[] = 'coming-soon.html';
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
$wp_template_part_filenames = array(
[317] Fix | Delete
'checkout-header.html',
[318] Fix | Delete
'coming-soon-social-links.html',
[319] Fix | Delete
'mini-cart.html',
[320] Fix | Delete
'simple-product-add-to-cart-with-options.html',
[321] Fix | Delete
'external-product-add-to-cart-with-options.html',
[322] Fix | Delete
'variable-product-add-to-cart-with-options.html',
[323] Fix | Delete
'grouped-product-add-to-cart-with-options.html',
[324] Fix | Delete
);
[325] Fix | Delete
[326] Fix | Delete
/*
[327] Fix | Delete
* This may return the blockified directory for wp_templates.
[328] Fix | Delete
* At the moment every template file has a corresponding blockified file.
[329] Fix | Delete
* If we decide to add a new template file that doesn't, we will need to update this logic.
[330] Fix | Delete
*/
[331] Fix | Delete
$directory = self::get_templates_directory( $template_type );
[332] Fix | Delete
[333] Fix | Delete
$path_list = array_map(
[334] Fix | Delete
function ( $filename ) use ( $directory ) {
[335] Fix | Delete
return $directory . DIRECTORY_SEPARATOR . $filename;
[336] Fix | Delete
},
[337] Fix | Delete
'wp_template' === $template_type ? $wp_template_filenames : $wp_template_part_filenames
[338] Fix | Delete
);
[339] Fix | Delete
[340] Fix | Delete
return $path_list;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Gets the directory where templates of a specific template type can be found.
[345] Fix | Delete
*
[346] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[347] Fix | Delete
*
[348] Fix | Delete
* @return string
[349] Fix | Delete
*/
[350] Fix | Delete
public static function get_templates_directory( $template_type = 'wp_template' ) {
[351] Fix | Delete
$root_path = dirname( __DIR__, 3 ) . '/' . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR;
[352] Fix | Delete
$templates_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATES'];
[353] Fix | Delete
$template_parts_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATE_PARTS'];
[354] Fix | Delete
[355] Fix | Delete
if ( 'wp_template_part' === $template_type ) {
[356] Fix | Delete
return $template_parts_directory;
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
if ( self::should_use_blockified_product_grid_templates() ) {
[360] Fix | Delete
return $templates_directory . '/blockified';
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
return $templates_directory;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Returns template title.
[368] Fix | Delete
*
[369] Fix | Delete
* @param string $template_slug The template slug (e.g. single-product).
[370] Fix | Delete
* @return string Human friendly title.
[371] Fix | Delete
*/
[372] Fix | Delete
public static function get_block_template_title( $template_slug ) {
[373] Fix | Delete
$registered_template = self::get_template( $template_slug );
[374] Fix | Delete
if ( isset( $registered_template ) ) {
[375] Fix | Delete
return $registered_template->get_template_title();
[376] Fix | Delete
} else {
[377] Fix | Delete
// Human friendly title converted from the slug.
[378] Fix | Delete
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
[379] Fix | Delete
}
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
/**
[383] Fix | Delete
* Returns template description.
[384] Fix | Delete
*
[385] Fix | Delete
* @param string $template_slug The template slug (e.g. single-product).
[386] Fix | Delete
* @return string Template description.
[387] Fix | Delete
*/
[388] Fix | Delete
public static function get_block_template_description( $template_slug ) {
[389] Fix | Delete
$registered_template = self::get_template( $template_slug );
[390] Fix | Delete
if ( isset( $registered_template ) ) {
[391] Fix | Delete
return $registered_template->get_template_description();
[392] Fix | Delete
}
[393] Fix | Delete
return '';
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Returns area for template parts.
[398] Fix | Delete
*
[399] Fix | Delete
* @param string $template_slug The template part slug (e.g. mini-cart).
[400] Fix | Delete
* @param string $template_type Either `wp_template` or `wp_template_part`.
[401] Fix | Delete
* @return string Template part area.
[402] Fix | Delete
*/
[403] Fix | Delete
public static function get_block_template_area( $template_slug, $template_type ) {
[404] Fix | Delete
if ( 'wp_template_part' === $template_type ) {
[405] Fix | Delete
$registered_template = self::get_template( $template_slug );
[406] Fix | Delete
if ( $registered_template && property_exists( $registered_template, 'template_area' ) ) {
[407] Fix | Delete
return $registered_template->template_area;
[408] Fix | Delete
}
[409] Fix | Delete
}
[410] Fix | Delete
return 'uncategorized';
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Converts template paths into a slug
[415] Fix | Delete
*
[416] Fix | Delete
* @param string $path The template's path.
[417] Fix | Delete
* @return string slug
[418] Fix | Delete
*/
[419] Fix | Delete
public static function generate_template_slug_from_path( $path ) {
[420] Fix | Delete
$template_extension = '.html';
[421] Fix | Delete
[422] Fix | Delete
return basename( $path, $template_extension );
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* Gets the first matching template part within themes directories
[427] Fix | Delete
*
[428] Fix | Delete
* Since [Gutenberg 12.1.0](https://github.com/WordPress/gutenberg/releases/tag/v12.1.0), the conventions for
[429] Fix | Delete
* block templates and parts directory has changed from `block-templates` and `block-templates-parts`
[430] Fix | Delete
* to `templates` and `parts` respectively.
[431] Fix | Delete
*
[432] Fix | Delete
* This function traverses all possible combinations of directory paths where a template or part
[433] Fix | Delete
* could be located and returns the first one which is readable, prioritizing the new convention
[434] Fix | Delete
* over the deprecated one, but maintaining that one for backwards compatibility.
[435] Fix | Delete
*
[436] Fix | Delete
* @param string $template_slug The slug of the template (i.e. without the file extension).
[437] Fix | Delete
* @param string $template_type Either `wp_template` or `wp_template_part`.
[438] Fix | Delete
*
[439] Fix | Delete
* @return string|null The matched path or `null` if no match was found.
[440] Fix | Delete
*/
[441] Fix | Delete
public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) {
[442] Fix | Delete
$template_filename = $template_slug . '.html';
[443] Fix | Delete
$possible_templates_dir = 'wp_template' === $template_type ? array(
[444] Fix | Delete
self::DIRECTORY_NAMES['TEMPLATES'],
[445] Fix | Delete
self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'],
[446] Fix | Delete
) : array(
[447] Fix | Delete
self::DIRECTORY_NAMES['TEMPLATE_PARTS'],
[448] Fix | Delete
self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'],
[449] Fix | Delete
);
[450] Fix | Delete
[451] Fix | Delete
// Combine the possible root directory names with either the template directory
[452] Fix | Delete
// or the stylesheet directory for child themes.
[453] Fix | Delete
$possible_paths = array_reduce(
[454] Fix | Delete
$possible_templates_dir,
[455] Fix | Delete
function ( $carry, $item ) use ( $template_filename ) {
[456] Fix | Delete
$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;
[457] Fix | Delete
[458] Fix | Delete
$carry[] = get_stylesheet_directory() . $filepath;
[459] Fix | Delete
$carry[] = get_template_directory() . $filepath;
[460] Fix | Delete
[461] Fix | Delete
return $carry;
[462] Fix | Delete
},
[463] Fix | Delete
array()
[464] Fix | Delete
);
[465] Fix | Delete
[466] Fix | Delete
// Return the first matching.
[467] Fix | Delete
foreach ( $possible_paths as $path ) {
[468] Fix | Delete
if ( is_readable( $path ) ) {
[469] Fix | Delete
return $path;
[470] Fix | Delete
}
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
return null;
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
/**
[477] Fix | Delete
* Check if the theme has a template. So we know if to load our own in or not.
[478] Fix | Delete
*
[479] Fix | Delete
* @param string $template_name name of the template file without .html extension e.g. 'single-product'.
[480] Fix | Delete
* @return boolean
[481] Fix | Delete
*/
[482] Fix | Delete
public static function theme_has_template( $template_name ) {
[483] Fix | Delete
return (bool) self::get_theme_template_path( $template_name, 'wp_template' );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
/**
[487] Fix | Delete
* Check if the theme has a template. So we know if to load our own in or not.
[488] Fix | Delete
*
[489] Fix | Delete
* @param string $template_name name of the template file without .html extension e.g. 'single-product'.
[490] Fix | Delete
* @return boolean
[491] Fix | Delete
*/
[492] Fix | Delete
public static function theme_has_template_part( $template_name ) {
[493] Fix | Delete
return (bool) self::get_theme_template_path( $template_name, 'wp_template_part' );
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
/**
[497] Fix | Delete
* Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed.
[498] Fix | Delete
*
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function