Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks
File: BlockTemplatesController.php
<?php
[0] Fix | Delete
declare( strict_types=1 );
[1] Fix | Delete
namespace Automattic\WooCommerce\Blocks;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
[4] Fix | Delete
use Automattic\WooCommerce\Blocks\Templates\ComingSoonTemplate;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Utils\Utils;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* BlockTemplatesController class.
[9] Fix | Delete
*
[10] Fix | Delete
* @internal
[11] Fix | Delete
*/
[12] Fix | Delete
class BlockTemplatesController {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Directory which contains all templates
[16] Fix | Delete
*
[17] Fix | Delete
* @var string
[18] Fix | Delete
*/
[19] Fix | Delete
const TEMPLATES_ROOT_DIR = 'templates';
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Initialization method.
[23] Fix | Delete
*/
[24] Fix | Delete
public function init() {
[25] Fix | Delete
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
[26] Fix | Delete
add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 3 );
[27] Fix | Delete
add_filter( 'get_block_templates', array( $this, 'add_db_templates_with_woo_slug' ), 10, 3 );
[28] Fix | Delete
add_filter( 'rest_pre_insert_wp_template', array( $this, 'dont_load_templates_for_suggestions' ), 10, 1 );
[29] Fix | Delete
add_filter( 'block_type_metadata_settings', array( $this, 'add_plugin_templates_parts_support' ), 10, 2 );
[30] Fix | Delete
add_filter( 'block_type_metadata_settings', array( $this, 'prevent_shortcodes_html_breakage' ), 10, 2 );
[31] Fix | Delete
add_action( 'current_screen', array( $this, 'hide_template_selector_in_cart_checkout_pages' ), 10 );
[32] Fix | Delete
add_action( 'wp_enqueue_scripts', [ $this, 'dequeue_legacy_scripts' ], 20 );
[33] Fix | Delete
[34] Fix | Delete
// Fix a bug in WordPress 6.8 and lower that caused block hooks not to
[35] Fix | Delete
// run in templates registered via the Template Registration API.
[36] Fix | Delete
// @see https://github.com/WordPress/gutenberg/issues/71139.
[37] Fix | Delete
if ( Utils::wp_version_compare( '6.8', '<=' ) ) {
[38] Fix | Delete
add_filter( 'get_block_templates', array( $this, 'run_hooks_on_block_templates' ), 10, 3 );
[39] Fix | Delete
}
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Dequeue legacy scripts that have no usage with block themes.
[44] Fix | Delete
*/
[45] Fix | Delete
public function dequeue_legacy_scripts() {
[46] Fix | Delete
if ( ! wp_is_block_theme() ) {
[47] Fix | Delete
return;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
if ( is_product() ) {
[51] Fix | Delete
wp_dequeue_script( 'wc-single-product' );
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Renders the `core/template-part` block on the server.
[57] Fix | Delete
*
[58] Fix | Delete
* This is done because the core handling for template parts only supports templates from the current theme, not
[59] Fix | Delete
* from a plugin.
[60] Fix | Delete
*
[61] Fix | Delete
* @param array $attributes The block attributes.
[62] Fix | Delete
* @return string The render.
[63] Fix | Delete
*/
[64] Fix | Delete
public function render_woocommerce_template_part( $attributes ) {
[65] Fix | Delete
if ( isset( $attributes['theme'] ) && 'woocommerce/woocommerce' === $attributes['theme'] ) {
[66] Fix | Delete
$template_part = get_block_template( $attributes['theme'] . '//' . $attributes['slug'], 'wp_template_part' );
[67] Fix | Delete
[68] Fix | Delete
if ( $template_part && ! empty( $template_part->content ) ) {
[69] Fix | Delete
$content = do_blocks( $template_part->content );
[70] Fix | Delete
[71] Fix | Delete
if ( empty( $attributes['tagName'] ) || tag_escape( $attributes['tagName'] ) !== $attributes['tagName'] ) {
[72] Fix | Delete
$html_tag = 'div';
[73] Fix | Delete
} else {
[74] Fix | Delete
$html_tag = esc_attr( $attributes['tagName'] );
[75] Fix | Delete
}
[76] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes();
[77] Fix | Delete
[78] Fix | Delete
return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
return function_exists( '\gutenberg_render_block_core_template_part' ) ? \gutenberg_render_block_core_template_part( $attributes ) : \render_block_core_template_part( $attributes );
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* By default, the Template Part Block only supports template parts that are in the current theme directory.
[86] Fix | Delete
* This render_callback wrapper allows us to add support for plugin-housed template parts.
[87] Fix | Delete
*
[88] Fix | Delete
* @param array $settings Array of determined settings for registering a block type.
[89] Fix | Delete
* @param array $metadata Metadata provided for registering a block type.
[90] Fix | Delete
*/
[91] Fix | Delete
public function add_plugin_templates_parts_support( $settings, $metadata ) {
[92] Fix | Delete
if (
[93] Fix | Delete
isset( $metadata['name'], $settings['render_callback'] ) &&
[94] Fix | Delete
'core/template-part' === $metadata['name'] &&
[95] Fix | Delete
in_array( $settings['render_callback'], array( 'render_block_core_template_part', 'gutenberg_render_block_core_template_part' ), true )
[96] Fix | Delete
) {
[97] Fix | Delete
$settings['render_callback'] = array( $this, 'render_woocommerce_template_part' );
[98] Fix | Delete
}
[99] Fix | Delete
return $settings;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Prevents shortcodes in templates having their HTML content broken by wpautop.
[105] Fix | Delete
*
[106] Fix | Delete
* @see https://core.trac.wordpress.org/ticket/58366 for more info.
[107] Fix | Delete
*
[108] Fix | Delete
* @param array $settings Array of determined settings for registering a block type.
[109] Fix | Delete
* @param array $metadata Metadata provided for registering a block type.
[110] Fix | Delete
*/
[111] Fix | Delete
public function prevent_shortcodes_html_breakage( $settings, $metadata ) {
[112] Fix | Delete
if (
[113] Fix | Delete
isset( $metadata['name'], $settings['render_callback'] ) &&
[114] Fix | Delete
'core/shortcode' === $metadata['name']
[115] Fix | Delete
) {
[116] Fix | Delete
$settings['original_render_callback'] = $settings['render_callback'];
[117] Fix | Delete
$settings['render_callback'] = function ( $attributes, $content ) use ( $settings ) {
[118] Fix | Delete
// The shortcode has already been rendered, so look for the cart/checkout HTML.
[119] Fix | Delete
if ( strstr( $content, 'woocommerce-cart-form' ) || strstr( $content, 'wc-empty-cart-message' ) || strstr( $content, 'woocommerce-checkout-form' ) ) {
[120] Fix | Delete
// Return early before wpautop runs again.
[121] Fix | Delete
return $content;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
$render_callback = $settings['original_render_callback'];
[125] Fix | Delete
[126] Fix | Delete
return $render_callback( $attributes, $content );
[127] Fix | Delete
};
[128] Fix | Delete
}
[129] Fix | Delete
return $settings;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Prevents the pages that are assigned as Cart/Checkout from showing the "template" selector in the page-editor.
[134] Fix | Delete
* We want to avoid this flow and point users towards the Site Editor instead.
[135] Fix | Delete
*
[136] Fix | Delete
* @return void
[137] Fix | Delete
*/
[138] Fix | Delete
public function hide_template_selector_in_cart_checkout_pages() {
[139] Fix | Delete
if ( ! is_admin() ) {
[140] Fix | Delete
return;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
$current_screen = get_current_screen();
[144] Fix | Delete
[145] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[146] Fix | Delete
if ( $current_screen && 'page' === $current_screen->id && ! empty( $_GET['post'] ) && in_array( absint( $_GET['post'] ), array( wc_get_page_id( 'cart' ), wc_get_page_id( 'checkout' ) ), true ) ) {
[147] Fix | Delete
wp_add_inline_style( 'wc-blocks-editor-style', '.edit-post-post-template { display: none; }' );
[148] Fix | Delete
}
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* This function checks if there's a block template file in `woocommerce/templates/templates/`
[153] Fix | Delete
* to return to pre_get_posts short-circuiting the query in Gutenberg.
[154] Fix | Delete
*
[155] Fix | Delete
* @param \WP_Block_Template|null $template Return a block template object to short-circuit the default query,
[156] Fix | Delete
* or null to allow WP to run its normal queries.
[157] Fix | Delete
* @param string $id Template unique identifier (example: theme_slug//template_slug).
[158] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[159] Fix | Delete
*
[160] Fix | Delete
* @return mixed|\WP_Block_Template|\WP_Error
[161] Fix | Delete
*/
[162] Fix | Delete
public function get_block_file_template( $template, $id, $template_type ) {
[163] Fix | Delete
$template_name_parts = explode( '//', $id );
[164] Fix | Delete
[165] Fix | Delete
if ( count( $template_name_parts ) < 2 ) {
[166] Fix | Delete
return $template;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
list( $template_id, $template_slug ) = $template_name_parts;
[170] Fix | Delete
[171] Fix | Delete
// This is a real edge-case, we are supporting users who have saved templates under the deprecated slug. See its definition for more information.
[172] Fix | Delete
// You can likely ignore this code unless you're supporting/debugging early customised templates.
[173] Fix | Delete
if ( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === strtolower( $template_id ) ) {
[174] Fix | Delete
// Because we are using get_block_templates we have to unhook this method to prevent a recursive loop where this filter is applied.
[175] Fix | Delete
remove_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
[176] Fix | Delete
$template_with_deprecated_id = get_block_template( $id, $template_type );
[177] Fix | Delete
// Let's hook this method back now that we have used the function.
[178] Fix | Delete
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
[179] Fix | Delete
[180] Fix | Delete
if ( null !== $template_with_deprecated_id ) {
[181] Fix | Delete
return $template_with_deprecated_id;
[182] Fix | Delete
}
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
// If we are not dealing with a WooCommerce template let's return early and let it continue through the process.
[186] Fix | Delete
if ( BlockTemplateUtils::PLUGIN_SLUG !== $template_id ) {
[187] Fix | Delete
return $template;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
// If we don't have a template let Gutenberg do its thing.
[191] Fix | Delete
if ( ! $this->block_template_is_available( $template_slug, $template_type ) ) {
[192] Fix | Delete
return $template;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
$directory = BlockTemplateUtils::get_templates_directory( $template_type );
[196] Fix | Delete
$template_file_path = $directory . '/' . $template_slug . '.html';
[197] Fix | Delete
$template_object = BlockTemplateUtils::create_new_block_template_object( $template_file_path, $template_type, $template_slug );
[198] Fix | Delete
$template_built = BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );
[199] Fix | Delete
[200] Fix | Delete
if ( null !== $template_built ) {
[201] Fix | Delete
return $template_built;
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
// Hand back over to Gutenberg if we can't find a template.
[205] Fix | Delete
return $template;
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Add the template title and description to WooCommerce templates.
[210] Fix | Delete
*
[211] Fix | Delete
* @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
[212] Fix | Delete
* @param string $id Template unique identifier (example: 'theme_slug//template_slug').
[213] Fix | Delete
* @param array $template_type Template type: 'wp_template' or 'wp_template_part'.
[214] Fix | Delete
* @return WP_Block_Template|null
[215] Fix | Delete
*/
[216] Fix | Delete
public function add_block_template_details( $block_template, $id, $template_type ) {
[217] Fix | Delete
return BlockTemplateUtils::update_template_data( $block_template, $template_type );
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Run hooks on block templates.
[222] Fix | Delete
*
[223] Fix | Delete
* @param array $templates The block templates.
[224] Fix | Delete
* @return array The block templates.
[225] Fix | Delete
*/
[226] Fix | Delete
public function run_hooks_on_block_templates( $templates ) {
[227] Fix | Delete
foreach ( $templates as $template ) {
[228] Fix | Delete
if ( 'plugin' === $template->source && 'woocommerce' === $template->plugin ) {
[229] Fix | Delete
$template->content = apply_block_hooks_to_content( $template->content, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
[230] Fix | Delete
}
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
return $templates;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Add the block template objects currently saved in the database with the WooCommerce slug.
[238] Fix | Delete
* That is, templates that have been customised before WooCommerce started to use the
[239] Fix | Delete
* Template Registration API.
[240] Fix | Delete
*
[241] Fix | Delete
* @param array $query_result Array of template objects.
[242] Fix | Delete
* @param array $query Optional. Arguments to retrieve templates.
[243] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[244] Fix | Delete
* @return array
[245] Fix | Delete
*/
[246] Fix | Delete
public function add_db_templates_with_woo_slug( $query_result, $query, $template_type ) {
[247] Fix | Delete
$slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
[248] Fix | Delete
[249] Fix | Delete
if ( ! BlockTemplateUtils::supports_block_templates( $template_type ) && ! in_array( ComingSoonTemplate::SLUG, $slugs, true ) ) {
[250] Fix | Delete
return $query_result;
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
// For templates, we only need to load templates from the database. For
[254] Fix | Delete
// template parts, we also need to load them from the filesystem, as
[255] Fix | Delete
// there is no Template registration API for template parts.
[256] Fix | Delete
$template_files = 'wp_template' === $template_type ? BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ) : $this->get_block_templates( $slugs, $template_type );
[257] Fix | Delete
$new_templates = array();
[258] Fix | Delete
[259] Fix | Delete
foreach ( $template_files as $template_file ) {
[260] Fix | Delete
// It would be custom if the template was modified in the editor, so if it's not custom we can load it from
[261] Fix | Delete
// the filesystem.
[262] Fix | Delete
if ( 'custom' === $template_file->source ) {
[263] Fix | Delete
if (
[264] Fix | Delete
BlockTemplateUtils::PLUGIN_SLUG === $template_file->theme ||
[265] Fix | Delete
BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === $template_file->theme
[266] Fix | Delete
) {
[267] Fix | Delete
array_unshift( $new_templates, $template_file );
[268] Fix | Delete
}
[269] Fix | Delete
continue;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
// We only need to build templates from the filesystem for template parts.
[273] Fix | Delete
// Regular templates are handled by the Template registration API.
[274] Fix | Delete
if ( 'wp_template_part' === $template_type ) {
[275] Fix | Delete
$theme_slug = get_stylesheet();
[276] Fix | Delete
$possible_template_ids = [
[277] Fix | Delete
$theme_slug . '//' . $template_file->slug,
[278] Fix | Delete
$theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/' . $template_file->slug,
[279] Fix | Delete
$theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'] . '/' . $template_file->slug,
[280] Fix | Delete
];
[281] Fix | Delete
[282] Fix | Delete
$is_custom = false;
[283] Fix | Delete
$query_result_template_ids = array_column( $query_result, 'id' );
[284] Fix | Delete
[285] Fix | Delete
foreach ( $possible_template_ids as $template_id ) {
[286] Fix | Delete
if ( in_array( $template_id, $query_result_template_ids, true ) ) {
[287] Fix | Delete
$is_custom = true;
[288] Fix | Delete
break;
[289] Fix | Delete
}
[290] Fix | Delete
}
[291] Fix | Delete
$fits_slug_query =
[292] Fix | Delete
! isset( $query['slug__in'] ) || in_array( $template_file->slug, $query['slug__in'], true );
[293] Fix | Delete
$fits_area_query =
[294] Fix | Delete
! isset( $query['area'] ) || ( property_exists( $template_file, 'area' ) && $template_file->area === $query['area'] );
[295] Fix | Delete
$is_from_filesystem = isset( $template_file->path );
[296] Fix | Delete
$should_include = ! $is_custom && $fits_slug_query && $fits_area_query && $is_from_filesystem;
[297] Fix | Delete
[298] Fix | Delete
if ( $should_include ) {
[299] Fix | Delete
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
[300] Fix | Delete
$query_result[] = $template;
[301] Fix | Delete
}
[302] Fix | Delete
}
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
$query_result = array_merge( $new_templates, $query_result );
[306] Fix | Delete
[307] Fix | Delete
if ( count( $new_templates ) > 0 ) {
[308] Fix | Delete
// If there are certain templates that have been customised with the `woocommerce/woocommerce` slug,
[309] Fix | Delete
// We prioritize them over the theme and WC templates. That is, we remove the theme and WC templates
[310] Fix | Delete
// from the results and only keep the customised ones.
[311] Fix | Delete
$query_result = BlockTemplateUtils::remove_templates_with_custom_alternative( $query_result );
[312] Fix | Delete
[313] Fix | Delete
// There is the chance that the user customized the default template, installed a theme with a custom template
[314] Fix | Delete
// and customized that one as well. When that happens, duplicates might appear in the list.
[315] Fix | Delete
// See: https://github.com/woocommerce/woocommerce/issues/42220.
[316] Fix | Delete
$query_result = BlockTemplateUtils::remove_duplicate_customized_templates( $query_result );
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
/**
[320] Fix | Delete
* WC templates from theme aren't included in `$this->get_block_templates()` but are handled by Gutenberg.
[321] Fix | Delete
* We need to do additional search through all templates file to update title and description for WC
[322] Fix | Delete
* templates that aren't listed in theme.json.
[323] Fix | Delete
*/
[324] Fix | Delete
$query_result = array_map(
[325] Fix | Delete
function ( $template ) use ( $template_type ) {
[326] Fix | Delete
return BlockTemplateUtils::update_template_data( $template, $template_type );
[327] Fix | Delete
},
[328] Fix | Delete
$query_result
[329] Fix | Delete
);
[330] Fix | Delete
[331] Fix | Delete
return $query_result;
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
/**
[335] Fix | Delete
* When creating a template from the WP suggestion, don't load the templates with the WooCommerce slug.
[336] Fix | Delete
* Otherwise they take precedence and the new template can't be created.
[337] Fix | Delete
*
[338] Fix | Delete
* @param stdClass $prepared_post An object representing a single post prepared
[339] Fix | Delete
* for inserting or updating the database.
[340] Fix | Delete
*/
[341] Fix | Delete
public function dont_load_templates_for_suggestions( $prepared_post ) {
[342] Fix | Delete
if ( isset( $prepared_post->meta_input['is_wp_suggestion'] ) ) {
[343] Fix | Delete
remove_filter( 'get_block_templates', array( $this, 'add_db_templates_with_woo_slug' ), 10, 3 );
[344] Fix | Delete
}
[345] Fix | Delete
return $prepared_post;
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* Gets the templates from the WooCommerce blocks directory, skipping those for which a template already exists
[350] Fix | Delete
* in the theme directory.
[351] Fix | Delete
*
[352] Fix | Delete
* @param string[] $slugs An array of slugs to filter templates by. Templates whose slug does not match will not be returned.
[353] Fix | Delete
* @param array $already_found_templates Templates that have already been found, these are customised templates that are loaded from the database.
[354] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[355] Fix | Delete
*
[356] Fix | Delete
* @return array Templates from the WooCommerce blocks plugin directory.
[357] Fix | Delete
*/
[358] Fix | Delete
public function get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type = 'wp_template' ) {
[359] Fix | Delete
$template_files = BlockTemplateUtils::get_template_paths( $template_type );
[360] Fix | Delete
$templates = array();
[361] Fix | Delete
[362] Fix | Delete
foreach ( $template_files as $template_file ) {
[363] Fix | Delete
// Skip the template if it's blockified, and we should only use classic ones.
[364] Fix | Delete
if ( ! BlockTemplateUtils::should_use_blockified_product_grid_templates() && strpos( $template_file, 'blockified' ) !== false ) {
[365] Fix | Delete
continue;
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
$template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file );
[369] Fix | Delete
[370] Fix | Delete
// This template does not have a slug we're looking for. Skip it.
[371] Fix | Delete
if ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) {
[372] Fix | Delete
continue;
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
// If the theme already has a template, or the template is already in the list (i.e. it came from the
[376] Fix | Delete
// database) then we should not overwrite it with the one from the filesystem.
[377] Fix | Delete
if (
[378] Fix | Delete
BlockTemplateUtils::theme_has_template( $template_slug ) ||
[379] Fix | Delete
count(
[380] Fix | Delete
array_filter(
[381] Fix | Delete
$already_found_templates,
[382] Fix | Delete
function ( $template ) use ( $template_slug ) {
[383] Fix | Delete
$template_obj = (object) $template; //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
[384] Fix | Delete
return $template_obj->slug === $template_slug;
[385] Fix | Delete
}
[386] Fix | Delete
)
[387] Fix | Delete
) > 0 ) {
[388] Fix | Delete
continue;
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
// At this point the template only exists in the Blocks filesystem and has not been saved in the DB,
[392] Fix | Delete
// or superseded by the theme.
[393] Fix | Delete
$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug );
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
return $templates;
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
/**
[400] Fix | Delete
* Get and build the block template objects from the block template files.
[401] Fix | Delete
*
[402] Fix | Delete
* @param array $slugs An array of slugs to retrieve templates for.
[403] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[404] Fix | Delete
*
[405] Fix | Delete
* @return array WP_Block_Template[] An array of block template objects.
[406] Fix | Delete
*/
[407] Fix | Delete
public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) {
[408] Fix | Delete
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type );
[409] Fix | Delete
$templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type );
[410] Fix | Delete
[411] Fix | Delete
return array_merge( $templates_from_db, $templates_from_woo );
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
/**
[415] Fix | Delete
* Checks whether a block template with that name exists in Woo Blocks
[416] Fix | Delete
*
[417] Fix | Delete
* @param string $template_name Template to check.
[418] Fix | Delete
* @param array $template_type wp_template or wp_template_part.
[419] Fix | Delete
*
[420] Fix | Delete
* @return boolean
[421] Fix | Delete
*/
[422] Fix | Delete
public function block_template_is_available( $template_name, $template_type = 'wp_template' ) {
[423] Fix | Delete
if ( ! $template_name ) {
[424] Fix | Delete
return false;
[425] Fix | Delete
}
[426] Fix | Delete
$directory = BlockTemplateUtils::get_templates_directory( $template_type ) . '/' . $template_name . '.html';
[427] Fix | Delete
[428] Fix | Delete
return is_readable(
[429] Fix | Delete
$directory
[430] Fix | Delete
) || $this->get_block_templates( array( $template_name ), $template_type );
[431] Fix | Delete
}
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function