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
ElementorShortcodeService.php
83 lines
| 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 |