PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.0
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Integrations / Elementor / ElementorFseScriptLoaderService.php
ElementorFseScriptLoaderService.php
95 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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