| @@ -22,9 +22,12 @@ | ||
| 22 | 22 | |
| 23 | 23 | add_action( 'get_header', [ $this, 'get_header' ], 0 ); |
| 24 | 24 | add_action( 'get_footer', [ $this, 'get_footer' ], 0 ); |
| 25 | 25 | add_action( 'elementor/document/wrapper_attributes', [ $this, 'wrapper_attributes' ], 10, 2 ); |
| 26 | + add_action( 'template_redirect', array( $this, 'set_global_product' ) ); | |
| 26 | 27 | |
| 28 | + add_action( 'templately_builder_header_after', [ self::class, 'print_style_tags' ], 0 ); | |
| 29 | + add_action( 'templately_builder_footer_before', [ self::class, 'print_style_tags' ], 0 ); | |
| 27 | 30 | /** |
| 28 | 31 | * Only for Development Mode. |
| 29 | 32 | */ |
| 30 | 33 | if ( defined( 'TEMPLATELY_DEV_VIEWS' ) && TEMPLATELY_DEV_VIEWS ) { |
| @@ -41,9 +44,9 @@ | ||
| 41 | 44 | echo '<small>Footer</small>'; |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | public function get_header() { |
| 45 | - if(!$this->is_header_footer()){ | |
| 48 | + if(!self::is_header_footer('header')){ | |
| 46 | 49 | return; |
| 47 | 50 | } |
| 48 | 51 | $this->views->get_header(); |
| 49 | 52 | |
| @@ -57,9 +60,9 @@ | ||
| 57 | 60 | ob_get_clean(); |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | 63 | public function get_footer( $name ) { |
| 61 | - if(!$this->is_header_footer()){ | |
| 64 | + if(!self::is_header_footer('footer')){ | |
| 62 | 65 | return; |
| 63 | 66 | } |
| 64 | 67 | $this->views->get_footer(); |
| 65 | 68 | |
| @@ -77,9 +80,9 @@ | ||
| 77 | 80 | locate_template( $templates, true ); |
| 78 | 81 | ob_get_clean(); |
| 79 | 82 | } |
| 80 | 83 | |
| 81 | - public function is_header_footer(){ | |
| 84 | + public static function is_header_footer($type = null){ | |
| 82 | 85 | if(class_exists( 'Elementor\Plugin' )){ |
| 83 | 86 | $pid = get_the_ID(); |
| 84 | 87 | $post_type = get_post_type($pid); |
| 85 | 88 | $document = Plugin::$instance->documents->get( $pid ); |
| @@ -96,8 +99,15 @@ | ||
| 96 | 99 | |
| 97 | 100 | $header = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'header' ); |
| 98 | 101 | $footer = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'footer' ); |
| 99 | 102 | |
| 103 | + if($type === 'header'){ | |
| 104 | + return $header; | |
| 105 | + } | |
| 106 | + else if($type === 'footer'){ | |
| 107 | + return $footer; | |
| 108 | + } | |
| 109 | + | |
| 100 | 110 | if(!empty($header) || !empty($footer)){ |
| 101 | 111 | return true; |
| 102 | 112 | } |
| 103 | 113 | return false; |
| @@ -106,15 +116,123 @@ | ||
| 106 | 116 | public function wrapper_attributes( $attributes, $document ) { |
| 107 | 117 | $post_type = get_post_type($document->get_main_id()); |
| 108 | 118 | |
| 109 | 119 | if( $post_type === 'templately_library' ){ |
| 110 | - $template = templately()->theme_builder->get_template( $document->get_main_id() ); | |
| 111 | - $attributes['data-elementor-type'] = 'templately-' . $template->get_type(); | |
| 112 | - $attributes['data-elementor-id'] = $template->get_main_id(); | |
| 113 | - $attributes['data-elementor-post-type'] = 'templately_library'; | |
| 114 | - $attributes['data-elementor-title'] = $template->get_title(); | |
| 120 | + $template = templately()->theme_builder->get_template( $document->get_main_id() ); | |
| 121 | + if($template){ | |
| 122 | + $attributes['data-elementor-type'] = 'templately-' . $template->get_type(); | |
| 123 | + $attributes['data-elementor-id'] = $template->get_main_id(); | |
| 124 | + $attributes['data-elementor-post-type'] = 'templately_library'; | |
| 125 | + $attributes['data-elementor-title'] = $template->get_title(); | |
| 126 | + $attributes['class'] .= ' ' . implode( ' ', get_post_class() ); | |
| 127 | + } | |
| 115 | 128 | } |
| 116 | 129 | |
| 117 | 130 | return $attributes; |
| 131 | + } | |
| 132 | + | |
| 133 | + public function set_global_product(){ | |
| 134 | + global $product; | |
| 135 | + | |
| 136 | + if ( ! function_exists( 'wc_setup_product_data' ) ) { | |
| 137 | + return; | |
| 138 | + } | |
| 139 | + | |
| 140 | + // A real product is already in context (e.g. a live single-product page) — never override it. | |
| 141 | + if ( $product instanceof \WC_Product ) { | |
| 142 | + return; | |
| 143 | + } | |
| 144 | + | |
| 145 | + $document_id = get_the_ID(); | |
| 146 | + if ( ! $document_id ) { | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + | |
| 150 | + // The current post is itself a product — set it up (prior behaviour, kept explicit). | |
| 151 | + if ( get_post_type( $document_id ) === 'product' ) { | |
| 152 | + wc_setup_product_data( $document_id ); | |
| 153 | + return; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // Otherwise only fabricate a preview product for a WooCommerce single-product | |
| 157 | + // Theme Builder template being rendered in the editor / preview. This mirrors what | |
| 158 | + // Elementor Pro's Single Product document does: point the global $product at a real | |
| 159 | + // product so any WooCommerce widget — ours and third-party (e.g. EA Woo Product Tabs) — | |
| 160 | + // renders real data in the editor instead of appearing empty. The live front end is | |
| 161 | + // unaffected (there a real product is always in context, handled above). | |
| 162 | + if ( ! $this->is_woo_single_preview( $document_id ) ) { | |
| 163 | + return; | |
| 164 | + } | |
| 165 | + | |
| 166 | + $preview_product_id = $this->get_preview_product_id(); | |
| 167 | + if ( $preview_product_id ) { | |
| 168 | + wc_setup_product_data( $preview_product_id ); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Whether the current request is the editor/preview render of a WooCommerce | |
| 174 | + * single-product ( `product_single` ) Theme Builder template. | |
| 175 | + * | |
| 176 | + * @param int $document_id The templately_library post being rendered. | |
| 177 | + * @return bool | |
| 178 | + */ | |
| 179 | + private function is_woo_single_preview( $document_id ) { | |
| 180 | + if ( get_post_meta( $document_id, Source::TYPE_META_KEY, true ) !== 'product_single' ) { | |
| 181 | + return false; | |
| 182 | + } | |
| 183 | + | |
| 184 | + if ( ! post_type_exists( 'product' ) ) { | |
| 185 | + return false; | |
| 186 | + } | |
| 187 | + | |
| 188 | + return Plugin::$instance->editor->is_edit_mode() | |
| 189 | + || Plugin::$instance->preview->is_preview_mode( $document_id ) | |
| 190 | + || ( isset( $_GET['templately_library'], $_GET['preview_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Resolve the newest published product to use as the editor preview product. | |
| 195 | + * Matches the "latest product" approach used by the dynamic Theme Builder | |
| 196 | + * widgets ( Post_Content / Post_Title / Featured_Image ) and Elementor Pro. | |
| 197 | + * | |
| 198 | + * @return int Product ID, or 0 when the store has no published products. | |
| 199 | + */ | |
| 200 | + private function get_preview_product_id() { | |
| 201 | + $products = get_posts( [ | |
| 202 | + 'post_type' => 'product', | |
| 203 | + 'post_status' => 'publish', | |
| 204 | + 'numberposts' => 1, | |
| 205 | + 'orderby' => 'date', | |
| 206 | + 'order' => 'DESC', | |
| 207 | + 'fields' => 'ids', | |
| 208 | + 'no_found_rows' => true, | |
| 209 | + 'suppress_filters' => false, | |
| 210 | + ] ); | |
| 211 | + | |
| 212 | + return ! empty( $products ) ? (int) $products[0] : 0; | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Print remaining enqueued styles with error handling. | |
| 217 | + */ | |
| 218 | + public static function print_style_tags() { | |
| 219 | + try { | |
| 220 | + $wp_styles = wp_styles(); | |
| 221 | + // handles dependencies | |
| 222 | + $wp_styles->do_items(); | |
| 223 | + if ( is_object( $wp_styles ) && is_array( $wp_styles->queue ?? null ) ) { | |
| 224 | + foreach ( $wp_styles->queue as $style ) { | |
| 225 | + if ( is_string( $style ) && ! $wp_styles->query( $style, 'done' ) ) { | |
| 226 | + $wp_styles->do_item( $style ); | |
| 227 | + $wp_styles->done[] = $style; | |
| 228 | + } | |
| 229 | + } | |
| 230 | + } | |
| 231 | + } catch ( \Throwable $e ) { | |
| 232 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { | |
| 233 | + error_log( 'Templately: Error printing styles - ' . $e->getMessage() ); | |
| 234 | + } | |
| 235 | + } | |
| 118 | 236 | } |
| 119 | 237 | |
| 120 | 238 | } |