PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.2
JetFormBuilder — Dynamic Blocks Form Builder v1.4.2
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
541 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 '' => '--',
198 'reload' => 'Page Reload',
199 'ajax' => 'AJAX',
200 );
201 $fields_layout = array(
202 '' => '--',
203 'column' => 'Column',
204 'row' => 'Row'
205 );
206
207 if ( ! $for_elementor ) {
208 $submit_type = self::prepare_list_for_js( $submit_type );
209 $fields_layout = self::prepare_list_for_js( $fields_layout );
210 }
211
212 return array(
213 'submit_type' => $submit_type,
214 'fields_layout' => $fields_layout
215 );
216 }
217
218 /**
219 * Returns all registered user roles
220 *
221 * @param string[] $exclude
222 *
223 * @return array [type] [description]
224 */
225 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
226
227 if ( ! function_exists( 'get_editable_roles' ) ) {
228 return array();
229 } else {
230 $roles = get_editable_roles();
231 $result = array();
232
233 foreach ( $roles as $role => $data ) {
234 if ( ! in_array( $role, $exclude ) ) {
235 $result[ $role ] = $data['name'];
236 }
237 }
238
239 return $result;
240 }
241 }
242
243 /**
244 * Prepare passed array for using in JS options
245 *
246 * @param array $array
247 * @param null $value_key
248 * @param null $label_key
249 * @param bool $for_elementor
250 *
251 * Only if $for_elementor === false
252 * @param array $additional_attrs
253 *
254 * @return array [type] [description]
255 */
256 public static function prepare_list_for_js(
257 $array = array(),
258 $value_key = null,
259 $label_key = null,
260 $for_elementor = false,
261 $additional_attrs = array()
262 ) {
263
264 $result = array();
265
266 if ( ! is_array( $array ) || empty( $array ) ) {
267 return $result;
268 }
269
270 foreach ( $array as $key => $item ) {
271
272 $value = null;
273 $label = null;
274
275 if ( is_scalar( $item ) ) {
276 $value = $key;
277 $label = $item;
278 } else {
279 $value = self::get_property( $item, $value_key );
280 $label = self::get_property( $item, $label_key );
281 }
282
283 if ( $for_elementor ) {
284 $result[ $value ] = $label;
285 } else {
286 $prepared = array(
287 'value' => $value,
288 'label' => $label,
289 );
290 foreach ( $additional_attrs as $attr ) {
291 $prepared[ $attr ] = self::get_property( $item, $attr );
292 }
293
294 $result[] = $prepared;
295 }
296 }
297
298 return $result;
299
300 }
301
302 public static function with_placeholder( $array, $label = '--' ) {
303 return array_merge(
304 array(
305 array( 'label' => $label, 'value' => '' ),
306 ),
307 $array
308 );
309 }
310
311 /**
312 * Check if is valid timestamp
313 *
314 * @param mixed $timestamp
315 *
316 * @return boolean
317 */
318 public static function is_valid_timestamp( $timestamp ) {
319 return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp )
320 && ( $timestamp <= PHP_INT_MAX )
321 && ( $timestamp >= ~PHP_INT_MAX );
322 }
323
324 public static function array_merge_intersect_key( $source, $arrays ) {
325 foreach ( $source as $index => $path ) {
326 $name = isset( $path['path'] ) ? $path['path'] : $index;
327
328 $deep_value = self::getDeepValue( $name, $arrays );
329
330 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
331 unset( $source[ $index ] );
332 } else {
333 $source[ $index ] = $deep_value;
334 }
335 }
336
337 return $source;
338 }
339
340 public static function getDeepValue( $key, $source ) {
341 $keys = explode( '/', $key );
342 $last = end( $keys );
343 reset( $keys );
344
345 return self::deep( $keys, current( $keys ), $last, $source );
346 }
347
348 private static function deep( $array, $key, $last, $source ) {
349
350 if ( isset( $source[ $key ] ) ) {
351 if ( $last !== $key ) {
352 return self::deep( $array, next( $array ), $last, $source[ $key ] );
353 }
354
355 return $source[ $key ];
356 }
357
358 return self::EMPTY_DEEP_VALUE;
359 }
360
361 public static function run_callbacks( $callbacks = array(), ...$params ) {
362 if ( ! $callbacks ) {
363 return;
364 }
365
366 foreach ( $callbacks as $callback ) {
367 if ( ! is_callable( $callback ) ) {
368 continue;
369 }
370 call_user_func( $callback, ...$params );
371 }
372 }
373
374 public static function decode_unserializable( $value ) {
375 $data = json_decode( $value, true );
376
377 return $data ? $data : maybe_unserialize( $value );
378 }
379
380 public static function sanitize_recursive( $source = null ) {
381 if ( ! is_array( $source ) ) {
382 return self::sanitize( $source );
383 }
384
385 $result = array();
386
387 foreach ( $source as $key => $value ) {
388 $result[ $key ] = self::sanitize_recursive( $value );
389 }
390
391 return $result;
392 }
393
394 public static function maybe_recursive_sanitize( $source = null ) {
395 return self::sanitize_recursive( $source );
396 }
397
398 public static function sanitize( $source ) {
399 if ( self::is_url( $source ) ) {
400 return esc_url_raw( $source );
401 }
402
403 if ( is_numeric( $source ) ) {
404 return (float) $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 return strlen( $filename ) <= PHP_MAXPATHLEN && is_readable( $filename );
495 }
496
497 /**
498 * Returns template path
499 *
500 * @param [type] $path [description]
501 *
502 * @return [type] [description]
503 */
504 public static function get_global_template( $path = '' ) {
505 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
506 }
507
508 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
509 $merged = $array1;
510
511 foreach ( $array2 as $key => &$value ) {
512 if ( is_array( $value )
513 && isset ( $merged [ $key ] )
514 && is_array( $merged [ $key ] )
515 ) {
516 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
517 } else {
518 $merged [ $key ] = $value;
519 }
520 }
521
522 return $merged;
523 }
524
525 public static function get_property( $source, $name, $if_not_exist = '' ) {
526 if ( is_object( $source ) ) {
527 return $source->{$name} ?? $if_not_exist;
528 }
529
530 return $source[ $name ] ?? $if_not_exist;
531 }
532
533 public static function render_block_with_context( $block, $context ) {
534 return ( new \WP_Block( $block, $context ) )->render();
535 }
536
537 public static function esc_template_string( $source, $replace_enqueue = true ) {
538 return self::call_escape_func( 'template', $source, $replace_enqueue );
539 }
540
541 }