PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.8
JetFormBuilder — Dynamic Blocks Form Builder v2.1.8
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
arguments 3 years ago arrayable 3 years ago filters 3 years ago http 3 years ago post 3 years ago repository 3 years ago resources 3 years ago security 3 years ago theme 3 years ago attributes-trait.php 3 years ago base-attributes-trait.php 3 years ago builder-helper.php 3 years ago compatibility.php 3 years ago gallery.php 3 years ago get-icon-trait.php 3 years ago get-template-trait.php 3 years ago html-attributes-trait.php 3 years ago instance-trait.php 3 years ago macros-parser.php 3 years ago messages-helper-trait.php 3 years ago tools.php 3 years ago
tools.php
562 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_taxonomies_for_modify( array $args = array() ): array {
121 $taxonomies = get_taxonomies( $args, 'objects' );
122 $response = array();
123
124 /** @var \WP_Taxonomy $taxonomy */
125 foreach ( $taxonomies as $taxonomy ) {
126 $response[] = array(
127 'label' => $taxonomy->label,
128 'value' => "jet_tax__{$taxonomy->name}",
129 );
130 }
131
132 return self::with_placeholder( $response );
133 }
134
135 public static function get_generators_list_for_js() {
136 $generators = Plugin::instance()->form->get_generators_list();
137
138 return self::prepare_list_for_js( $generators );
139 }
140
141 public static function get_allowed_mimes_list_for_js() {
142 return array_values( get_allowed_mime_types() );
143 }
144
145 /**
146 * Returns all registeredroles for JS
147 */
148 public static function get_user_roles_for_js( $exclude = array( 'administrator' ) ) {
149
150 $roles = self::get_user_roles( $exclude );
151 $result = array();
152
153 foreach ( $roles as $role => $label ) {
154 $result[] = array(
155 'value' => $role,
156 'label' => $label,
157 );
158 }
159
160 return self::with_placeholder( $result );
161 }
162
163 public static function get_options_pages_for_js() {
164 $pages = array();
165
166 if ( function_exists( 'jet_engine' ) ) {
167 $pages = jet_engine()->options_pages->get_options_pages_for_select();
168 }
169
170 return self::prepare_list_for_js( $pages );
171 }
172
173 /**
174 * Returns pages list
175 *
176 * @return [type] [description]
177 */
178 public static function get_pages_list_for_js() {
179 $pages = get_pages();
180
181 return self::prepare_list_for_js( $pages, 'ID', 'post_title' );
182 }
183
184 /**
185 * Returns pages list
186 *
187 * @param bool $for_elementor
188 *
189 * @param array $args
190 *
191 * @return array [description]
192 */
193 public static function get_forms_list_for_js( $for_elementor = false, $args = array() ) {
194 $posts = get_posts(
195 array_merge(
196 array(
197 'post_status' => 'publish',
198 'posts_per_page' => - 1,
199 'post_type' => jet_form_builder()->post_type->slug(),
200 ),
201 $args
202 )
203 );
204
205 return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor );
206 }
207
208 /**
209 * Returns all registered user roles
210 *
211 * @param string[] $exclude
212 *
213 * @return array [type] [description]
214 */
215 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
216
217 if ( ! function_exists( 'get_editable_roles' ) ) {
218 return array();
219 } else {
220 $roles = get_editable_roles();
221 $result = array();
222
223 foreach ( $roles as $role => $data ) {
224 if ( ! in_array( $role, $exclude ) ) {
225 $result[ $role ] = $data['name'];
226 }
227 }
228
229 return $result;
230 }
231 }
232
233 /**
234 * Prepare passed array for using in JS options
235 *
236 * @param array $array
237 * @param null $value_key
238 * @param null $label_key
239 * @param bool $for_elementor
240 *
241 * Only if $for_elementor === false
242 * @param array $additional_attrs
243 *
244 * @return array [type] [description]
245 */
246 public static function prepare_list_for_js(
247 $array = array(),
248 $value_key = null,
249 $label_key = null,
250 $for_elementor = false,
251 $additional_attrs = array()
252 ) {
253
254 $result = array();
255
256 if ( ! is_array( $array ) || empty( $array ) ) {
257 return $result;
258 }
259
260 foreach ( $array as $key => $item ) {
261
262 $value = null;
263 $label = null;
264
265 if ( is_scalar( $item ) ) {
266 $value = $key;
267 $label = $item;
268 } else {
269 $value = self::get_property( $item, $value_key );
270 $label = self::get_property( $item, $label_key );
271 }
272
273 if ( $for_elementor ) {
274 $result[ $value ] = $label;
275 } else {
276 $prepared = array(
277 'value' => $value,
278 'label' => $label,
279 );
280 foreach ( $additional_attrs as $attr ) {
281 $prepared[ $attr ] = self::get_property( $item, $attr );
282 }
283
284 $result[] = $prepared;
285 }
286 }
287
288 return $result;
289
290 }
291
292 public static function with_placeholder( $array, $label = '--' ) {
293 return array_merge(
294 array(
295 array( 'label' => $label, 'value' => '' ),
296 ),
297 $array
298 );
299 }
300
301 /**
302 * Check if is valid timestamp
303 *
304 * @param mixed $timestamp
305 *
306 * @return boolean
307 */
308 public static function is_valid_timestamp( $timestamp ) {
309 return ( (string) (int) $timestamp === $timestamp || (int) $timestamp === $timestamp )
310 && ( $timestamp <= PHP_INT_MAX )
311 && ( $timestamp >= ~PHP_INT_MAX );
312 }
313
314 public static function array_merge_intersect_key( $source, $arrays ) {
315 foreach ( $source as $index => $path ) {
316 $name = isset( $path['path'] ) ? $path['path'] : $index;
317
318 $deep_value = self::getDeepValue( $name, $arrays );
319
320 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
321 unset( $source[ $index ] );
322 } else {
323 $source[ $index ] = $deep_value;
324 }
325 }
326
327 return $source;
328 }
329
330 public static function getDeepValue( $key, $source ) {
331 $keys = explode( '/', $key );
332 $last = end( $keys );
333 reset( $keys );
334
335 return self::deep( $keys, current( $keys ), $last, $source );
336 }
337
338 private static function deep( $array, $key, $last, $source ) {
339
340 if ( isset( $source[ $key ] ) ) {
341 if ( $last !== $key ) {
342 return self::deep( $array, next( $array ), $last, $source[ $key ] );
343 }
344
345 return $source[ $key ];
346 }
347
348 return self::EMPTY_DEEP_VALUE;
349 }
350
351 public static function call( $callback, ...$params ) {
352 if ( ! is_callable( $callback ) ) {
353 return;
354 }
355
356 call_user_func( $callback, ...$params );
357 }
358
359 public static function decode_unserializable( $value ) {
360 $data = self::decode_json( $value );
361
362 return $data ?: maybe_unserialize( $value );
363 }
364
365 public static function decode_json( $json ) {
366 if ( is_array( $json ) ) {
367 foreach ( $json as $key => $row ) {
368 $json[ $key ] = static::decode_json( $row );
369 }
370
371 return $json;
372 }
373 if ( defined( 'JSON_INVALID_UTF8_IGNORE' ) ) {
374 return json_decode( $json, true, 512, JSON_INVALID_UTF8_IGNORE );
375 }
376
377 return json_decode( $json, true );
378 }
379
380 public static function encode_json( $json ) {
381 return wp_json_encode( $json, JSON_UNESCAPED_UNICODE );
382 }
383
384 public static function sanitize_recursive( $source = null ) {
385 if ( ! is_array( $source ) ) {
386 return self::sanitize( $source );
387 }
388
389 $result = array();
390
391 foreach ( $source as $key => $value ) {
392 $result[ $key ] = self::sanitize_recursive( $value );
393 }
394
395 return $result;
396 }
397
398 public static function maybe_recursive_sanitize( $source = null ) {
399 return self::sanitize_recursive( $source );
400 }
401
402 public static function sanitize( $source ) {
403 if ( self::is_url( $source ) ) {
404 return esc_url_raw( $source );
405 }
406
407 return self::sanitize_text_field( $source );
408 }
409
410 public static function is_url( $url ) {
411 return wp_http_validate_url( $url );
412 }
413
414 public static function sanitize_text_field( $source, $replace_enqueue = true ) {
415 $str = (string) $source;
416
417 $filtered = wp_check_invalid_utf8( $str );
418 if ( $replace_enqueue ) {
419 $filtered = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $filtered );
420 }
421
422 return trim( $filtered );
423 }
424
425 /**
426 * @param $type
427 * @param mixed ...$values
428 *
429 * @return mixed
430 */
431 private static function call_escape_func( $type, ...$values ) {
432 return call_user_func( self::get_escape_func( $type ), ...$values );
433 }
434
435 public static function recursive_wp_kses( $source, $allowed_html = 'strip' ) {
436 if ( ! is_array( $source ) ) {
437 return wp_kses( $source, $allowed_html );
438 }
439
440 $result = array();
441 foreach ( $source as $key => $value ) {
442 $result[ $key ] = self::sanitize_recursive( $value );
443 }
444
445 return $result;
446 }
447
448 public static function sanitize_files( $source ) {
449 if ( ! is_array( $source ) ) {
450 return false;
451 }
452
453 $response = array();
454
455 foreach ( $source as $index => $item ) {
456 foreach ( $item as $key => $value ) {
457 switch ( $key ) {
458 case 'error':
459 case 'size':
460 $response[ $index ][ $key ] = absint( $value );
461 break;
462 case 'name':
463 $response[ $index ][ $key ] = sanitize_file_name( $value );
464 break;
465 case 'type':
466 $response[ $index ][ $key ] = sanitize_mime_type( $value );
467 break;
468 case 'tmp_name':
469 $response[ $index ][ $key ] = sanitize_text_field( $value );
470 break;
471 }
472 }
473 }
474
475 return $response;
476 }
477
478 /**
479 * @param $source
480 *
481 * @return string
482 */
483 private static function esc_template( $source, $replace_enqueue = true ): string {
484 return self::sanitize_text_field( $source, $replace_enqueue );
485 }
486
487 public static function get_jet_engine_version() {
488 return function_exists( 'jet_engine' )
489 ? jet_engine()->get_version()
490 : false;
491 }
492
493 public static function is_readable( string $filename ) {
494 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
495 return strlen( $filename ) <= PHP_MAXPATHLEN && @is_readable( $filename );
496 }
497
498 /**
499 * Returns template path
500 *
501 * @param [type] $path [description]
502 *
503 * @return [type] [description]
504 */
505 public static function get_global_template( $path = '' ) {
506 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
507 }
508
509 public static function get_property( $source, $name, $if_not_exist = '' ) {
510 if ( is_object( $source ) ) {
511 return $source->{$name} ?? $if_not_exist;
512 }
513
514 return $source[ $name ] ?? $if_not_exist;
515 }
516
517 public static function esc_template_string( $source, $replace_enqueue = true ) {
518 return self::call_escape_func( 'template', $source, $replace_enqueue );
519 }
520
521 public static function is_repeater_val( $value ): bool {
522 if ( is_array( $value ) && ! empty( $value ) ) {
523 foreach ( $value as $item ) {
524 return is_array( $item );
525 }
526 }
527
528 return false;
529 }
530
531 public static function set_current_post( $post_id ) {
532 global $post;
533
534 $post = get_post( absint( $post_id ) );
535 }
536
537 public static function prepare_repeater_value( $value, $fields_map ): array {
538 $prepared_value = array();
539
540 foreach ( $value as $index => $row ) {
541 $prepared_row = array();
542
543 foreach ( $row as $item_key => $item_value ) {
544 $item_key = ! empty( $fields_map[ $item_key ] ) ? self::sanitize_text_field( $fields_map[ $item_key ] ) : $item_key;
545 $prepared_row[ $item_key ] = $item_value;
546 }
547
548 $prepared_value[ 'item-' . $index ] = $prepared_row;
549 }
550
551 return $prepared_value;
552 }
553
554 public static function is_webhook(): bool {
555 return (
556 defined( 'JET_FB_REST_WEBHOOK' ) &&
557 JET_FB_REST_WEBHOOK
558 );
559 }
560
561 }
562