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

113 lines 3.0 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\Parsers\Style_Parser;
6 use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
7 use Elementor\Modules\AtomicWidgets\Styles\Utils as Atomic_Styles_Utils;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Global_Classes_Repository {
15 const META_KEY = '_elementor_global_classes';
16
17 public static function make(): Global_Classes_Repository {
18 return new self();
19 }
20
21 public function all() {
22 $all = Plugin::$instance->kits_manager->get_active_kit()->get_json_meta( self::META_KEY );
23
24 return Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] );
25 }
26
27 public function get( string $id ) {
28 return $this->all()->get_items()->get( $id );
29 }
30
31 public function delete( string $id ) {
32 $all = $this->all();
33
34 if ( ! isset( $all->get_items()[ $id ] ) ) {
35 throw new \Exception( "Global class with id ${id} not found" );
36 }
37
38 Plugin::$instance->kits_manager->get_active_kit()->update_json_meta( self::META_KEY, [
39 'items' => $all->get_items()->except( [ $id ] )->all(),
40 'order' => $all->get_order()->filter( fn( $item ) => $item !== $id )->all(),
41 ] );
42 }
43
44 public function put( string $id, array $value ) {
45 $all = $this->all();
46
47 unset( $value['id'] );
48
49 if ( ! isset( $all->get_items()[ $id ] ) ) {
50 throw new \Exception( "Global class with id {$id} not found" );
51 }
52
53 if ( $value === $all->get_items()[ $id ] ) {
54 return $value;
55 }
56
57 $value = Plugin::$instance->kits_manager->get_active_kit()->update_json_meta( self::META_KEY, [
58 'items' => $all->get_items()->merge( [ $id => $value ] )->all(),
59 'order' => $all->get_order()->all(),
60 ] );
61
62 if ( ! $value ) {
63 throw new \Exception( 'Failed to update global class' );
64 }
65
66 return $this->get( $id );
67 }
68
69 public function create( array $value ) {
70 $all = $this->all();
71 $id = $this->generate_global_class_id();
72
73 $value['id'] = $id;
74
75 $updated = Plugin::$instance->kits_manager->get_active_kit()->update_json_meta( self::META_KEY, [
76 'items' => $all->get_items()->merge( [ $id => $value ] )->all(),
77 'order' => $all->get_order()->push( $id )->all(),
78 ] );
79
80 if ( ! $updated ) {
81 throw new \Exception( 'Failed to create global class' );
82 }
83
84 return $this->get( $id );
85 }
86
87 public function arrange( array $value ) {
88 $all = $this->all();
89
90 if ( $all->get_order()->all() === $value ) {
91 return $value;
92 }
93
94 $updated = Plugin::$instance->kits_manager->get_active_kit()->update_json_meta( self::META_KEY, [
95 'items' => $all->get_items()->all(),
96 'order' => $value,
97 ] );
98
99 if ( ! $updated ) {
100 throw new \Exception( 'Failed to arrange global classes' );
101 }
102
103 return $this->all()->get_order()->all();
104 }
105
106 private function generate_global_class_id() {
107 $existing_ids = $this->all()->get_items()->keys();
108 $kit_id = Plugin::$instance->kits_manager->get_active_kit()->get_id();
109
110 return Atomic_Styles_Utils::generate_id( 'g-' . $kit_id . '-', $existing_ids );
111 }
112 }
113