first commit

This commit is contained in:
Roman Pyrih
2026-05-21 15:33:11 +02:00
commit acb036dbd9
8059 changed files with 2885104 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
<?php
namespace Elementor\Modules\Variables\Utils;
use Elementor\Core\Utils\Template_Library_Element_Iterator;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Template_Library_Variables_Element_Transformer {
public static function rewrite_elements_variable_ids( array $elements, array $id_map ): array {
if ( empty( $elements ) || empty( $id_map ) ) {
return $elements;
}
$variable_types_map = array_fill_keys( Variable_Type_Keys::get_all(), true );
return Template_Library_Element_Iterator::iterate(
$elements,
function ( $element_data ) use ( $id_map, $variable_types_map ) {
return self::rewrite_variable_ids_in_element( $element_data, $id_map, $variable_types_map );
}
);
}
public static function flatten_elements_variables( array $elements, array $global_variables, ?array $only_ids = null ): array {
$variable_data = $global_variables['data'] ?? [];
if ( empty( $elements ) || empty( $variable_data ) ) {
return $elements;
}
$variable_types_map = array_fill_keys( Variable_Type_Keys::get_all(), true );
$ids_to_flatten = null !== $only_ids ? array_fill_keys( $only_ids, true ) : null;
return Template_Library_Element_Iterator::iterate(
$elements,
function ( $element_data ) use ( $variable_data, $variable_types_map, $ids_to_flatten ) {
return self::flatten_variable_refs_in_element( $element_data, $variable_data, $variable_types_map, $ids_to_flatten );
}
);
}
private static function rewrite_variable_ids_in_element( array $element_data, array $id_map, array $variable_types_map ): array {
if ( ! empty( $element_data['settings'] ) && is_array( $element_data['settings'] ) ) {
$element_data['settings'] = self::rewrite_variable_ids_recursive( $element_data['settings'], $id_map, $variable_types_map );
}
if ( ! empty( $element_data['styles'] ) && is_array( $element_data['styles'] ) ) {
$element_data['styles'] = self::rewrite_variable_ids_recursive( $element_data['styles'], $id_map, $variable_types_map );
}
return $element_data;
}
private static function rewrite_variable_ids_recursive( $data, array $id_map, array $variable_types_map ) {
if ( ! is_array( $data ) ) {
return $data;
}
if ( isset( $data['$$type'], $variable_types_map[ $data['$$type'] ] ) ) {
if ( isset( $data['value'] ) && is_string( $data['value'] ) && isset( $id_map[ $data['value'] ] ) ) {
$data['value'] = $id_map[ $data['value'] ];
}
return $data;
}
foreach ( $data as $key => $value ) {
if ( is_array( $value ) ) {
$data[ $key ] = self::rewrite_variable_ids_recursive( $value, $id_map, $variable_types_map );
}
}
return $data;
}
private static function flatten_variable_refs_in_element( array $element_data, array $variable_data, array $variable_types_map, ?array $ids_to_flatten ): array {
if ( ! empty( $element_data['settings'] ) && is_array( $element_data['settings'] ) ) {
$element_data['settings'] = self::flatten_variable_refs_recursive( $element_data['settings'], $variable_data, $variable_types_map, $ids_to_flatten );
}
if ( ! empty( $element_data['styles'] ) && is_array( $element_data['styles'] ) ) {
$element_data['styles'] = self::flatten_variable_refs_recursive( $element_data['styles'], $variable_data, $variable_types_map, $ids_to_flatten );
}
return $element_data;
}
private static function flatten_variable_refs_recursive( $data, array $variable_data, array $variable_types_map, ?array $ids_to_flatten = null ) {
if ( ! is_array( $data ) ) {
return $data;
}
if ( isset( $data['$$type'], $variable_types_map[ $data['$$type'] ] ) ) {
$var_id = $data['value'] ?? null;
if ( is_string( $var_id ) && isset( $variable_data[ $var_id ] ) ) {
if ( null !== $ids_to_flatten && ! isset( $ids_to_flatten[ $var_id ] ) ) {
return $data;
}
$variable = $variable_data[ $var_id ];
$resolved_value = $variable['value'] ?? null;
$resolved_type = Variable_Type_Keys::get_resolved_type( $data['$$type'] );
if ( null !== $resolved_value && null !== $resolved_type ) {
return [
'$$type' => $resolved_type,
'value' => Variable_Type_Keys::convert_value_for_resolved_type( $resolved_type, $resolved_value ),
];
}
}
return $data;
}
foreach ( $data as $key => $value ) {
if ( is_array( $value ) ) {
$data[ $key ] = self::flatten_variable_refs_recursive( $value, $variable_data, $variable_types_map, $ids_to_flatten );
}
}
return $data;
}
}

