PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-beta2
Elementor Website Builder – more than just a page builder v4.1.0-beta2
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-labels.php

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

108 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\GlobalClasses;
4
5 use Elementor\Core\Kits\Documents\Kit;
6 use Elementor\Modules\GlobalClasses\Concerns\Has_Kit_Dependency;
7 use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 class Global_Classes_Labels {
14 use Has_Kit_Dependency;
15 use Has_Preview_Context;
16
17 const META_KEY_FRONTEND = '_elementor_global_classes_labels';
18 const META_KEY_PREVIEW = '_elementor_global_classes_labels_preview';
19
20 const META_KEY = self::META_KEY_FRONTEND;
21
22 protected array $context_keys = [
23 'labels' => [
24 'frontend' => self::META_KEY_FRONTEND,
25 'preview' => self::META_KEY_PREVIEW,
26 ],
27 ];
28
29 private ?array $cache = null;
30
31 private function __construct() {
32 }
33
34 public static function make( Kit $kit ): self {
35 return ( new self() )->set_kit( $kit );
36 }
37
38 protected function on_preview_change(): void {
39 $this->cache = null;
40 }
41
42 public function get_labels(): array {
43 $stored = $this->read_stored();
44
45 if ( empty( $stored ) ) {
46 return [];
47 }
48
49 return $stored;
50 }
51
52 public function get_ordered_labels(): array {
53 $order = Global_Classes_Order::make( $this->get_kit() )->get_order();
54 $map = $this->get_labels();
55
56 if ( $this->is_preview() ) {
57 $frontend_map = self::make( $this->get_kit() )->get_labels();
58 foreach ( $order as $id ) {
59 if ( ! isset( $map[ $id ] ) && isset( $frontend_map[ $id ] ) ) {
60 $map[ $id ] = $frontend_map[ $id ];
61 }
62 }
63 }
64
65 $result = [];
66
67 foreach ( $order as $id ) {
68 if ( isset( $map[ $id ] ) ) {
69 $result[ $id ] = $map[ $id ];
70 }
71 }
72
73 return $result;
74 }
75
76 public function set_labels( array $id_to_label ): bool {
77 $kit = $this->get_kit();
78
79 if ( ! $kit ) {
80 return false;
81 }
82
83 $result = $kit->update_meta( $this->get_context_key( 'labels' ), $id_to_label );
84 $this->cache = $id_to_label;
85
86 return false !== $result;
87 }
88
89 private function read_stored(): array {
90 if ( null !== $this->cache ) {
91 return $this->cache;
92 }
93
94 $kit = $this->get_kit();
95
96 if ( ! $kit ) {
97 $this->cache = [];
98
99 return [];
100 }
101
102 $raw = $kit->get_meta( $this->get_context_key( 'labels' ) );
103 $this->cache = is_array( $raw ) ? $raw : [];
104
105 return $this->cache;
106 }
107 }
108