| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Interactions\Cache; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Elements_Interactions { |
| 10 |
private $map; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
$this->map = []; |
| 14 |
} |
| 15 |
|
| 16 |
public function all() { |
| 17 |
return $this->map; |
| 18 |
} |
| 19 |
|
| 20 |
public function parse_from( array $payload ) { |
| 21 |
if ( ! isset( $payload['elements'] ) || ! is_array( $payload['elements'] ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$elements = $payload['elements']; |
| 26 |
|
| 27 |
if ( empty( $elements ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
foreach ( $elements as $element ) { |
| 32 |
$element_id = $this->extract_element_id( $element ); |
| 33 |
$interactions = $this->extract_interactions( $element ); |
| 34 |
|
| 35 |
if ( $element_id && $interactions ) { |
| 36 |
$this->map[ $element_id ] = $interactions; |
| 37 |
} |
| 38 |
|
| 39 |
$this->parse_from( $element ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private function extract_element_id( array $element ) { |
| 44 |
if ( ! isset( $element['id'] ) || empty( $element['id'] ) ) { |
| 45 |
return null; |
| 46 |
} |
| 47 |
return $element['id']; |
| 48 |
} |
| 49 |
|
| 50 |
private function extract_interactions( $element ) { |
| 51 |
if ( ! isset( $element['interactions'] ) ) { |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
$interactions_value = $this->decode_interactions( $element['interactions'] ); |
| 56 |
|
| 57 |
if ( ! is_array( $interactions_value ) ) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! isset( $interactions_value['items'] ) || ! is_array( $interactions_value['items'] ) ) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
return $interactions_value['items']; |
| 66 |
} |
| 67 |
|
| 68 |
private function decode_interactions( $source ) { |
| 69 |
if ( is_string( $source ) ) { |
| 70 |
$decoded = json_decode( $source, true ); |
| 71 |
|
| 72 |
if ( JSON_ERROR_NONE === json_last_error() && is_array( $decoded ) ) { |
| 73 |
return $decoded; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $source; |
| 78 |
} |
| 79 |
} |
| 80 |
|