| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\ImportExport\Modifiers; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 7 |
use Elementor\Modules\AtomicWidgets\Utils; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Styles_Ids_Modifier { |
| 14 |
private Collection $old_to_new_ids; |
| 15 |
|
| 16 |
public static function make() { |
| 17 |
return new self(); |
| 18 |
} |
| 19 |
|
| 20 |
public function run( array $element ) { |
| 21 |
$this->old_to_new_ids = Collection::make(); |
| 22 |
|
| 23 |
$element = $this->replace_styles_ids( $element ); |
| 24 |
$element = $this->replace_references( $element ); |
| 25 |
|
| 26 |
return $element; |
| 27 |
} |
| 28 |
|
| 29 |
private function replace_styles_ids( array $element ) { |
| 30 |
if ( empty( $element['styles'] ) || empty( $element['id'] ) ) { |
| 31 |
return $element; |
| 32 |
} |
| 33 |
|
| 34 |
$styles = Collection::make( $element['styles'] )->map_with_keys( function ( $style, $id ) use ( $element ) { |
| 35 |
$style['id'] = $this->generate_id( $element['id'], $id ); |
| 36 |
|
| 37 |
return [ $style['id'] => $style ]; |
| 38 |
} )->all(); |
| 39 |
|
| 40 |
$element['styles'] = $styles; |
| 41 |
|
| 42 |
return $element; |
| 43 |
} |
| 44 |
|
| 45 |
private function replace_references( array $element ) { |
| 46 |
if ( empty( $element['settings'] ) ) { |
| 47 |
return $element; |
| 48 |
} |
| 49 |
|
| 50 |
$element['settings'] = Collection::make( $element['settings'] )->map( function ( $setting ) { |
| 51 |
if ( ! $setting || ! Classes_Prop_Type::make()->validate( $setting ) ) { |
| 52 |
return $setting; |
| 53 |
} |
| 54 |
|
| 55 |
$setting['value'] = Collection::make( $setting['value'] ) |
| 56 |
->map( fn( $style_id ) => $this->old_to_new_ids->get( $style_id ) ?? $style_id ) |
| 57 |
->all(); |
| 58 |
|
| 59 |
return $setting; |
| 60 |
} )->all(); |
| 61 |
|
| 62 |
return $element; |
| 63 |
} |
| 64 |
|
| 65 |
private function generate_id( $element_id, $old_id ): string { |
| 66 |
$id = Utils::generate_id( "e-{$element_id}-", $this->old_to_new_ids->values() ); |
| 67 |
|
| 68 |
$this->old_to_new_ids[ $old_id ] = $id; |
| 69 |
|
| 70 |
return $id; |
| 71 |
} |
| 72 |
} |
| 73 |
|