| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\Utils; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Template_Library_Element_Iterator; |
| 6 |
use Elementor\Core\Utils\Template_Library_Import_Export_Utils; |
| 7 |
use Elementor\Core\Utils\Template_Library_Snapshot_Processor; |
| 8 |
use Elementor\Modules\GlobalClasses\Global_Classes_Parser; |
| 9 |
use Elementor\Modules\GlobalClasses\Global_Classes_Repository; |
| 10 |
use Elementor\Modules\GlobalClasses\Global_Classes_Rest_Api; |
| 11 |
use Elementor\Plugin; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class Template_Library_Global_Classes_Snapshot_Builder extends Template_Library_Snapshot_Processor { |
| 18 |
|
| 19 |
private static ?self $instance = null; |
| 20 |
|
| 21 |
public static function make(): self { |
| 22 |
if ( null === self::$instance ) { |
| 23 |
self::$instance = new self(); |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
public static function extract_used_class_ids_from_elements( array $elements ): array { |
| 29 |
$ids = []; |
| 30 |
|
| 31 |
if ( empty( $elements ) ) { |
| 32 |
return []; |
| 33 |
} |
| 34 |
|
| 35 |
Template_Library_Element_Iterator::iterate( |
| 36 |
$elements, |
| 37 |
function ( $element_data ) use ( &$ids ) { |
| 38 |
$class_values = $element_data['settings']['classes']['value'] ?? []; |
| 39 |
|
| 40 |
if ( is_array( $class_values ) ) { |
| 41 |
foreach ( $class_values as $class_id ) { |
| 42 |
if ( is_string( $class_id ) && '' !== $class_id ) { |
| 43 |
$ids[] = $class_id; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $element_data; |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
return array_values( array_unique( $ids ) ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function build_snapshot_for_ids( array $ids ): ?array { |
| 56 |
if ( empty( $ids ) || ! self::make()->can_access_repository() ) { |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
$ids = Template_Library_Import_Export_Utils::normalize_string_ids( $ids ); |
| 61 |
|
| 62 |
if ( empty( $ids ) ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$current = self::make()->load_current_data(); |
| 67 |
$filtered_items = Template_Library_Import_Export_Utils::filter_items_by_ids( $current['items'], $ids ); |
| 68 |
|
| 69 |
if ( empty( $filtered_items ) ) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
$filtered_order = Template_Library_Import_Export_Utils::build_filtered_order( $current['order'], $filtered_items ); |
| 74 |
|
| 75 |
return self::parse_snapshot_or_null( [ |
| 76 |
'items' => $filtered_items, |
| 77 |
'order' => $filtered_order, |
| 78 |
] ); |
| 79 |
} |
| 80 |
|
| 81 |
public static function build_snapshot_for_elements( array $elements ): ?array { |
| 82 |
$ids = self::extract_used_class_ids_from_elements( $elements ); |
| 83 |
|
| 84 |
if ( empty( $ids ) ) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
return self::build_snapshot_for_ids( $ids ); |
| 89 |
} |
| 90 |
|
| 91 |
public static function merge_snapshot_and_get_id_map( array $snapshot ): array { |
| 92 |
return self::make()->merge_and_get_id_map( $snapshot ); |
| 93 |
} |
| 94 |
|
| 95 |
public static function create_snapshot_as_new( array $snapshot ): array { |
| 96 |
return self::make()->create_all_as_new( $snapshot ); |
| 97 |
} |
| 98 |
|
| 99 |
protected function is_matching_item( array $existing_item, array $incoming_item ): bool { |
| 100 |
// For global classes, if the labels match, we consider them the same item |
| 101 |
// when merging, so we reuse the existing class and ignore incoming variants or extra fields. |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
protected function normalize_for_comparison( array $item ): array { |
| 106 |
$id = $item['id'] ?? ''; |
| 107 |
|
| 108 |
if ( '' === $id ) { |
| 109 |
return $item; |
| 110 |
} |
| 111 |
|
| 112 |
$parsed = self::parse_snapshot_or_null( [ |
| 113 |
'items' => [ $id => $item ], |
| 114 |
'order' => [ $id ], |
| 115 |
] ); |
| 116 |
|
| 117 |
if ( null !== $parsed && isset( $parsed['items'][ $id ] ) ) { |
| 118 |
return $parsed['items'][ $id ]; |
| 119 |
} |
| 120 |
|
| 121 |
return $item; |
| 122 |
} |
| 123 |
|
| 124 |
protected function get_item_prefix(): string { |
| 125 |
return Template_Library_Import_Export_Utils::GLOBAL_CLASS_ID_PREFIX; |
| 126 |
} |
| 127 |
|
| 128 |
protected function get_max_items(): int { |
| 129 |
return Global_Classes_Rest_Api::MAX_ITEMS; |
| 130 |
} |
| 131 |
|
| 132 |
protected function can_access_repository(): bool { |
| 133 |
return class_exists( Global_Classes_Repository::class ) && $this->has_active_kit(); |
| 134 |
} |
| 135 |
|
| 136 |
protected function load_current_data(): array { |
| 137 |
$current = Global_Classes_Repository::make()->context( Global_Classes_Repository::CONTEXT_PREVIEW )->all()->get(); |
| 138 |
|
| 139 |
return [ |
| 140 |
'items' => $current['items'] ?? [], |
| 141 |
'order' => $current['order'] ?? [], |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
protected function parse_incoming_snapshot( array $snapshot ): ?array { |
| 146 |
return self::parse_snapshot_or_null( $snapshot ); |
| 147 |
} |
| 148 |
|
| 149 |
protected function get_incoming_items( array $parsed_snapshot ): array { |
| 150 |
$items = []; |
| 151 |
$order = $parsed_snapshot['order'] ?? array_keys( $parsed_snapshot['items'] ?? [] ); |
| 152 |
|
| 153 |
foreach ( $order as $id ) { |
| 154 |
if ( isset( $parsed_snapshot['items'][ $id ] ) ) { |
| 155 |
$items[ $id ] = $parsed_snapshot['items'][ $id ]; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return $items; |
| 160 |
} |
| 161 |
|
| 162 |
protected function count_current_items( array $items ): int { |
| 163 |
return count( $items ); |
| 164 |
} |
| 165 |
|
| 166 |
protected function save_data( array $items, array $metadata ): array { |
| 167 |
$order = $metadata['order'] ?? []; |
| 168 |
|
| 169 |
Global_Classes_Repository::make()->put( $items, $order ); |
| 170 |
|
| 171 |
return [ |
| 172 |
'global_classes' => [ |
| 173 |
'items' => $items, |
| 174 |
'order' => $order, |
| 175 |
], |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
protected function prepare_item_for_save( array $item, string $target_id ): array { |
| 180 |
$item['id'] = $target_id; |
| 181 |
return $item; |
| 182 |
} |
| 183 |
|
| 184 |
private function has_active_kit(): bool { |
| 185 |
return (bool) Plugin::instance()->kits_manager->get_active_kit(); |
| 186 |
} |
| 187 |
|
| 188 |
private static function parse_snapshot_or_null( array $snapshot ): ?array { |
| 189 |
$parse_result = Global_Classes_Parser::make()->parse( $snapshot ); |
| 190 |
if ( ! $parse_result->is_valid() ) { |
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
return $parse_result->unwrap(); |
| 195 |
} |
| 196 |
} |
| 197 |
|