← All changes
|
modules/global-classes/global-classes-repository.php
+13
-40
4.0.9
→
3.29.0-beta4
View file →
| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\GlobalClasses; |
| 3 | 3 | |
| 4 | -use Elementor\Core\Kits\Documents\Kit; | |
| 5 | -use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator; | |
| 6 | -use Elementor\Modules\GlobalClasses\Global_Classes_Parser; | |
| 4 | +use Elementor\Core\Base\Document; | |
| 5 | +use Elementor\Core\Utils\Collection; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base; | |
| 7 | +use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base; | |
| 8 | +use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils; | |
| 7 | 9 | use Elementor\Plugin; |
| 8 | 10 | |
| 9 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 12 | exit; // Exit if accessed directly. |
| @@ -19,57 +21,30 @@ | ||
| 19 | 21 | const CONTEXT_PREVIEW = 'preview'; |
| 20 | 22 | |
| 21 | 23 | private string $context = self::CONTEXT_FRONTEND; |
| 22 | 24 | |
| 23 | - private ?Global_Classes $cache = null; | |
| 24 | - | |
| 25 | - private ?Kit $kit = null; | |
| 26 | - | |
| 27 | - public function __construct( ?Kit $kit = null ) { | |
| 28 | - $this->kit = $kit; | |
| 25 | + public static function make(): Global_Classes_Repository { | |
| 26 | + return new self(); | |
| 29 | 27 | } |
| 30 | 28 | |
| 31 | - public static function make( ?Kit $kit = null ): Global_Classes_Repository { | |
| 32 | - return new self( $kit ); | |
| 33 | - } | |
| 34 | - | |
| 35 | 29 | public function context( string $context ): self { |
| 36 | 30 | $this->context = $context; |
| 37 | - $this->cache = null; | |
| 38 | 31 | |
| 39 | 32 | return $this; |
| 40 | 33 | } |
| 41 | 34 | |
| 42 | - public function all( bool $force = false ): Global_Classes { | |
| 43 | - if ( ! $force && null !== $this->cache ) { | |
| 44 | - return $this->cache; | |
| 45 | - } | |
| 46 | - | |
| 35 | + public function all() { | |
| 47 | 36 | $meta_key = $this->get_meta_key(); |
| 48 | - $kit = $this->get_kit(); | |
| 49 | - $all = $kit->get_json_meta( $meta_key ); | |
| 37 | + $all = $this->get_kit()->get_json_meta( $meta_key ); | |
| 50 | 38 | |
| 51 | 39 | $is_preview = static::META_KEY_PREVIEW === $meta_key; |
| 52 | 40 | $is_empty = empty( $all ); |
| 53 | 41 | |
| 54 | 42 | if ( $is_preview && $is_empty ) { |
| 55 | - $all = $kit->get_json_meta( static::META_KEY_FRONTEND ); | |
| 43 | + $all = $this->get_kit()->get_json_meta( static::META_KEY_FRONTEND ); | |
| 56 | 44 | } |
| 57 | 45 | |
| 58 | - $all['order'] = Global_Classes_Parser::sanitize_order( $all['items'] ?? [], $all['order'] ?? [] ); | |
| 59 | - | |
| 60 | - Migrations_Orchestrator::make()->migrate( | |
| 61 | - $all, | |
| 62 | - $kit->get_id(), | |
| 63 | - $meta_key, | |
| 64 | - function( $migrated_data ) use ( $kit, $meta_key ) { | |
| 65 | - $kit->update_json_meta( $meta_key, $migrated_data ); | |
| 66 | - } | |
| 67 | - ); | |
| 68 | - | |
| 69 | - $this->cache = Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] ); | |
| 70 | - | |
| 71 | - return $this->cache; | |
| 46 | + return Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] ); | |
| 72 | 47 | } |
| 73 | 48 | |
| 74 | 49 | public function put( array $items, array $order ) { |
| 75 | 50 | $current_value = $this->all()->get(); |
| @@ -96,10 +71,8 @@ | ||
| 96 | 71 | if ( ! $value ) { |
| 97 | 72 | throw new \Exception( 'Failed to update global classes' ); |
| 98 | 73 | } |
| 99 | 74 | |
| 100 | - $this->cache = null; | |
| 101 | - | |
| 102 | 75 | do_action( 'elementor/global_classes/update', $this->context, $updated_value, $current_value ); |
| 103 | 76 | } |
| 104 | 77 | |
| 105 | 78 | private function get_meta_key(): string { |
| @@ -107,8 +80,8 @@ | ||
| 107 | 80 | ? static::META_KEY_FRONTEND |
| 108 | 81 | : static::META_KEY_PREVIEW; |
| 109 | 82 | } |
| 110 | 83 | |
| 111 | - private function get_kit(): Kit { | |
| 112 | - return $this->kit ?? Plugin::$instance->kits_manager->get_active_kit(); | |
| 84 | + private function get_kit() { | |
| 85 | + return Plugin::$instance->kits_manager->get_active_kit(); | |
| 113 | 86 | } |
| 114 | 87 | } |