PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.3
Elementor Website Builder – more than just a page builder v4.2.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-post-ids.php

global-classes-post-ids.php in Elementor Website Builder – more than just a page builder 4.2.3, at modules/global-classes/global-classes-post-ids.php

172 lines 3.3 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\Utils\Kit_Utils;
8 use WP_Post;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Global_Classes_Post_IDs {
15 use Has_Kit_Dependency;
16
17 const META_KEY = '_elementor_global_classes_post_ids';
18
19 private ?array $cache = null;
20
21 public static function make( ?Kit $kit = null ): self {
22 $instance = new self();
23
24 if ( null !== $kit ) {
25 $instance->set_kit( $kit );
26 }
27
28 return $instance;
29 }
30
31 public function register_hooks(): void {
32 add_action( 'deleted_post', [ self::class, 'on_deleted_post' ], 10, 2 );
33 }
34
35 public static function on_deleted_post( int $post_id, WP_Post $post ): void {
36 if ( Global_Class_Post_Type::CPT !== $post->post_type ) {
37 return;
38 }
39
40 foreach ( Kit_Utils::get_all_kit_documents() as $kit ) {
41 self::make( $kit )->remove_post_id( $post_id );
42 }
43 }
44
45 public function get_post_id( string $class_id ): ?int {
46 $map = $this->read_map();
47
48 if ( ! isset( $map[ $class_id ] ) ) {
49 return null;
50 }
51
52 $post_id = (int) $map[ $class_id ];
53
54 if ( get_post( $post_id ) ) {
55 return $post_id;
56 }
57
58 $this->remove_post_id( $post_id );
59
60 return null;
61 }
62
63 public function get_post_ids( array $class_ids ): array {
64 if ( empty( $class_ids ) ) {
65 return [];
66 }
67
68 $map = $this->read_map();
69 $resolved = [];
70
71 foreach ( $class_ids as $class_id ) {
72 if ( ! isset( $map[ $class_id ] ) ) {
73 continue;
74 }
75
76 $post_id = (int) $map[ $class_id ];
77
78 if ( get_post( $post_id ) ) {
79 $resolved[ $class_id ] = $post_id;
80 } else {
81 $this->remove_post_id( $post_id );
82 }
83 }
84
85 return $resolved;
86 }
87
88 public function set( string $class_id, int $post_id ): void {
89 $this->set_many( [ $class_id => $post_id ] );
90 }
91
92 public function set_many( array $class_id_to_post_id ): void {
93 if ( empty( $class_id_to_post_id ) ) {
94 return;
95 }
96
97 $map = $this->read_map();
98 $changed = false;
99
100 foreach ( $class_id_to_post_id as $class_id => $post_id ) {
101 $post_id = (int) $post_id;
102
103 if ( ! is_string( $class_id ) || '' === $class_id || $post_id <= 0 ) {
104 continue;
105 }
106
107 if ( ! isset( $map[ $class_id ] ) || (int) $map[ $class_id ] !== $post_id ) {
108 $map[ $class_id ] = $post_id;
109 $changed = true;
110 }
111 }
112
113 if ( $changed ) {
114 $this->write_map( $map );
115 }
116 }
117
118 public function remove_class_id( string $class_id ): void {
119 $map = $this->read_map();
120
121 if ( ! isset( $map[ $class_id ] ) ) {
122 return;
123 }
124
125 unset( $map[ $class_id ] );
126 $this->write_map( $map );
127 }
128
129 public function remove_post_id( int $post_id ): void {
130 $map = $this->read_map();
131 $filtered = array_filter( $map, fn( $id ) => (int) $id !== $post_id );
132
133 if ( count( $filtered ) === count( $map ) ) {
134 return;
135 }
136
137 $this->write_map( $filtered );
138 }
139
140 private function read_map(): array {
141 if ( null !== $this->cache ) {
142 return $this->cache;
143 }
144
145 $kit = $this->get_kit();
146
147 if ( ! $kit ) {
148 $this->cache = [];
149
150 return [];
151 }
152
153 $raw = $kit->get_meta( self::META_KEY );
154 $this->cache = is_array( $raw ) ? $raw : [];
155
156 return $this->cache;
157 }
158
159 private function write_map( array $map ): bool {
160 $kit = $this->get_kit();
161
162 if ( ! $kit ) {
163 return false;
164 }
165
166 $result = $kit->update_meta( self::META_KEY, $map );
167 $this->cache = $map;
168
169 return false !== $result;
170 }
171 }
172