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 / includes / helper.php

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

589 lines 17.5 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\Classes\FileUpload;
9 use ABlocks\Classes\BlockGlobal;
10 use ABlocks\traits\Importer;
11
12 class Helper {
13
14 use Importer;
15
16 public static function get_time() {
17 return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
18 }
19
20 public static function get_settings( $key, $default = null ) {
21 global $ablocks_settings;
22
23 if ( isset( $ablocks_settings->{$key} ) ) {
24 return $ablocks_settings->{$key};
25 }
26
27 return $default;
28 }
29
30 public static function is_enabled_block( $block_name, $parent_block_name = '' ) {
31 global $ablocks_blocks;
32 $block_name = ! empty( $parent_block_name ) ? $parent_block_name : $block_name;
33 if ( isset( $ablocks_blocks->{$block_name} ) ) {
34 return (bool) $ablocks_blocks->{$block_name};
35 }
36 return false;
37 }
38
39
40 public static function is_plugin_installed( $path ) {
41 $installed_plugins = get_plugins();
42 return isset( $installed_plugins[ $path ] );
43 }
44
45 public static function is_active_academy() {
46 $academy = 'academy/academy.php';
47 return self::is_plugin_active( $academy );
48 }
49
50
51 public static function is_active_storeengine() {
52 $storeengine = 'storeengine/storeengine.php';
53 return self::is_plugin_active( $storeengine );
54 }
55
56 public static function is_active_ablocks_pro() {
57 $ablocks = 'ablocks-pro/ablocks-pro.php';
58 return self::is_plugin_active( $ablocks );
59 }
60 public static function is_active_wp_map_block() {
61 $wp_map_block = 'wp-map-block/wp-map-block.php';
62 return self::is_plugin_active( $wp_map_block );
63 }
64
65 public static function is_enabled_assets_generation() {
66 $flag = false;
67 if ( (bool) self::get_settings( 'enabled_assets_file_generation' ) ) {
68 $flag = function_exists( 'wp_is_block_theme' ) ? ! wp_is_block_theme() : true;
69 }
70 return apply_filters( 'ablocks/is_enabled_assets_generation', $flag );
71 }
72
73 public static function is_plugin_active( $basename ) {
74 if ( ! function_exists( 'get_plugins' ) ) {
75 include_once ABSPATH . '/wp-admin/includes/plugin.php';
76 }
77 return is_plugin_active( $basename );
78 }
79
80 public static function is_dev_mode_enable() {
81 $environment = wp_get_environment_type();
82 if ( 'local' === $environment || 'development' === $environment ) {
83 return true;
84 }
85 }
86
87 public static function get_admin_menu_list() {
88 $menu = [];
89 $menu[ ABLOCKS_PLUGIN_SLUG ] = [
90 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
91 'title' => __( 'Dashboard', 'ablocks' ),
92 'capability' => 'manage_options',
93 ];
94 if ( self::is_enabled_block( 'form-builder' ) ) {
95 $menu[ ABLOCKS_PLUGIN_SLUG . '-submissions' ] = [
96 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
97 'title' => __( 'Submissions', 'ablocks' ),
98 'capability' => 'manage_options',
99 ];
100 }
101 if ( self::get_addon_active_status( 'theme-builder' ) ) {
102 $menu[ ABLOCKS_PLUGIN_SLUG . '-theme-builder' ] = [
103 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
104 'title' => __( 'Theme Builder', 'ablocks' ),
105 'capability' => 'manage_options',
106 ];
107 }
108 $menu[ ABLOCKS_PLUGIN_SLUG . '-addons' ] = [
109 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
110 'title' => __( 'Add-ons', 'ablocks' ),
111 'capability' => 'manage_options',
112 ];
113 $menu[ ABLOCKS_PLUGIN_SLUG . '-settings' ] = [
114 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
115 'title' => __( 'Settings', 'ablocks' ),
116 'capability' => 'manage_options',
117 ];
118 if ( ! defined( 'ABLOCKS_PRO_VERSION' ) ) {
119 $menu[ ABLOCKS_PLUGIN_SLUG . '-get-pro' ] = [
120 'parent_slug' => ABLOCKS_PLUGIN_SLUG,
121 'title' => '<span class="dashicons dashicons-awards academy-blue-color"></span> ' . __( 'Get Pro', 'ablocks' ),
122 'capability' => 'manage_options',
123 ];
124 }
125 return apply_filters( 'ablocks/admin_menu_list', $menu );
126 }
127
128 public static function get_preloader_html() {
129 ob_start();
130 ?>
131 <div class="ablocks-initial-preloader"><?php esc_html_e( 'Loading...', 'ablocks' ); ?></div>
132 <?php
133 return ob_get_clean();
134 }
135 public static function has_value( $value ) {
136 return isset( $value ) && ! empty( $value );
137 }
138 public static function is_gutenberg_editor() {
139 global $pagenow;
140 if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
141 return true;
142 }
143 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
144 return ( isset( $_GET['context'] ) && 'edit' === $_GET['context'] ) || ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] );
145 }
146 public static function attr_shortcode( $attr_array ) {
147 $html_attr = '';
148 foreach ( $attr_array as $attr_name => $attr_val ) {
149 if ( empty( $attr_val ) ) {
150 continue;
151 }
152 if ( is_array( $attr_val ) ) {
153 $html_attr .= $attr_name . '="' . implode( ',', $attr_val ) . '" ';
154 } else {
155 $html_attr .= $attr_name . '="' . $attr_val . '" ';
156 }
157 }
158 return $html_attr;
159 }
160
161 public static function get_attribute_value( $attributes, $attribute_name ) {
162 return isset( $attributes[ $attribute_name ] ) ? $attributes[ $attribute_name ] : '';
163 }
164
165 public static function get_terms_list( $taxonomy = 'category' ) {
166 $options = [];
167 $terms = get_terms( [
168 'taxonomy' => $taxonomy,
169 'hide_empty' => true,
170 ] );
171
172 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
173 foreach ( $terms as $term ) {
174 $options[] = [
175 'label' => $term->name,
176 'value' => $term->term_id,
177 ];
178 }
179 }
180
181 return $options;
182 }
183
184 public static function get_author_data( $post_id, $author_id = false ) {
185 $author_id = $author_id ?: get_post_field( 'post_author', $post_id );
186 $user = get_userdata( $author_id );
187
188 if ( ! $user ) {
189 wp_send_json_error( 'Author not found.', 404 );
190 }
191
192 $data = [
193 'author_id' => $author_id,
194 'author_name' => sanitize_text_field( $user->display_name ),
195 'author_posts_count' => count_user_posts( $author_id ),
196 'author_posts_url' => esc_url( get_author_posts_url( $author_id ) ),
197 'author_profile_picture_url' => esc_url( get_avatar_url( $author_id ) ),
198 'author_bio' => sanitize_text_field( get_user_meta( $author_id, 'description', true ) ),
199 'author_email' => sanitize_email( $user->user_email ),
200 'author_website' => esc_url( $user->user_url ),
201 'author_first_name' => sanitize_text_field( get_user_meta( $author_id, 'first_name', true ) ),
202 'author_last_name' => sanitize_text_field( get_user_meta( $author_id, 'last_name', true ) )
203 ];
204
205 return $data;
206 }
207
208 public static function get_icon_picker_attribute( $attributePrefix = 'icon', $defaultValue = [] ) {
209 $svgPathKey = $attributePrefix . 'SvgPath';
210 $svgViewBoxKey = $attributePrefix . 'SvgViewBox';
211 $svgClassKey = $attributePrefix . 'Class';
212
213 $attribute = [
214 $svgPathKey => [
215 'type' => 'string',
216 'source' => 'attribute',
217 'selector' => 'svg.ablocks-svg-icon path',
218 'attribute' => 'd',
219 ],
220 $svgViewBoxKey => [
221 'type' => 'string',
222 'source' => 'attribute',
223 'selector' => 'svg.ablocks-svg-icon',
224 'attribute' => 'viewBox',
225 ],
226 $svgClassKey => [
227 'type' => 'string',
228 ],
229 ];
230
231 if ( isset( $defaultValue['path'] ) && isset( $defaultValue['viewBox'] ) ) {
232 $attribute[ $svgPathKey ]['default'] = $defaultValue['path'];
233 $attribute[ $svgViewBoxKey ]['default'] = $defaultValue['viewBox'];
234 }
235 if ( isset( $defaultValue['className'] ) ) {
236 $attribute[ $svgClassKey ]['default'] = $defaultValue['className'];
237 }
238 return $attribute;
239 }
240
241 public static function get_terms_for_post( $taxonomy, $post_id ) {
242 $terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'all' ] );
243
244 if ( is_wp_error( $terms ) ) {
245 return [];
246 }
247
248 return array_map( function( $term ) {
249 return [
250 'id' => $term->term_id,
251 'name' => $term->name,
252 'slug' => $term->slug,
253 ];
254 }, $terms );
255 }
256
257 public static function get_taxonomies_data_for_post_type( $post_type ) {
258 $all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
259 return array_values(array_map( function( $taxonomy ) {
260 return [
261 'value' => $taxonomy->name,
262 'label' => $taxonomy->label,
263 ];
264 }, $all_taxonomies ));
265 }
266
267 public static function get_post_excerpt( $post_id, $length = false ) {
268 $excerpt = get_the_excerpt( $post_id );
269 if ( $length ) {
270 return wp_trim_words( $excerpt, $length, '...' );
271 }
272 return $excerpt;
273 }
274
275 public static function get_post_terms_as_string( $post_id, $taxonomy, $separator = ', ' ) {
276 $terms = wp_get_post_terms( $post_id, $taxonomy );
277
278 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
279 $term_names = wp_list_pluck( $terms, 'name' );
280 return implode( $separator, $term_names );
281 }
282
283 return '';
284 }
285
286 public static function get_post_time_date( $post_id, $dynamicContentAttribute, $is_time = false ) {
287 $dateTimeType = $dynamicContentAttribute['dateTimeType'];
288 $dateTimeFormat = $dynamicContentAttribute['dateTimeFormat'];
289 $customDateTimeFormat = $dynamicContentAttribute['customDateTimeFormat'];
290
291 $format = $dateTimeFormat;
292 $defaultFormat = $is_time ? 'g:i a' : 'j M, Y';
293 if ( ! $dateTimeFormat ) {
294 $format = $defaultFormat;
295 } elseif ( 'custom' === $dateTimeFormat ) {
296 $format = $customDateTimeFormat || $defaultFormat;
297 }
298
299 $date_time = '';
300 if ( ! $dateTimeType || 'post_published' === $dateTimeType ) {
301 $date_time = get_the_date( $format, $post_id );
302 } elseif ( 'post_modified' === $dateTimeType ) {
303 $date_time = get_the_modified_date( $format, $post_id );
304 }
305
306 return $date_time;
307 }
308
309 public static function is_fse_theme() {
310 return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
311 }
312
313 public static function check_post_type_from_admin( $post_type ) {
314 global $post;
315 if ( is_admin() ) {
316 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
317 if ( $post && get_post_type( $post ) === $post_type ) {
318 return true;
319 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
320 } elseif ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $post_type ) {
321 return true;
322 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
323 } elseif ( isset( $_GET['post'] ) ) {
324 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
325 $queried_post_type = get_post_type( sanitize_text_field( wp_unslash( $_GET['post'] ) ) );
326 if ( $queried_post_type === $post_type ) {
327 return true;
328 }
329 }
330 }
331 return false;
332 }
333
334 public static function get_content_by_object_id( string $id_or_fse_slug ) : ?string {
335 if ( is_numeric( $id_or_fse_slug ) ) {
336 return get_post_field( 'post_content', intval( $id_or_fse_slug ) );
337 } elseif (
338 ! empty( $template = get_block_template( $id_or_fse_slug, 'wp_template_part' ) ) ||
339 ! empty( $template = get_block_template( $id_or_fse_slug ) )
340 ) {
341 return $template->content;
342 }
343 return null;
344 }
345
346 public static function get_block_attributes( string $post_id, string $block_id, string $block_name ) : array {
347 if ( ! is_null( $post_content = self::get_content_by_object_id( $post_id ) ) ) {
348 if ( is_array( $blocks = parse_blocks( $post_content ) ) ) {
349 return self::get_block_attributes_recursive( $block_id, $block_name, $blocks );
350 }
351 }
352 return [];
353 }
354
355 public static function get_block_attributes_recursive( string $block_id, string $block_name, array $blocks ) : array {
356 foreach ( $blocks as $block ) {
357
358 if (
359 ( $block['attrs']['block_id'] ?? '' ) === $block_id &&
360 ( $block['blockName'] ?? '' ) === $block_name
361 ) {
362 return [
363 'parentAttributes' => $block['attrs'] ?? [],
364 'innerBlocks' => self::extract_inner_blocks( $block['innerBlocks'] ?? [] ),
365 ];
366 }
367
368 if (
369 array_key_exists( 'innerBlocks', $block ) &&
370 is_array( $block['innerBlocks'] ) &&
371 count( $block['innerBlocks'] ) > 0
372 ) {
373 $data = self::get_block_attributes_recursive(
374 $block_id,
375 $block_name,
376 $block['innerBlocks']
377 );
378 if ( ! empty( $data ) ) {
379 return $data;
380 }
381 }
382 }//end foreach
383 return [];
384 }
385
386 public static function extract_inner_blocks( $innerBlocks ) {
387 return array_map(function ( $inner_block ) {
388 $block_data = [
389 'blockName' => $inner_block['blockName'],
390 'attributes' => $inner_block['attrs'],
391 ];
392 if ( ! empty( $inner_block['innerBlocks'] ) ) {
393 $block_data['innerBlocks'] = self::extract_inner_blocks( $inner_block['innerBlocks'] );
394 }
395 return $block_data;
396 }, $innerBlocks);
397 }
398
399 public static function generate_schema_using_form_data( $customFields ) {
400 $schema = [];
401
402 foreach ( $customFields as $inputField ) {
403 // Check if 'name' exists in the input field
404 if ( isset( $inputField['name'] ) ) {
405 $field_name = $inputField['name'];
406 $input_type = $inputField['inputType'] ?? 'text'; // Default to 'text' if not specified
407
408 // Map the input types to schema types
409 switch ( $input_type ) {
410 case 'Text':
411 $schema[ $field_name ] = 'string';
412 break;
413 case 'Email':
414 $schema[ $field_name ] = 'email';
415 break;
416 case 'Password':
417 $schema[ $field_name ] = 'string';
418 break;
419 case 'Number':
420 $schema[ $field_name ] = 'number';
421 break;
422 case 'Url':
423 $schema[ $field_name ] = 'url';
424 break;
425 case 'Boolean':
426 $schema[ $field_name ] = 'boolean';
427 break;
428 case 'Textarea':
429 $schema[ $field_name ] = 'textarea';
430 break;
431 default:
432 $schema[ $field_name ] = 'string'; // Default to string for unknown types
433 break;
434 }//end switch
435 }//end if
436 }//end foreach
437
438 return $schema;
439 }
440
441 public static function sorted_input_fields_by_input_type( $blockData ) {
442 $sorted_custom_fields = [];
443 foreach ( $blockData as $block ) {
444 $name = $block['attributes']['name'] ?? null;
445 $inputType = $block['attributes']['inputType'] ?? null;
446
447 if ( $name && isset( $custom_fields[ $name ] ) ) {
448 $sorted_custom_fields[] = [
449 'name' => $name,
450 'inputType' => $inputType,
451 'value' => $custom_fields[ $name ]
452 ];
453 }
454 }
455
456 return $sorted_custom_fields;
457 }
458
459
460 public static function render_svg_icon_using_attr( $attributes = array() ) {
461 $default_attributes = array(
462 'path' => '',
463 'viewBox' => '0 0 24 24',
464 'className' => 'icon-class',
465 'width' => '24',
466 'height' => '24',
467 );
468
469 // Merge passed attributes with default values
470 $attributes = array_merge( $default_attributes, $attributes );
471
472 // Sanitize attributes for safety
473 $path = esc_attr( $attributes['path'] );
474 $viewBox = esc_attr( $attributes['viewBox'] );
475 $className = esc_attr( $attributes['className'] );
476 $width = esc_attr( $attributes['width'] );
477 $height = esc_attr( $attributes['height'] );
478
479 // Output the SVG
480 return '
481 <svg
482 xmlns="http://www.w3.org/2000/svg"
483 viewBox="' . $viewBox . '"
484 class="' . $className . '"
485 width="' . $width . '"
486 height="' . $height . '">
487 <path d="' . $path . '"></path>
488 </svg>';
489 }
490
491 public static function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
492 $template = false;
493
494 if ( ! $template ) {
495 $template = self::locate_template( $template_name, $template_path, $default_path );
496 }
497
498 // Allow 3rd party plugin filter template file from their plugin.
499 $filter_template = apply_filters( 'ablocks/get_template', $template, $template_name, $args, $template_path, $default_path );
500
501 if ( $filter_template !== $template ) {
502 if ( ! file_exists( $filter_template ) ) {
503 /* translators: %s template */
504 wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'ablocks' ), '<code>' . $filter_template . '</code>' ), '1.0.0' );
505
506 return;
507 }
508 $template = $filter_template;
509 }
510
511 $action_args = array(
512 'template_name' => $template_name,
513 'template_path' => $template_path,
514 'located' => $template,
515 'args' => $args,
516 );
517
518 if ( ! empty( $args ) && is_array( $args ) ) {
519 if ( isset( $args['action_args'] ) ) {
520 wc_doing_it_wrong(
521 __FUNCTION__,
522 __( 'action_args should not be overwritten when calling ablocks/get_template.', 'ablocks' ),
523 '1.0.0'
524 );
525 unset( $args['action_args'] );
526 }
527 extract( $args ); // @codingStandardsIgnoreLine
528 }
529
530 do_action( 'ablocks/before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
531 include $action_args['located'];
532
533 do_action( 'ablocks/after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
534 }
535
536 public static function locate_template( $template_name, $template_path = '', $default_path = '' ) {
537 if ( ! $template_path ) {
538 $template_path = self::template_path();
539 }
540
541 if ( ! $default_path ) {
542 $default_path = self::plugin_path() . 'templates/';
543 }
544
545 if ( empty( $template ) ) {
546 $template = locate_template(
547 array(
548 trailingslashit( $template_path ) . $template_name,
549 $template_name,
550 )
551 );
552 }
553 if ( ! $template ) {
554 $template = $default_path . $template_name;
555 }
556
557 // Return what we found.
558 return apply_filters( 'ablocks/locate_template', $template, $template_name, $template_path );
559 }
560
561 public static function template_path() {
562 return apply_filters( 'ablocks/template_path', 'ablocks/' );
563 }
564 public static function plugin_path() {
565 return apply_filters( 'ablocks/plugin_path', ABLOCKS_ROOT_DIR_PATH );
566 }
567
568 public static function get_addon_active_status( $addon_name, $is_pro = false ) {
569 global $ablocks_addons;
570 if ( $is_pro && ! self::is_active_ablocks_pro() ) {
571 return false;
572 }
573 if ( isset( $ablocks_addons->{$addon_name} ) ) {
574 return (bool) $ablocks_addons->{$addon_name};
575 }
576
577 return false;
578 }
579
580 public static function sanitize_checkbox_field( $boolean ) {
581 return filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN );
582 }
583
584 public static function is_valid_site_url( string $url ): bool {
585 return str_starts_with( $url, get_option( 'siteurl' ) );
586 }
587
588 }
589