template_id; } /** * Picks the template type for the current request, most specific first. * * @return string[] */ private function candidate_types() { $woo = Plugin::is_woocommerce_active(); if ( is_404() ) { return array( 'error-404' ); } if ( is_search() ) { return array( 'search', 'archive' ); } if ( $woo && is_singular( 'product' ) ) { return array( 'single-product', 'single' ); } if ( $woo && ( is_shop() || is_product_taxonomy() ) ) { return array( 'product-archive', 'archive' ); } if ( is_singular() ) { // WooCommerce cart, checkout and account pages keep their own templates unless targeted explicitly. return array( 'single' ); } if ( is_archive() || is_home() ) { return array( 'archive' ); } return array(); } /** * Resolves the content template. */ private function resolve() { if ( is_singular( Post_Type::POST_TYPE ) || is_embed() || is_feed() || ! Settings::is_on( 'theme_builder' ) ) { return; } $conditions = Plugin::instance()->get( 'conditions' ); foreach ( $this->candidate_types() as $type ) { if ( ! Template_Types::exists( $type ) ) { continue; } $location = in_array( $type, array( 'single', 'single-product' ), true ) ? 'single' : 'archive'; if ( Compatibility::elementor_pro_has_location( $location ) ) { return; } $template_id = $conditions->get_template_for( $type ); if ( $template_id ) { $this->template_id = $template_id; return; } } } /** * Filters the template file. * * @param string $template Template path. * @return string */ public function template_include( $template ) { // Another plugin already printed the page, for example the WooCommerce "Coming soon" screen. if ( '' === $template ) { self::$page_taken_over = true; return $template; } // Template previews in the Elementor editor. if ( is_singular( Post_Type::POST_TYPE ) ) { return BUILDIFY_PATH . 'templates/preview.php'; } $this->resolve(); if ( ! $this->template_id ) { return $template; } $settings = Locations::document_settings( $this->template_id ); $layout = isset( $settings['buildify_layout'] ) ? $settings['buildify_layout'] : 'full_width'; if ( 'single-product' === Template_Types::of( $this->template_id ) ) { $this->prepare_product_page(); } /** * Filters the PHP file used to output a Buildify content template. * * @param string $file File path. * @param int $template_id Template ID. * @param string $layout full_width or canvas. */ return (string) apply_filters( 'buildify_template_file', BUILDIFY_PATH . ( 'canvas' === $layout ? 'templates/canvas.php' : 'templates/full-width.php' ), $this->template_id, $layout ); } /** * Enables WooCommerce gallery features when a product template is used. */ private function prepare_product_page() { foreach ( array( 'wc-product-gallery-zoom', 'wc-product-gallery-lightbox', 'wc-product-gallery-slider' ) as $feature ) { if ( ! current_theme_supports( $feature ) ) { add_theme_support( $feature ); } } } /** * Enqueues the template CSS early. */ public function enqueue() { if ( $this->template_id ) { Renderer::enqueue( $this->template_id ); wp_enqueue_style( 'buildify-frontend' ); wp_enqueue_script( 'buildify-frontend' ); if ( 'single-product' === Template_Types::of( $this->template_id ) ) { foreach ( array( 'wc-single-product', 'zoom', 'flexslider', 'photoswipe-ui-default', 'wc-add-to-cart-variation' ) as $handle ) { if ( wp_script_is( $handle, 'registered' ) ) { wp_enqueue_script( $handle ); } } if ( wp_style_is( 'photoswipe-default-skin', 'registered' ) ) { wp_enqueue_style( 'photoswipe-default-skin' ); } } } } /** * Adds body classes. * * @param string[] $classes Classes. * @return string[] */ public function body_class( $classes ) { if ( $this->template_id ) { $classes[] = 'buildify-template'; $classes[] = 'buildify-template-' . sanitize_html_class( Template_Types::of( $this->template_id ) ); } return $classes; } /** * Prints the content template. Called from the template files. */ public function print_content() { if ( ! $this->template_id ) { return; } $type = Template_Types::of( $this->template_id ); if ( 'single-product' === $type && function_exists( 'wc_get_product' ) ) { while ( have_posts() ) { the_post(); global $product; $product = wc_get_product( get_the_ID() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce expects the global product on product pages. // Prints notices and runs hooks other plugins rely on. do_action( 'woocommerce_before_single_product' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce hook. if ( post_password_required() ) { echo get_the_password_form(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core output. return; } printf( '