Conditions
1 year ago
Documents
1 year ago
DynamicTags
4 months ago
Widgets
2 months ago
assets
4 months ago
ElementorBlockAdapterService.php
4 months ago
ElementorCoreBlockStylesService.php
1 year ago
ElementorDocumentsService.php
1 year ago
ElementorDynamicTagsService.php
1 year ago
ElementorEditorService.php
1 year ago
ElementorFseScriptLoaderService.php
1 year ago
ElementorServiceProvider.php
8 months ago
ElementorShortcodeService.php
1 year ago
ElementorTemplatesService.php
4 months ago
ElementorWidgetsService.php
8 months ago
ElementorFseScriptLoaderService.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\Elementor; |
| 4 | |
| 5 | /** |
| 6 | * Class to handle FSE script loading for Elementor integration. |
| 7 | */ |
| 8 | class ElementorFseScriptLoaderService { |
| 9 | /** |
| 10 | * Bootstrap the service. |
| 11 | * |
| 12 | * @return void |
| 13 | */ |
| 14 | public function bootstrap() { |
| 15 | if ( ! \SureCart::utility()->blockTemplates()->isFSETheme() ) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | add_filter( 'template_include', [ $this, 'maybePreloadProductScripts' ], 999 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Check if the current template might be a product page |
| 24 | * |
| 25 | * @param string $template The template being loaded. |
| 26 | * @return string The template path unchanged. |
| 27 | */ |
| 28 | public function maybePreloadProductScripts( $template ) { |
| 29 | if ( empty( $template ) || ! $this->isProductPage() ) { |
| 30 | return $template; |
| 31 | } |
| 32 | |
| 33 | add_action( 'wp_enqueue_scripts', [ $this, 'loadElementorAssetsForProductTemplate' ], 9 ); |
| 34 | return $template; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Determine if the current page is a surecart product page. |
| 39 | * |
| 40 | * @return bool True if this page contains product elements. |
| 41 | */ |
| 42 | private function isProductPage(): bool { |
| 43 | global $post; |
| 44 | if ( ! $post ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | // Check if the current post is a product post type or if the query variable indicates a product. |
| 49 | return get_query_var( 'surecart_current_product' ) || 'sc_product' === $post->post_type; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Force load Elementor scripts for SureCart product templates. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function loadElementorAssetsForProductTemplate(): void { |
| 58 | $template_id = $this->get_surecart_elementor_template_id(); |
| 59 | if ( ! $template_id ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $document = \Elementor\Plugin::$instance->documents->get( $template_id ); |
| 64 | if ( ! $document || ! $document->is_built_with_elementor() ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $elementor_frontend = \Elementor\Plugin::instance()->frontend; |
| 69 | $elementor_frontend->enqueue_scripts(); |
| 70 | $elementor_frontend->get_builder_content_for_display( $template_id ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the SureCart Elementor template ID. |
| 75 | * |
| 76 | * @return int|null The ID of the SureCart Elementor template or null if not found. |
| 77 | */ |
| 78 | private function get_surecart_elementor_template_id() { |
| 79 | $query = new \WP_Query( |
| 80 | [ |
| 81 | 'post_type' => 'elementor_library', |
| 82 | 'meta_query' => [ |
| 83 | [ |
| 84 | 'key' => '_elementor_template_type', |
| 85 | 'value' => 'surecart-product', |
| 86 | ], |
| 87 | ], |
| 88 | 'posts_per_page' => 1, |
| 89 | ] |
| 90 | ); |
| 91 | |
| 92 | return $query->have_posts() ? $query->posts[0]->ID : null; |
| 93 | } |
| 94 | } |
| 95 |