PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.0
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 / includes / helper.php

helper.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.0, at includes/helper.php

787 lines 23.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use ABlocks\traits\Importer;
9
10 class Helper {
11
12 use Importer;
13
14 public static function get_time() {
15 return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
16 }
17
18 public static function get_settings( $key, $default = null ) {
19 global $ablocks_settings;
20
21 if ( isset( $ablocks_settings->{$key} ) ) {
22 return $ablocks_settings->{$key};
23 }
24
25 return $default;
26 }
27
28 public static function get_page_permalink( $page, $fallback = null ) {
29 $page_id = self::get_settings( $page );
30 $permalink = 0 < $page_id ? get_permalink( $page_id ) : '';
31 if ( ! $permalink ) {
32 $permalink = is_null( $fallback ) ? get_home_url() : $fallback;
33 }
34
35 return apply_filters( 'ablocks/get_' . $page . '_permalink', $permalink );
36 }
37
38 public static function get_logout_url( $redirect = '' ) {
39 $redirect = $redirect ? $redirect : apply_filters( 'ablocks/logout_default_redirect_url', self::get_page_permalink( 'dashboard_page' ) );
40
41 return wp_logout_url( $redirect );
42 }
43 public static function is_enabled_block( $block_name, $parent_block_name = '' ) {
44 global $ablocks_blocks;
45 $block_name = ! empty( $parent_block_name ) ? $parent_block_name : $block_name;
46 if ( isset( $ablocks_blocks->{$block_name} ) ) {
47 return (bool) $ablocks_blocks->{$block_name};
48 }
49 return false;
50 }
51
52
53 public static function is_plugin_installed( $path ) {
54 $installed_plugins = get_plugins();
55 return isset( $installed_plugins[ $path ] );
56 }
57
58 public static function is_active_academy() {
59 $academy = 'academy/academy.php';
60 return self::is_plugin_active( $academy );
61 }
62
63
64 public static function is_active_storeengine() {
65 $storeengine = 'storeengine/storeengine.php';
66 return self::is_plugin_active( $storeengine );
67 }
68
69 public static function is_active_ablocks_pro() {
70 $ablocks = 'ablocks-pro/ablocks-pro.php';
71 return self::is_plugin_active( $ablocks );
72 }
73 public static function is_active_wp_map_block() {
74 $wp_map_block = 'wp-map-block/wp-map-block.php';
75 return self::is_plugin_active( $wp_map_block );
76 }
77 public static function is_active_quizpress() {
78 return class_exists( 'QuizPress' );
79 }
80 public static function is_active_easy_content_manager() {
81 return class_exists( 'EasyContentManager' );
82 }
83
84 public static function is_enabled_assets_generation() {
85 // Default OFF — combining/generating per-page asset files churns while a
86 // site is still being built, so it's recommended (via the Performance tab
87 // notice) once the site is complete rather than forced on. When enabled it
88 // merges every block's CSS/JS into one per-page file, inlined when small
89 // (see Assets::enqueue_frontend_assets), removing the per-block
90 // render-blocking stylesheets.
91 $flag = (bool) self::get_settings( 'enabled_assets_file_generation', false );
92 return apply_filters( 'ablocks/is_enabled_assets_generation', $flag );
93 }
94
95 public static function is_plugin_active( $basename ) {
96 if ( ! function_exists( 'get_plugins' ) ) {
97 include_once ABSPATH . '/wp-admin/includes/plugin.php';
98 }
99 return is_plugin_active( $basename );
100 }
101
102 public static function is_dev_mode_enable() {
103 $environment = wp_get_environment_type();
104 if ( 'local' === $environment || 'development' === $environment ) {
105 return true;
106 }
107 }
108
109 public static function get_admin_menu_list() {
110 $menu = [];
111 $menu[ ABLOCKS_PLUGIN_SLUG ] = [
112 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
113 'title' => __( 'Dashboard', 'ablocks' ),
114 'capability' => 'manage_options',
115 ];
116 if ( self::is_enabled_block( 'form-builder' ) ) {
117 $menu[ ABLOCKS_PLUGIN_SLUG . '-submissions' ] = [
118 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
119 'title' => __( 'Submissions', 'ablocks' ),
120 'capability' => 'manage_options',
121 ];
122 }
123 if ( self::get_addon_active_status( 'theme-builder' ) ) {
124 $menu[ ABLOCKS_PLUGIN_SLUG . '-theme-builder' ] = [
125 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
126 'title' => __( 'Theme Builder', 'ablocks' ),
127 'capability' => 'manage_options',
128 ];
129 }
130 $menu[ ABLOCKS_PLUGIN_SLUG . '-addons' ] = [
131 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
132 'title' => __( 'Add-ons', 'ablocks' ),
133 'capability' => 'manage_options',
134 ];
135 $menu[ ABLOCKS_PLUGIN_SLUG . '-scanner' ] = [
136 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
137 'title' => __( 'Site Scanner', 'ablocks' ),
138 'capability' => 'manage_options',
139 ];
140 $menu[ ABLOCKS_PLUGIN_SLUG . '-settings' ] = [
141 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
142 'title' => __( 'Settings', 'ablocks' ),
143 'capability' => 'manage_options',
144 ];
145 if ( ! defined( 'ABLOCKS_PRO_VERSION' ) ) {
146 $menu[ ABLOCKS_PLUGIN_SLUG . '-get-pro' ] = [
147 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
148 'title' => '<span class="dashicons dashicons-awards academy-blue-color"></span> ' . __( 'Get Pro', 'ablocks' ),
149 'capability' => 'manage_options',
150 ];
151 }
152 return apply_filters( 'ablocks/admin_menu_list', $menu );
153 }
154
155 public static function get_preloader_html() {
156 ob_start();
157 ?>
158 <div class="ablocks-initial-preloader"><?php esc_html_e( 'Loading...', 'ablocks' ); ?></div>
159 <?php
160 return ob_get_clean();
161 }
162 public static function has_value( $value ) {
163 return isset( $value ) && ! empty( $value );
164 }
165
166 public static function get_array_value( $array, $key, $default ) {
167 return ( isset( $array[ $key ] ) && ! empty( $array[ $key ] ) ) ? $array[ $key ] : $default;
168 }
169
170 public static function get_responsive_value( $attribute, $attribute_object_key, $device, $attribute_default_value = [] ) {
171 // Closure to "clean" a value (similar to your JS clean() helper)
172 $clean = function( $value ) {
173 return self::has_value( $value ) ? $value : null;
174 };
175
176 // Desktop value (fallback to default, or false if nothing found)
177 $desktop_value =
178 $clean( $attribute[ $attribute_object_key ] ?? null ) ??
179 $clean( $attribute_default_value[ $attribute_object_key ] ?? null ) ??
180 false;
181
182 // Tablet value (fallback to desktop if nothing found)
183 $tablet_key = $attribute_object_key . 'Tablet';
184 $tablet_value =
185 $clean( $attribute[ $tablet_key ] ?? null ) ??
186 $clean( $attribute_default_value[ $tablet_key ] ?? null ) ??
187 $desktop_value;
188
189 // Mobile value (fallback to tablet if nothing found)
190 $mobile_key = $attribute_object_key . 'Mobile';
191 $mobile_value =
192 $clean( $attribute[ $mobile_key ] ?? null ) ??
193 $clean( $attribute_default_value[ $mobile_key ] ?? null ) ??
194 $tablet_value;
195
196 // Return based on device
197 switch ( $device ) {
198 case 'Mobile':
199 return $mobile_value;
200 case 'Tablet':
201 return $tablet_value;
202 default:
203 return $desktop_value;
204 }
205 }
206
207 public static function is_gutenberg_editor() {
208 global $pagenow;
209 if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
210 return true;
211 }
212 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
213 return ( isset( $_GET['context'] ) && 'edit' === $_GET['context'] ) || ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] );
214 }
215 public static function attr_shortcode( $attr_array ) {
216 $html_attr = '';
217 foreach ( $attr_array as $attr_name => $attr_val ) {
218 if ( empty( $attr_val ) ) {
219 continue;
220 }
221 if ( is_array( $attr_val ) ) {
222 $html_attr .= $attr_name . '="' . implode( ',', $attr_val ) . '" ';
223 } else {
224 $html_attr .= $attr_name . '="' . $attr_val . '" ';
225 }
226 }
227 return $html_attr;
228 }
229
230 public static function get_attribute_value( $attributes, $attribute_name ) {
231 return isset( $attributes[ $attribute_name ] ) ? $attributes[ $attribute_name ] : '';
232 }
233
234 public static function get_terms_list( $taxonomy = 'category' ) {
235 $options = [];
236 $terms = get_terms( [
237 'taxonomy' => $taxonomy,
238 'hide_empty' => true,
239 ] );
240
241 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
242 foreach ( $terms as $term ) {
243 $options[] = [
244 'label' => $term->name,
245 'value' => $term->term_id,
246 ];
247 }
248 }
249
250 return $options;
251 }
252
253 public static function get_author_data( $post_id, $author_id = false ) {
254 $author_id = $author_id ?: get_post_field( 'post_author', $post_id );
255 $user = get_userdata( $author_id );
256
257 if ( ! $user ) {
258 wp_send_json_error( 'Author not found.', 404 );
259 }
260
261 $data = [
262 'author_id' => $author_id,
263 'author_name' => sanitize_text_field( $user->display_name ),
264 'author_posts_count' => count_user_posts( $author_id ),
265 'author_posts_url' => esc_url( get_author_posts_url( $author_id ) ),
266 'author_profile_picture_url' => esc_url( get_avatar_url( $author_id ) ),
267 'author_bio' => sanitize_text_field( get_user_meta( $author_id, 'description', true ) ),
268 'author_email' => sanitize_email( $user->user_email ),
269 'author_website' => esc_url( $user->user_url ),
270 'author_first_name' => sanitize_text_field( get_user_meta( $author_id, 'first_name', true ) ),
271 'author_last_name' => sanitize_text_field( get_user_meta( $author_id, 'last_name', true ) )
272 ];
273
274 return $data;
275 }
276
277 public static function get_icon_picker_attribute( $attributePrefix = 'icon', $defaultValue = [] ) {
278 $svgPathKey = $attributePrefix . 'SvgPath';
279 $svgViewBoxKey = $attributePrefix . 'SvgViewBox';
280 $svgClassKey = $attributePrefix . 'Class';
281
282 $attribute = [
283 $svgPathKey => [
284 'type' => 'string',
285 'source' => 'attribute',
286 'selector' => 'svg.ablocks-svg-icon path',
287 'attribute' => 'd',
288 ],
289 $svgViewBoxKey => [
290 'type' => 'string',
291 'source' => 'attribute',
292 'selector' => 'svg.ablocks-svg-icon',
293 'attribute' => 'viewBox',
294 ],
295 $svgClassKey => [
296 'type' => 'string',
297 ],
298 ];
299
300 if ( isset( $defaultValue['path'] ) && isset( $defaultValue['viewBox'] ) ) {
301 $attribute[ $svgPathKey ]['default'] = $defaultValue['path'];
302 $attribute[ $svgViewBoxKey ]['default'] = $defaultValue['viewBox'];
303 }
304 if ( isset( $defaultValue['className'] ) ) {
305 $attribute[ $svgClassKey ]['default'] = $defaultValue['className'];
306 }
307 return $attribute;
308 }
309
310 public static function get_terms_for_post( $taxonomy, $post_id ) {
311 $terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'all' ] );
312
313 if ( is_wp_error( $terms ) ) {
314 return [];
315 }
316
317 return array_map( function( $term ) {
318 return [
319 'id' => $term->term_id,
320 'name' => $term->name,
321 'slug' => $term->slug,
322 ];
323 }, $terms );
324 }
325
326 public static function get_taxonomies_data_for_post_type( $post_type ) {
327 $all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
328 return array_values(array_map( function( $taxonomy ) {
329 return [
330 'value' => $taxonomy->name,
331 'label' => $taxonomy->label,
332 ];
333 }, $all_taxonomies ));
334 }
335
336 public static function get_post_excerpt( $post_id, $length = false ) {
337 $excerpt = get_the_excerpt( $post_id );
338 if ( $length ) {
339 return wp_trim_words( $excerpt, $length, '...' );
340 }
341 return $excerpt;
342 }
343
344 public static function get_post_terms_as_string( $post_id, $taxonomy, $separator = ', ' ) {
345 $terms = wp_get_post_terms( $post_id, $taxonomy );
346
347 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
348 $term_names = wp_list_pluck( $terms, 'name' );
349 return implode( $separator, $term_names );
350 }
351
352 return '';
353 }
354
355 public static function get_post_time_date( $post_id, $dynamicContentAttribute, $is_time = false ) {
356 $dateTimeType = $dynamicContentAttribute['dateTimeType'];
357 $dateTimeFormat = $dynamicContentAttribute['dateTimeFormat'];
358 $customDateTimeFormat = $dynamicContentAttribute['customDateTimeFormat'];
359
360 $format = $dateTimeFormat;
361 $defaultFormat = $is_time ? 'g:i a' : 'j M, Y';
362 if ( ! $dateTimeFormat ) {
363 $format = $defaultFormat;
364 } elseif ( 'custom' === $dateTimeFormat ) {
365 $format = $customDateTimeFormat || $defaultFormat;
366 }
367
368 $date_time = '';
369 if ( ! $dateTimeType || 'post_published' === $dateTimeType ) {
370 $date_time = get_the_date( $format, $post_id );
371 } elseif ( 'post_modified' === $dateTimeType ) {
372 $date_time = get_the_modified_date( $format, $post_id );
373 }
374
375 return $date_time;
376 }
377
378 public static function is_fse_theme() {
379 return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
380 }
381
382 public static function check_post_type_from_admin( $post_type ) {
383 global $post;
384 if ( is_admin() ) {
385 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
386 if ( $post && get_post_type( $post ) === $post_type ) {
387 return true;
388 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
389 } elseif ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $post_type ) {
390 return true;
391 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
392 } elseif ( isset( $_GET['post'] ) ) {
393 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
394 $queried_post_type = get_post_type( sanitize_text_field( wp_unslash( $_GET['post'] ) ) );
395 if ( $queried_post_type === $post_type ) {
396 return true;
397 }
398 }
399 }
400 return false;
401 }
402
403 public static function get_content_by_object_id( string $id_or_fse_slug ) : ?string {
404 if ( is_numeric( $id_or_fse_slug ) ) {
405 if (
406 ! current_user_can( 'manage_options' ) &&
407 get_post_status( $id_or_fse_slug ) !== 'publish'
408 ) {
409 return null;
410 }
411 return get_post_field( 'post_content', intval( $id_or_fse_slug ) );
412 } elseif (
413 ! empty( $template = get_block_template( $id_or_fse_slug, 'wp_template_part' ) ) ||
414 ! empty( $template = get_block_template( $id_or_fse_slug ) )
415 ) {
416 return $template->content;
417 }
418 return null;
419 }
420
421 public static function get_block_attributes( string $post_id, string $block_id, string $block_name ) : array {
422 // Cache parsed blocks per object id for the request — this is called once
423 // per loop/REST lookup and would otherwise re-fetch + re-parse the whole
424 // post content every time.
425 static $parsed_cache = [];
426 if ( ! array_key_exists( $post_id, $parsed_cache ) ) {
427 $post_content = self::get_content_by_object_id( $post_id );
428 $parsed_cache[ $post_id ] = ( ! is_null( $post_content ) && is_array( $blocks = parse_blocks( $post_content ) ) )
429 ? $blocks
430 : null;
431 }
432 if ( is_array( $parsed_cache[ $post_id ] ) ) {
433 return self::get_block_attributes_recursive( $block_id, $block_name, $parsed_cache[ $post_id ] );
434 }
435 return [];
436 }
437
438 public static function get_block_attributes_recursive( string $block_id, string $block_name, array $blocks ) : array {
439 foreach ( $blocks as $block ) {
440
441 if (
442 ( $block['attrs']['block_id'] ?? '' ) === $block_id &&
443 ( $block['blockName'] ?? '' ) === $block_name
444 ) {
445 return [
446 'parentAttributes' => $block['attrs'] ?? [],
447 'innerBlocks' => self::extract_inner_blocks( $block['innerBlocks'] ?? [] ),
448 ];
449 }
450
451 if (
452 array_key_exists( 'innerBlocks', $block ) &&
453 is_array( $block['innerBlocks'] ) &&
454 count( $block['innerBlocks'] ) > 0
455 ) {
456 $data = self::get_block_attributes_recursive(
457 $block_id,
458 $block_name,
459 $block['innerBlocks']
460 );
461 if ( ! empty( $data ) ) {
462 return $data;
463 }
464 }
465
466 if (
467 isset( $block['blockName'] ) &&
468 $block['blockName'] === 'core/template-part' &&
469 ! empty( $block['attrs']['slug'] )
470 ) {
471 $part_slug = $block['attrs']['theme'] . '//' . $block['attrs']['slug'];
472 $data = self::get_block_attributes( $part_slug, $block_id, $block_name );
473 if ( ! empty( $data ) ) {
474 return $data;
475 }
476 }
477 }//end foreach
478 return [];
479 }
480
481 public static function extract_inner_blocks( $innerBlocks ) {
482 return array_map(function ( $inner_block ) {
483 $block_data = [
484 'blockName' => $inner_block['blockName'],
485 'attributes' => $inner_block['attrs'],
486 ];
487 if ( ! empty( $inner_block['innerBlocks'] ) ) {
488 $block_data['innerBlocks'] = self::extract_inner_blocks( $inner_block['innerBlocks'] );
489 }
490 return $block_data;
491 }, $innerBlocks);
492 }
493
494 public static function generate_schema_using_form_data( $customFields ) {
495 $schema = [];
496
497 foreach ( $customFields as $inputField ) {
498 // Check if 'name' exists in the input field
499 if ( isset( $inputField['name'] ) ) {
500 $field_name = $inputField['name'];
501 $input_type = $inputField['inputType'] ?? 'text'; // Default to 'text' if not specified
502
503 // Map the input types to schema types
504 switch ( $input_type ) {
505 case 'Text':
506 $schema[ $field_name ] = 'string';
507 break;
508 case 'Email':
509 $schema[ $field_name ] = 'email';
510 break;
511 case 'Password':
512 $schema[ $field_name ] = 'string';
513 break;
514 case 'Number':
515 $schema[ $field_name ] = 'number';
516 break;
517 case 'Url':
518 $schema[ $field_name ] = 'url';
519 break;
520 case 'Boolean':
521 $schema[ $field_name ] = 'boolean';
522 break;
523 case 'Textarea':
524 $schema[ $field_name ] = 'textarea';
525 break;
526 default:
527 $schema[ $field_name ] = 'string'; // Default to string for unknown types
528 break;
529 }//end switch
530 }//end if
531 }//end foreach
532
533 return $schema;
534 }
535
536 public static function sorted_input_fields_by_input_type( $blockData ) {
537 $sorted_custom_fields = [];
538 foreach ( $blockData as $block ) {
539 $name = $block['attributes']['name'] ?? null;
540 $inputType = $block['attributes']['inputType'] ?? null;
541
542 if ( $name && isset( $custom_fields[ $name ] ) ) {
543 $sorted_custom_fields[] = [
544 'name' => $name,
545 'inputType' => $inputType,
546 'value' => $custom_fields[ $name ]
547 ];
548 }
549 }
550
551 return $sorted_custom_fields;
552 }
553
554
555 public static function render_svg_icon_using_attr( $attributes = array() ) {
556 $default_attributes = array(
557 'path' => '',
558 'viewBox' => '0 0 24 24',
559 'className' => 'icon-class',
560 'width' => '24',
561 'height' => '24',
562 );
563
564 // Merge passed attributes with default values
565 $attributes = array_merge( $default_attributes, $attributes );
566
567 // Sanitize attributes for safety
568 $path = esc_attr( $attributes['path'] );
569 $viewBox = esc_attr( $attributes['viewBox'] );
570 $className = esc_attr( $attributes['className'] );
571 $width = esc_attr( $attributes['width'] );
572 $height = esc_attr( $attributes['height'] );
573
574 // Output the SVG
575 return '
576 <svg
577 xmlns="http://www.w3.org/2000/svg"
578 viewBox="' . $viewBox . '"
579 class="' . $className . '"
580 width="' . $width . '"
581 height="' . $height . '">
582 <path d="' . $path . '"></path>
583 </svg>';
584 }
585
586 public static function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
587 $template = false;
588
589 if ( ! $template ) {
590 $template = self::locate_template( $template_name, $template_path, $default_path );
591 }
592
593 // Allow 3rd party plugin filter template file from their plugin.
594 $filter_template = apply_filters( 'ablocks/get_template', $template, $template_name, $args, $template_path, $default_path );
595
596 if ( $filter_template !== $template ) {
597 if ( ! file_exists( $filter_template ) ) {
598 /* translators: %s template */
599 wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'ablocks' ), '<code>' . $filter_template . '</code>' ), '1.0.0' );
600
601 return;
602 }
603 $template = $filter_template;
604 }
605
606 $action_args = array(
607 'template_name' => $template_name,
608 'template_path' => $template_path,
609 'located' => $template,
610 'args' => $args,
611 );
612
613 if ( ! empty( $args ) && is_array( $args ) ) {
614 if ( isset( $args['action_args'] ) ) {
615 wc_doing_it_wrong(
616 __FUNCTION__,
617 __( 'action_args should not be overwritten when calling ablocks/get_template.', 'ablocks' ),
618 '1.0.0'
619 );
620 unset( $args['action_args'] );
621 }
622 extract( $args ); // @codingStandardsIgnoreLine
623 }
624
625 do_action( 'ablocks/before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
626 include $action_args['located'];
627
628 do_action( 'ablocks/after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
629 }
630
631 public static function locate_template( $template_name, $template_path = '', $default_path = '' ) {
632 if ( ! $template_path ) {
633 $template_path = self::template_path();
634 }
635
636 if ( ! $default_path ) {
637 $default_path = self::plugin_path() . 'templates/';
638 }
639
640 if ( empty( $template ) ) {
641 $template = locate_template(
642 array(
643 trailingslashit( $template_path ) . $template_name,
644 $template_name,
645 )
646 );
647 }
648 if ( ! $template ) {
649 $template = $default_path . $template_name;
650 }
651
652 // Return what we found.
653 return apply_filters( 'ablocks/locate_template', $template, $template_name, $template_path );
654 }
655
656 public static function template_path() {
657 return apply_filters( 'ablocks/template_path', 'ablocks/' );
658 }
659 public static function plugin_path() {
660 return apply_filters( 'ablocks/plugin_path', ABLOCKS_ROOT_DIR_PATH );
661 }
662
663 public static function get_addon_active_status( $addon_name, $is_pro = false ) {
664 global $ablocks_addons;
665 if ( $is_pro && ! self::is_active_ablocks_pro() ) {
666 return false;
667 }
668 if ( isset( $ablocks_addons->{$addon_name} ) ) {
669 return (bool) $ablocks_addons->{$addon_name};
670 }
671
672 return false;
673 }
674
675 public static function sanitize_checkbox_field( $boolean ) {
676 return filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN );
677 }
678
679 public static function is_valid_site_url( string $url ): bool {
680 return str_starts_with( $url, get_option( 'siteurl' ) );
681 }
682
683 public static function get_script_loading_strategy() {
684 return 'defer';
685 }
686
687 public static function is_static_front_page( $post_id ) {
688 $show_on_front = get_option( 'show_on_front' );
689 $page_on_front = (int) get_option( 'page_on_front' );
690 return ( $show_on_front === 'page' && $page_on_front === (int) $post_id );
691 }
692
693 public static function get_public_post_type_options() {
694 $args = [
695 'public' => true,
696 '_builtin' => true,
697 ];
698 $post_types = get_post_types( $args, 'objects' );
699 unset( $post_types['attachment'] ); // Remove 'attachment' if present
700
701 // Get custom post types
702 $args['_builtin'] = false;
703 $custom_post_types = get_post_types( $args, 'objects' );
704
705 // Allow filters to modify the combined post types
706 $all_post_types = apply_filters(
707 'ablocks_theme_builder/location_rule_post_types',
708 array_merge( $post_types, $custom_post_types )
709 );
710
711 // Format result
712 $result = [];
713 foreach ( $all_post_types as $post_type => $post_type_obj ) {
714 $result[] = [
715 'label' => $post_type_obj->label,
716 'value' => $post_type,
717 ];
718 }
719
720 return $result;
721 }
722
723 public static function clear_third_party_plugin_cache() {
724
725 // Breeze
726 try {
727 if ( class_exists( 'Breeze_Purge' ) ) {
728 \Breeze_Purge::breeze_cache_flush();
729 }
730 } catch ( \Throwable $e ) {
731 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
732 error_log( 'Breeze Cache Clear Failed: ' . $e->getMessage() );
733 }
734
735 // W3 Total Cache
736 try {
737 if ( function_exists( 'w3tc_flush_all' ) ) {
738 \w3tc_flush_all();
739 }
740 } catch ( \Throwable $e ) {
741 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
742 error_log( 'W3 Total Cache Clear Failed: ' . $e->getMessage() );
743 }
744
745 // WP Super Cache
746 try {
747 if ( function_exists( 'wp_cache_clear_cache' ) ) {
748 \wp_cache_clear_cache();
749 }
750 } catch ( \Throwable $e ) {
751 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
752 error_log( 'WP Super Cache Clear Failed: ' . $e->getMessage() );
753 }
754
755 // LiteSpeed Cache
756 try {
757 if ( class_exists( 'LiteSpeed_Cache_API' ) ) {
758 \LiteSpeed_Cache_API::purge_all();
759 }
760 } catch ( \Throwable $e ) {
761 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
762 error_log( 'LiteSpeed Cache Clear Failed: ' . $e->getMessage() );
763 }
764
765 // WP Fastest Cache
766 try {
767 if ( class_exists( 'WpFastestCache' ) ) {
768 $wpfc = new \WpFastestCache();
769 $wpfc->deleteCache();
770 }
771 } catch ( \Throwable $e ) {
772 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
773 error_log( 'WP Fastest Cache Clear Failed: ' . $e->getMessage() );
774 }
775
776 // Autoptimize
777 try {
778 if ( function_exists( 'autoptimize_clearall' ) ) {
779 \autoptimize_clearall();
780 }
781 } catch ( \Throwable $e ) {
782 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
783 error_log( 'Autoptimize Clear Failed: ' . $e->getMessage() );
784 }
785 }
786 }
787