Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Utils
File: BlockTemplateUtils.php
* 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.
[500] Fix | Delete
*
[501] Fix | Delete
* @param string $template_type Optional. Template type: `wp_template` or `wp_template_part`.
[502] Fix | Delete
* Default `wp_template`.
[503] Fix | Delete
* @return boolean
[504] Fix | Delete
*/
[505] Fix | Delete
public static function supports_block_templates( $template_type = 'wp_template' ) {
[506] Fix | Delete
if ( 'wp_template_part' === $template_type && ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) ) {
[507] Fix | Delete
return true;
[508] Fix | Delete
} elseif ( 'wp_template' === $template_type && wp_is_block_theme() ) {
[509] Fix | Delete
return true;
[510] Fix | Delete
}
[511] Fix | Delete
return false;
[512] Fix | Delete
}
[513] Fix | Delete
[514] Fix | Delete
/**
[515] Fix | Delete
* Gets the `archive-product` fallback template stored on the db for a given slug.
[516] Fix | Delete
*
[517] Fix | Delete
* @param string $template_slug Slug to check for fallbacks.
[518] Fix | Delete
* @param array $db_templates Templates that have already been found on the db.
[519] Fix | Delete
* @return boolean|object
[520] Fix | Delete
*/
[521] Fix | Delete
public static function get_fallback_template_from_db( $template_slug, $db_templates ) {
[522] Fix | Delete
$registered_template = self::get_template( $template_slug );
[523] Fix | Delete
[524] Fix | Delete
if ( $registered_template && isset( $registered_template->fallback_template ) ) {
[525] Fix | Delete
foreach ( $db_templates as $template ) {
[526] Fix | Delete
if ( $registered_template->fallback_template === $template->slug ) {
[527] Fix | Delete
return $template;
[528] Fix | Delete
}
[529] Fix | Delete
}
[530] Fix | Delete
}
[531] Fix | Delete
[532] Fix | Delete
return false;
[533] Fix | Delete
}
[534] Fix | Delete
[535] Fix | Delete
/**
[536] Fix | Delete
* Removes templates from the theme or WooCommerce which have the same slug
[537] Fix | Delete
* as template saved in the database with the `woocommerce/woocommerce` theme.
[538] Fix | Delete
* Before WC migrated to the Template Registration API from WordPress, templates
[539] Fix | Delete
* were saved in the database with the `woocommerce/woocommerce` theme instead
[540] Fix | Delete
* of the theme's slug.
[541] Fix | Delete
*
[542] Fix | Delete
* @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on.
[543] Fix | Delete
*
[544] Fix | Delete
* @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default.
[545] Fix | Delete
*/
[546] Fix | Delete
public static function remove_templates_with_custom_alternative( $templates ) {
[547] Fix | Delete
[548] Fix | Delete
// Get the slugs of all templates that have been customised and saved in the database.
[549] Fix | Delete
$customised_template_slugs = array_column(
[550] Fix | Delete
array_filter(
[551] Fix | Delete
$templates,
[552] Fix | Delete
function ( $template ) {
[553] Fix | Delete
// This template has been customised and saved as a post.
[554] Fix | Delete
return 'custom' === $template->source && ( self::PLUGIN_SLUG === $template->theme || self::DEPRECATED_PLUGIN_SLUG === $template->theme );
[555] Fix | Delete
}
[556] Fix | Delete
),
[557] Fix | Delete
'slug'
[558] Fix | Delete
);
[559] Fix | Delete
[560] Fix | Delete
// Remove theme and WC templates that have the same slug as a customised one.
[561] Fix | Delete
return array_values(
[562] Fix | Delete
array_filter(
[563] Fix | Delete
$templates,
[564] Fix | Delete
function ( $template ) use ( $customised_template_slugs ) {
[565] Fix | Delete
// This template has been customised and saved as a post, so return it.
[566] Fix | Delete
return ! ( 'custom' !== $template->source && in_array( $template->slug, $customised_template_slugs, true ) );
[567] Fix | Delete
}
[568] Fix | Delete
)
[569] Fix | Delete
);
[570] Fix | Delete
}
[571] Fix | Delete
[572] Fix | Delete
/**
[573] Fix | Delete
* Removes customized templates that shouldn't be available. That means customized templates based on the
[574] Fix | Delete
* WooCommerce default template when there is a customized template based on the theme template.
[575] Fix | Delete
*
[576] Fix | Delete
* @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on.
[577] Fix | Delete
*
[578] Fix | Delete
* @return array Filtered list of templates with only relevant templates available.
[579] Fix | Delete
*/
[580] Fix | Delete
public static function remove_duplicate_customized_templates( $templates ) {
[581] Fix | Delete
$theme_slug = get_stylesheet();
[582] Fix | Delete
[583] Fix | Delete
$customized_theme_template_slugs = array_column(
[584] Fix | Delete
array_filter(
[585] Fix | Delete
$templates,
[586] Fix | Delete
function ( $template ) use ( $theme_slug ) {
[587] Fix | Delete
// This template has been customised and saved as a post.
[588] Fix | Delete
return 'custom' === $template->source && $theme_slug === $template->theme;
[589] Fix | Delete
}
[590] Fix | Delete
),
[591] Fix | Delete
'slug'
[592] Fix | Delete
);
[593] Fix | Delete
[594] Fix | Delete
return array_filter(
[595] Fix | Delete
$templates,
[596] Fix | Delete
function ( $template ) use ( $theme_slug, $customized_theme_template_slugs ) {
[597] Fix | Delete
if ( $template->theme === $theme_slug ) {
[598] Fix | Delete
// This is a customized template based on the theme template, so it should be returned.
[599] Fix | Delete
return true;
[600] Fix | Delete
}
[601] Fix | Delete
// Customized from the WooCommerce default template: keep only if there is no customized theme template with same slug.
[602] Fix | Delete
if ( 'custom' === $template->source ) {
[603] Fix | Delete
return ! in_array( $template->slug, $customized_theme_template_slugs, true );
[604] Fix | Delete
}
[605] Fix | Delete
return true;
[606] Fix | Delete
}
[607] Fix | Delete
);
[608] Fix | Delete
}
[609] Fix | Delete
[610] Fix | Delete
/**
[611] Fix | Delete
* Returns whether the blockified templates should be used or not.
[612] Fix | Delete
* If the option is not stored on the db, we need to check if the current theme is a block one or not.
[613] Fix | Delete
*
[614] Fix | Delete
* @return boolean
[615] Fix | Delete
*/
[616] Fix | Delete
public static function should_use_blockified_product_grid_templates() {
[617] Fix | Delete
$use_blockified_templates = get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
[618] Fix | Delete
[619] Fix | Delete
if ( false === $use_blockified_templates ) {
[620] Fix | Delete
return wp_is_block_theme();
[621] Fix | Delete
}
[622] Fix | Delete
[623] Fix | Delete
return wc_string_to_bool( $use_blockified_templates );
[624] Fix | Delete
}
[625] Fix | Delete
[626] Fix | Delete
/**
[627] Fix | Delete
* Determines whether the provided $blocks contains any of the $block_names,
[628] Fix | Delete
* or if they contain a pattern that contains any of the $block_names.
[629] Fix | Delete
*
[630] Fix | Delete
* @param string[] $block_names Full block types to look for.
[631] Fix | Delete
* @param WP_Block[] $blocks Array of block objects.
[632] Fix | Delete
* @return bool Whether the content contains the specified block.
[633] Fix | Delete
*/
[634] Fix | Delete
public static function has_block_including_patterns( $block_names, $blocks ) {
[635] Fix | Delete
$flattened_blocks = self::flatten_blocks( $blocks );
[636] Fix | Delete
[637] Fix | Delete
foreach ( $flattened_blocks as &$block ) {
[638] Fix | Delete
if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $block_names, true ) ) {
[639] Fix | Delete
return true;
[640] Fix | Delete
}
[641] Fix | Delete
if (
[642] Fix | Delete
'core/pattern' === $block['blockName'] &&
[643] Fix | Delete
isset( $block['attrs']['slug'] )
[644] Fix | Delete
) {
[645] Fix | Delete
$registry = WP_Block_Patterns_Registry::get_instance();
[646] Fix | Delete
$pattern = $registry->get_registered( $block['attrs']['slug'] );
[647] Fix | Delete
if ( isset( $pattern['content'] ) ) {
[648] Fix | Delete
$pattern_blocks = parse_blocks( $pattern['content'] );
[649] Fix | Delete
if ( self::has_block_including_patterns( $block_names, $pattern_blocks ) ) {
[650] Fix | Delete
return true;
[651] Fix | Delete
}
[652] Fix | Delete
}
[653] Fix | Delete
}
[654] Fix | Delete
}
[655] Fix | Delete
[656] Fix | Delete
return false;
[657] Fix | Delete
}
[658] Fix | Delete
[659] Fix | Delete
/**
[660] Fix | Delete
* Returns whether the passed `$template` has the legacy template block.
[661] Fix | Delete
*
[662] Fix | Delete
* @param object $template The template object.
[663] Fix | Delete
* @return boolean
[664] Fix | Delete
*/
[665] Fix | Delete
public static function template_has_legacy_template_block( $template ) {
[666] Fix | Delete
if ( has_block( 'woocommerce/legacy-template', $template->content ) ) {
[667] Fix | Delete
return true;
[668] Fix | Delete
}
[669] Fix | Delete
[670] Fix | Delete
$blocks = parse_blocks( $template->content );
[671] Fix | Delete
[672] Fix | Delete
return self::has_block_including_patterns( array( 'woocommerce/legacy-template' ), $blocks );
[673] Fix | Delete
}
[674] Fix | Delete
[675] Fix | Delete
/**
[676] Fix | Delete
* Updates the title, description and area of a template to the correct values and to make them more user-friendly.
[677] Fix | Delete
* For example, instead of:
[678] Fix | Delete
* - Title: `Tag (product_tag)`
[679] Fix | Delete
* - Description: `Displays taxonomy: Tag.`
[680] Fix | Delete
* we display:
[681] Fix | Delete
* - Title: `Products by Tag`
[682] Fix | Delete
* - Description: `Displays products filtered by a tag.`.
[683] Fix | Delete
*
[684] Fix | Delete
* @param WP_Block_Template $template The template object.
[685] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[686] Fix | Delete
*
[687] Fix | Delete
* @return WP_Block_Template
[688] Fix | Delete
*/
[689] Fix | Delete
public static function update_template_data( $template, $template_type ) {
[690] Fix | Delete
if ( ! $template ) {
[691] Fix | Delete
return $template;
[692] Fix | Delete
}
[693] Fix | Delete
if ( empty( $template->title ) || $template->title === $template->slug ) {
[694] Fix | Delete
$template->title = self::get_block_template_title( $template->slug );
[695] Fix | Delete
}
[696] Fix | Delete
if ( empty( $template->description ) ) {
[697] Fix | Delete
$template->description = self::get_block_template_description( $template->slug );
[698] Fix | Delete
}
[699] Fix | Delete
if ( empty( $template->area ) || 'uncategorized' === $template->area ) {
[700] Fix | Delete
$template->area = self::get_block_template_area( $template->slug, $template_type );
[701] Fix | Delete
}
[702] Fix | Delete
[703] Fix | Delete
return $template;
[704] Fix | Delete
}
[705] Fix | Delete
[706] Fix | Delete
/**
[707] Fix | Delete
* Gets the templates saved in the database.
[708] Fix | Delete
*
[709] Fix | Delete
* @param array $slugs An array of slugs to retrieve templates for.
[710] Fix | Delete
* @param string $template_type wp_template or wp_template_part.
[711] Fix | Delete
*
[712] Fix | Delete
* @return \WP_Block_Template[] An array of found templates.
[713] Fix | Delete
*/
[714] Fix | Delete
public static function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
[715] Fix | Delete
$check_query_args = array(
[716] Fix | Delete
'post_type' => $template_type,
[717] Fix | Delete
'posts_per_page' => -1,
[718] Fix | Delete
'no_found_rows' => true,
[719] Fix | Delete
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
[720] Fix | Delete
array(
[721] Fix | Delete
'taxonomy' => 'wp_theme',
[722] Fix | Delete
'field' => 'name',
[723] Fix | Delete
'terms' => array( self::DEPRECATED_PLUGIN_SLUG, self::PLUGIN_SLUG, get_stylesheet() ),
[724] Fix | Delete
),
[725] Fix | Delete
),
[726] Fix | Delete
);
[727] Fix | Delete
[728] Fix | Delete
if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
[729] Fix | Delete
$check_query_args['post_name__in'] = $slugs;
[730] Fix | Delete
}
[731] Fix | Delete
[732] Fix | Delete
$check_query = new \WP_Query( $check_query_args );
[733] Fix | Delete
$saved_woo_templates = $check_query->posts;
[734] Fix | Delete
[735] Fix | Delete
return array_map(
[736] Fix | Delete
function ( $saved_woo_template ) {
[737] Fix | Delete
return self::build_template_result_from_post( $saved_woo_template );
[738] Fix | Delete
},
[739] Fix | Delete
$saved_woo_templates
[740] Fix | Delete
);
[741] Fix | Delete
}
[742] Fix | Delete
[743] Fix | Delete
/**
[744] Fix | Delete
* Gets the template part by slug
[745] Fix | Delete
*
[746] Fix | Delete
* @param string $slug The template part slug.
[747] Fix | Delete
*
[748] Fix | Delete
* @return string The template part content.
[749] Fix | Delete
*/
[750] Fix | Delete
public static function get_template_part( $slug ) {
[751] Fix | Delete
$templates_from_db = self::get_block_templates_from_db( array( $slug ), 'wp_template_part' );
[752] Fix | Delete
if ( count( $templates_from_db ) > 0 ) {
[753] Fix | Delete
$template_slug_to_load = $templates_from_db[0]->theme;
[754] Fix | Delete
} else {
[755] Fix | Delete
$theme_has_template = self::theme_has_template_part( $slug );
[756] Fix | Delete
$template_slug_to_load = $theme_has_template ? get_stylesheet() : self::PLUGIN_SLUG;
[757] Fix | Delete
}
[758] Fix | Delete
$template_part = get_block_template( $template_slug_to_load . '//' . $slug, 'wp_template_part' );
[759] Fix | Delete
[760] Fix | Delete
if ( $template_part && ! empty( $template_part->content ) ) {
[761] Fix | Delete
return $template_part->content;
[762] Fix | Delete
}
[763] Fix | Delete
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
[764] Fix | Delete
return file_get_contents( self::get_templates_directory( 'wp_template_part' ) . DIRECTORY_SEPARATOR . $slug . '.html' );
[765] Fix | Delete
}
[766] Fix | Delete
}
[767] Fix | Delete
[768] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function