PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.1
Elementor Website Builder – more than just a page builder v3.34.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / interactions / parser.php

parser.php in Elementor Website Builder – more than just a page builder 3.34.1, at modules/interactions/parser.php

89 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 Elementor\Modules\Interactions;
4
5 use Elementor\Modules\AtomicWidgets\Utils\Utils;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Parser {
12 protected $post_id;
13 protected $ids_lookup = [];
14
15 public function __construct( $post_id ) {
16 $this->post_id = $post_id;
17 }
18
19 public function assign_interaction_ids( $data ) {
20 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
21 $data['elements'] = $this->process_interactions_for( $data['elements'] );
22 }
23
24 return $data;
25 }
26
27 private function process_interactions_for( $elements ) {
28 if ( ! is_array( $elements ) ) {
29 return $elements;
30 }
31
32 foreach ( $elements as &$element ) {
33 if ( isset( $element['interactions'] ) ) {
34 $element['interactions'] = $this->maybe_assign_interaction_ids( $element['interactions'], $element['id'] );
35 }
36
37 if ( isset( $element['elements'] ) && is_array( $element['elements'] ) ) {
38 $element['elements'] = $this->process_interactions_for( $element['elements'] );
39 }
40 }
41
42 return $elements;
43 }
44
45 private function maybe_assign_interaction_ids( $interactions_json, $element_id ) {
46 $interactions = $this->decode_interactions( $interactions_json );
47
48 if ( ! isset( $interactions['items'] ) ) {
49 return [];
50 }
51
52 foreach ( $interactions['items'] as &$interaction ) {
53 if ( array_key_exists( 'interaction_id', $interaction ) ) {
54 $this->ids_lookup[] = $interaction['interaction_id'];
55 } else {
56 $interaction = array_merge( [
57 'interaction_id' => $this->get_next_interaction_id( $element_id ),
58 ], $interaction );
59 }
60 }
61
62 return wp_json_encode( $interactions );
63 }
64
65 private function decode_interactions( $interactions ) {
66 if ( is_array( $interactions ) ) {
67 return $interactions;
68 }
69
70 if ( is_string( $interactions ) ) {
71 $decoded = json_decode( $interactions, true );
72 if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
73 return $decoded;
74 }
75 }
76
77 return [
78 'items' => [],
79 'version' => 1,
80 ];
81 }
82
83 protected function get_next_interaction_id( $prefix ) {
84 $next_id = Utils::generate_id( "{$this->post_id}-{$prefix}-", $this->ids_lookup );
85 $this->ids_lookup[] = $next_id;
86 return $next_id;
87 }
88 }
89