| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements\Promotions; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Pro_Promotion_Data_Preservation { |
| 13 |
|
| 14 |
const EMAIL_ACTION_COUNT = 2; |
| 15 |
|
| 16 |
public function register_hooks(): void { |
| 17 |
if ( Utils::has_pro() ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
add_filter( 'elementor/document/save/data', fn ( $data, $document ) => $this->preserve( $data, $document ), 10, 2 ); |
| 22 |
add_filter( 'elementor/atomic/form/email_action_count', fn ( $count ) => max( $count, self::EMAIL_ACTION_COUNT ) ); |
| 23 |
} |
| 24 |
|
| 25 |
public function preserve( $data, $document ) { |
| 26 |
if ( empty( $data['elements'] ) || ! is_array( $data['elements'] ) ) { |
| 27 |
return $data; |
| 28 |
} |
| 29 |
|
| 30 |
$promotion_types = $this->get_promotion_types(); |
| 31 |
|
| 32 |
if ( empty( $promotion_types ) ) { |
| 33 |
return $data; |
| 34 |
} |
| 35 |
|
| 36 |
$stored = []; |
| 37 |
$this->map_promotion_elements( $document->get_elements_data(), $promotion_types, $stored ); |
| 38 |
|
| 39 |
if ( empty( $stored ) ) { |
| 40 |
return $data; |
| 41 |
} |
| 42 |
|
| 43 |
$data['elements'] = $this->restore_promotion_elements( $data['elements'], $promotion_types, $stored ); |
| 44 |
|
| 45 |
return $data; |
| 46 |
} |
| 47 |
|
| 48 |
private function get_promotion_types(): array { |
| 49 |
$types = []; |
| 50 |
|
| 51 |
foreach ( Plugin::$instance->elements_manager->get_element_types() as $type => $element ) { |
| 52 |
if ( method_exists( $element, 'get_meta_item' ) && $element->get_meta_item( 'is_pro_promotion' ) ) { |
| 53 |
$types[] = $type; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $types; |
| 58 |
} |
| 59 |
|
| 60 |
private function map_promotion_elements( $elements, array $promotion_types, array &$map ): void { |
| 61 |
if ( ! is_array( $elements ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
foreach ( $elements as $element ) { |
| 66 |
if ( ! is_array( $element ) ) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
$id = $element['id'] ?? ''; |
| 71 |
$is_promotion = in_array( $element['elType'] ?? '', $promotion_types, true ); |
| 72 |
|
| 73 |
if ( $id && $is_promotion && ! empty( $element['elements'] ) ) { |
| 74 |
$map[ $id ] = [ |
| 75 |
'settings' => $element['settings'] ?? [], |
| 76 |
'elements' => $element['elements'], |
| 77 |
]; |
| 78 |
|
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
$this->map_promotion_elements( $element['elements'] ?? [], $promotion_types, $map ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private function restore_promotion_elements( array $elements, array $promotion_types, array $map ): array { |
| 87 |
foreach ( $elements as &$element ) { |
| 88 |
if ( ! is_array( $element ) ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$id = $element['id'] ?? ''; |
| 93 |
$is_promotion = in_array( $element['elType'] ?? '', $promotion_types, true ); |
| 94 |
|
| 95 |
if ( $is_promotion && empty( $element['elements'] ) && isset( $map[ $id ] ) ) { |
| 96 |
$element['settings'] = $map[ $id ]['settings']; |
| 97 |
$element['elements'] = $map[ $id ]['elements']; |
| 98 |
|
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { |
| 103 |
$element['elements'] = $this->restore_promotion_elements( $element['elements'], $promotion_types, $map ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
unset( $element ); |
| 108 |
|
| 109 |
return $elements; |
| 110 |
} |
| 111 |
} |
| 112 |
|