PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev3
Elementor Website Builder – more than just a page builder v4.0.0-dev3
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 4.0.7 All 451 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.0-dev3, at modules/global-classes/global-classes-repository.php

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