PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.3
Elementor Website Builder – more than just a page builder v4.0.3
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.3, at modules/global-classes/global-classes-repository.php

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