View File

@@ -0,0 +1,238 @@
<?php
namespace Elementor\Modules\Variables\Utils;
use Elementor\Core\Utils\Template_Library_Element_Iterator;
use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
use Elementor\Core\Utils\Template_Library_Snapshot_Processor;
use Elementor\Modules\Variables\Storage\Constants;
use Elementor\Modules\Variables\Storage\Variables_Collection;
use Elementor\Modules\Variables\Storage\Variables_Repository;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Template_Library_Variables_Snapshot_Builder extends Template_Library_Snapshot_Processor {
private static ?self $instance = null;
public static function make(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public static function extract_used_variable_ids_from_elements( array $elements ): array {
$ids = [];
$variable_types = Variable_Type_Keys::get_all();
if ( empty( $elements ) ) {
return [];
}
Template_Library_Element_Iterator::iterate(
$elements,
function ( $element_data ) use ( &$ids, $variable_types ) {
self::collect_variable_ids_from_element( $element_data, $ids, $variable_types );
return $element_data;
}
);
return array_values( array_unique( $ids ) );
}
public static function build_snapshot_for_ids( array $ids ): ?array {
if ( empty( $ids ) ) {
return null;
}
$instance = self::make();
if ( ! $instance->can_access_repository() ) {
return null;
}
$ids = Template_Library_Import_Export_Utils::normalize_string_ids( $ids );
if ( empty( $ids ) ) {
return null;
}
$all_data = $instance->load_current_data();
$all_variables = $all_data['items'] ?? [];
$filtered_data = Template_Library_Import_Export_Utils::filter_items_by_ids( $all_variables, $ids );
if ( empty( $filtered_data ) ) {
return null;
}
return self::build_snapshot_from_data(
$filtered_data,
$all_data['version'] ?? Constants::FORMAT_VERSION_V1
);
}
public static function build_snapshot_for_elements( array $elements, ?array $global_classes_snapshot = null ): ?array {
$ids = self::extract_used_variable_ids_from_elements( $elements );
if ( ! empty( $global_classes_snapshot ) ) {
$ids_from_classes = self::extract_variable_ids_from_data( $global_classes_snapshot );
$ids = array_merge( $ids, $ids_from_classes );
}
$ids = array_values( array_unique( $ids ) );
if ( empty( $ids ) ) {
return null;
}
return self::build_snapshot_for_ids( $ids );
}
public static function extract_variable_ids_from_data( array $data ): array {
$ids = [];
$variable_types = Variable_Type_Keys::get_all();
self::extract_variable_ids_recursive( $data, $ids, $variable_types );
return array_values( array_unique( $ids ) );
}
public static function merge_snapshot_and_get_id_map( array $snapshot ): array {
return self::make()->merge_and_get_id_map( $snapshot );
}
public static function create_snapshot_as_new( array $snapshot ): array {
return self::make()->create_all_as_new( $snapshot );
}
protected function is_matching_item( array $existing_item, array $incoming_item ): bool {
// For variables, if the type and label match, we consider them the same item
// when merging, so we reuse the existing variable and ignore incoming values or extra fields.
return ( $existing_item['type'] ?? '' ) === ( $incoming_item['type'] ?? '' );
}
protected function get_item_prefix(): string {
return Template_Library_Import_Export_Utils::VARIABLE_ID_PREFIX;
}
protected function get_max_items(): int {
return Constants::TOTAL_VARIABLES_COUNT;
}
protected function can_access_repository(): bool {
return null !== $this->get_repository_or_null();
}
protected function load_current_data(): array {
$repository = $this->get_repository_or_null();
if ( ! $repository ) {
return [
'items' => [],
'order' => [],
'watermark' => 0,
'version' => Constants::FORMAT_VERSION_V1,
];
}
$collection = $repository->load();
$serialized = $collection->serialize();
return [
'items' => $serialized['data'] ?? [],
'order' => [],
'watermark' => $serialized['watermark'] ?? 0,
'version' => $serialized['version'] ?? Constants::FORMAT_VERSION_V1,
];
}
protected function parse_incoming_snapshot( array $snapshot ): ?array {
$incoming_data = $snapshot['data'] ?? [];
if ( empty( $incoming_data ) ) {
return null;
}
return $snapshot;
}
protected function get_incoming_items( array $parsed_snapshot ): array {
return $parsed_snapshot['data'] ?? [];
}
protected function count_current_items( array $items ): int {
$count = 0;
foreach ( $items as $item ) {
if ( empty( $item['deleted'] ) ) {
++$count;
}
}
return $count;
}
protected function save_data( array $items, array $metadata ): array {
$repository = $this->get_repository_or_null();
if ( ! $repository ) {
return [];
}
$updated_collection = Variables_Collection::hydrate( [
'data' => $items,
'watermark' => $metadata['watermark'] ?? 0,
'version' => $metadata['version'] ?? Constants::FORMAT_VERSION_V1,
] );
$repository->save( $updated_collection );
return [
'variables' => [
'data' => $items,
'watermark' => $updated_collection->watermark(),
],
];
}
private static function extract_variable_ids_recursive( $data, array &$ids, array $variable_types ): void {
if ( ! is_array( $data ) ) {
return;
}
if ( isset( $data['$$type'] ) && in_array( $data['$$type'], $variable_types, true ) ) {
if ( isset( $data['value'] ) && is_string( $data['value'] ) && '' !== $data['value'] ) {
$ids[] = $data['value'];
}
}
foreach ( $data as $value ) {
if ( is_array( $value ) ) {
self::extract_variable_ids_recursive( $value, $ids, $variable_types );
}
}
}
private static function collect_variable_ids_from_element( array $element_data, array &$ids, array $variable_types ): void {
if ( ! empty( $element_data['settings'] ) && is_array( $element_data['settings'] ) ) {
self::extract_variable_ids_recursive( $element_data['settings'], $ids, $variable_types );
}
if ( ! empty( $element_data['styles'] ) && is_array( $element_data['styles'] ) ) {
self::extract_variable_ids_recursive( $element_data['styles'], $ids, $variable_types );
}
}
private function get_repository_or_null(): ?Variables_Repository {
$kit = Plugin::instance()->kits_manager->get_active_kit();
if ( ! $kit ) {
return null;
}
return new Variables_Repository( $kit );
}
private static function build_snapshot_from_data( array $data, int $version ): array {
return [
'data' => $data,
'watermark' => 0,
'version' => $version,
];
}
}

View File

@@ -0,0 +1,211 @@
<?php
namespace Elementor\Modules\Variables\Utils;
use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Template_Library_Variables {
public static function add_variables_snapshot( array $snapshots, $content, $template_id, array $export_data ): array {
if ( ! is_array( $content ) ) {
return $snapshots;
}
if ( ! empty( $snapshots['global_variables'] ) ) {
return $snapshots;
}
$global_classes_snapshot = $snapshots['global_classes'] ?? null;
$snapshot = self::build_snapshot_for_elements( $content, $global_classes_snapshot );
if ( ! empty( $snapshot ) ) {
$snapshots['global_variables'] = $snapshot;
}
return $snapshots;
}
public static function extract_variables_from_data( array $snapshots, array $decoded_data, array $data ): array {
$snapshot = $decoded_data['global_variables'] ?? null;
if ( ! empty( $snapshot ) && is_array( $snapshot ) ) {
$snapshots['global_variables'] = $snapshot;
}
return $snapshots;
}
public static function process_variables_import( array $result, string $import_mode, array $data ): array {
$snapshot = $data['global_variables'] ?? null;
if ( empty( $snapshot ) || ! is_array( $snapshot ) ) {
return $result;
}
$processed = Template_Library_Import_Export_Utils::process_import_by_mode(
$import_mode,
$result['content'],
$snapshot,
[ self::class, 'merge_snapshot_and_get_id_map' ],
[ self::class, 'create_all_as_new' ],
[ self::class, 'rewrite_elements_variable_ids' ],
[ self::class, 'flatten_elements_variables' ]
);
$result['content'] = $processed['content'];
$result['updated_global_variables'] = $processed['operation_result']['variables'] ?? null;
$result['variables_id_map'] = $processed['id_map'];
$result['variables_to_flatten'] = $processed['ids_to_flatten'];
$result['variables_snapshot'] = $snapshot;
return $result;
}
private static function build_snapshot_for_elements( array $elements, ?array $global_classes_snapshot = null ): ?array {
return Template_Library_Variables_Snapshot_Builder::build_snapshot_for_elements( $elements, $global_classes_snapshot );
}
public static function merge_snapshot_and_get_id_map( array $snapshot ): array {
return Template_Library_Variables_Snapshot_Builder::merge_snapshot_and_get_id_map( $snapshot );
}
public static function rewrite_elements_variable_ids( array $elements, array $id_map ): array {
return Template_Library_Variables_Element_Transformer::rewrite_elements_variable_ids( $elements, $id_map );
}
public static function flatten_elements_variables( array $elements, array $global_variables, ?array $only_ids = null ): array {
return Template_Library_Variables_Element_Transformer::flatten_elements_variables( $elements, $global_variables, $only_ids );
}
public static function create_all_as_new( array $snapshot ): array {
return Template_Library_Variables_Snapshot_Builder::create_snapshot_as_new( $snapshot );
}
public static function transform_variables_in_classes_snapshot( array $classes_snapshot, string $import_mode, array $result, array $data ): array {
$variables_id_map = $result['variables_id_map'] ?? [];
$variables_to_flatten = $result['variables_to_flatten'] ?? [];
$variables_snapshot = $result['variables_snapshot'] ?? ( $data['global_variables'] ?? null );
if ( Template_Library_Import_Export_Utils::IMPORT_MODE_KEEP_FLATTEN === $import_mode && ! empty( $variables_snapshot ) ) {
$classes_snapshot = self::flatten_variables_in_classes_snapshot( $classes_snapshot, $variables_snapshot );
} else {
if ( ! empty( $variables_id_map ) ) {
$classes_snapshot = self::rewrite_variable_ids_in_classes_snapshot( $classes_snapshot, $variables_id_map );
}
if ( ! empty( $variables_to_flatten ) && ! empty( $variables_snapshot ) ) {
$classes_snapshot = self::flatten_variables_in_classes_snapshot( $classes_snapshot, $variables_snapshot, $variables_to_flatten );
}
}
return $classes_snapshot;
}
public static function rewrite_variable_ids_in_classes_snapshot( array $snapshot, array $id_map ): array {
if ( empty( $snapshot['items'] ) || empty( $id_map ) ) {
return $snapshot;
}
$variable_types = Variable_Type_Keys::get_all();
foreach ( $snapshot['items'] as $class_id => &$class_item ) {
if ( empty( $class_item['variants'] ) || ! is_array( $class_item['variants'] ) ) {
continue;
}
foreach ( $class_item['variants'] as &$variant ) {
if ( empty( $variant['props'] ) || ! is_array( $variant['props'] ) ) {
continue;
}
$variant['props'] = self::rewrite_variable_refs_recursive( $variant['props'], $id_map, $variable_types );
}
}
return $snapshot;
}
private static function rewrite_variable_refs_recursive( array $data, array $id_map, array $variable_types ): array {
foreach ( $data as $key => $value ) {
if ( ! is_array( $value ) ) {
continue;
}
$type = $value['$$type'] ?? null;
$val = $value['value'] ?? null;
if ( $type && in_array( $type, $variable_types, true ) && is_string( $val ) && isset( $id_map[ $val ] ) ) {
$data[ $key ]['value'] = $id_map[ $val ];
continue;
}
$data[ $key ] = self::rewrite_variable_refs_recursive( $value, $id_map, $variable_types );
}
return $data;
}
public static function flatten_variables_in_classes_snapshot( array $classes_snapshot, array $variables_snapshot, ?array $only_ids = null ): array {
if ( empty( $classes_snapshot['items'] ) ) {
return $classes_snapshot;
}
$variable_data = $variables_snapshot['data'] ?? [];
$variable_types = Variable_Type_Keys::get_all();
$type_map = Variable_Type_Keys::get_type_mappings();
$ids_to_flatten = null !== $only_ids ? array_fill_keys( $only_ids, true ) : null;
foreach ( $classes_snapshot['items'] as $class_id => &$class_item ) {
if ( empty( $class_item['variants'] ) || ! is_array( $class_item['variants'] ) ) {
continue;
}
foreach ( $class_item['variants'] as &$variant ) {
if ( empty( $variant['props'] ) || ! is_array( $variant['props'] ) ) {
continue;
}
$variant['props'] = self::flatten_variable_refs_in_props( $variant['props'], $variable_data, $variable_types, $type_map, $ids_to_flatten );
}
}
return $classes_snapshot;
}
private static function flatten_variable_refs_in_props( array $data, array $variable_data, array $variable_types, array $type_map, ?array $ids_to_flatten = null ): array {
foreach ( $data as $key => $value ) {
if ( ! is_array( $value ) ) {
continue;
}
$type = $value['$$type'] ?? null;
$var_id = $value['value'] ?? null;
if ( $type && in_array( $type, $variable_types, true ) && is_string( $var_id ) && isset( $variable_data[ $var_id ] ) ) {
if ( null !== $ids_to_flatten && ! isset( $ids_to_flatten[ $var_id ] ) ) {
continue;
}
$resolved_type = $type_map[ $type ] ?? null;
$resolved_value = $variable_data[ $var_id ]['value'] ?? null;
if ( $resolved_type && null !== $resolved_value ) {
$data[ $key ] = [
'$$type' => $resolved_type,
'value' => Variable_Type_Keys::convert_value_for_resolved_type( $resolved_type, $resolved_value ),
];
}
continue;
}
$data[ $key ] = self::flatten_variable_refs_in_props( $value, $variable_data, $variable_types, $type_map, $ids_to_flatten );
}
return $data;
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Elementor\Modules\Variables\Utils;
use Elementor\Modules\AtomicWidgets\Styles\Size_Constants;
use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter;
use Elementor\Modules\Variables\PropTypes\Color_Variable_Prop_Type;
use Elementor\Modules\Variables\PropTypes\Font_Variable_Prop_Type;
use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Variable_Type_Keys {
private static ?array $types_cache = null;
private static ?array $mappings_cache = null;
public static function get_all(): array {
if ( null === self::$types_cache ) {
self::$types_cache = [
Color_Variable_Prop_Type::get_key(),
Font_Variable_Prop_Type::get_key(),
Size_Variable_Prop_Type::get_key(),
Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY,
];
}
return self::$types_cache;
}
public static function is_variable_type( $type ): bool {
if ( ! is_string( $type ) || '' === $type ) {
return false;
}
return in_array( $type, self::get_all(), true );
}
public static function get_type_mappings(): array {
if ( null === self::$mappings_cache ) {
self::$mappings_cache = [
Color_Variable_Prop_Type::get_key() => 'color',
Font_Variable_Prop_Type::get_key() => 'string',
Size_Variable_Prop_Type::get_key() => 'size',
Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY => 'size',
];
}
return self::$mappings_cache;
}
public static function get_resolved_type( string $variable_type ): ?string {
return self::get_type_mappings()[ $variable_type ] ?? null;
}
public static function convert_value_for_resolved_type( string $resolved_type, $value ) {
if ( 'size' !== $resolved_type || ! is_string( $value ) ) {
return $value;
}
return self::parse_size_string( $value );
}
private static function parse_size_string( string $value ): array {
$value = trim( strtolower( $value ) );
if ( 'auto' === $value ) {
return [
'size' => '',
'unit' => 'auto',
];
}
if ( preg_match( '/^(-?\d*\.?\d+)([a-z%]+)$/i', $value, $matches ) ) {
return [
'size' => $matches[1] + 0,
'unit' => strtolower( $matches[2] ),
];
}
return [
'size' => $value,
'unit' => Size_Constants::DEFAULT_UNIT,
];
}
}