| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Template_Library_Import_Export_Utils { |
| 10 |
const LABEL_PREFIX = 'DUP_'; |
| 11 |
const MAX_LABEL_LENGTH = 50; |
| 12 |
const DEFAULT_RANDOM_STRING_LENGTH = 7; |
| 13 |
const IMPORT_MODE_MATCH_SITE = 'match_site'; |
| 14 |
const IMPORT_MODE_KEEP_CREATE = 'keep_create'; |
| 15 |
const IMPORT_MODE_KEEP_FLATTEN = 'keep_flatten'; |
| 16 |
const LOCAL_CLASS_LABEL = 'local'; |
| 17 |
const GLOBAL_CLASS_ID_PREFIX = 'g-'; |
| 18 |
const VARIABLE_ID_PREFIX = 'e-gv-'; |
| 19 |
const LOCAL_CLASS_ID_PREFIX = 'e-'; |
| 20 |
|
| 21 |
public static function items_equal( array $a, array $b ): bool { |
| 22 |
$a = self::recursive_ksort( $a ); |
| 23 |
$b = self::recursive_ksort( $b ); |
| 24 |
|
| 25 |
$encoded_a = wp_json_encode( $a ); |
| 26 |
$encoded_b = wp_json_encode( $b ); |
| 27 |
|
| 28 |
if ( false === $encoded_a || false === $encoded_b ) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
return $encoded_a === $encoded_b; |
| 33 |
} |
| 34 |
|
| 35 |
public static function items_equal_ignoring_keys( array $a, array $b, array $ignore_keys = [] ): bool { |
| 36 |
foreach ( $ignore_keys as $key ) { |
| 37 |
unset( $a[ $key ], $b[ $key ] ); |
| 38 |
} |
| 39 |
|
| 40 |
return self::items_equal( $a, $b ); |
| 41 |
} |
| 42 |
|
| 43 |
public static function build_label_to_id_index( array $items, string $label_key = 'label' ): array { |
| 44 |
$index = []; |
| 45 |
|
| 46 |
foreach ( $items as $id => $item ) { |
| 47 |
$label = $item[ $label_key ] ?? null; |
| 48 |
if ( is_string( $label ) && '' !== $label && ! isset( $index[ $label ] ) ) { |
| 49 |
$index[ $label ] = $id; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $index; |
| 54 |
} |
| 55 |
|
| 56 |
public static function recursive_ksort( $value ) { |
| 57 |
if ( is_array( $value ) ) { |
| 58 |
foreach ( $value as $k => $v ) { |
| 59 |
$value[ $k ] = self::recursive_ksort( $v ); |
| 60 |
} |
| 61 |
ksort( $value ); |
| 62 |
} |
| 63 |
|
| 64 |
return $value; |
| 65 |
} |
| 66 |
|
| 67 |
public static function normalize_string_ids( array $ids ): array { |
| 68 |
return array_values( |
| 69 |
array_unique( |
| 70 |
array_filter( $ids, fn( $id ) => is_string( $id ) && '' !== $id ) |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public static function extract_labels( array $items, string $label_key = 'label' ): array { |
| 76 |
$labels = []; |
| 77 |
|
| 78 |
foreach ( $items as $item ) { |
| 79 |
if ( isset( $item[ $label_key ] ) && is_string( $item[ $label_key ] ) ) { |
| 80 |
$labels[] = $item[ $label_key ]; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $labels; |
| 85 |
} |
| 86 |
|
| 87 |
public static function apply_unique_label( array $item, array &$existing_labels, string $label_key = 'label' ): array { |
| 88 |
if ( ! isset( $item[ $label_key ] ) || ! is_string( $item[ $label_key ] ) || '' === $item[ $label_key ] ) { |
| 89 |
return $item; |
| 90 |
} |
| 91 |
|
| 92 |
$label = $item[ $label_key ]; |
| 93 |
|
| 94 |
if ( in_array( $label, $existing_labels, true ) ) { |
| 95 |
$label = self::generate_unique_label( $label, $existing_labels ); |
| 96 |
$item[ $label_key ] = $label; |
| 97 |
} |
| 98 |
|
| 99 |
$existing_labels[] = $label; |
| 100 |
|
| 101 |
return $item; |
| 102 |
} |
| 103 |
|
| 104 |
public static function generate_unique_id( array $existing_ids, string $prefix = self::GLOBAL_CLASS_ID_PREFIX ): string { |
| 105 |
$existing = array_fill_keys( $existing_ids, true ); |
| 106 |
$max_attempts = 1000; |
| 107 |
$attempts = 0; |
| 108 |
|
| 109 |
do { |
| 110 |
if ( ++$attempts > $max_attempts ) { |
| 111 |
return $prefix . self::generate_random_string( 12 ); |
| 112 |
} |
| 113 |
|
| 114 |
$random = substr( strtolower( dechex( wp_rand( 0, PHP_INT_MAX ) ) ), 0, self::DEFAULT_RANDOM_STRING_LENGTH ); |
| 115 |
$id = $prefix . $random; |
| 116 |
} while ( isset( $existing[ $id ] ) ); |
| 117 |
|
| 118 |
return $id; |
| 119 |
} |
| 120 |
|
| 121 |
public static function generate_random_string( int $length = self::DEFAULT_RANDOM_STRING_LENGTH ): string { |
| 122 |
$length = max( 1, $length ); |
| 123 |
$chars = '0123456789abcdef'; |
| 124 |
$out = ''; |
| 125 |
|
| 126 |
for ( $i = 0; $i < $length; $i++ ) { |
| 127 |
$out .= $chars[ wp_rand( 0, 15 ) ]; |
| 128 |
} |
| 129 |
|
| 130 |
return $out; |
| 131 |
} |
| 132 |
|
| 133 |
public static function sanitize_import_mode( $import_mode ): string { |
| 134 |
$allowed = [ |
| 135 |
self::IMPORT_MODE_MATCH_SITE, |
| 136 |
self::IMPORT_MODE_KEEP_CREATE, |
| 137 |
self::IMPORT_MODE_KEEP_FLATTEN, |
| 138 |
]; |
| 139 |
|
| 140 |
if ( is_string( $import_mode ) && in_array( $import_mode, $allowed, true ) ) { |
| 141 |
return $import_mode; |
| 142 |
} |
| 143 |
|
| 144 |
return self::IMPORT_MODE_MATCH_SITE; |
| 145 |
} |
| 146 |
|
| 147 |
public static function generate_unique_label( string $original_label, array $existing_labels ): string { |
| 148 |
$prefix = self::LABEL_PREFIX; |
| 149 |
$max_length = self::MAX_LABEL_LENGTH; |
| 150 |
|
| 151 |
$has_prefix = 0 === strpos( $original_label, $prefix ); |
| 152 |
|
| 153 |
if ( $has_prefix ) { |
| 154 |
$base_label = substr( $original_label, strlen( $prefix ) ); |
| 155 |
$counter = 1; |
| 156 |
$new_label = $prefix . $base_label . $counter; |
| 157 |
|
| 158 |
while ( in_array( $new_label, $existing_labels, true ) ) { |
| 159 |
++$counter; |
| 160 |
$new_label = $prefix . $base_label . $counter; |
| 161 |
} |
| 162 |
|
| 163 |
if ( strlen( $new_label ) > $max_length ) { |
| 164 |
$available_length = $max_length - strlen( $prefix . $counter ); |
| 165 |
$base_label = substr( $base_label, 0, $available_length ); |
| 166 |
$new_label = $prefix . $base_label . $counter; |
| 167 |
} |
| 168 |
} else { |
| 169 |
$available_length = strlen( $original_label ); |
| 170 |
$new_label = $prefix . $original_label; |
| 171 |
|
| 172 |
if ( strlen( $new_label ) > $max_length ) { |
| 173 |
$available_length = $max_length - strlen( $prefix ); |
| 174 |
$new_label = $prefix . substr( $original_label, 0, $available_length ); |
| 175 |
} |
| 176 |
|
| 177 |
$counter = 1; |
| 178 |
$base_label = substr( $original_label, 0, $available_length ); |
| 179 |
|
| 180 |
while ( in_array( $new_label, $existing_labels, true ) ) { |
| 181 |
$new_label = $prefix . $base_label . $counter; |
| 182 |
|
| 183 |
if ( strlen( $new_label ) > $max_length ) { |
| 184 |
$available_length = $max_length - strlen( $prefix . $counter ); |
| 185 |
$base_label = substr( $original_label, 0, $available_length ); |
| 186 |
$new_label = $prefix . $base_label . $counter; |
| 187 |
} |
| 188 |
|
| 189 |
++$counter; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return $new_label; |
| 194 |
} |
| 195 |
|
| 196 |
public static function process_import_by_mode( |
| 197 |
string $import_mode, |
| 198 |
array $content, |
| 199 |
array $snapshot, |
| 200 |
callable $merge_fn, |
| 201 |
callable $create_fn, |
| 202 |
callable $rewrite_fn, |
| 203 |
callable $flatten_fn |
| 204 |
): array { |
| 205 |
$id_map = []; |
| 206 |
$ids_to_flatten = []; |
| 207 |
$operation_result = []; |
| 208 |
|
| 209 |
switch ( $import_mode ) { |
| 210 |
case self::IMPORT_MODE_KEEP_FLATTEN: |
| 211 |
$content = $flatten_fn( $content, $snapshot ); |
| 212 |
break; |
| 213 |
|
| 214 |
case self::IMPORT_MODE_KEEP_CREATE: |
| 215 |
$operation_result = $create_fn( $snapshot ); |
| 216 |
$id_map = $operation_result['id_map'] ?? []; |
| 217 |
$ids_to_flatten = $operation_result['ids_to_flatten'] ?? []; |
| 218 |
|
| 219 |
if ( ! empty( $id_map ) ) { |
| 220 |
$content = $rewrite_fn( $content, $id_map ); |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! empty( $ids_to_flatten ) ) { |
| 224 |
$content = $flatten_fn( $content, $snapshot, $ids_to_flatten ); |
| 225 |
} |
| 226 |
break; |
| 227 |
|
| 228 |
case self::IMPORT_MODE_MATCH_SITE: |
| 229 |
default: |
| 230 |
$operation_result = $merge_fn( $snapshot ); |
| 231 |
$id_map = $operation_result['id_map'] ?? []; |
| 232 |
$ids_to_flatten = $operation_result['ids_to_flatten'] ?? []; |
| 233 |
|
| 234 |
if ( ! empty( $id_map ) ) { |
| 235 |
$content = $rewrite_fn( $content, $id_map ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( ! empty( $ids_to_flatten ) ) { |
| 239 |
$content = $flatten_fn( $content, $snapshot, $ids_to_flatten ); |
| 240 |
} |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
return [ |
| 245 |
'content' => $content, |
| 246 |
'id_map' => $id_map, |
| 247 |
'ids_to_flatten' => $ids_to_flatten, |
| 248 |
'operation_result' => $operation_result, |
| 249 |
]; |
| 250 |
} |
| 251 |
|
| 252 |
public static function filter_items_by_ids( array $items, array $ids ): array { |
| 253 |
$filtered_items = []; |
| 254 |
|
| 255 |
foreach ( $ids as $id ) { |
| 256 |
if ( isset( $items[ $id ] ) ) { |
| 257 |
$filtered_items[ $id ] = $items[ $id ]; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return $filtered_items; |
| 262 |
} |
| 263 |
|
| 264 |
public static function build_filtered_order( array $order, array $items ): array { |
| 265 |
$filtered_order = []; |
| 266 |
|
| 267 |
foreach ( $order as $id ) { |
| 268 |
if ( isset( $items[ $id ] ) ) { |
| 269 |
$filtered_order[] = $id; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
foreach ( array_keys( $items ) as $id ) { |
| 274 |
if ( ! in_array( $id, $filtered_order, true ) ) { |
| 275 |
$filtered_order[] = $id; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $filtered_order; |
| 280 |
} |
| 281 |
} |
| 282 |
|