Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Query legacy and correct plugin slug for WooCommerce block templates (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcafferkey authored Jan 7, 2022
1 parent b81c221 commit e220727
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function maybe_return_blocks_template( $template, $id, $template_type ) {
// been unhooked so won't run again.
add_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 );
$maybe_template = function_exists( 'gutenberg_get_block_template' ) ?
gutenberg_get_block_template( 'woocommerce//' . $slug, $template_type ) :
get_block_template( 'woocommerce//' . $slug, $template_type );
gutenberg_get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//' . $slug, $template_type ) :
get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//' . $slug, $template_type );

// Re-hook this function, it was only unhooked to stop recursion.
add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
Expand Down Expand Up @@ -265,6 +265,11 @@ function( $template ) use ( $customised_template_slugs ) {
* @return int[]|\WP_Post[] An array of found templates.
*/
public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
// This was the previously incorrect slug used to save DB templates against.
// To maintain compatibility with users sites who have already customised WooCommerce block templates using this slug we have to still use it to query those.
// More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
$invalid_plugin_slug = 'woocommerce';

$check_query_args = array(
'post_type' => $template_type,
'posts_per_page' => -1,
Expand All @@ -273,13 +278,15 @@ public function get_block_templates_from_db( $slugs = array(), $template_type =
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => array( 'woocommerce', get_stylesheet() ),
'terms' => array( $invalid_plugin_slug, BlockTemplateUtils::PLUGIN_SLUG, get_stylesheet() ),
),
),
);

if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
$check_query_args['post_name__in'] = $slugs;
}

$check_query = new \WP_Query( $check_query_args );
$saved_woo_templates = $check_query->posts;

Expand Down
24 changes: 18 additions & 6 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class BlockTemplateUtils {
'TEMPLATE_PARTS' => 'parts',
);

/**
* WooCommerce plugin slug
*
* This is used to save templates to the DB which are stored against this value in the wp_terms table.
*
* @var string
*/
const PLUGIN_SLUG = 'woocommerce/woocommerce';

/**
* Returns an array containing the references of
* the passed blocks and their inner blocks.
Expand Down Expand Up @@ -119,7 +128,7 @@ public static function gutenberg_build_template_result_from_post( $post ) {
$template = new \WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->theme = 'woocommerce' === $theme ? 'WooCommerce' : $theme;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->source = 'custom';
Expand All @@ -138,7 +147,10 @@ public static function gutenberg_build_template_result_from_post( $post ) {
}
}

if ( 'woocommerce' === $theme ) {
// We are checking 'woocommerce' to maintain legacy templates which are saved to the DB,
// prior to updating to use the correct slug.
// More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
if ( self::PLUGIN_SLUG === $theme || 'woocommerce' === strtolower( $theme ) ) {
$template->origin = 'plugin';
}

Expand All @@ -164,8 +176,8 @@ public static function gutenberg_build_template_result_from_file( $template_file
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$template_content = file_get_contents( $template_file->path );
$template = new \WP_Block_Template();
$template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : 'woocommerce//' . $template_file->slug;
$template->theme = $template_is_from_theme ? $theme_name : 'WooCommerce';
$template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
$template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
$template->content = self::gutenberg_inject_theme_attribute_in_content( $template_content );
// 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.
$template->source = $template_file->source ? $template_file->source : 'plugin';
Expand Down Expand Up @@ -196,10 +208,10 @@ public static function create_new_block_template_object( $template_file, $templa

$new_template_item = array(
'slug' => $template_slug,
'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : 'woocommerce//' . $template_slug,
'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug,
'path' => $template_file,
'type' => $template_type,
'theme' => $template_is_from_theme ? $theme_name : 'woocommerce',
'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG,
// 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.
'source' => $template_is_from_theme ? 'theme' : 'plugin',
'title' => self::convert_slug_to_title( $template_slug ),
Expand Down

0 comments on commit e220727

Please sign in to comment.