PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.3
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 / ElementorShortcodeService.php
ElementorShortcodeService.php
83 lines 2.2 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 * Elementor shortcode service.
7 * Handles the wrapping of SureCart shortcodes in the Elementor content.
8 */
9 class ElementorShortcodeService {
10 /**
11 * Should wrap.
12 *
13 * @var bool
14 */
15 protected $should_wrap = false;
16
17 /**
18 * Bootstrap the service.
19 *
20 * @return void
21 */
22 public function bootstrap() {
23 add_action( 'elementor/frontend/before_get_builder_content', array( $this, 'wrapShortcodesInProductWrapper' ) );
24 }
25
26 /**
27 * Wrap shortcodes in product wrapper.
28 *
29 * @param \Elementor\Document $document The Elementor document.
30 *
31 * @return \Elementor\Document
32 */
33 public function wrapShortcodesInProductWrapper( $document ) {
34 $sc_shortcodes = $this->findSurecartShortcodes( $document->get_elements_data() );
35
36 if ( ! empty( $sc_shortcodes ) ) {
37 add_filter(
38 'elementor/frontend/the_content',
39 function ( $content ) {
40 if ( empty( sc_get_product() ) ) {
41 return $content;
42 }
43 return '<!-- wp:surecart/product-page {"align":"wide"} -->' . $content . '<!-- /wp:surecart/product-page -->';
44 },
45 9 // this is important to run this filter at the end of the content.
46 );
47 }
48 return $document;
49 }
50
51 /**
52 * Find all SureCart shortcodes in the Elementor data.
53 *
54 * @param array $elements The elements data to search through.
55 * @param array $found_shortcodes Array to store found shortcodes.
56 *
57 * @return array Found shortcodes.
58 */
59 private function findSurecartShortcodes( $elements, &$found_shortcodes = [] ) {
60 if ( ! is_array( $elements ) ) {
61 return $found_shortcodes;
62 }
63
64 foreach ( $elements as $element ) {
65 // Check if this is a widget with a shortcode.
66 if ( isset( $element['widgetType'] ) &&
67 'shortcode' === $element['widgetType'] &&
68 isset( $element['settings']['shortcode'] ) &&
69 0 === strpos( $element['settings']['shortcode'], '[sc_' )
70 ) {
71 $found_shortcodes[] = $element['settings']['shortcode'];
72 }
73
74 // Recursively search through nested elements.
75 if ( isset( $element['elements'] ) && is_array( $element['elements'] ) ) {
76 $this->findSurecartShortcodes( $element['elements'], $found_shortcodes );
77 }
78 }
79
80 return $found_shortcodes;
81 }
82 }
83