PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.5.1
JetFormBuilder — Dynamic Blocks Form Builder v1.5.1
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
549 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 if ( is_numeric( $source ) ) {
412 return (float) $source;
413 }
414
415 return self::sanitize_text_field( $source );
416 }
417
418 public static function is_url( $url ) {
419 return wp_http_validate_url( $url );
420 }
421
422 public static function sanitize_text_field( $source, $replace_enqueue = true ) {
423 $str = (string) $source;
424
425 $filtered = wp_check_invalid_utf8( $str );
426 if ( $replace_enqueue ) {
427 $filtered = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $filtered );
428 }
429
430 return trim( $filtered );
431 }
432
433 /**
434 * @param $type
435 * @param mixed ...$values
436 *
437 * @return mixed
438 */
439 private static function call_escape_func( $type, ...$values ) {
440 return call_user_func( self::get_escape_func( $type ), ...$values );
441 }
442
443 public static function recursive_wp_kses( $source, $allowed_html = 'strip' ) {
444 if ( ! is_array( $source ) ) {
445 return wp_kses( $source, $allowed_html );
446 }
447
448 $result = array();
449 foreach ( $source as $key => $value ) {
450 $result[ $key ] = self::sanitize_recursive( $value );
451 }
452
453 return $result;
454 }
455
456 public static function sanitize_files( $source ) {
457 if ( ! is_array( $source ) ) {
458 return false;
459 }
460
461 $response = array();
462
463 foreach ( $source as $index => $item ) {
464 foreach ( $item as $key => $value ) {
465 switch ( $key ) {
466 case 'error':
467 case 'size':
468 $response[ $index ][ $key ] = absint( $value );
469 break;
470 case 'name':
471 $response[ $index ][ $key ] = sanitize_file_name( $value );
472 break;
473 case 'type':
474 $response[ $index ][ $key ] = sanitize_mime_type( $value );
475 break;
476 case 'tmp_name':
477 $response[ $index ][ $key ] = sanitize_text_field( $value );
478 break;
479 }
480 }
481 }
482
483 return $response;
484 }
485
486 /**
487 * @param $source
488 *
489 * @return string
490 */
491 private static function esc_template( $source, $replace_enqueue = true ): string {
492 return self::sanitize_text_field( $source, $replace_enqueue );
493 }
494
495 public static function get_jet_engine_version() {
496 return function_exists( 'jet_engine' )
497 ? jet_engine()->get_version()
498 : false;
499 }
500
501 public static function is_readable( string $filename ) {
502 return strlen( $filename ) <= PHP_MAXPATHLEN && is_readable( $filename );
503 }
504
505 /**
506 * Returns template path
507 *
508 * @param [type] $path [description]
509 *
510 * @return [type] [description]
511 */
512 public static function get_global_template( $path = '' ) {
513 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
514 }
515
516 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
517 $merged = $array1;
518
519 foreach ( $array2 as $key => &$value ) {
520 if ( is_array( $value )
521 && isset ( $merged [ $key ] )
522 && is_array( $merged [ $key ] )
523 ) {
524 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
525 } else {
526 $merged [ $key ] = $value;
527 }
528 }
529
530 return $merged;
531 }
532
533 public static function get_property( $source, $name, $if_not_exist = '' ) {
534 if ( is_object( $source ) ) {
535 return $source->{$name} ?? $if_not_exist;
536 }
537
538 return $source[ $name ] ?? $if_not_exist;
539 }
540
541 public static function render_block_with_context( $block, $context ) {
542 return ( new \WP_Block( $block, $context ) )->render();
543 }
544
545 public static function esc_template_string( $source, $replace_enqueue = true ) {
546 return self::call_escape_func( 'template', $source, $replace_enqueue );
547 }
548
549 }