PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.2
Elementor Website Builder – more than just a page builder v4.0.2
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / global-classes / global-classes-repository.php

global-classes-repository.php in Elementor Website Builder – more than just a page builder 4.0.2, at modules/global-classes/global-classes-repository.php

112 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\GlobalClasses;
3
4 use Elementor\Core\Kits\Documents\Kit;
5 use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Global_Classes_Repository {
13
14 const META_KEY_FRONTEND = '_elementor_global_classes';
15 const META_KEY_PREVIEW = '_elementor_global_classes_preview';
16
17 const CONTEXT_FRONTEND = 'frontend';
18 const CONTEXT_PREVIEW = 'preview';
19
20 private string $context = self::CONTEXT_FRONTEND;
21
22 private ?Global_Classes $cache = null;
23
24 private ?Kit $kit = null;
25
26 public function __construct( ?Kit $kit = null ) {
27 $this->kit = $kit;
28 }
29
30 public static function make( ?Kit $kit = null ): Global_Classes_Repository {
31 return new self( $kit );
32 }
33
34 public function context( string $context ): self {
35 $this->context = $context;
36 $this->cache = null;
37
38 return $this;
39 }
40
41 public function all( bool $force = false ): Global_Classes {
42 if ( ! $force && null !== $this->cache ) {
43 return $this->cache;
44 }
45
46 $meta_key = $this->get_meta_key();
47 $kit = $this->get_kit();
48 $all = $kit->get_json_meta( $meta_key );
49
50 $is_preview = static::META_KEY_PREVIEW === $meta_key;
51 $is_empty = empty( $all );
52
53 if ( $is_preview && $is_empty ) {
54 $all = $kit->get_json_meta( static::META_KEY_FRONTEND );
55 }
56
57 Migrations_Orchestrator::make()->migrate(
58 $all,
59 $kit->get_id(),
60 $meta_key,
61 function( $migrated_data ) use ( $kit, $meta_key ) {
62 $kit->update_json_meta( $meta_key, $migrated_data );
63 }
64 );
65
66 $this->cache = Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] );
67
68 return $this->cache;
69 }
70
71 public function put( array $items, array $order ) {
72 $current_value = $this->all()->get();
73
74 $updated_value = [
75 'items' => $items,
76 'order' => $order,
77 ];
78
79 // `update_metadata` fails for identical data, so we skip it.
80 if ( $current_value === $updated_value ) {
81 return;
82 }
83
84 $meta_key = $this->get_meta_key();
85 $value = $this->get_kit()->update_json_meta( $meta_key, $updated_value );
86
87 $should_delete_preview = static::META_KEY_FRONTEND === $meta_key;
88
89 if ( $should_delete_preview ) {
90 $this->get_kit()->delete_meta( static::META_KEY_PREVIEW );
91 }
92
93 if ( ! $value ) {
94 throw new \Exception( 'Failed to update global classes' );
95 }
96
97 $this->cache = null;
98
99 do_action( 'elementor/global_classes/update', $this->context, $updated_value, $current_value );
100 }
101
102 private function get_meta_key(): string {
103 return static::CONTEXT_FRONTEND === $this->context
104 ? static::META_KEY_FRONTEND
105 : static::META_KEY_PREVIEW;
106 }
107
108 private function get_kit(): Kit {
109 return $this->kit ?? Plugin::$instance->kits_manager->get_active_kit();
110 }
111 }
112