| 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 ( ! isset( $interaction['$$type'] ) || 'interaction-item' !== $interaction['$$type'] ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
$existing_id = null; |
| 58 |
if ( isset( $interaction['value']['interaction_id']['value'] ) ) { |
| 59 |
$existing_id = $interaction['value']['interaction_id']['value']; |
| 60 |
} |
| 61 |
|
| 62 |
if ( $existing_id && $this->is_temp_id( $existing_id ) ) { |
| 63 |
$interaction['value']['interaction_id'] = [ |
| 64 |
'$$type' => 'string', |
| 65 |
'value' => $this->get_next_interaction_id( $element_id ), |
| 66 |
]; |
| 67 |
} elseif ( $existing_id ) { |
| 68 |
$this->ids_lookup[] = $existing_id; |
| 69 |
} else { |
| 70 |
$interaction['value']['interaction_id'] = [ |
| 71 |
'$$type' => 'string', |
| 72 |
'value' => $this->get_next_interaction_id( $element_id ), |
| 73 |
]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return wp_json_encode( $interactions ); |
| 78 |
} |
| 79 |
|
| 80 |
private function is_temp_id( $id ) { |
| 81 |
return is_string( $id ) && strpos( $id, 'temp-' ) === 0; |
| 82 |
} |
| 83 |
|
| 84 |
private function decode_interactions( $interactions ) { |
| 85 |
if ( is_array( $interactions ) ) { |
| 86 |
return $interactions; |
| 87 |
} |
| 88 |
|
| 89 |
if ( is_string( $interactions ) ) { |
| 90 |
$decoded = json_decode( $interactions, true ); |
| 91 |
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { |
| 92 |
return $decoded; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return [ |
| 97 |
'items' => [], |
| 98 |
'version' => 1, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
protected function get_next_interaction_id( $prefix ) { |
| 103 |
$next_id = Utils::generate_id( "{$this->post_id}-{$prefix}-", $this->ids_lookup ); |
| 104 |
$this->ids_lookup[] = $next_id; |
| 105 |
return $next_id; |
| 106 |
} |
| 107 |
} |
| 108 |
|