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

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