PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / Managers / ConditionManager.php

ConditionManager.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.1.0, at includes/Builder/Managers/ConditionManager.php

311 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder\Managers;
4
5 use Templately\Builder\Conditions\Condition;
6 use Templately\Builder\ThemeBuilder;
7 use Templately\Builder\Types\ThemeTemplate;
8
9 class ConditionManager {
10 const NONCE_KEY = 'templately_ajax_nonce';
11 const META_KEY = '_templately_conditions';
12
13 private $cache;
14
15 private $locations = [];
16
17 /**
18 * @var Condition[]
19 */
20 private $conditions = [];
21
22 /**
23 * @var ThemeBuilder
24 */
25 private $builder;
26
27 private $location_cache = [];
28
29 public function __construct( $builder ) {
30 $this->cache = new Cache();
31 $this->builder = $builder;
32
33 add_action( 'wp_loaded', [ $this, 'register' ] );
34 add_action( 'wp_ajax_templately_conditions', [ $this, 'get_conditions' ] );
35 add_action( 'wp_ajax_templately_save_conditions', [ $this, 'save_conditions' ] );
36 add_action( 'wp_ajax_templately_check_conditions', [ $this, 'check_conditions' ] );
37 }
38
39 public function get_conditions() {
40 $config = [];
41
42 foreach ( $this->conditions as $condition ) {
43 $config[ $condition->get_name() ] = $condition->get_config();
44 }
45
46 // throw new \Exception('Something went wrong');
47
48 wp_send_json( $config );
49 }
50
51 public function get_conditions_for_display( $template_id = null ): array {
52 if ( ! is_numeric( $template_id ) ) {
53 $template_id = null;
54 }
55
56 $config = [];
57 foreach ( $this->conditions as $condition ) {
58 $config[ $condition->get_name() ] = $condition->get_config();
59 }
60
61 if ( ! $template_id ) {
62 return [
63 'config' => $config
64 ];
65 }
66
67 $template = $this->builder::$templates_manager->get( $template_id );
68 $conditions_meta = $template->get_meta( self::META_KEY );
69
70 $conditions_for_display = [];
71
72 if ( is_array( $conditions_meta ) && ! empty( $conditions_meta ) ) {
73 foreach ( $conditions_meta as $c ) {
74 $conditions_for_display[] = $this->parse_condition( $c );
75 }
76 }
77
78
79 return [
80 'config' => $config,
81 'template_conditions' => $conditions_for_display
82 ];
83 }
84
85 private function verify_nonce(): bool {
86 return true;
87 // return isset( $_REQUEST['_nonce'] ) && wp_verify_nonce( $_REQUEST['_nonce'], self::NONCE_KEY );
88 }
89
90 public function save_conditions( $post_id, $conditions = [] ): bool {
91 $conditions_to_save = [];
92 foreach ( $conditions as $condition ) {
93 if ( is_string( $condition ) ) {
94 $conditions_to_save[] = rtrim( $condition, '/' );
95 continue;
96 }
97
98 unset( $condition['id'] );
99 $conditions_to_save[] = rtrim( implode( '/', $condition ), '/' );
100 }
101
102 /**
103 * @var ThemeTemplate $template
104 */
105 $template = $this->builder->get_template( $post_id );
106
107 if ( empty( $conditions_to_save ) ) {
108 $template->delete_meta( self::META_KEY );
109 $this->cache->remove( $post_id )->save();
110 } else {
111 $template->update_meta( self::META_KEY, $conditions );
112 $this->cache->add( $template, $conditions )->save();
113 }
114
115 $this->cache->regenerate();
116
117 return true;
118 }
119
120 public function check_conditions() {
121 if ( ! $this->verify_nonce() ) {
122 wp_send_json_error( [ 'Unauthorized to take action.' ], 401 );
123 }
124 }
125
126 public function register() {
127 $this->register_condition( 'general' );
128 }
129
130 public function register_condition( string $id, $args = [] ) {
131 if ( isset( $this->conditions[ $id ] ) ) {
132 return;
133 }
134
135 // if ( strpos( $id, '_' ) !== false ) {
136 // $id = implode( '', array_map( 'ucfirst', explode( '_', $id ) ) );
137 // }
138
139 $class_name = '\\Templately\\Builder\\Conditions\\' . ucfirst( $id );
140 /** @var Condition $condition */
141 $condition = new $class_name( $args );
142 $this->register_condition_instance( $condition );
143
144 foreach ( $condition->get_sub_conditions() as $key => $_sub_id ) {
145 if ( is_numeric( $key ) ) {
146 $id = $_sub_id;
147 $args = [];
148 } else {
149 $id = $key;
150 $args = $_sub_id;
151 }
152 $this->register_condition( $_sub_id, $args );
153 }
154 }
155
156 /**
157 * @param Condition $object
158 */
159 public function register_condition_instance( Condition $object ) {
160 $this->conditions[ $object->get_name() ] = $object;
161 }
162
163 public function get_templates_by_location( $location ): array {
164 if ( isset( $this->location_cache[ $location ] ) ) {
165 return $this->location_cache[ $location ];
166 }
167
168 $template_ids = $this->get_template_ids( $location );
169
170 // dump( $location, $template_ids );
171
172 $templates = [];
173
174 $location_settings = templately()->theme_builder::$location_manager->get_location( $location );
175
176 if ( ! empty( $template_ids ) ) {
177 foreach ( $template_ids as $template_id => $priority ) {
178 $template = $this->builder::$templates_manager->get( $template_id );
179
180 if ( $template ) {
181 $templates[ $template_id ] = $template;
182 }
183
184 if ( empty( $location_settings['multiple'] ) ) {
185 break;
186 }
187 }
188 }
189
190 $this->location_cache[ $location ] = $templates;
191
192 return $templates;
193 }
194
195 private function get_template_ids( $location ): array {
196 $current_post_id = get_the_ID();
197 $template = $this->builder->get_template( $current_post_id );
198 if ( $template && $location === $template->get_location() ) {
199 return [ $current_post_id => 1 ];
200 }
201
202 return $this->get_location_templates( $location );
203 }
204
205 public function get_location_templates( string $location ): array {
206 $conditions_priority = [];
207 $conditions_groups = $this->cache->get_by_location( $location );
208
209 if ( empty( $conditions_groups ) ) {
210 return $conditions_priority;
211 }
212
213
214 $excludes = [];
215
216 // dump( $conditions_groups );
217
218 foreach ( $conditions_groups as $template_id => $conditions ) {
219 foreach ( $conditions as $condition ) {
220 $origin_c = $condition;
221
222 extract( $this->parse_condition( $condition ) );
223
224 $is_include = $type === 'include';
225 $condition = $this->get_condition( $name );
226
227 if ( ! $condition ) {
228 continue;
229 }
230
231 $is_passed = $condition->check();
232 $sub_condition = null;
233
234
235 if ( $is_passed && $sub_name ) {
236 $sub_condition = $this->get_condition( $sub_name );
237
238
239 if ( ! $sub_condition ) {
240 continue;
241 }
242
243 $is_passed = $sub_condition->check( [ 'id' => $sub_id ] );
244 }
245
246 if ( $is_passed ) {
247 $post_status = get_post_status( $template_id );
248
249 if ( $post_status !== 'publish' ) {
250 continue;
251 }
252
253 if ( $is_include ) {
254 $conditions_priority[ $template_id ] = $this->get_priority( $condition, $sub_condition, $sub_id );
255 } else {
256 $excludes[] = $template_id;
257 }
258 }
259 }
260 }
261
262 foreach ( $excludes as $exclude_id ) {
263 unset( $conditions_priority[ $exclude_id ] );
264 }
265
266 asort( $conditions_priority );
267
268 return $conditions_priority;
269 }
270
271 private function get_condition( $name ) {
272 return $this->conditions[ $name ] ?? null;
273 }
274
275 /**
276 * @param $condition
277 * @param $sub_condition
278 * @param $sub_id
279 *
280 * @return int|mixed
281 */
282 private function get_priority( $condition, $sub_condition, $sub_id ) {
283 $priority = $condition->get_priority();
284
285 if ( $sub_condition ) {
286 if ( $sub_condition->get_priority() < $priority ) {
287 $priority = $sub_condition->get_priority();
288 }
289
290 $priority -= 10;
291
292 if ( $sub_id ) {
293 $priority -= 10;
294 } elseif ( 0 === count( $sub_condition->get_sub_conditions() ) ) {
295 $priority -= 5;
296 }
297 }
298
299 return $priority;
300 }
301
302 protected function parse_condition( $condition ): array {
303 if ( is_string( $condition ) ) {
304 list( $type, $name, $sub_name, $sub_id ) = array_pad( explode( '/', $condition ), 4, '' );
305 $condition = compact( 'type', 'name', 'sub_name', 'sub_id' );
306 }
307
308 return $condition;
309 }
310
311 }