diff --git a/src/BlockTemplatesRefactorController.php b/src/BlockTemplatesRefactorController.php new file mode 100644 index 00000000000..f1f48c0d932 --- /dev/null +++ b/src/BlockTemplatesRefactorController.php @@ -0,0 +1,230 @@ +package = $package; + + $feature_gating = $package->feature(); + $is_block_templates_controller_refactor_enabled = $feature_gating->is_block_templates_controller_refactor_enabled(); + + // This feature is gated for WooCommerce versions 6.0.0 and above. + if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '6.0.0', '>=' ) && $is_block_templates_controller_refactor_enabled ) { + $this->init(); + } + } + + /** + * Initialization method. + */ + protected function init() { + add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); + // This is necessary for the REST API used by the Site Editor to update a specific template. + add_filter( 'get_block_file_template', array( $this, 'get_block_template_by_id' ), 10, 3 ); + add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); + } + + /** + * Gets the templates saved in the database. + * + * @param array $slugs An array of slugs to retrieve templates for. + * @param string $template_type wp_template or wp_template_part. + * + * @return int[]|\WP_Post[] An array of found templates. + */ + public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { + return BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ); + } + + /** + * Adds block templates to the query result. This function is used to return the list of all the templates via the REST API for the Site Editor. We don't want to apply any changes when the REST API is used. + * + * @param WP_Block_Template[] $query_result An array of templates to render that matches the query. + * @param array $query An array of query vars. + * @param string $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template[] An array of found templates. + */ + public function add_block_templates( $query_result, $query, $template_type ) { + $slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array(); + $templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, array(), $template_type ); + $templates_from_db = $this->get_block_templates_from_db( $slugs, $template_type ); + $all_templates = array_merge( $query_result, $templates_from_woo, $templates_from_db ); + $organized_templates = $this->organize_templates( $all_templates ); + + if ( ! isset( $all_templates ) || isset( $all_templates ) && count( $all_templates ) === 0 || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + return $organized_templates; + } + $templates = $this->render_template( end( $organized_templates ), $organized_templates ); + + return is_array( $templates ) ? $templates : array( $templates ); + } + + /** + * Organize templates by priority. + * + * @param WP_Block_Template[] $all_templates An array of templates to render that matches the query. + * + * @return WP_Block_Template[] An array of found templates. + */ + private function organize_templates( $all_templates ) { + return array_reduce( + $all_templates, + function( $carry, $template ) { + $current_template = $carry[ $template->slug ] ?? null; + if ( ! $current_template ) { + $carry[ $template->slug ] = $template; + return $carry; + } + + $carry[ $template->slug ] = $this->has_high_priority( $current_template, $template ); + return $carry; + }, + array() + ); + } + + /** + * Ensure that the template with the highest priority is returned. + * The priority is as follows: + * 1. Custom templates + * 2. Theme templates + * 3. Plugin templates + * + * @param WP_Block_Template $current_template Current to template. + * @param WP_Block_Template $template_to_compare An array of query vars. + * + * @return boolean + */ + private function has_high_priority( $current_template, $template_to_compare ) { + $priority_source = array( + 'plugin' => 0, + 'theme' => 1, + 'custom' => 2, + ); + + $current_template_source = $current_template->source; + $template_to_compare_source = $template_to_compare->source; + + if ( $current_template_source === $template_to_compare_source ) { + return str_contains( $current_template->id, 'woocommerce' ) ? $template_to_compare : $current_template; + } + + return ( $priority_source[ $current_template_source ] > $priority_source[ $template_to_compare_source ] ) ? $current_template : $template_to_compare; + + } + + /** + * Renders the template. + * + * @param WP_Template $template_to_render The template to render. + * @param WP_Block_Template[] $all_templates An array of templates to render that matches the query. + * + * @return WP_Block_Template[] An array of found templates. + */ + public function render_template( $template_to_render, $all_templates ) { + return $all_templates; + } + + + /** + * Gets the templates from the WooCommerce blocks directory, skipping those for which a template already exists + * in the theme directory. + * + * @param string[] $slugs An array of slugs to filter templates by. Templates whose slug does not match will not be returned. + * @param array $already_found_templates Templates that have already been found, these are customised templates that are loaded from the database. + * @param string $template_type wp_template or wp_template_part. + * + * @return array Templates from the WooCommerce blocks plugin directory. + */ + public function get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type = 'wp_template' ) { + $directory = BlockTemplatesUtilsRefactor::get_templates_directory( $template_type ); + $template_files = BlockTemplatesUtilsRefactor::get_template_paths( $directory ); + $templates = array(); + + foreach ( $template_files as $template_file ) { + $template_slug = BlockTemplatesUtilsRefactor::generate_template_slug_from_path( $template_file ); + + // This template does not have a slug we're looking for. Skip it. + if ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) { + continue; + } + $template_slug = BlockTemplatesUtilsRefactor::generate_template_slug_from_path( $template_file ); + $template_object = BlockTemplatesUtilsRefactor::create_new_block_template_object( $template_file, $template_type, $template_slug ); + $template_built = BlockTemplatesUtilsRefactor::build_template_result_from_file( $template_object, $template_type ); + $templates[] = $template_built; + } + + return $templates; + } + + /** + * Gets the templates by id. + * This is necessary for the REST API used by the Site Editor to update a specific template. + * + * @param null $_null . + * @param string $id Template id. + * @param string $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template|null + */ + public function get_block_template_by_id( $_null, $id, $template_type = 'wp_template' ) { + $templates = $this->get_block_templates_from_woocommerce( array(), array(), $template_type ); + + // array_values is necessary to reset the index. + $template = array_values( + array_filter( + $templates, + function( $template ) use ( $id ) { + return $template->id === $id; + } + ) + ); + if ( isset( $template ) && isset( $template[0] ) ) { + return $this->update_template_data_rest_api( $template[0] ); + } + return $_null; + } + + /** + * Update template data for the REST API. This is necessary for the Site Editor to update a specific template. + * + * @param WP_Block_Template $template template. + * + * @return WP_Block_Template|null + */ + public function update_template_data_rest_api( $template ) { + return $template; + } + +} diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 58b1f13ac0e..6f7fb413c2f 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -6,6 +6,7 @@ use Automattic\WooCommerce\Blocks\AssetsController; use Automattic\WooCommerce\Blocks\BlockPatterns; use Automattic\WooCommerce\Blocks\BlockTemplatesController; +use Automattic\WooCommerce\Blocks\BlockTemplatesRefactorController; use Automattic\WooCommerce\Blocks\BlockTypesController; use Automattic\WooCommerce\Blocks\Domain\Services\CreateAccount; use Automattic\WooCommerce\Blocks\Domain\Services\JetpackWooCommerceAnalytics; @@ -35,6 +36,7 @@ use Automattic\WooCommerce\StoreApi\SchemaController; use Automattic\WooCommerce\StoreApi\StoreApi; use Automattic\WooCommerce\Blocks\Shipping\ShippingController; +use Automattic\WooCommerce\Blocks\SingleProductTemplateController; use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility; use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility; use Automattic\WooCommerce\Blocks\Domain\Services\OnboardingTasks\TasksController; @@ -150,6 +152,8 @@ function() { $this->container->get( BlockPatterns::class ); $this->container->get( BlockTypesController::class ); $this->container->get( BlockTemplatesController::class ); + $this->container->get( BlockTemplatesRefactorController::class ); + $this->container->get( SingleProductTemplateController::class ); $this->container->get( ProductSearchResultsTemplate::class ); $this->container->get( ProductAttributeTemplate::class ); $this->container->get( CartTemplate::class ); @@ -291,6 +295,18 @@ function ( Container $container ) { return new BlockTemplatesController( $container->get( Package::class ) ); } ); + $this->container->register( + BlockTemplatesRefactorController::class, + function ( Container $container ) { + return new BlockTemplatesRefactorController( $container->get( Package::class ) ); + } + ); + $this->container->register( + SingleProductTemplateController::class, + function ( Container $container ) { + return new SingleProductTemplateController( $container->get( Package::class ) ); + } + ); $this->container->register( ProductSearchResultsTemplate::class, function () { diff --git a/src/SingleProductTemplateController.php b/src/SingleProductTemplateController.php new file mode 100644 index 00000000000..896cb724664 --- /dev/null +++ b/src/SingleProductTemplateController.php @@ -0,0 +1,72 @@ +template_title = _x( 'Single Product', 'Template name', 'woo-gutenberg-products-block' ); + $this->template_description = __( 'Displays a single product.', 'woo-gutenberg-products-block' ); + + } + + /** + * Renders the template. + * + * @param WP_Template $template_to_render The template to render. + * @param WP_Block_Template[] $all_templates An array of templates to render that matches the query. + * + * @return WP_Block_Template[] An array of found templates. + */ + public function render_template( $template_to_render, $all_templates ) { + if ( is_singular( 'product' ) ) { + $update_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template_to_render->content ); + $template_to_render->content = $update_content; + return $template_to_render; + } + return $all_templates; + } + + + /** + * Update template data for the REST API. This is necessary for the Site Editor to update a specific template. + * + * @param WP_Block_Template $template template. + * + * @return WP_Block_Template|null + */ + public function update_template_data_rest_api( $template ) { + $template->title = $this->template_title; + $template->description = $this->template_description; + return $template; + } + +} diff --git a/src/Utils/BlockTemplatesUtilsRefactor.php b/src/Utils/BlockTemplatesUtilsRefactor.php new file mode 100644 index 00000000000..e3c24d28aac --- /dev/null +++ b/src/Utils/BlockTemplatesUtilsRefactor.php @@ -0,0 +1,700 @@ + 'block-templates', + 'DEPRECATED_TEMPLATE_PARTS' => 'block-template-parts', + 'TEMPLATES' => 'templates', + 'TEMPLATE_PARTS' => 'parts', + ); + + const TEMPLATES_ROOT_DIR = 'templates'; + + /** + * 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'; + + /** + * Deprecated WooCommerce plugin slug + * + * For supporting users who have customized templates under the incorrect plugin slug during the first release. + * More context found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423. + * + * @var string + */ + const DEPRECATED_PLUGIN_SLUG = 'woocommerce'; + + /** + * Returns an array containing the references of + * the passed blocks and their inner blocks. + * + * @param array $blocks array of blocks. + * + * @return array block references to the passed blocks and their inner blocks. + */ + public static function flatten_blocks( &$blocks ) { + $all_blocks = array(); + $queue = array(); + foreach ( $blocks as &$block ) { + $queue[] = &$block; + } + $queue_count = count( $queue ); + + while ( $queue_count > 0 ) { + $block = &$queue[0]; + array_shift( $queue ); + $all_blocks[] = &$block; + + if ( ! empty( $block['innerBlocks'] ) ) { + foreach ( $block['innerBlocks'] as &$inner_block ) { + $queue[] = &$inner_block; + } + } + + $queue_count = count( $queue ); + } + + return $all_blocks; + } + + /** + * Parses wp_template content and injects the current theme's + * stylesheet as a theme attribute into each wp_template_part + * + * @param string $template_content serialized wp_template content. + * + * @return string Updated wp_template content. + */ + public static function inject_theme_attribute_in_content( $template_content ) { + $has_updated_content = false; + $new_content = ''; + $template_blocks = parse_blocks( $template_content ); + + $blocks = self::flatten_blocks( $template_blocks ); + foreach ( $blocks as &$block ) { + if ( + 'core/template-part' === $block['blockName'] && + ! isset( $block['attrs']['theme'] ) + ) { + $block['attrs']['theme'] = wp_get_theme()->get_stylesheet(); + $has_updated_content = true; + } + } + + if ( $has_updated_content ) { + foreach ( $template_blocks as &$block ) { + $new_content .= serialize_block( $block ); + } + + return $new_content; + } + + return $template_content; + } + + /** + * Build a unified template object based a post Object. + * 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. + * + * @param \WP_Post $post Template post. + * + * @return \WP_Block_Template|\WP_Error Template. + */ + public static function build_template_result_from_post( $post ) { + $terms = get_the_terms( $post, 'wp_theme' ); + + if ( is_wp_error( $terms ) ) { + return $terms; + } + + if ( ! $terms ) { + return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woo-gutenberg-products-block' ) ); + } + + $theme = $terms[0]->name; + $has_theme_file = true; + + $template = new \WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->source = 'custom'; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + $template->has_theme_file = $has_theme_file; + $template->is_custom = false; + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. + + if ( 'wp_template_part' === $post->post_type ) { + $type_terms = get_the_terms( $post, 'wp_template_part_area' ); + if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { + $template->area = $type_terms[0]->name; + } + } + + // We are checking 'woocommerce' to maintain classic 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 || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) { + $template->origin = 'plugin'; + } + + return $template; + } + + /** + * Build a unified template object based on a theme file. + * 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. + * + * @param array|object $template_file Theme file. + * @param string $template_type wp_template or wp_template_part. + * + * @return \WP_Block_Template Template. + */ + public static function build_template_result_from_file( $template_file, $template_type ) { + $template_file = (object) $template_file; + + // If the theme has an archive-products.html template but does not have product taxonomy templates + // then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend. + $template_is_from_theme = 'theme' === $template_file->source; + $theme_name = wp_get_theme()->get( 'TextDomain' ); + + // 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 : self::PLUGIN_SLUG . '//' . $template_file->slug; + $template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG; + $template->content = self::inject_theme_attribute_in_content( $template_content ); + $template->source = $template_file->source ? $template_file->source : 'plugin'; + $template->slug = $template_file->slug; + $template->type = $template_type; + $template->title = $template_file->title; + $template->description = $template_file->description; + $template->status = 'publish'; + $template->has_theme_file = true; + $template->origin = $template_file->source; + $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. + $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. + $template->area = 'uncategorized'; + return $template; + } + + /** + * Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any. + * + * @param string $template_file Block template file path. + * @param string $template_type wp_template or wp_template_part. + * @param string $template_slug Block template slug e.g. single-product. + * @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks. + * + * @return object Block template object. + */ + public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) { + $theme_name = wp_get_theme()->get( 'TextDomain' ); + + $new_template_item = array( + 'slug' => $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 : self::PLUGIN_SLUG, + 'source' => $template_is_from_theme ? 'theme' : 'plugin', + 'title' => self::get_block_template_title( $template_slug ), + 'description' => '', + 'post_types' => array(), + ); + + return (object) $new_template_item; + } + + /** + * Finds all nested template part file paths in a theme's directory. + * + * @param string $base_directory The theme's file path. + * @return array $path_list A list of paths to all template part files. + */ + public static function get_template_paths( $base_directory ) { + $path_list = array(); + if ( file_exists( $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; + } + } + return $path_list; + } + + /** + * Gets the directory where templates of a specific template type can be found. + * + * @param string $template_type wp_template or wp_template_part. + * + * @return string + */ + public static function get_templates_directory( $template_type = 'wp_template' ) { + $root_path = dirname( __DIR__, 2 ) . '/' . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR; + $templates_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATES']; + $template_parts_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATE_PARTS']; + + if ( 'wp_template_part' === $template_type ) { + return $template_parts_directory; + } + + if ( self::should_use_blockified_product_grid_templates() ) { + return $templates_directory . '/blockified'; + } + + return $templates_directory; + } + + /** + * Returns template titles. + * + * @param string $template_slug The templates slug (e.g. single-product). + * @return string Human friendly title. + */ + public static function get_block_template_title( $template_slug ) { + return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) ); + } + + /** + * Converts template paths into a slug + * + * @param string $path The template's path. + * @return string slug + */ + public static function generate_template_slug_from_path( $path ) { + $template_extension = '.html'; + + return basename( $path, $template_extension ); + } + + /** + * Gets the first matching template part within themes directories + * + * Since [Gutenberg 12.1.0](https://github.com/WordPress/gutenberg/releases/tag/v12.1.0), the conventions for + * block templates and parts directory has changed from `block-templates` and `block-templates-parts` + * to `templates` and `parts` respectively. + * + * This function traverses all possible combinations of directory paths where a template or part + * could be located and returns the first one which is readable, prioritizing the new convention + * over the deprecated one, but maintaining that one for backwards compatibility. + * + * @param string $template_slug The slug of the template (i.e. without the file extension). + * @param string $template_type Either `wp_template` or `wp_template_part`. + * + * @return string|null The matched path or `null` if no match was found. + */ + public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) { + $template_filename = $template_slug . '.html'; + $possible_templates_dir = 'wp_template' === $template_type ? array( + self::DIRECTORY_NAMES['TEMPLATES'], + self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'], + ) : array( + self::DIRECTORY_NAMES['TEMPLATE_PARTS'], + self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'], + ); + + // Combine the possible root directory names with either the template directory + // or the stylesheet directory for child themes. + $possible_paths = array_reduce( + $possible_templates_dir, + function( $carry, $item ) use ( $template_filename ) { + $filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename; + + $carry[] = get_stylesheet_directory() . $filepath; + $carry[] = get_template_directory() . $filepath; + + return $carry; + }, + array() + ); + + // Return the first matching. + foreach ( $possible_paths as $path ) { + if ( is_readable( $path ) ) { + return $path; + } + } + + return null; + } + + /** + * Check if the theme has a template. So we know if to load our own in or not. + * + * @param string $template_name name of the template file without .html extension e.g. 'single-product'. + * @return boolean + */ + public static function theme_has_template( $template_name ) { + return ! ! self::get_theme_template_path( $template_name, 'wp_template' ); + } + + /** + * Check if the theme has a template. So we know if to load our own in or not. + * + * @param string $template_name name of the template file without .html extension e.g. 'single-product'. + * @return boolean + */ + public static function theme_has_template_part( $template_name ) { + return ! ! self::get_theme_template_path( $template_name, 'wp_template_part' ); + } + + /** + * 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. + * + * @param string $template_type Optional. Template type: `wp_template` or `wp_template_part`. + * Default `wp_template`. + * @return boolean + */ + public static function supports_block_templates( $template_type = 'wp_template' ) { + if ( 'wp_template_part' === $template_type && ( wc_current_theme_is_fse_theme() || current_theme_supports( 'block-template-parts' ) ) ) { + return true; + } elseif ( 'wp_template' === $template_type && wc_current_theme_is_fse_theme() ) { + return true; + } + return false; + } + + /** + * Retrieves a single unified template object using its id. + * + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param string $template_type Optional. Template type: `wp_template` or 'wp_template_part`. + * Default `wp_template`. + * + * @return WP_Block_Template|null Template. + */ + public static function get_block_template( $id, $template_type ) { + if ( function_exists( 'get_block_template' ) ) { + return get_block_template( $id, $template_type ); + } + + if ( function_exists( 'gutenberg_get_block_template' ) ) { + return gutenberg_get_block_template( $id, $template_type ); + } + + return null; + + } + + /** + * Checks if we can fall back to the `archive-product` template for a given slug. + * + * `taxonomy-product_cat`, `taxonomy-product_tag`, `taxonomy-product_attribute` templates can + * generally use the `archive-product` as a fallback if there are no specific overrides. + * + * @param string $template_slug Slug to check for fallbacks. + * @return boolean + */ + public static function template_is_eligible_for_product_archive_fallback( $template_slug ) { + return in_array( $template_slug, self::ELIGIBLE_FOR_ARCHIVE_PRODUCT_FALLBACK, true ); + } + + /** + * Checks if we can fall back to an `archive-product` template stored on the db for a given slug. + * + * @param string $template_slug Slug to check for fallbacks. + * @param array $db_templates Templates that have already been found on the db. + * @return boolean + */ + public static function template_is_eligible_for_product_archive_fallback_from_db( $template_slug, $db_templates ) { + $eligible_for_fallback = self::template_is_eligible_for_product_archive_fallback( $template_slug ); + if ( ! $eligible_for_fallback ) { + return false; + } + + $array_filter = array_filter( + $db_templates, + function ( $template ) use ( $template_slug ) { + return 'archive-product' === $template->slug; + } + ); + + return count( $array_filter ) > 0; + } + + /** + * Gets the `archive-product` fallback template stored on the db for a given slug. + * + * @param string $template_slug Slug to check for fallbacks. + * @param array $db_templates Templates that have already been found on the db. + * @return boolean|object + */ + public static function get_fallback_template_from_db( $template_slug, $db_templates ) { + $eligible_for_fallback = self::template_is_eligible_for_product_archive_fallback( $template_slug ); + if ( ! $eligible_for_fallback ) { + return false; + } + + foreach ( $db_templates as $template ) { + if ( 'archive-product' === $template->slug ) { + return $template; + } + } + + return false; + } + + /** + * Checks if we can fall back to the `archive-product` file template for a given slug in the current theme. + * + * `taxonomy-product_cat`, `taxonomy-product_tag`, `taxonomy-attribute` templates can + * generally use the `archive-product` as a fallback if there are no specific overrides. + * + * @param string $template_slug Slug to check for fallbacks. + * @return boolean + */ + public static function template_is_eligible_for_product_archive_fallback_from_theme( $template_slug ) { + return self::template_is_eligible_for_product_archive_fallback( $template_slug ) + && ! self::theme_has_template( $template_slug ) + && self::theme_has_template( 'archive-product' ); + } + + /** + * Sets the `has_theme_file` to `true` for templates with fallbacks + * + * There are cases (such as tags, categories and attributes) in which fallback templates + * can be used; so, while *technically* the theme doesn't have a specific file + * for them, it is important that we tell Gutenberg that we do, in fact, + * have a theme file (i.e. the fallback one). + * + * **Note:** this function changes the array that has been passed. + * + * It returns `true` if anything was changed, `false` otherwise. + * + * @param array $query_result Array of template objects. + * @param object $template A specific template object which could have a fallback. + * + * @return boolean + */ + public static function set_has_theme_file_if_fallback_is_available( $query_result, $template ) { + foreach ( $query_result as &$query_result_template ) { + if ( + $query_result_template->slug === $template->slug + && $query_result_template->theme === $template->theme + ) { + if ( self::template_is_eligible_for_product_archive_fallback_from_theme( $template->slug ) ) { + $query_result_template->has_theme_file = true; + } + + return true; + } + } + + return false; + } + + /** + * Filter block templates by feature flag. + * + * @param WP_Block_Template[] $block_templates An array of block template objects. + * + * @return WP_Block_Template[] An array of block template objects. + */ + public static function filter_block_templates_by_feature_flag( $block_templates ) { + $feature_gating = new FeatureGating(); + $flag = $feature_gating->get_flag(); + + /** + * An array of block templates with slug as key and flag as value. + * + * @var array + */ + $block_templates_with_feature_gate = array(); + + return array_filter( + $block_templates, + function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) { + if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) { + return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag; + } + return true; + } + ); + } + + /** + * Removes templates that were added to a theme's block-templates directory, but already had a customised version saved in the database. + * + * @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on. + * + * @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default. + */ + public static function remove_theme_templates_with_custom_alternative( $templates ) { + + // Get the slugs of all templates that have been customised and saved in the database. + $customised_template_slugs = array_map( + function( $template ) { + return $template->slug; + }, + array_values( + array_filter( + $templates, + function( $template ) { + // This template has been customised and saved as a post. + return 'custom' === $template->source; + } + ) + ) + ); + + // Remove theme (i.e. filesystem) templates that have the same slug as a customised one. We don't need to check + // for `woocommerce` in $template->source here because woocommerce templates won't have been added to $templates + // if a saved version was found in the db. This only affects saved templates that were saved BEFORE a theme + // template with the same slug was added. + return array_values( + array_filter( + $templates, + function( $template ) use ( $customised_template_slugs ) { + // This template has been customised and saved as a post, so return it. + return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) ); + } + ) + ); + } + + /** + * Returns whether the blockified templates should be used or not. + * First, we need to make sure WordPress version is higher than 6.1 (lowest that supports Products block). + * Then, if the option is not stored on the db, we need to check if the current theme is a block one or not. + * + * @return boolean + */ + public static function should_use_blockified_product_grid_templates() { + $minimum_wp_version = '6.1'; + + if ( version_compare( $GLOBALS['wp_version'], $minimum_wp_version, '<' ) ) { + return false; + } + + $use_blockified_templates = get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE ); + + if ( false === $use_blockified_templates ) { + return wc_current_theme_is_fse_theme(); + } + + return wc_string_to_bool( $use_blockified_templates ); + } + + /** + * Returns whether the passed `$template` has a title, and it's different from the slug. + * + * @param object $template The template object. + * @return boolean + */ + public static function template_has_title( $template ) { + return ! empty( $template->title ) && $template->title !== $template->slug; + } + + /** + * Returns whether the passed `$template` has the legacy template block. + * + * @param object $template The template object. + * @return boolean + */ + public static function template_has_legacy_template_block( $template ) { + return has_block( 'woocommerce/legacy-template', $template->content ); + } + + /** + * Gets the templates saved in the database. + * + * @param array $slugs An array of slugs to retrieve templates for. + * @param string $template_type wp_template or wp_template_part. + * + * @return int[]|\WP_Post[] An array of found templates. + */ + public static function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { + $check_query_args = array( + 'post_type' => $template_type, + 'posts_per_page' => -1, + 'no_found_rows' => true, + 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => array( self::DEPRECATED_PLUGIN_SLUG, self::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; + + return array_map( + function( $saved_woo_template ) { + return self::build_template_result_from_post( $saved_woo_template ); + }, + $saved_woo_templates + ); + } + + /** + * Gets the template part by slug + * + * @param string $slug The template part slug. + * + * @return string The template part content. + */ + public static function get_template_part( $slug ) { + $templates_from_db = self::get_block_templates_from_db( array( $slug ), 'wp_template_part' ); + if ( count( $templates_from_db ) > 0 ) { + $template_slug_to_load = $templates_from_db[0]->theme; + } else { + $theme_has_template = self::theme_has_template_part( $slug ); + $template_slug_to_load = $theme_has_template ? get_stylesheet() : self::PLUGIN_SLUG; + } + $template_part = self::get_block_template( $template_slug_to_load . '//' . $slug, 'wp_template_part' ); + + if ( $template_part && ! empty( $template_part->content ) ) { + return $template_part->content; + } + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + return file_get_contents( self::get_templates_directory( 'wp_template_part' ) . DIRECTORY_SEPARATOR . $slug . '.html' ); + } +} diff --git a/templates/templates/order-confirmation.html b/templates/templates/order-confirmation.html deleted file mode 100644 index 7f23177326d..00000000000 --- a/templates/templates/order-confirmation.html +++ /dev/null @@ -1,7 +0,0 @@ - - -
- -
- - diff --git a/templates/templates/single-product.html b/templates/templates/single-product.html index aa382cef594..5634a0c6bd7 100644 --- a/templates/templates/single-product.html +++ b/templates/templates/single-product.html @@ -2,4 +2,4 @@
- + \ No newline at end of file