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