PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.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.13.1, at addons/theme-builder/helper.php

492 lines 14.0 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 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
164 $query = $wpdb->prepare( $query, $location, $post_type ) . " AND ({$meta_where}) ORDER BY p.post_date DESC";
165
166 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
167 $posts = $wpdb->get_results( $query );
168
169 foreach ( $posts as $local_post ) {
170 self::$current_page_data[ $post_type ][ $local_post->ID ] = [
171 'id' => $local_post->ID,
172 'location' => unserialize( $local_post->meta_value ),
173 ];
174 }
175
176 $option['current_post_id'] = $current_post_id;
177
178 self::remove_exclusion_rule_posts( $post_type, $option );
179 self::remove_user_rule_posts( $post_type, $option );
180 }//end if
181
182 return self::$current_page_data[ $post_type ];
183 }
184
185 public static function remove_exclusion_rule_posts( $post_type, $option ) {
186 $exclusion = isset( $option['exclusion'] ) ? $option['exclusion'] : '';
187 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
188
189 foreach ( self::$current_page_data[ $post_type ] as $c_post_id => $c_data ) {
190 $exclusion_rules = get_post_meta( $c_post_id, $exclusion, true );
191 $is_exclude = self::parse_layout_display_condition( $current_post_id, $exclusion_rules );
192
193 if ( $is_exclude ) {
194 unset( self::$current_page_data[ $post_type ][ $c_post_id ] );
195 }
196 }
197 }
198
199 public static function remove_user_rule_posts( $post_type, $option ) {
200 $users = isset( $option['users'] ) ? $option['users'] : '';
201 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
202
203 foreach ( self::$current_page_data[ $post_type ] as $c_post_id => $c_data ) {
204 $user_rules = get_post_meta( $c_post_id, $users, true );
205 $is_user = self::parse_user_role_condition( $current_post_id, $user_rules );
206
207 if ( ! $is_user ) {
208 unset( self::$current_page_data[ $post_type ][ $c_post_id ] );
209 }
210 }
211 }
212
213 public static function get_current_page_type() {
214 if ( null === self::$current_page_type ) {
215 $page_type = '';
216 $current_id = false;
217
218 if ( is_404() ) {
219 $page_type = 'is_404';
220 } elseif ( is_search() ) {
221 $page_type = 'is_search';
222 } elseif ( is_archive() ) {
223 $page_type = 'is_archive';
224
225 if ( is_category() || is_tag() || is_tax() ) {
226 $page_type = 'is_tax';
227 } elseif ( is_date() ) {
228 $page_type = 'is_date';
229 } elseif ( is_author() ) {
230 $page_type = 'is_author';
231 } elseif ( function_exists( 'is_shop' ) && is_shop() ) {
232 $page_type = 'is_woo_shop_page';
233 }
234 } elseif ( is_home() ) {
235 $page_type = 'is_home';
236 } elseif ( is_front_page() ) {
237 $page_type = 'is_front_page';
238 $current_id = get_the_id();
239 } elseif ( is_singular() ) {
240 $page_type = 'is_singular';
241 $current_id = get_the_id();
242 } else {
243 $current_id = get_the_id();
244 }//end if
245
246 self::$current_page_data['ID'] = $current_id;
247 self::$current_page_type = $page_type;
248 }//end if
249
250 return self::$current_page_type;
251 }
252
253 public static function get_meta_option_post( $post_type, $option ) {
254 $page_meta = ( isset( $option['page_meta'] ) && '' != $option['page_meta'] ) ? $option['page_meta'] : false;
255
256 if ( false !== $page_meta ) {
257 $current_post_id = isset( $option['current_post_id'] ) ? $option['current_post_id'] : false;
258 $meta_id = get_post_meta( $current_post_id, $option['page_meta'], true );
259
260 if ( false !== $meta_id && '' != $meta_id ) {
261 self::$current_page_data[ $post_type ][ $meta_id ] = array(
262 'id' => $meta_id,
263 'location' => '',
264 );
265
266 return self::$current_page_data[ $post_type ];
267 }
268 }
269
270 return false;
271 }
272
273 public static function parse_layout_display_condition( $post_id, $rules ) {
274 $display = false;
275 $current_post_type = get_post_type( $post_id );
276 if ( is_array( $rules ) && count( $rules ) ) {
277 foreach ( $rules as $key => $rule_item ) {
278 $rule = $rule_item['value'];
279 if ( strrpos( $rule, 'all' ) !== false ) {
280 $rule_case = 'all';
281 } else {
282 $rule_case = $rule;
283 }
284
285 switch ( $rule_case ) {
286 case 'basic-global':
287 $display = true;
288 break;
289
290 case 'basic-singulars':
291 if ( is_singular() ) {
292 $display = true;
293 }
294 break;
295
296 case 'basic-archives':
297 if ( is_archive() ) {
298 $display = true;
299 }
300 break;
301
302 case 'special-404':
303 if ( is_404() ) {
304 $display = true;
305 }
306 break;
307
308 case 'special-search':
309 if ( is_search() ) {
310 $display = true;
311 }
312 break;
313
314 case 'special-blog':
315 if ( is_home() ) {
316 $display = true;
317 }
318 break;
319
320 case 'special-front':
321 if ( is_front_page() ) {
322 $display = true;
323 }
324 break;
325
326 case 'special-date':
327 if ( is_date() ) {
328 $display = true;
329 }
330 break;
331
332 case 'special-author':
333 if ( is_author() ) {
334 $display = true;
335 }
336 break;
337
338 case 'special-woo-shop':
339 if ( function_exists( 'is_shop' ) && is_shop() ) {
340 $display = true;
341 }
342 break;
343
344 case 'all':
345 $rule_data = explode( '|', $rule );
346
347 $post_type = isset( $rule_data[0] ) ? $rule_data[0] : false;
348 $archieve_type = isset( $rule_data[2] ) ? $rule_data[2] : false;
349 $taxonomy = isset( $rule_data[3] ) ? $rule_data[3] : false;
350 if ( false === $archieve_type ) {
351 $current_post_type = get_post_type( $post_id );
352
353 if ( false !== $post_id && $current_post_type == $post_type ) {
354 $display = true;
355 }
356 } else {
357 if ( is_archive() ) {
358 $current_post_type = get_post_type();
359 if ( $current_post_type == $post_type ) {
360 if ( 'archive' == $archieve_type ) {
361 $display = true;
362 } elseif ( 'taxarchive' == $archieve_type ) {
363 $obj = \get_queried_object();
364 $current_taxonomy = '';
365 if ( '' !== $obj && null !== $obj ) {
366 $current_taxonomy = $obj->taxonomy;
367 }
368
369 if ( $current_taxonomy == $taxonomy ) {
370 $display = true;
371 }
372 }
373 }
374 }
375 }//end if
376 break;
377
378 case 'specifics':
379 if ( isset( $rule_item['specific'] ) && is_array( $rule_item['specific'] ) ) {
380 foreach ( $rule_item['specific'] as $specific_page ) {
381 $specific_page = $specific_page['value'];
382 $specific_data = explode( '-', $specific_page );
383
384 $specific_post_type = isset( $specific_data[0] ) ? $specific_data[0] : false;
385 $specific_post_id = isset( $specific_data[1] ) ? $specific_data[1] : false;
386 if ( 'post' == $specific_post_type ) {
387 if ( $specific_post_id == $post_id ) {
388 $display = true;
389 }
390 } elseif ( isset( $specific_data[2] ) && ( 'single' == $specific_data[2] ) && 'tax' == $specific_post_type ) {
391 if ( is_singular() ) {
392 $term_details = get_term( $specific_post_id );
393
394 if ( isset( $term_details->taxonomy ) ) {
395 $has_term = has_term( (int) $specific_post_id, $term_details->taxonomy, $post_id );
396
397 if ( $has_term ) {
398 $display = true;
399 }
400 }
401 }
402 } elseif ( 'tax' == $specific_post_type ) {
403 $tax_id = get_queried_object_id();
404 if ( $specific_post_id == $tax_id ) {
405 $display = true;
406 }
407 }//end if
408 }//end foreach
409 }//end if
410 break;
411
412 default:
413 break;
414 }//end switch
415
416 if ( $display ) {
417 break;
418 }
419 }//end foreach
420 }//end if
421
422 return $display;
423 }
424
425 public static function parse_user_role_condition( $post_id, $rules ) {
426 $show_popup = true;
427
428 if ( is_array( $rules ) && count( $rules ) ) {
429 $show_popup = false;
430
431 foreach ( $rules as $i => $rule ) {
432 if ( ! isset( $rule['value'] ) ) {
433 continue;
434 }
435 $rule = $rule['value'];
436 switch ( $rule ) {
437 case '':
438 case 'all':
439 $show_popup = true;
440 break;
441
442 case 'logged-in':
443 if ( is_user_logged_in() ) {
444 $show_popup = true;
445 }
446 break;
447
448 case 'logged-out':
449 if ( ! is_user_logged_in() ) {
450 $show_popup = true;
451 }
452 break;
453
454 default:
455 if ( is_user_logged_in() ) {
456 $current_user = wp_get_current_user();
457
458 if ( isset( $current_user->roles )
459 && is_array( $current_user->roles )
460 && in_array( $rule, $current_user->roles )
461 ) {
462 $show_popup = true;
463 }
464 }
465 break;
466 }//end switch
467
468 if ( $show_popup ) {
469 break;
470 }
471 }//end foreach
472 }//end if
473
474 return $show_popup;
475 }
476
477 public static function get_info_popup_builder_content() {
478 global $wpdb;
479 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
480 return $wpdb->get_results("
481 SELECT p.ID, p.post_content
482 FROM {$wpdb->prefix}posts AS p
483 INNER JOIN {$wpdb->prefix}postmeta AS pm
484 ON p.ID = pm.post_id
485 WHERE p.post_type = 'ablocks_tb'
486 AND p.post_status = 'publish'
487 AND pm.meta_key = 'ablocks_tb_template_type'
488 AND pm.meta_value = 'info-popup-builder'
489 ");
490 }
491 }
492