| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
abstract class Template_Library_Snapshot_Processor { |
| 10 |
|
| 11 |
abstract protected function get_item_prefix(): string; |
| 12 |
abstract protected function get_max_items(): int; |
| 13 |
abstract protected function can_access_repository(): bool; |
| 14 |
abstract protected function load_current_data(): array; |
| 15 |
abstract protected function parse_incoming_snapshot( array $snapshot ): ?array; |
| 16 |
abstract protected function get_incoming_items( array $parsed_snapshot ): array; |
| 17 |
abstract protected function count_current_items( array $items ): int; |
| 18 |
abstract protected function save_data( array $items, array $metadata ): array; |
| 19 |
|
| 20 |
protected function normalize_for_comparison( array $item ): array { |
| 21 |
return $item; |
| 22 |
} |
| 23 |
|
| 24 |
protected function is_matching_item( array $existing_item, array $incoming_item ): bool { |
| 25 |
return Template_Library_Import_Export_Utils::items_equal_ignoring_keys( |
| 26 |
$this->normalize_for_comparison( $existing_item ), |
| 27 |
$incoming_item, |
| 28 |
[] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
protected function get_empty_result(): array { |
| 33 |
return [ |
| 34 |
'id_map' => [], |
| 35 |
'ids_to_flatten' => [], |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function merge_and_get_id_map( array $snapshot ): array { |
| 40 |
if ( ! $this->can_access_repository() ) { |
| 41 |
return $this->get_empty_result(); |
| 42 |
} |
| 43 |
|
| 44 |
$parsed = $this->parse_incoming_snapshot( $snapshot ); |
| 45 |
if ( null === $parsed ) { |
| 46 |
return $this->get_empty_result(); |
| 47 |
} |
| 48 |
|
| 49 |
$current = $this->load_current_data(); |
| 50 |
$current_items = $current['items'] ?? []; |
| 51 |
$existing_labels = Template_Library_Import_Export_Utils::extract_labels( $current_items ); |
| 52 |
$label_to_id = Template_Library_Import_Export_Utils::build_label_to_id_index( $current_items ); |
| 53 |
|
| 54 |
$id_map = []; |
| 55 |
$ids_to_flatten = []; |
| 56 |
$updated_items = $current_items; |
| 57 |
$existing_ids = array_fill_keys( array_keys( $updated_items ), true ); |
| 58 |
$updated_order = $current['order'] ?? []; |
| 59 |
|
| 60 |
$incoming_items = $this->get_incoming_items( $parsed ); |
| 61 |
foreach ( $incoming_items as $incoming_id => $incoming_item ) { |
| 62 |
if ( empty( $incoming_item ) ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$incoming_label = $incoming_item['label'] ?? null; |
| 67 |
$matching_id = is_string( $incoming_label ) ? ( $label_to_id[ $incoming_label ] ?? null ) : null; |
| 68 |
|
| 69 |
if ( null !== $matching_id && isset( $updated_items[ $matching_id ] ) ) { |
| 70 |
$is_same = $this->is_matching_item( $updated_items[ $matching_id ], $incoming_item ); |
| 71 |
|
| 72 |
if ( $is_same ) { |
| 73 |
if ( $matching_id !== $incoming_id ) { |
| 74 |
$id_map[ $incoming_id ] = $matching_id; |
| 75 |
} |
| 76 |
continue; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ( $this->count_current_items( $updated_items ) >= $this->get_max_items() ) { |
| 81 |
$ids_to_flatten[] = $incoming_id; |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
if ( null !== $matching_id && isset( $updated_items[ $matching_id ] ) ) { |
| 86 |
$target_id = Template_Library_Import_Export_Utils::generate_unique_id( |
| 87 |
array_keys( $updated_items ), |
| 88 |
$this->get_item_prefix() |
| 89 |
); |
| 90 |
} elseif ( isset( $existing_ids[ $incoming_id ] ) ) { |
| 91 |
$target_id = Template_Library_Import_Export_Utils::generate_unique_id( |
| 92 |
array_keys( $updated_items ), |
| 93 |
$this->get_item_prefix() |
| 94 |
); |
| 95 |
} else { |
| 96 |
$target_id = $incoming_id; |
| 97 |
} |
| 98 |
|
| 99 |
if ( $target_id !== $incoming_id ) { |
| 100 |
$id_map[ $incoming_id ] = $target_id; |
| 101 |
} |
| 102 |
|
| 103 |
$this->add_item_with_label( |
| 104 |
$incoming_item, |
| 105 |
$target_id, |
| 106 |
$updated_items, |
| 107 |
$existing_ids, |
| 108 |
$existing_labels, |
| 109 |
$updated_order |
| 110 |
); |
| 111 |
|
| 112 |
$new_label = $updated_items[ $target_id ]['label'] ?? null; |
| 113 |
if ( is_string( $new_label ) && ! isset( $label_to_id[ $new_label ] ) ) { |
| 114 |
$label_to_id[ $new_label ] = $target_id; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$saved_result = $this->save_data( $updated_items, array_merge( $current, [ 'order' => $updated_order ] ) ); |
| 119 |
|
| 120 |
return array_merge($this->get_empty_result(), [ |
| 121 |
'id_map' => $id_map, |
| 122 |
'ids_to_flatten' => $ids_to_flatten, |
| 123 |
], $saved_result); |
| 124 |
} |
| 125 |
|
| 126 |
public function create_all_as_new( array $snapshot ): array { |
| 127 |
return $this->process_snapshot($snapshot, function ( $incoming_id, $incoming_item, $updated_items ) { |
| 128 |
return Template_Library_Import_Export_Utils::generate_unique_id( array_keys( $updated_items ), $this->get_item_prefix() ); |
| 129 |
}); |
| 130 |
} |
| 131 |
|
| 132 |
protected function process_snapshot( array $snapshot, callable $id_strategy ): array { |
| 133 |
if ( ! $this->can_access_repository() ) { |
| 134 |
return $this->get_empty_result(); |
| 135 |
} |
| 136 |
|
| 137 |
$parsed = $this->parse_incoming_snapshot( $snapshot ); |
| 138 |
if ( null === $parsed ) { |
| 139 |
return $this->get_empty_result(); |
| 140 |
} |
| 141 |
|
| 142 |
$current = $this->load_current_data(); |
| 143 |
$current_items = $current['items'] ?? []; |
| 144 |
$existing_labels = Template_Library_Import_Export_Utils::extract_labels( $current_items ); |
| 145 |
|
| 146 |
$id_map = []; |
| 147 |
$ids_to_flatten = []; |
| 148 |
$updated_items = $current_items; |
| 149 |
$existing_ids = array_fill_keys( array_keys( $updated_items ), true ); |
| 150 |
$updated_order = $current['order'] ?? []; |
| 151 |
|
| 152 |
$incoming_items = $this->get_incoming_items( $parsed ); |
| 153 |
|
| 154 |
foreach ( $incoming_items as $incoming_id => $incoming_item ) { |
| 155 |
if ( empty( $incoming_item ) ) { |
| 156 |
continue; |
| 157 |
} |
| 158 |
|
| 159 |
if ( $this->count_current_items( $updated_items ) >= $this->get_max_items() ) { |
| 160 |
$ids_to_flatten[] = $incoming_id; |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
$target_id = $id_strategy( $incoming_id, $incoming_item, $updated_items, $existing_ids ); |
| 165 |
|
| 166 |
if ( null === $target_id ) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
if ( $target_id !== $incoming_id ) { |
| 171 |
$id_map[ $incoming_id ] = $target_id; |
| 172 |
} |
| 173 |
|
| 174 |
$this->add_item_with_label( |
| 175 |
$incoming_item, |
| 176 |
$target_id, |
| 177 |
$updated_items, |
| 178 |
$existing_ids, |
| 179 |
$existing_labels, |
| 180 |
$updated_order |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
$saved_result = $this->save_data( $updated_items, array_merge( $current, [ 'order' => $updated_order ] ) ); |
| 185 |
|
| 186 |
return array_merge($this->get_empty_result(), [ |
| 187 |
'id_map' => $id_map, |
| 188 |
'ids_to_flatten' => $ids_to_flatten, |
| 189 |
], $saved_result); |
| 190 |
} |
| 191 |
|
| 192 |
protected function add_item_with_label( array $item, string $target_id, array &$updated_items, array &$existing_ids, array &$existing_labels, array &$updated_order ): void { |
| 193 |
$item = $this->prepare_item_for_save( $item, $target_id ); |
| 194 |
$item = Template_Library_Import_Export_Utils::apply_unique_label( $item, $existing_labels ); |
| 195 |
$updated_items[ $target_id ] = $item; |
| 196 |
$existing_ids[ $target_id ] = true; |
| 197 |
|
| 198 |
if ( ! in_array( $target_id, $updated_order, true ) ) { |
| 199 |
$updated_order[] = $target_id; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
protected function prepare_item_for_save( array $item, string $target_id ): array { |
| 204 |
return $item; |
| 205 |
} |
| 206 |
} |
| 207 |
|