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 ); } }