/var/www/html/wp-content/plugins/elementor/modules/global-classes/global-classes-repository.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Elementor\Modules\GlobalClasses;

use 
Elementor\Core\Kits\Documents\Kit;
use 
Elementor\Modules\AtomicWidgets\Parsers\Style_Parser;
use 
Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
use 
Elementor\Modules\AtomicWidgets\Styles\Utils as Atomic_Styles_Utils;
use 
Elementor\Plugin;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

class 
Global_Classes_Repository {
    const 
META_KEY '_elementor_global_classes';

    public static function 
make(): Global_Classes_Repository {
        return new 
self();
    }

    public function 
all() {
        
$all Plugin::$instance->kits_manager->get_active_kit()->get_json_metaself::META_KEY );

        return 
Global_Classes::make$all['items'] ?? [], $all['order'] ?? [] );
    }

    public function 
getstring $id ) {
        return 
$this->all()->get_items()->get$id );
    }

    public function 
deletestring $id ) {
        
$all $this->all();

        if ( ! isset( 
$all->get_items()[ $id ] ) ) {
            throw new 
\Exception"Global class with id ${id} not found" );
        }

        
Plugin::$instance->kits_manager->get_active_kit()->update_json_metaself::META_KEY, [
            
'items' => $all->get_items()->except( [ $id ] )->all(),
            
'order' => $all->get_order()->filter( fn( $item ) => $item !== $id )->all(),
        ] );
    }

    public function 
putstring $id, array $value ) {
        
$all $this->all();

        unset( 
$value['id'] );

        if ( ! isset( 
$all->get_items()[ $id ] ) ) {
            throw new 
\Exception"Global class with id {$id} not found" );
        }

        if ( 
$value === $all->get_items()[ $id ] ) {
            return 
$value;
        }

        
$value Plugin::$instance->kits_manager->get_active_kit()->update_json_metaself::META_KEY, [
            
'items' => $all->get_items()->merge( [ $id => $value ] )->all(),
            
'order' => $all->get_order()->all(),
        ] );

        if ( ! 
$value ) {
            throw new 
\Exception'Failed to update global class' );
        }

        return 
$this->get$id );
    }

    public function 
create( array $value ) {
        
$all $this->all();
        
$id $this->generate_global_class_id();

        
$value['id'] = $id;

        
$updated Plugin::$instance->kits_manager->get_active_kit()->update_json_metaself::META_KEY, [
            
'items' => $all->get_items()->merge( [ $id => $value ] )->all(),
            
'order' => $all->get_order()->push$id )->all(),
        ] );

        if ( ! 
$updated ) {
            throw new 
\Exception'Failed to create global class' );
        }

        return 
$this->get$id );
    }

    public function 
arrange( array $value ) {
        
$all $this->all();

        if ( 
$all->get_order()->all() === $value ) {
            return 
$value;
        }

        
$updated Plugin::$instance->kits_manager->get_active_kit()->update_json_metaself::META_KEY, [
            
'items' => $all->get_items()->all(),
            
'order' => $value,
        ] );

        if ( ! 
$updated ) {
            throw new 
\Exception'Failed to arrange global classes' );
        }

        return 
$this->all()->get_order()->all();
    }

    private function 
generate_global_class_id() {
        
$existing_ids $this->all()->get_items()->keys();
        
$kit_id Plugin::$instance->kits_manager->get_active_kit()->get_id();

        return 
Atomic_Styles_Utils::generate_id'g-' $kit_id '-'$existing_ids );
    }
}