first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\GlobalClasses\Utils;
|
||||
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base;
|
||||
use Elementor\Plugin;
|
||||
|
||||
class Atomic_Elements_Utils {
|
||||
|
||||
public static function is_classes_prop( $prop ) {
|
||||
// phpcs:ignore
|
||||
return 'plain' === $prop::$KIND && 'classes' === $prop->get_key();
|
||||
}
|
||||
|
||||
public static function get_element_type( $element ) {
|
||||
return 'widget' === $element['elType'] ? $element['widgetType'] : $element['elType'];
|
||||
}
|
||||
|
||||
public static function get_element_instance( $element_type ) {
|
||||
$widget = Plugin::instance()->widgets_manager->get_widget_types( $element_type );
|
||||
$element = Plugin::instance()->elements_manager->get_element_types( $element_type );
|
||||
|
||||
return $widget ?? $element;
|
||||
}
|
||||
|
||||
public static function is_atomic_element( $element_instance ) {
|
||||
if ( ! $element_instance ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
$element_instance instanceof Atomic_Element_Base ||
|
||||
$element_instance instanceof Atomic_Widget_Base
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\GlobalClasses\Utils;
|
||||
|
||||
use Elementor\Core\Utils\Template_Library_Element_Iterator;
|
||||
use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Template_Library_Global_Classes_Element_Transformer {
|
||||
|
||||
public static function rewrite_elements_classes_ids( array $elements, array $id_map ): array {
|
||||
if ( empty( $elements ) || empty( $id_map ) ) {
|
||||
return $elements;
|
||||
}
|
||||
|
||||
return Template_Library_Element_Iterator::iterate(
|
||||
$elements,
|
||||
function ( $element_data ) use ( $id_map ) {
|
||||
$class_values = $element_data['settings']['classes']['value'] ?? null;
|
||||
|
||||
if ( ! is_array( $class_values ) ) {
|
||||
return $element_data;
|
||||
}
|
||||
|
||||
$element_data['settings']['classes']['value'] = self::map_class_values( $class_values, $id_map );
|
||||
|
||||
return $element_data;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static function flatten_elements_classes( array $elements, array $global_classes, ?array $only_ids = null ): array {
|
||||
$items = $global_classes['items'] ?? [];
|
||||
|
||||
if ( empty( $elements ) || empty( $items ) ) {
|
||||
return $elements;
|
||||
}
|
||||
|
||||
$ids_to_flatten = null !== $only_ids ? array_fill_keys( $only_ids, true ) : null;
|
||||
|
||||
return Template_Library_Element_Iterator::iterate(
|
||||
$elements,
|
||||
function ( $element_data ) use ( $items, $ids_to_flatten ) {
|
||||
$class_values = $element_data['settings']['classes']['value'] ?? null;
|
||||
|
||||
if ( ! is_array( $class_values ) || empty( $class_values ) ) {
|
||||
return $element_data;
|
||||
}
|
||||
|
||||
[ $updated_values, $element_styles ] = self::flatten_class_values( $class_values, $element_data, $items, $ids_to_flatten );
|
||||
$element_data['settings']['classes']['value'] = array_values( array_unique( $updated_values ) );
|
||||
$element_data['styles'] = $element_styles;
|
||||
|
||||
return $element_data;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static function map_class_values( array $class_values, array $id_map ): array {
|
||||
$updated_values = [];
|
||||
|
||||
foreach ( $class_values as $class_id ) {
|
||||
if ( ! is_string( $class_id ) || '' === $class_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$updated_values[] = $id_map[ $class_id ] ?? $class_id;
|
||||
}
|
||||
|
||||
return array_values( array_unique( $updated_values ) );
|
||||
}
|
||||
|
||||
private static function flatten_class_values( array $class_values, array $element_data, array $items, ?array $ids_to_flatten ): array {
|
||||
$updated_values = [];
|
||||
$element_styles = $element_data['styles'] ?? [];
|
||||
|
||||
$local_style_id = self::find_existing_local_style_id( $element_styles );
|
||||
$local_style_used = false;
|
||||
|
||||
foreach ( $class_values as $class_id ) {
|
||||
if ( ! is_string( $class_id ) || '' === $class_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( self::should_flatten_class_id( $class_id, $items, $ids_to_flatten ) ) {
|
||||
$incoming_variants = $items[ $class_id ]['variants'] ?? [];
|
||||
|
||||
if ( null === $local_style_id ) {
|
||||
$local_style_id = self::create_local_class_id( $element_data );
|
||||
$element_styles[ $local_style_id ] = self::build_local_class_style( $local_style_id, [] );
|
||||
}
|
||||
|
||||
$element_styles[ $local_style_id ]['variants'] = array_merge(
|
||||
$element_styles[ $local_style_id ]['variants'],
|
||||
$incoming_variants
|
||||
);
|
||||
|
||||
if ( ! $local_style_used ) {
|
||||
$updated_values[] = $local_style_id;
|
||||
$local_style_used = true;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$is_global = self::is_global_class_id( $class_id );
|
||||
|
||||
if ( ! $is_global || ( null !== $ids_to_flatten && isset( $items[ $class_id ] ) ) ) {
|
||||
$updated_values[] = $class_id;
|
||||
}
|
||||
}
|
||||
|
||||
return [ $updated_values, $element_styles ];
|
||||
}
|
||||
|
||||
private static function find_existing_local_style_id( array $element_styles ): ?string {
|
||||
foreach ( $element_styles as $style_id => $style ) {
|
||||
if ( isset( $style['label'] ) && Template_Library_Import_Export_Utils::LOCAL_CLASS_LABEL === $style['label'] ) {
|
||||
return $style_id;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function is_global_class_id( string $class_id ): bool {
|
||||
return str_starts_with( $class_id, Template_Library_Import_Export_Utils::GLOBAL_CLASS_ID_PREFIX );
|
||||
}
|
||||
|
||||
private static function should_flatten_class_id( string $class_id, array $items, ?array $ids_to_flatten ): bool {
|
||||
if ( ! isset( $items[ $class_id ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( null !== $ids_to_flatten && ! isset( $ids_to_flatten[ $class_id ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function create_local_class_id( array $element_data ): string {
|
||||
return Template_Library_Import_Export_Utils::LOCAL_CLASS_ID_PREFIX
|
||||
. substr( $element_data['id'] ?? '', 0, 8 )
|
||||
. '-'
|
||||
. Template_Library_Import_Export_Utils::generate_random_string();
|
||||
}
|
||||
|
||||
private static function build_local_class_style( string $local_id, array $variants ): array {
|
||||
return [
|
||||
'id' => $local_id,
|
||||
'label' => Template_Library_Import_Export_Utils::LOCAL_CLASS_LABEL,
|
||||
'type' => 'class',
|
||||
'variants' => $variants,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\GlobalClasses\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\GlobalClasses\Global_Classes_Parser;
|
||||
use Elementor\Modules\GlobalClasses\Global_Classes_Repository;
|
||||
use Elementor\Modules\GlobalClasses\Global_Classes_Rest_Api;
|
||||
use Elementor\Plugin;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Template_Library_Global_Classes_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_class_ids_from_elements( array $elements ): array {
|
||||
$ids = [];
|
||||
|
||||
if ( empty( $elements ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
Template_Library_Element_Iterator::iterate(
|
||||
$elements,
|
||||
function ( $element_data ) use ( &$ids ) {
|
||||
$class_values = $element_data['settings']['classes']['value'] ?? [];
|
||||
|
||||
if ( is_array( $class_values ) ) {
|
||||
foreach ( $class_values as $class_id ) {
|
||||
if ( is_string( $class_id ) && '' !== $class_id ) {
|
||||
$ids[] = $class_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $element_data;
|
||||
}
|
||||
);
|
||||
|
||||
return array_values( array_unique( $ids ) );
|
||||
}
|
||||
|
||||
public static function build_snapshot_for_ids( array $ids ): ?array {
|
||||
if ( empty( $ids ) || ! self::make()->can_access_repository() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$ids = Template_Library_Import_Export_Utils::normalize_string_ids( $ids );
|
||||
|
||||
if ( empty( $ids ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$current = self::make()->load_current_data();
|
||||
$filtered_items = Template_Library_Import_Export_Utils::filter_items_by_ids( $current['items'], $ids );
|
||||
|
||||
if ( empty( $filtered_items ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$filtered_order = Template_Library_Import_Export_Utils::build_filtered_order( $current['order'], $filtered_items );
|
||||
|
||||
return self::parse_snapshot_or_null( [
|
||||
'items' => $filtered_items,
|
||||
'order' => $filtered_order,
|
||||
] );
|
||||
}
|
||||
|
||||
public static function build_snapshot_for_elements( array $elements ): ?array {
|
||||
$ids = self::extract_used_class_ids_from_elements( $elements );
|
||||
|
||||
if ( empty( $ids ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::build_snapshot_for_ids( $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 global classes, if the labels match, we consider them the same item
|
||||
// when merging, so we reuse the existing class and ignore incoming variants or extra fields.
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function normalize_for_comparison( array $item ): array {
|
||||
$id = $item['id'] ?? '';
|
||||
|
||||
if ( '' === $id ) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
$parsed = self::parse_snapshot_or_null( [
|
||||
'items' => [ $id => $item ],
|
||||
'order' => [ $id ],
|
||||
] );
|
||||
|
||||
if ( null !== $parsed && isset( $parsed['items'][ $id ] ) ) {
|
||||
return $parsed['items'][ $id ];
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
protected function get_item_prefix(): string {
|
||||
return Template_Library_Import_Export_Utils::GLOBAL_CLASS_ID_PREFIX;
|
||||
}
|
||||
|
||||
protected function get_max_items(): int {
|
||||
return Global_Classes_Rest_Api::MAX_ITEMS;
|
||||
}
|
||||
|
||||
protected function can_access_repository(): bool {
|
||||
return class_exists( Global_Classes_Repository::class ) && $this->has_active_kit();
|
||||
}
|
||||
|
||||
protected function load_current_data(): array {
|
||||
$current = Global_Classes_Repository::make()->context( Global_Classes_Repository::CONTEXT_PREVIEW )->all()->get();
|
||||
|
||||
return [
|
||||
'items' => $current['items'] ?? [],
|
||||
'order' => $current['order'] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
protected function parse_incoming_snapshot( array $snapshot ): ?array {
|
||||
return self::parse_snapshot_or_null( $snapshot );
|
||||
}
|
||||
|
||||
protected function get_incoming_items( array $parsed_snapshot ): array {
|
||||
$items = [];
|
||||
$order = $parsed_snapshot['order'] ?? array_keys( $parsed_snapshot['items'] ?? [] );
|
||||
|
||||
foreach ( $order as $id ) {
|
||||
if ( isset( $parsed_snapshot['items'][ $id ] ) ) {
|
||||
$items[ $id ] = $parsed_snapshot['items'][ $id ];
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
protected function count_current_items( array $items ): int {
|
||||
return count( $items );
|
||||
}
|
||||
|
||||
protected function save_data( array $items, array $metadata ): array {
|
||||
$order = $metadata['order'] ?? [];
|
||||
|
||||
Global_Classes_Repository::make()->put( $items, $order );
|
||||
|
||||
return [
|
||||
'global_classes' => [
|
||||
'items' => $items,
|
||||
'order' => $order,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepare_item_for_save( array $item, string $target_id ): array {
|
||||
$item['id'] = $target_id;
|
||||
return $item;
|
||||
}
|
||||
|
||||
private function has_active_kit(): bool {
|
||||
return (bool) Plugin::instance()->kits_manager->get_active_kit();
|
||||
}
|
||||
|
||||
private static function parse_snapshot_or_null( array $snapshot ): ?array {
|
||||
$parse_result = Global_Classes_Parser::make()->parse( $snapshot );
|
||||
if ( ! $parse_result->is_valid() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $parse_result->unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\GlobalClasses\Utils;
|
||||
|
||||
use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Template_Library_Global_Classes {
|
||||
|
||||
public static function add_global_classes_snapshot( array $snapshots, $content, $template_id, array $export_data ): array {
|
||||
if ( ! is_array( $content ) ) {
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
if ( ! empty( $snapshots['global_classes'] ) ) {
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
$snapshot = Template_Library_Global_Classes_Snapshot_Builder::build_snapshot_for_elements( $content );
|
||||
|
||||
if ( ! empty( $snapshot ) ) {
|
||||
$snapshots['global_classes'] = $snapshot;
|
||||
}
|
||||
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
public static function extract_global_classes_from_data( array $snapshots, array $decoded_data, array $data ): array {
|
||||
$snapshot = $decoded_data['global_classes'] ?? null;
|
||||
|
||||
if ( ! empty( $snapshot ) && is_array( $snapshot ) ) {
|
||||
$snapshots['global_classes'] = $snapshot;
|
||||
}
|
||||
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
public static function process_global_classes_import( array $result, string $import_mode, array $data ): array {
|
||||
$snapshot = $data['global_classes'] ?? null;
|
||||
|
||||
if ( empty( $snapshot ) || ! is_array( $snapshot ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$snapshot = apply_filters(
|
||||
'elementor/global_classes/import/transform_snapshot',
|
||||
$snapshot,
|
||||
$import_mode,
|
||||
$result,
|
||||
$data
|
||||
);
|
||||
|
||||
$processed = Template_Library_Import_Export_Utils::process_import_by_mode(
|
||||
$import_mode,
|
||||
$result['content'],
|
||||
$snapshot,
|
||||
[ Template_Library_Global_Classes_Snapshot_Builder::class, 'merge_snapshot_and_get_id_map' ],
|
||||
[ Template_Library_Global_Classes_Snapshot_Builder::class, 'create_snapshot_as_new' ],
|
||||
[ Template_Library_Global_Classes_Element_Transformer::class, 'rewrite_elements_classes_ids' ],
|
||||
[ Template_Library_Global_Classes_Element_Transformer::class, 'flatten_elements_classes' ]
|
||||
);
|
||||
|
||||
$result['content'] = $processed['content'];
|
||||
$result['updated_global_classes'] = $processed['operation_result']['global_classes'] ?? null;
|
||||
$result['classes_to_flatten'] = $processed['ids_to_flatten'];
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user