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

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