| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\ImportExport\Modifiers; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Utils\Utils; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Interactions_Ids_Modifier { |
| 12 |
|
| 13 |
public static function make() { |
| 14 |
return new self(); |
| 15 |
} |
| 16 |
|
| 17 |
public function run( array $element ) { |
| 18 |
if ( empty( $element['id'] ) || ! isset( $element['interactions'] ) ) { |
| 19 |
return $element; |
| 20 |
} |
| 21 |
|
| 22 |
$interactions = $element['interactions']; |
| 23 |
if ( is_string( $interactions ) ) { |
| 24 |
$decoded = json_decode( $interactions, true ); |
| 25 |
if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $decoded ) ) { |
| 26 |
return $element; |
| 27 |
} |
| 28 |
$interactions = $decoded; |
| 29 |
} |
| 30 |
|
| 31 |
if ( empty( $interactions['items'] ) || ! is_array( $interactions['items'] ) ) { |
| 32 |
return $element; |
| 33 |
} |
| 34 |
|
| 35 |
$existing_ids = []; |
| 36 |
$prefix = "e-{$element['id']}-"; |
| 37 |
|
| 38 |
foreach ( $interactions['items'] as $index => $item ) { |
| 39 |
if ( ! is_array( $item ) || ( $item['$$type'] ?? '' ) !== 'interaction-item' || ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
$new_id = Utils::generate_id( $prefix, $existing_ids ); |
| 44 |
$existing_ids[] = $new_id; |
| 45 |
|
| 46 |
$interactions['items'][ $index ]['value']['interaction_id'] = [ |
| 47 |
'$$type' => 'string', |
| 48 |
'value' => $new_id, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
$element['interactions'] = $interactions; |
| 53 |
|
| 54 |
return $element; |
| 55 |
} |
| 56 |
} |
| 57 |
|