PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.5.5
JetFormBuilder — Dynamic Blocks Form Builder v1.5.5
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
attributes-trait.php 4 years ago base-attributes-trait.php 4 years ago builder-helper.php 4 years ago condition-helper.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
570 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 * @return [type] [description]
171 */
172 public static function get_pages_list_for_js() {
173 $pages = get_pages();
174
175 return self::prepare_list_for_js( $pages, 'ID', 'post_title' );
176 }
177
178 /**
179 * Returns pages list
180 *
181 * @param bool $for_elementor
182 *
183 * @return array [description]
184 */
185 public static function get_forms_list_for_js( $for_elementor = false ) {
186 $posts = get_posts( array(
187 'post_status' => 'publish',
188 'posts_per_page' => - 1,
189 'post_type' => jet_form_builder()->post_type->slug(),
190 ) );
191
192 return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor );
193 }
194
195 public static function get_form_settings_options( $for_elementor = false ) {
196 $submit_type = array(
197 '' => __( 'Default', 'jet-form-builder' ),
198 'reload' => __( 'Page Reload', 'jet-form-builder' ),
199 'ajax' => __( 'AJAX', 'jet-form-builder' ),
200 );
201 $fields_layout = array(
202 '' => __( 'Default', 'jet-form-builder' ),
203 'column' => __( 'Column', 'jet-form-builder' ),
204 'row' => __( 'Row', 'jet-form-builder' )
205 );
206
207 $label_tag = array(
208 '' => 'Default',
209 'div' => __( 'DIV', 'jet-form-builder' ),
210 'label' => __( 'LABEL', 'jet-form-builder' ),
211 );
212
213 if ( ! $for_elementor ) {
214 $submit_type = self::prepare_list_for_js( $submit_type );
215 $fields_layout = self::prepare_list_for_js( $fields_layout );
216 $label_tag = self::prepare_list_for_js( $label_tag );
217 }
218
219 return array(
220 'submit_type' => $submit_type,
221 'fields_layout' => $fields_layout,
222 'fields_label_tag' => $label_tag
223 );
224 }
225
226 /**
227 * Returns all registered user roles
228 *
229 * @param string[] $exclude
230 *
231 * @return array [type] [description]
232 */
233 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
234
235 if ( ! function_exists( 'get_editable_roles' ) ) {
236 return array();
237 } else {
238 $roles = get_editable_roles();
239 $result = array();
240
241 foreach ( $roles as $role => $data ) {
242 if ( ! in_array( $role, $exclude ) ) {
243 $result[ $role ] = $data['name'];
244 }
245 }
246
247 return $result;
248 }
249 }
250
251 /**
252 * Prepare passed array for using in JS options
253 *
254 * @param array $array
255 * @param null $value_key
256 * @param null $label_key
257 * @param bool $for_elementor
258 *
259 * Only if $for_elementor === false
260 * @param array $additional_attrs
261 *
262 * @return array [type] [description]
263 */
264 public static function prepare_list_for_js(
265 $array = array(),
266 $value_key = null,
267 $label_key = null,
268 $for_elementor = false,
269 $additional_attrs = array()
270 ) {
271
272 $result = array();
273
274 if ( ! is_array( $array ) || empty( $array ) ) {
275 return $result;
276 }
277
278 foreach ( $array as $key => $item ) {
279
280 $value = null;
281 $label = null;
282
283 if ( is_scalar( $item ) ) {
284 $value = $key;
285 $label = $item;
286 } else {
287 $value = self::get_property( $item, $value_key );
288 $label = self::get_property( $item, $label_key );
289 }
290
291 if ( $for_elementor ) {
292 $result[ $value ] = $label;
293 } else {
294 $prepared = array(
295 'value' => $value,
296 'label' => $label,
297 );
298 foreach ( $additional_attrs as $attr ) {
299 $prepared[ $attr ] = self::get_property( $item, $attr );
300 }
301
302 $result[] = $prepared;
303 }
304 }
305
306 return $result;
307
308 }
309
310 public static function with_placeholder( $array, $label = '--' ) {
311 return array_merge(
312 array(
313 array( 'label' => $label, 'value' => '' ),
314 ),
315 $array
316 );
317 }
318
319 /**
320 * Check if is valid timestamp
321 *
322 * @param mixed $timestamp
323 *
324 * @return boolean
325 */
326 public static function is_valid_timestamp( $timestamp ) {
327 return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp )
328 && ( $timestamp <= PHP_INT_MAX )
329 && ( $timestamp >= ~PHP_INT_MAX );
330 }
331
332 public static function array_merge_intersect_key( $source, $arrays ) {
333 foreach ( $source as $index => $path ) {
334 $name = isset( $path['path'] ) ? $path['path'] : $index;
335
336 $deep_value = self::getDeepValue( $name, $arrays );
337
338 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
339 unset( $source[ $index ] );
340 } else {
341 $source[ $index ] = $deep_value;
342 }
343 }
344
345 return $source;
346 }
347
348 public static function getDeepValue( $key, $source ) {
349 $keys = explode( '/', $key );
350 $last = end( $keys );
351 reset( $keys );
352
353 return self::deep( $keys, current( $keys ), $last, $source );
354 }
355
356 private static function deep( $array, $key, $last, $source ) {
357
358 if ( isset( $source[ $key ] ) ) {
359 if ( $last !== $key ) {
360 return self::deep( $array, next( $array ), $last, $source[ $key ] );
361 }
362
363 return $source[ $key ];
364 }
365
366 return self::EMPTY_DEEP_VALUE;
367 }
368
369 public static function run_callbacks( $callbacks = array(), ...$params ) {
370 if ( ! $callbacks ) {
371 return;
372 }
373
374 foreach ( $callbacks as $callback ) {
375 if ( ! is_callable( $callback ) ) {
376 continue;
377 }
378 call_user_func( $callback, ...$params );
379 }
380 }
381
382 public static function decode_unserializable( $value ) {
383 $data = json_decode( $value, true );
384
385 return $data ? $data : maybe_unserialize( $value );
386 }
387
388 public static function sanitize_recursive( $source = null ) {
389 if ( ! is_array( $source ) ) {
390 return self::sanitize( $source );
391 }
392
393 $result = array();
394
395 foreach ( $source as $key => $value ) {
396 $result[ $key ] = self::sanitize_recursive( $value );
397 }
398
399 return $result;
400 }
401
402 public static function maybe_recursive_sanitize( $source = null ) {
403 return self::sanitize_recursive( $source );
404 }
405
406 public static function sanitize( $source ) {
407 if ( self::is_url( $source ) ) {
408 return esc_url_raw( $source );
409 }
410
411 return self::sanitize_text_field( $source );
412 }
413
414 public static function is_url( $url ) {
415 return wp_http_validate_url( $url );
416 }
417
418 public static function sanitize_text_field( $source, $replace_enqueue = true ) {
419 $str = (string) $source;
420
421 $filtered = wp_check_invalid_utf8( $str );
422 if ( $replace_enqueue ) {
423 $filtered = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $filtered );
424 }
425
426 return trim( $filtered );
427 }
428
429 /**
430 * @param $type
431 * @param mixed ...$values
432 *
433 * @return mixed
434 */
435 private static function call_escape_func( $type, ...$values ) {
436 return call_user_func( self::get_escape_func( $type ), ...$values );
437 }
438
439 public static function recursive_wp_kses( $source, $allowed_html = 'strip' ) {
440 if ( ! is_array( $source ) ) {
441 return wp_kses( $source, $allowed_html );
442 }
443
444 $result = array();
445 foreach ( $source as $key => $value ) {
446 $result[ $key ] = self::sanitize_recursive( $value );
447 }
448
449 return $result;
450 }
451
452 public static function sanitize_files( $source ) {
453 if ( ! is_array( $source ) ) {
454 return false;
455 }
456
457 $response = array();
458
459 foreach ( $source as $index => $item ) {
460 foreach ( $item as $key => $value ) {
461 switch ( $key ) {
462 case 'error':
463 case 'size':
464 $response[ $index ][ $key ] = absint( $value );
465 break;
466 case 'name':
467 $response[ $index ][ $key ] = sanitize_file_name( $value );
468 break;
469 case 'type':
470 $response[ $index ][ $key ] = sanitize_mime_type( $value );
471 break;
472 case 'tmp_name':
473 $response[ $index ][ $key ] = sanitize_text_field( $value );
474 break;
475 }
476 }
477 }
478
479 return $response;
480 }
481
482 /**
483 * @param $source
484 *
485 * @return string
486 */
487 private static function esc_template( $source, $replace_enqueue = true ): string {
488 return self::sanitize_text_field( $source, $replace_enqueue );
489 }
490
491 public static function get_jet_engine_version() {
492 return function_exists( 'jet_engine' )
493 ? jet_engine()->get_version()
494 : false;
495 }
496
497 public static function is_readable( string $filename ) {
498 return strlen( $filename ) <= PHP_MAXPATHLEN && is_readable( $filename );
499 }
500
501 /**
502 * Returns template path
503 *
504 * @param [type] $path [description]
505 *
506 * @return [type] [description]
507 */
508 public static function get_global_template( $path = '' ) {
509 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
510 }
511
512 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
513 $merged = $array1;
514
515 foreach ( $array2 as $key => &$value ) {
516 if ( is_array( $value )
517 && isset ( $merged [ $key ] )
518 && is_array( $merged [ $key ] )
519 ) {
520 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
521 } else {
522 $merged [ $key ] = $value;
523 }
524 }
525
526 return $merged;
527 }
528
529 public static function get_property( $source, $name, $if_not_exist = '' ) {
530 if ( is_object( $source ) ) {
531 return $source->{$name} ?? $if_not_exist;
532 }
533
534 return $source[ $name ] ?? $if_not_exist;
535 }
536
537 public static function render_block_with_context( $block, $context ) {
538 return ( new \WP_Block( $block, $context ) )->render();
539 }
540
541 public static function esc_template_string( $source, $replace_enqueue = true ) {
542 return self::call_escape_func( 'template', $source, $replace_enqueue );
543 }
544
545 public static function is_repeater_val( $value ) {
546 if ( is_array( $value ) && ! empty( $value ) ) {
547 return is_array( array_shift( $value ) );
548 } else {
549 return false;
550 }
551 }
552
553 public static function prepare_repeater_value( $value, $fields_map ) {
554 $prepared_value = array();
555
556 foreach ( $value as $index => $row ) {
557 $prepared_row = array();
558
559 foreach ( $row as $item_key => $item_value ) {
560 $item_key = ! empty( $fields_map[ $item_key ] ) ? Tools::sanitize_text_field( $fields_map[ $item_key ] ) : $item_key;
561 $prepared_row[ $item_key ] = $item_value;
562 }
563
564 $prepared_value[ 'item-' . $index ] = $prepared_row;
565 }
566
567 return $prepared_value;
568 }
569
570 }