elementor
/
modules
/
global-classes
/
utils
/
template-library-global-classes-element-transformer.php
template-library-global-classes-element-transformer.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/global-classes/utils/template-library-global-classes-element-transformer.php
| 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 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | class Template_Library_Global_Classes_Element_Transformer { |
| 13 | |
| 14 | public static function rewrite_elements_classes_ids( array $elements, array $id_map ): array { |
| 15 | if ( empty( $elements ) || empty( $id_map ) ) { |
| 16 | return $elements; |
| 17 | } |
| 18 | |
| 19 | return Template_Library_Element_Iterator::iterate( |
| 20 | $elements, |
| 21 | function ( $element_data ) use ( $id_map ) { |
| 22 | $class_values = $element_data['settings']['classes']['value'] ?? null; |
| 23 | |
| 24 | if ( ! is_array( $class_values ) ) { |
| 25 | return $element_data; |
| 26 | } |
| 27 | |
| 28 | $element_data['settings']['classes']['value'] = self::map_class_values( $class_values, $id_map ); |
| 29 | |
| 30 | return $element_data; |
| 31 | } |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public static function flatten_elements_classes( array $elements, array $global_classes, ?array $only_ids = null ): array { |
| 36 | $items = $global_classes['items'] ?? []; |
| 37 | |
| 38 | if ( empty( $elements ) || empty( $items ) ) { |
| 39 | return $elements; |
| 40 | } |
| 41 | |
| 42 | $ids_to_flatten = null !== $only_ids ? array_fill_keys( $only_ids, true ) : null; |
| 43 | |
| 44 | return Template_Library_Element_Iterator::iterate( |
| 45 | $elements, |
| 46 | function ( $element_data ) use ( $items, $ids_to_flatten ) { |
| 47 | $class_values = $element_data['settings']['classes']['value'] ?? null; |
| 48 | |
| 49 | if ( ! is_array( $class_values ) || empty( $class_values ) ) { |
| 50 | return $element_data; |
| 51 | } |
| 52 | |
| 53 | [ $updated_values, $element_styles ] = self::flatten_class_values( $class_values, $element_data, $items, $ids_to_flatten ); |
| 54 | $element_data['settings']['classes']['value'] = array_values( array_unique( $updated_values ) ); |
| 55 | $element_data['styles'] = $element_styles; |
| 56 | |
| 57 | return $element_data; |
| 58 | } |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | private static function map_class_values( array $class_values, array $id_map ): array { |
| 63 | $updated_values = []; |
| 64 | |
| 65 | foreach ( $class_values as $class_id ) { |
| 66 | if ( ! is_string( $class_id ) || '' === $class_id ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | $updated_values[] = $id_map[ $class_id ] ?? $class_id; |
| 71 | } |
| 72 | |
| 73 | return array_values( array_unique( $updated_values ) ); |
| 74 | } |
| 75 | |
| 76 | private static function flatten_class_values( array $class_values, array $element_data, array $items, ?array $ids_to_flatten ): array { |
| 77 | $updated_values = []; |
| 78 | $element_styles = $element_data['styles'] ?? []; |
| 79 | |
| 80 | $local_style_id = self::find_existing_local_style_id( $element_styles ); |
| 81 | $local_style_used = false; |
| 82 | |
| 83 | foreach ( $class_values as $class_id ) { |
| 84 | if ( ! is_string( $class_id ) || '' === $class_id ) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if ( self::should_flatten_class_id( $class_id, $items, $ids_to_flatten ) ) { |
| 89 | $incoming_variants = $items[ $class_id ]['variants'] ?? []; |
| 90 | |
| 91 | if ( null === $local_style_id ) { |
| 92 | $local_style_id = self::create_local_class_id( $element_data ); |
| 93 | $element_styles[ $local_style_id ] = self::build_local_class_style( $local_style_id, [] ); |
| 94 | } |
| 95 | |
| 96 | $element_styles[ $local_style_id ]['variants'] = array_merge( |
| 97 | $element_styles[ $local_style_id ]['variants'], |
| 98 | $incoming_variants |
| 99 | ); |
| 100 | |
| 101 | if ( ! $local_style_used ) { |
| 102 | $updated_values[] = $local_style_id; |
| 103 | $local_style_used = true; |
| 104 | } |
| 105 | |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | $is_global = self::is_global_class_id( $class_id ); |
| 110 | |
| 111 | if ( ! $is_global || ( null !== $ids_to_flatten && isset( $items[ $class_id ] ) ) ) { |
| 112 | $updated_values[] = $class_id; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return [ $updated_values, $element_styles ]; |
| 117 | } |
| 118 | |
| 119 | private static function find_existing_local_style_id( array $element_styles ): ?string { |
| 120 | foreach ( $element_styles as $style_id => $style ) { |
| 121 | if ( isset( $style['label'] ) && Template_Library_Import_Export_Utils::LOCAL_CLASS_LABEL === $style['label'] ) { |
| 122 | return $style_id; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | private static function is_global_class_id( string $class_id ): bool { |
| 130 | return str_starts_with( $class_id, Template_Library_Import_Export_Utils::GLOBAL_CLASS_ID_PREFIX ); |
| 131 | } |
| 132 | |
| 133 | private static function should_flatten_class_id( string $class_id, array $items, ?array $ids_to_flatten ): bool { |
| 134 | if ( ! isset( $items[ $class_id ] ) ) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if ( null !== $ids_to_flatten && ! isset( $ids_to_flatten[ $class_id ] ) ) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | private static function create_local_class_id( array $element_data ): string { |
| 146 | return Template_Library_Import_Export_Utils::LOCAL_CLASS_ID_PREFIX |
| 147 | . substr( $element_data['id'] ?? '', 0, 8 ) |
| 148 | . '-' |
| 149 | . Template_Library_Import_Export_Utils::generate_random_string(); |
| 150 | } |
| 151 | |
| 152 | private static function build_local_class_style( string $local_id, array $variants ): array { |
| 153 | return [ |
| 154 | 'id' => $local_id, |
| 155 | 'label' => Template_Library_Import_Export_Utils::LOCAL_CLASS_LABEL, |
| 156 | 'type' => 'class', |
| 157 | 'variants' => $variants, |
| 158 | ]; |
| 159 | } |
| 160 | } |
| 161 |