get( 'Version' ) ); // Enqueue theme stylesheet. wp_enqueue_style( 'epi-style' ); } endif; add_action( 'wp_enqueue_scripts', 'epi_styles' ); if ( ! function_exists( 'epi_is_woocommerce_active' ) ) : /** * Check whether WooCommerce is active. * * @since Epi 1.0 * * @return bool */ function epi_is_woocommerce_active(): bool { return class_exists( 'WooCommerce' ); } endif; if ( ! function_exists( 'epi_get_woocommerce_template_slugs' ) ) : /** * Get WooCommerce block template slugs bundled with the theme. * * @since Epi 1.0 * * @return string[] */ function epi_get_woocommerce_template_slugs(): array { return array( 'archive-product', 'page-cart', 'order-confirmation', 'product-search-results', 'single-product', 'taxonomy-product_cat', 'taxonomy-product_tag', ); } endif; if ( ! function_exists( 'epi_get_woocommerce_pattern_slugs' ) ) : /** * Get WooCommerce block pattern slugs bundled with the theme. * * @since Epi 1.0 * * @return string[] */ function epi_get_woocommerce_pattern_slugs(): array { return array( 'epi/cart', ); } endif; if ( ! function_exists( 'epi_filter_woocommerce_block_templates' ) ) : /** * Hide WooCommerce block templates when WooCommerce is inactive. * * @since Epi 1.0 * * @param \WP_Block_Template[] $templates The list of templates. * @param array $query Arguments to retrieve templates. * @param string $template_type Template type. * * @return \WP_Block_Template[] */ function epi_filter_woocommerce_block_templates( array $templates, array $query, string $template_type ): array { if ( epi_is_woocommerce_active() || 'wp_template' !== $template_type ) { return $templates; } $slugs = epi_get_woocommerce_template_slugs(); foreach ( $templates as $key => $template ) { if ( in_array( $template->slug, $slugs, true ) ) { unset( $templates[ $key ] ); } } return array_values( $templates ); } endif; add_filter( 'get_block_templates', 'epi_filter_woocommerce_block_templates', 99, 3 ); if ( ! function_exists( 'epi_filter_woocommerce_theme_json' ) ) : /** * Hide WooCommerce templates from theme.json when WooCommerce is inactive. * * @since Epi 1.0 * * @param \WP_Theme_JSON_Data $theme_json Theme JSON data. * * @return \WP_Theme_JSON_Data */ function epi_filter_woocommerce_theme_json( $theme_json ) { if ( epi_is_woocommerce_active() ) { return $theme_json; } $data = $theme_json->get_data(); $data['customTemplates'] = array_values( array_filter( $data['customTemplates'] ?? array(), function ( $template ) { return ! in_array( $template['name'], epi_get_woocommerce_template_slugs(), true ); } ) ); /* * Return a fresh data object rather than update_with(): WP_Theme_JSON::merge() * unions sequential arrays such as customTemplates by index, so update_with() * can add entries but never remove them. The resolver consumes the returned * object's data directly, so rebuilding from the reduced set drops the * WooCommerce templates as intended. */ return new WP_Theme_JSON_Data( $data, 'theme' ); } endif; add_filter( 'wp_theme_json_data_theme', 'epi_filter_woocommerce_theme_json' ); if ( ! function_exists( 'epi_unregister_woocommerce_patterns' ) ) : /** * Unregister WooCommerce block patterns when WooCommerce is inactive. * * @since Epi 1.0 * * @return void */ function epi_unregister_woocommerce_patterns() { if ( epi_is_woocommerce_active() ) { return; } foreach ( epi_get_woocommerce_pattern_slugs() as $pattern_slug ) { $pattern = \WP_Block_Patterns_Registry::get_instance()->get_registered( $pattern_slug ); if ( $pattern ) { unregister_block_pattern( $pattern_slug ); } } } endif; add_action( 'init', 'epi_unregister_woocommerce_patterns', 20 ); add_action( 'wp_loaded', 'epi_unregister_woocommerce_patterns' );