PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.0
JetFormBuilder — Dynamic Blocks Form Builder v2.0.0
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / classes / tools.php
jetformbuilder / includes / classes Last commit date
arrayable 4 years ago http 4 years ago post 4 years ago repository 4 years ago theme 4 years ago attributes-trait.php 4 years ago base-attributes-trait.php 4 years ago builder-helper.php 4 years ago compatibility.php 4 years ago gallery.php 4 years ago get-icon-trait.php 4 years ago get-template-trait.php 4 years ago html-attributes-trait.php 4 years ago instance-trait.php 4 years ago listing-filter-manager.php 4 years ago listing-filter.php 4 years ago macros-parser.php 4 years ago messages-helper-trait.php 4 years ago tools.php 4 years ago
tools.php
574 lines
1 <?php
2
3 namespace Jet_Form_Builder\Classes;
4
5 // If this file is called directly, abort.
6 use Jet_Form_Builder\Plugin;
7
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12
13 class Tools {
14
15 const EMPTY_DEEP_VALUE = self::class;
16
17 public static function is_editor() {
18 return self::is_block_editor() || self::is_elementor_editor();
19 }
20
21 public static function is_block_editor() {
22 $allowed_actions = array( 'add', 'edit' );
23
24 return (
25 in_array( self::sanitize_get_param( 'context' ), $allowed_actions, true )
26 || in_array( self::sanitize_get_param( 'action' ), $allowed_actions, true )
27 );
28 }
29
30 public static function sanitize_get_param( $param_name ) {
31 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
32 return ! empty( $_GET[ $param_name ] ) ? sanitize_key( $_GET[ $param_name ] ) : '';
33 }
34
35 public static function is_elementor_editor() {
36 if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
37 return false;
38 }
39
40 return ( \Elementor\Plugin::instance()->editor->is_edit_mode() );
41 }
42
43 /**
44 * Returns all post types list to use in JS components
45 *
46 * @param bool $placeholder
47 *
48 * @param array $args
49 * @param string $operator
50 *
51 * @return array [type] [description]
52 */
53 public static function get_post_types_for_js( $placeholder = false, $args = array(), $operator = 'and' ) {
54
55 $post_types = get_post_types( $args, 'objects', $operator );
56
57 $post_types_list = array();
58
59 if ( $placeholder && is_array( $placeholder ) ) {
60 $placeholder['value'] = isset( $placeholder['value'] ) ? $placeholder['value'] : '';
61 $post_types_list[] = $placeholder;
62 }
63
64 foreach ( $post_types as $post_type ) {
65 if ( $post_type->name !== Plugin::instance()->post_type->slug() ) {
66 $post_types_list[] = array(
67 'value' => $post_type->name,
68 'label' => $post_type->label,
69 );
70 }
71 }
72
73 return self::with_placeholder( $post_types_list );
74 }
75
76 /**
77 * Get post types list for options.
78 *
79 * @return array
80 */
81 public static function get_post_types_for_options() {
82 return self::get_post_types_for_js( false, array( 'public' => true ) );
83 }
84
85 private static function get_escape_func( $type ) {
86 switch ( $type ) {
87 case 'template':
88 default:
89 return array( self::class, 'esc_template' );
90 }
91 }
92
93 /**
94 * Sanitize WYSIWYG field
95 *
96 * @param $input
97 *
98 * @return string
99 */
100 public static function sanitize_wysiwyg( $input ) {
101 $input = wp_kses_post( $input );
102 $input = wp_specialchars_decode( stripslashes( $input ), ENT_COMPAT );
103
104 return $input;
105 }
106
107 /**
108 * Return all taxonomies list to use in JS components
109 *
110 * @param array $args
111 *
112 * @return array
113 */
114 public static function get_taxonomies_for_js( $args = array() ) {
115 $taxonomies = get_taxonomies( $args, 'objects' );
116
117 return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) );
118 }
119
120 public static function get_generators_list_for_js() {
121 $generators = Plugin::instance()->form->get_generators_list();
122
123 return self::prepare_list_for_js( $generators );
124 }
125
126 public static function get_allowed_mimes_list_for_js() {
127 $mimes = get_allowed_mime_types();
128
129 $mimes_list = array();
130 foreach ( $mimes as $mime ) {
131 $mimes_list[] = array(
132 'label' => $mime,
133 'value' => $mime,
134 );
135 }
136
137 return $mimes_list;
138 }
139
140 /**
141 * Returns all registeredroles for JS
142 */
143 public static function get_user_roles_for_js( $exclude = array( 'administrator' ) ) {
144
145 $roles = self::get_user_roles( $exclude );
146 $result = array();
147
148 foreach ( $roles as $role => $label ) {
149 $result[] = array(
150 'value' => $role,
151 'label' => $label,
152 );
153 }
154
155 return self::with_placeholder( $result );
156 }
157
158 public static function get_options_pages_for_js() {
159 $pages = array();
160
161 if ( function_exists( 'jet_engine' ) ) {
162 $pages = jet_engine()->options_pages->get_options_pages_for_select();
163 }
164
165 return self::prepare_list_for_js( $pages );
166 }
167
168 /**
169 * Returns pages list
170 *
171 * @return [type] [description]
172 */
173 public static function get_pages_list_for_js() {
174 $pages = get_pages();
175
176 return self::prepare_list_for_js( $pages, 'ID', 'post_title' );
177 }
178
179 /**
180 * Returns pages list
181 *
182 * @param bool $for_elementor
183 *
184 * @param array $args
185 *
186 * @return array [description]
187 */
188 public static function get_forms_list_for_js( $for_elementor = false, $args = array() ) {
189 $posts = get_posts(
190 array_merge(
191 array(
192 'post_status' => 'publish',
193 'posts_per_page' => - 1,
194 'post_type' => jet_form_builder()->post_type->slug(),
195 ),
196 $args
197 )
198 );
199
200 return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor );
201 }
202
203 public static function get_form_settings_options( $for_elementor = false ) {
204 $submit_type = array(
205 '' => __( 'Default', 'jet-form-builder' ),
206 'reload' => __( 'Page Reload', 'jet-form-builder' ),
207 'ajax' => __( 'AJAX', 'jet-form-builder' ),
208 );
209 $fields_layout = array(
210 '' => __( 'Default', 'jet-form-builder' ),
211 'column' => __( 'Column', 'jet-form-builder' ),
212 'row' => __( 'Row', 'jet-form-builder' ),
213 );
214
215 $label_tag = array(
216 '' => 'Default',
217 'div' => __( 'DIV', 'jet-form-builder' ),
218 'label' => __( 'LABEL', 'jet-form-builder' ),
219 );
220
221 if ( ! $for_elementor ) {
222 $submit_type = self::prepare_list_for_js( $submit_type );
223 $fields_layout = self::prepare_list_for_js( $fields_layout );
224 $label_tag = self::prepare_list_for_js( $label_tag );
225 }
226
227 return array(
228 'submit_type' => $submit_type,
229 'fields_layout' => $fields_layout,
230 'fields_label_tag' => $label_tag,
231 );
232 }
233
234 /**
235 * Returns all registered user roles
236 *
237 * @param string[] $exclude
238 *
239 * @return array [type] [description]
240 */
241 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
242
243 if ( ! function_exists( 'get_editable_roles' ) ) {
244 return array();
245 } else {
246 $roles = get_editable_roles();
247 $result = array();
248
249 foreach ( $roles as $role => $data ) {
250 if ( ! in_array( $role, $exclude ) ) {
251 $result[ $role ] = $data['name'];
252 }
253 }
254
255 return $result;
256 }
257 }
258
259 /**
260 * Prepare passed array for using in JS options
261 *
262 * @param array $array
263 * @param null $value_key
264 * @param null $label_key
265 * @param bool $for_elementor
266 *
267 * Only if $for_elementor === false
268 * @param array $additional_attrs
269 *
270 * @return array [type] [description]
271 */
272 public static function prepare_list_for_js(
273 $array = array(),
274 $value_key = null,
275 $label_key = null,
276 $for_elementor = false,
277 $additional_attrs = array()
278 ) {
279
280 $result = array();
281
282 if ( ! is_array( $array ) || empty( $array ) ) {
283 return $result;
284 }
285
286 foreach ( $array as $key => $item ) {
287
288 $value = null;
289 $label = null;
290
291 if ( is_scalar( $item ) ) {
292 $value = $key;
293 $label = $item;
294 } else {
295 $value = self::get_property( $item, $value_key );
296 $label = self::get_property( $item, $label_key );
297 }
298
299 if ( $for_elementor ) {
300 $result[ $value ] = $label;
301 } else {
302 $prepared = array(
303 'value' => $value,
304 'label' => $label,
305 );
306 foreach ( $additional_attrs as $attr ) {
307 $prepared[ $attr ] = self::get_property( $item, $attr );
308 }
309
310 $result[] = $prepared;
311 }
312 }
313
314 return $result;
315
316 }
317
318 public static function with_placeholder( $array, $label = '--' ) {
319 return array_merge(
320 array(
321 array( 'label' => $label, 'value' => '' ),
322 ),
323 $array
324 );
325 }
326
327 /**
328 * Check if is valid timestamp
329 *
330 * @param mixed $timestamp
331 *
332 * @return boolean
333 */
334 public static function is_valid_timestamp( $timestamp ) {
335 return ( (string) (int) $timestamp === $timestamp || (int) $timestamp === $timestamp )
336 && ( $timestamp <= PHP_INT_MAX )
337 && ( $timestamp >= ~PHP_INT_MAX );
338 }
339
340 public static function array_merge_intersect_key( $source, $arrays ) {
341 foreach ( $source as $index => $path ) {
342 $name = isset( $path['path'] ) ? $path['path'] : $index;
343
344 $deep_value = self::getDeepValue( $name, $arrays );
345
346 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
347 unset( $source[ $index ] );
348 } else {
349 $source[ $index ] = $deep_value;
350 }
351 }
352
353 return $source;
354 }
355
356 public static function getDeepValue( $key, $source ) {
357 $keys = explode( '/', $key );
358 $last = end( $keys );
359 reset( $keys );
360
361 return self::deep( $keys, current( $keys ), $last, $source );
362 }
363
364 private static function deep( $array, $key, $last, $source ) {
365
366 if ( isset( $source[ $key ] ) ) {
367 if ( $last !== $key ) {
368 return self::deep( $array, next( $array ), $last, $source[ $key ] );
369 }
370
371 return $source[ $key ];
372 }
373
374 return self::EMPTY_DEEP_VALUE;
375 }
376
377 public static function call( $callback, ...$params ) {
378 if ( ! is_callable( $callback ) ) {
379 return;
380 }
381
382 call_user_func( $callback, ...$params );
383 }
384
385 public static function decode_unserializable( $value ) {
386 $data = self::decode_json( $value );
387
388 return $data ?: maybe_unserialize( $value );
389 }
390
391 public static function decode_json( $json ) {
392 if ( defined( 'JSON_INVALID_UTF8_IGNORE' ) ) {
393 return json_decode( $json, true, 512, JSON_INVALID_UTF8_IGNORE );
394 }
395
396 return json_decode( $json, true );
397 }
398
399 public static function encode_json( $json ) {
400 return wp_json_encode( $json, JSON_UNESCAPED_UNICODE );
401 }
402
403 public static function sanitize_recursive( $source = null ) {
404 if ( ! is_array( $source ) ) {
405 return self::sanitize( $source );
406 }
407
408 $result = array();
409
410 foreach ( $source as $key => $value ) {
411 $result[ $key ] = self::sanitize_recursive( $value );
412 }
413
414 return $result;
415 }
416
417 public static function maybe_recursive_sanitize( $source = null ) {
418 return self::sanitize_recursive( $source );
419 }
420
421 public static function sanitize( $source ) {
422 if ( self::is_url( $source ) ) {
423 return esc_url_raw( $source );
424 }
425
426 return self::sanitize_text_field( $source );
427 }
428
429 public static function is_url( $url ) {
430 return wp_http_validate_url( $url );
431 }
432
433 public static function sanitize_text_field( $source, $replace_enqueue = true ) {
434 $str = (string) $source;
435
436 $filtered = wp_check_invalid_utf8( $str );
437 if ( $replace_enqueue ) {
438 $filtered = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $filtered );
439 }
440
441 return trim( $filtered );
442 }
443
444 /**
445 * @param $type
446 * @param mixed ...$values
447 *
448 * @return mixed
449 */
450 private static function call_escape_func( $type, ...$values ) {
451 return call_user_func( self::get_escape_func( $type ), ...$values );
452 }
453
454 public static function recursive_wp_kses( $source, $allowed_html = 'strip' ) {
455 if ( ! is_array( $source ) ) {
456 return wp_kses( $source, $allowed_html );
457 }
458
459 $result = array();
460 foreach ( $source as $key => $value ) {
461 $result[ $key ] = self::sanitize_recursive( $value );
462 }
463
464 return $result;
465 }
466
467 public static function sanitize_files( $source ) {
468 if ( ! is_array( $source ) ) {
469 return false;
470 }
471
472 $response = array();
473
474 foreach ( $source as $index => $item ) {
475 foreach ( $item as $key => $value ) {
476 switch ( $key ) {
477 case 'error':
478 case 'size':
479 $response[ $index ][ $key ] = absint( $value );
480 break;
481 case 'name':
482 $response[ $index ][ $key ] = sanitize_file_name( $value );
483 break;
484 case 'type':
485 $response[ $index ][ $key ] = sanitize_mime_type( $value );
486 break;
487 case 'tmp_name':
488 $response[ $index ][ $key ] = sanitize_text_field( $value );
489 break;
490 }
491 }
492 }
493
494 return $response;
495 }
496
497 /**
498 * @param $source
499 *
500 * @return string
501 */
502 private static function esc_template( $source, $replace_enqueue = true ): string {
503 return self::sanitize_text_field( $source, $replace_enqueue );
504 }
505
506 public static function get_jet_engine_version() {
507 return function_exists( 'jet_engine' )
508 ? jet_engine()->get_version()
509 : false;
510 }
511
512 public static function is_readable( string $filename ) {
513 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
514 return strlen( $filename ) <= PHP_MAXPATHLEN && @is_readable( $filename );
515 }
516
517 /**
518 * Returns template path
519 *
520 * @param [type] $path [description]
521 *
522 * @return [type] [description]
523 */
524 public static function get_global_template( $path = '' ) {
525 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
526 }
527
528 public static function get_property( $source, $name, $if_not_exist = '' ) {
529 if ( is_object( $source ) ) {
530 return $source->{$name} ?? $if_not_exist;
531 }
532
533 return $source[ $name ] ?? $if_not_exist;
534 }
535
536 public static function esc_template_string( $source, $replace_enqueue = true ) {
537 return self::call_escape_func( 'template', $source, $replace_enqueue );
538 }
539
540 public static function is_repeater_val( $value ): bool {
541 if ( is_array( $value ) && ! empty( $value ) ) {
542 foreach ( $value as $item ) {
543 return is_array( $item );
544 }
545 }
546
547 return false;
548 }
549
550 public static function set_current_post( $post_id ) {
551 global $post;
552
553 $post = get_post( absint( $post_id ) );
554 }
555
556 public static function prepare_repeater_value( $value, $fields_map ): array {
557 $prepared_value = array();
558
559 foreach ( $value as $index => $row ) {
560 $prepared_row = array();
561
562 foreach ( $row as $item_key => $item_value ) {
563 $item_key = ! empty( $fields_map[ $item_key ] ) ? self::sanitize_text_field( $fields_map[ $item_key ] ) : $item_key;
564 $prepared_row[ $item_key ] = $item_value;
565 }
566
567 $prepared_value[ 'item-' . $index ] = $prepared_row;
568 }
569
570 return $prepared_value;
571 }
572
573 }
574