PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / addons / theme-builder / helper.php

helper.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at addons/theme-builder/helper.php

475 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocksThemeBuilder;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 class Helper {
9 private static $current_page_type = null;
10
11 protected static $current_page_data = [];
12
13 public static function get_template_id( $type ) {
14 $option = [
15 'location' => 'ablocks_tb_template_display_locations',
16 'exclusion' => 'ablocks_tb_template_not_display_locations',
17 'users' => 'ablocks_tb_template_target_user_roles',
18 ];
19
20 $ablocks_templates = self::get_posts_by_conditions( 'ablocks_tb', $option );
21 foreach ( $ablocks_templates as $template ) {
22 if ( get_post_meta( absint( $template['id'] ), 'ablocks_tb_template_type', true ) === $type ) {
23 if ( function_exists( 'pll_current_language' ) ) {
24 if ( pll_current_language( 'slug' ) === pll_get_post_language( $template['id'], 'slug' ) ) {
25 return $template['id'];
26 }
27 } else {
28 return $template['id'];
29 }
30 }
31 }
32
33 return '';
34 }
35
36 public static function get_posts_by_conditions( $post_type, $option ) {
37 global $wpdb;
38 global $post;
39
40 $post_type = $post_type ? esc_sql( $post_type ) : esc_sql( $post->post_type );
41
42 if ( is_array( self::$current_page_data ) && isset( self::$current_page_data[ $post_type ] ) ) {
43 return self::$current_page_data[ $post_type ];
44 }
45
46 $current_page_type = self::get_current_page_type();
47
48 self::$current_page_data[ $post_type ] = [];
49
50 $option['current_post_id'] = get_the_ID();
51 $meta_header = self::get_meta_option_post( $post_type, $option );
52
53 if ( false === $meta_header ) {
54 $current_post_type = esc_sql( get_post_type() );
55 $current_post_id = false;
56 $q_obj = \get_queried_object();
57 $current_id = esc_sql( get_the_ID() );
58
59 if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
60 $default_lang = apply_filters( 'wpml_default_language', '' );
61 $current_lang = apply_filters( 'wpml_current_language', '' );
62
63 if ( $default_lang !== $current_lang ) {
64 $current_post_type = get_post_type( $current_id );
65 $current_id = apply_filters( 'wpml_object_id', $current_id, $current_post_type, true, $default_lang );
66 }
67 }
68
69 $location = isset( $option['location'] ) ? $option['location'] : '';
70 $location = esc_sql( $location );
71
72 $query = "
73 SELECT p.ID, pm.meta_value
74 FROM {$wpdb->postmeta} AS pm
75 INNER JOIN {$wpdb->posts} AS p ON pm.post_id = p.ID
76 WHERE pm.meta_key = %s
77 AND p.post_type = %s
78 AND p.post_status = 'publish'
79 ";
80
81 $meta_conditions = [
82 "pm.meta_value LIKE '%s:5:\"value\";s:12:\"basic-global\";%'"
83 ];
84
85 switch ( $current_page_type ) {
86 case 'is_404':
87 $meta_conditions[] = "pm.meta_value LIKE '%\"special-404\"%'";
88 break;
89 case 'is_search':
90 $meta_conditions[] = "pm.meta_value LIKE '%\"special-search\"%'";
91 break;
92 case 'is_archive':
93 case 'is_tax':
94 case 'is_date':
95 case 'is_author':
96 $meta_conditions[] = "pm.meta_value LIKE '%\"basic-archives\"%'";
97 $meta_conditions[] = $wpdb->prepare(
98 'pm.meta_value LIKE %s',
99 "%\"{$current_post_type}|all|archive\"%"
100 );
101
102 if ( 'is_tax' === $current_page_type && is_object( $q_obj ) ) {
103 $meta_conditions[] = $wpdb->prepare(
104 'pm.meta_value LIKE %s',
105 "%\"{$current_post_type}|all|taxarchive|{$q_obj->taxonomy}\"%"
106 );
107 $meta_conditions[] = $wpdb->prepare(
108 'pm.meta_value LIKE %s',
109 "%\"tax-{$q_obj->term_id}\"%"
110 );
111 } elseif ( 'is_date' === $current_page_type ) {
112 $meta_conditions[] = "pm.meta_value LIKE '%\"special-date\"%'";
113 } elseif ( 'is_author' === $current_page_type ) {
114 $meta_conditions[] = "pm.meta_value LIKE '%\"special-author\"%'";
115 }
116 break;
117 case 'is_home':
118 $meta_conditions[] = "pm.meta_value LIKE '%\"special-blog\"%'";
119 break;
120 case 'is_front_page':
121 $current_post_id = $current_id;
122 $meta_conditions[] = "pm.meta_value LIKE '%\"special-front\"%'";
123 $meta_conditions[] = $wpdb->prepare(
124 'pm.meta_value LIKE %s',
125 "%\"{$current_post_type}|all\"%"
126 );
127 $meta_conditions[] = $wpdb->prepare(
128 'pm.meta_value LIKE %s',
129 "%\"post-{$current_id}\"%"
130 );
131 break;
132 case 'is_singular':
133 $current_post_id = $current_id;
134 $meta_conditions[] = "pm.meta_value LIKE '%\"basic-singulars\"%'";
135 $meta_conditions[] = $wpdb->prepare(
136 'pm.meta_value LIKE %s',
137 "%\"{$current_post_type}|all\"%"
138 );
139 $meta_conditions[] = $wpdb->prepare(
140 'pm.meta_value LIKE %s',
141 "%\"post-{$current_id}\"%"
142 );
143
144 $taxonomies = get_object_taxonomies( $q_obj->post_type );
145 $terms = wp_get_post_terms( $q_obj->ID, $taxonomies );
146
147 foreach ( $terms as $term ) {
148 $meta_conditions[] = $wpdb->prepare(
149 'pm.meta_value LIKE %s',
150 "%\"tax-{$term->term_id}-single-{$term->taxonomy}\"%"
151 );
152 }
153 break;
154 case 'is_woo_shop_page':
155 $meta_conditions[] = "pm.meta_value LIKE '%\"special-woo-shop\"%'";
156 break;
157 case '':
158 $current_post_id = $current_id;
159 break;
160 }//end switch
161
162 $meta_where = implode( ' OR ', $meta_conditions );
163 $query = $wpdb->prepare( $query, $location, $post_type ) . " AND ({$meta_where}) ORDER BY p.post_date DESC";
164
165 $posts = $wpdb->get_results( $query );
166
167 foreach ( $posts as $local_post ) {
168 self::$current_page_data[ $post_type ][ $local_post->ID ] = [
169 'id' => $local_post->ID,
170 'location' => unserialize( $local_post->meta_value ),
171 ];
172 }
173
174 $option['current_post_id'] = $current_post_id;
175
176 self::remove_exclusion_rule_posts( $post_type, $option );
177 self::remove_user_rule_posts( $post_type, $option );
178 }//end if
179
180 return self::$current_page_data[ $post_type ];
181 }
182
183 public static function remove_exclusion_rule_posts( $post_type, $option ) {
184 $exclusion = isset( $option['exclusion'] ) ? $option['exclusion'] : '';
185 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
186
187 foreach ( self::$current_page_data[ $post_type ] as $c_post_id => $c_data ) {
188 $exclusion_rules = get_post_meta( $c_post_id, $exclusion, true );
189 $is_exclude = self::parse_layout_display_condition( $current_post_id, $exclusion_rules );
190
191 if ( $is_exclude ) {
192 unset( self::$current_page_data[ $post_type ][ $c_post_id ] );
193 }
194 }
195 }
196
197 public static function remove_user_rule_posts( $post_type, $option ) {
198 $users = isset( $option['users'] ) ? $option['users'] : '';
199 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
200
201 foreach ( self::$current_page_data[ $post_type ] as $c_post_id => $c_data ) {
202 $user_rules = get_post_meta( $c_post_id, $users, true );
203 $is_user = self::parse_user_role_condition( $current_post_id, $user_rules );
204
205 if ( ! $is_user ) {
206 unset( self::$current_page_data[ $post_type ][ $c_post_id ] );
207 }
208 }
209 }
210
211 public static function get_current_page_type() {
212 if ( null === self::$current_page_type ) {
213 $page_type = '';
214 $current_id = false;
215
216 if ( is_404() ) {
217 $page_type = 'is_404';
218 } elseif ( is_search() ) {
219 $page_type = 'is_search';
220 } elseif ( is_archive() ) {
221 $page_type = 'is_archive';
222
223 if ( is_category() || is_tag() || is_tax() ) {
224 $page_type = 'is_tax';
225 } elseif ( is_date() ) {
226 $page_type = 'is_date';
227 } elseif ( is_author() ) {
228 $page_type = 'is_author';
229 } elseif ( function_exists( 'is_shop' ) && is_shop() ) {
230 $page_type = 'is_woo_shop_page';
231 }
232 } elseif ( is_home() ) {
233 $page_type = 'is_home';
234 } elseif ( is_front_page() ) {
235 $page_type = 'is_front_page';
236 $current_id = get_the_id();
237 } elseif ( is_singular() ) {
238 $page_type = 'is_singular';
239 $current_id = get_the_id();
240 } else {
241 $current_id = get_the_id();
242 }//end if
243
244 self::$current_page_data['ID'] = $current_id;
245 self::$current_page_type = $page_type;
246 }//end if
247
248 return self::$current_page_type;
249 }
250
251 public static function get_meta_option_post( $post_type, $option ) {
252 $page_meta = ( isset( $option['page_meta'] ) && '' != $option['page_meta'] ) ? $option['page_meta'] : false;
253
254 if ( false !== $page_meta ) {
255 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
256 $meta_id = get_post_meta( $current_post_id, $option['page_meta'], true );
257
258 if ( false !== $meta_id && '' != $meta_id ) {
259 self::$current_page_data[ $post_type ][ $meta_id ] = array(
260 'id' => $meta_id,
261 'location' => '',
262 );
263
264 return self::$current_page_data[ $post_type ];
265 }
266 }
267
268 return false;
269 }
270
271 public static function parse_layout_display_condition( $post_id, $rules ) {
272 $display = false;
273 $current_post_type = get_post_type( $post_id );
274 if ( is_array( $rules ) && count( $rules ) ) {
275 foreach ( $rules as $key => $rule_item ) {
276 $rule = $rule_item['value'];
277 if ( strrpos( $rule, 'all' ) !== false ) {
278 $rule_case = 'all';
279 } else {
280 $rule_case = $rule;
281 }
282
283 switch ( $rule_case ) {
284 case 'basic-global':
285 $display = true;
286 break;
287
288 case 'basic-singulars':
289 if ( is_singular() ) {
290 $display = true;
291 }
292 break;
293
294 case 'basic-archives':
295 if ( is_archive() ) {
296 $display = true;
297 }
298 break;
299
300 case 'special-404':
301 if ( is_404() ) {
302 $display = true;
303 }
304 break;
305
306 case 'special-search':
307 if ( is_search() ) {
308 $display = true;
309 }
310 break;
311
312 case 'special-blog':
313 if ( is_home() ) {
314 $display = true;
315 }
316 break;
317
318 case 'special-front':
319 if ( is_front_page() ) {
320 $display = true;
321 }
322 break;
323
324 case 'special-date':
325 if ( is_date() ) {
326 $display = true;
327 }
328 break;
329
330 case 'special-author':
331 if ( is_author() ) {
332 $display = true;
333 }
334 break;
335
336 case 'special-woo-shop':
337 if ( function_exists( 'is_shop' ) && is_shop() ) {
338 $display = true;
339 }
340 break;
341
342 case 'all':
343 $rule_data = explode( '|', $rule );
344
345 $post_type = isset( $rule_data[0] ) ? $rule_data[0] : false;
346 $archieve_type = isset( $rule_data[2] ) ? $rule_data[2] : false;
347 $taxonomy = isset( $rule_data[3] ) ? $rule_data[3] : false;
348 if ( false === $archieve_type ) {
349 $current_post_type = get_post_type( $post_id );
350
351 if ( false !== $post_id && $current_post_type == $post_type ) {
352 $display = true;
353 }
354 } else {
355 if ( is_archive() ) {
356 $current_post_type = get_post_type();
357 if ( $current_post_type == $post_type ) {
358 if ( 'archive' == $archieve_type ) {
359 $display = true;
360 } elseif ( 'taxarchive' == $archieve_type ) {
361 $obj = \get_queried_object();
362 $current_taxonomy = '';
363 if ( '' !== $obj && null !== $obj ) {
364 $current_taxonomy = $obj->taxonomy;
365 }
366
367 if ( $current_taxonomy == $taxonomy ) {
368 $display = true;
369 }
370 }
371 }
372 }
373 }//end if
374 break;
375
376 case 'specifics':
377 if ( isset( $rule_item['specific'] ) && is_array( $rule_item['specific'] ) ) {
378 foreach ( $rule_item['specific'] as $specific_page ) {
379 $specific_page = $specific_page['value'];
380 $specific_data = explode( '-', $specific_page );
381
382 $specific_post_type = isset( $specific_data[0] ) ? $specific_data[0] : false;
383 $specific_post_id = isset( $specific_data[1] ) ? $specific_data[1] : false;
384 if ( 'post' == $specific_post_type ) {
385 if ( $specific_post_id == $post_id ) {
386 $display = true;
387 }
388 } elseif ( isset( $specific_data[2] ) && ( 'single' == $specific_data[2] ) && 'tax' == $specific_post_type ) {
389 if ( is_singular() ) {
390 $term_details = get_term( $specific_post_id );
391
392 if ( isset( $term_details->taxonomy ) ) {
393 $has_term = has_term( (int) $specific_post_id, $term_details->taxonomy, $post_id );
394
395 if ( $has_term ) {
396 $display = true;
397 }
398 }
399 }
400 } elseif ( 'tax' == $specific_post_type ) {
401 $tax_id = get_queried_object_id();
402 if ( $specific_post_id == $tax_id ) {
403 $display = true;
404 }
405 }//end if
406 }//end foreach
407 }//end if
408 break;
409
410 default:
411 break;
412 }//end switch
413
414 if ( $display ) {
415 break;
416 }
417 }//end foreach
418 }//end if
419
420 return $display;
421 }
422
423 public static function parse_user_role_condition( $post_id, $rules ) {
424 $show_popup = true;
425
426 if ( is_array( $rules ) && count( $rules ) ) {
427 $show_popup = false;
428
429 foreach ( $rules as $i => $rule ) {
430 if ( ! isset( $rule['value'] ) ) {
431 continue;
432 }
433 $rule = $rule['value'];
434 switch ( $rule ) {
435 case '':
436 case 'all':
437 $show_popup = true;
438 break;
439
440 case 'logged-in':
441 if ( is_user_logged_in() ) {
442 $show_popup = true;
443 }
444 break;
445
446 case 'logged-out':
447 if ( ! is_user_logged_in() ) {
448 $show_popup = true;
449 }
450 break;
451
452 default:
453 if ( is_user_logged_in() ) {
454 $current_user = wp_get_current_user();
455
456 if ( isset( $current_user->roles )
457 && is_array( $current_user->roles )
458 && in_array( $rule, $current_user->roles )
459 ) {
460 $show_popup = true;
461 }
462 }
463 break;
464 }//end switch
465
466 if ( $show_popup ) {
467 break;
468 }
469 }//end foreach
470 }//end if
471
472 return $show_popup;
473 }
474 }
475