PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.1
JetFormBuilder — Dynamic Blocks Form Builder v1.3.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 builder-helper.php 4 years ago condition-helper.php 4 years ago curl-helper.php 4 years ago factory.php 4 years ago gallery.php 4 years ago get-icon-trait.php 4 years ago get-template-trait.php 4 years ago instance-trait.php 4 years ago listing-filter-manager.php 4 years ago listing-filter.php 4 years ago messages-helper-trait.php 4 years ago tools.php 4 years ago
tools.php
448 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 $action = ! empty( $_GET['context'] ) ? $_GET['context'] : '';
23
24 if ( isset( $_GET['action'] ) ) {
25 $action = $action ? $action : $_GET['action'];
26 }
27
28 return in_array( $action, array( 'add', 'edit' ) );
29 }
30
31 public static function is_elementor_editor() {
32 if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
33 return false;
34 }
35
36 return ( \Elementor\Plugin::instance()->editor->is_edit_mode() );
37 }
38
39 /**
40 * Returns all post types list to use in JS components
41 *
42 * @param bool $placeholder
43 *
44 * @param array $args
45 * @param string $operator
46 *
47 * @return array [type] [description]
48 */
49 public static function get_post_types_for_js( $placeholder = false, $args = array(), $operator = 'and' ) {
50
51 $post_types = get_post_types( $args, 'objects', $operator );
52
53 $post_types_list = array();
54
55 if ( $placeholder && is_array( $placeholder ) ) {
56 $placeholder['value'] = isset( $placeholder['value'] ) ? $placeholder['value'] : '';
57 $post_types_list[] = $placeholder;
58 }
59
60 foreach ( $post_types as $post_type ) {
61 if ( $post_type->name !== Plugin::instance()->post_type->slug() ) {
62 $post_types_list[] = array(
63 'value' => $post_type->name,
64 'label' => $post_type->label,
65 );
66 }
67 }
68
69 return self::with_placeholder( $post_types_list );
70 }
71
72 /**
73 * Get post types list for options.
74 *
75 * @return array
76 */
77 public static function get_post_types_for_options() {
78 return self::get_post_types_for_js( false, array( 'public' => true ) );
79 }
80
81 /**
82 * Sanitize WYSIWYG field
83 *
84 * @param $input
85 *
86 * @return string
87 */
88 public static function sanitize_wysiwyg( $input ) {
89 $input = wp_kses_post( $input );
90 $input = wp_specialchars_decode( stripslashes( $input ), ENT_COMPAT );
91
92 return $input;
93 }
94
95 /**
96 * Return all taxonomies list to use in JS components
97 *
98 * @param array $args
99 *
100 * @return array
101 */
102 public static function get_taxonomies_for_js( $args = array() ) {
103 $taxonomies = get_taxonomies( $args, 'objects' );
104
105 return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) );
106 }
107
108 public static function get_generators_list_for_js() {
109 $generators = Plugin::instance()->form->get_generators_list();
110
111 return self::prepare_list_for_js( $generators );
112 }
113
114 public static function get_allowed_mimes_list_for_js() {
115 $mimes = get_allowed_mime_types();
116
117 $mimes_list = array();
118 foreach ( $mimes as $mime ) {
119 $mimes_list[] = array(
120 'label' => $mime,
121 'value' => $mime
122 );
123 }
124
125 return $mimes_list;
126 }
127
128 /**
129 * Returns all registeredroles for JS
130 */
131 public static function get_user_roles_for_js( $exclude = array( 'administrator' ) ) {
132
133 $roles = self::get_user_roles( $exclude );
134 $result = array();
135
136 foreach ( $roles as $role => $label ) {
137 $result[] = array(
138 'value' => $role,
139 'label' => $label,
140 );
141 }
142
143 return self::with_placeholder( $result );
144 }
145
146 public static function get_options_pages_for_js() {
147 $pages = array();
148
149 if ( function_exists( 'jet_engine' ) ) {
150 $pages = jet_engine()->options_pages->get_options_pages_for_select();
151 }
152
153 return self::prepare_list_for_js( $pages );
154 }
155
156 /**
157 * Returns pages list
158 * @return [type] [description]
159 */
160 public static function get_pages_list_for_js() {
161 $pages = get_pages();
162
163 return self::prepare_list_for_js( $pages, 'ID', 'post_title' );
164 }
165
166 /**
167 * Returns pages list
168 *
169 * @param bool $for_elementor
170 *
171 * @return array [description]
172 */
173 public static function get_forms_list_for_js( $for_elementor = false ) {
174 $posts = get_posts( array(
175 'post_status' => 'publish',
176 'posts_per_page' => - 1,
177 'post_type' => jet_form_builder()->post_type->slug(),
178 ) );
179
180 return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor );
181 }
182
183 public static function get_form_settings_options( $for_elementor = false ) {
184 $submit_type = array(
185 'reload' => 'Page Reload',
186 'ajax' => 'AJAX',
187 );
188 $fields_layout = array(
189 'column' => 'Column',
190 'row' => 'Row'
191 );
192
193 if ( ! $for_elementor ) {
194 $submit_type = self::prepare_list_for_js( $submit_type );
195 $fields_layout = self::prepare_list_for_js( $fields_layout );
196 }
197
198 return array(
199 'submit_type' => $submit_type,
200 'fields_layout' => $fields_layout
201 );
202 }
203
204 /**
205 * Returns all registered user roles
206 *
207 * @param string[] $exclude
208 *
209 * @return array [type] [description]
210 */
211 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
212
213 if ( ! function_exists( 'get_editable_roles' ) ) {
214 return array();
215 } else {
216 $roles = get_editable_roles();
217 $result = array();
218
219 foreach ( $roles as $role => $data ) {
220 if ( ! in_array( $role, $exclude ) ) {
221 $result[ $role ] = $data['name'];
222 }
223 }
224
225 return $result;
226 }
227 }
228
229 /**
230 * Prepare passed array for using in JS options
231 *
232 * @param array $array
233 * @param null $value_key
234 * @param null $label_key
235 * @param bool $for_elementor
236 *
237 * Only if $for_elementor === false
238 * @param array $additional_attrs
239 *
240 * @return array [type] [description]
241 */
242 public static function prepare_list_for_js(
243 $array = array(),
244 $value_key = null,
245 $label_key = null,
246 $for_elementor = false,
247 $additional_attrs = array()
248 ) {
249
250 $result = array();
251
252 if ( ! is_array( $array ) || empty( $array ) ) {
253 return $result;
254 }
255
256 foreach ( $array as $key => $item ) {
257
258 $value = null;
259 $label = null;
260
261 if ( is_scalar( $item ) ) {
262 $value = $key;
263 $label = $item;
264 } else {
265 $value = self::get_property( $item, $value_key );
266 $label = self::get_property( $item, $label_key );
267 }
268
269 if ( $for_elementor ) {
270 $result[ $value ] = $label;
271 } else {
272 $prepared = array(
273 'value' => $value,
274 'label' => $label,
275 );
276 foreach ( $additional_attrs as $attr ) {
277 $prepared[ $attr ] = self::get_property( $item, $attr );
278 }
279
280 $result[] = $prepared;
281 }
282 }
283
284 return $result;
285
286 }
287
288 public static function with_placeholder( $array, $label = '--' ) {
289 return array_merge(
290 array(
291 array( 'label' => $label, 'value' => '' ),
292 ),
293 $array
294 );
295 }
296
297 /**
298 * Check if is valid timestamp
299 *
300 * @param mixed $timestamp
301 *
302 * @return boolean
303 */
304 public static function is_valid_timestamp( $timestamp ) {
305 return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp )
306 && ( $timestamp <= PHP_INT_MAX )
307 && ( $timestamp >= ~PHP_INT_MAX );
308 }
309
310 public static function array_merge_intersect_key( $source, $arrays ) {
311 foreach ( $source as $index => $path ) {
312 $name = isset( $path['path'] ) ? $path['path'] : $index;
313
314 $deep_value = self::getDeepValue( $name, $arrays );
315
316 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
317 unset( $source[ $index ] );
318 } else {
319 $source[ $index ] = $deep_value;
320 }
321 }
322
323 return $source;
324 }
325
326 public static function getDeepValue( $key, $source ) {
327 $keys = explode( '/', $key );
328 $last = end( $keys );
329 reset( $keys );
330
331 return self::deep( $keys, current( $keys ), $last, $source );
332 }
333
334 private static function deep( $array, $key, $last, $source ) {
335
336 if ( isset( $source[ $key ] ) ) {
337 if ( $last !== $key ) {
338 return self::deep( $array, next( $array ), $last, $source[ $key ] );
339 }
340
341 return $source[ $key ];
342 }
343
344 return self::EMPTY_DEEP_VALUE;
345 }
346
347 public static function run_callbacks( $callbacks = array(), ...$params ) {
348 if ( ! $callbacks ) {
349 return;
350 }
351
352 foreach ( $callbacks as $callback ) {
353 if ( ! is_callable( $callback ) ) {
354 continue;
355 }
356 call_user_func( $callback, ...$params );
357 }
358 }
359
360 public static function decode_unserializable( $value ) {
361 $data = json_decode( $value, true );
362
363 return $data ? $data : maybe_unserialize( $value );
364 }
365
366 public static function maybe_recursive_sanitize( $source = null ) {
367 if ( ! is_array( $source ) ) {
368 return esc_attr( $source );
369 }
370
371 $result = array();
372 foreach ( $source as $key => $value ) {
373 $result[ $key ] = self::maybe_recursive_sanitize( $value );
374 }
375
376 return $result;
377 }
378
379 public static function sanitize_files( $source ) {
380 if ( ! is_array( $source ) ) {
381 return false;
382 }
383
384 $response = array();
385
386 foreach ( $source as $index => $item ) {
387 foreach ( $item as $key => $value ) {
388 switch ( $key ) {
389 case 'error':
390 case 'size':
391 $response[ $index ][ $key ] = absint( $value );
392 break;
393 default:
394 $response[ $index ][ $key ] = esc_attr( $value );
395 }
396 }
397 }
398
399 return $response;
400 }
401
402 public static function get_jet_engine_version() {
403 return function_exists( 'jet_engine' )
404 ? jet_engine()->get_version()
405 : false;
406 }
407
408 public static function is_readable( string $filename ) {
409 return strlen( $filename ) <= PHP_MAXPATHLEN && is_readable( $filename );
410 }
411
412 /**
413 * Returns template path
414 *
415 * @param [type] $path [description]
416 *
417 * @return [type] [description]
418 */
419 public static function get_global_template( $path = '' ) {
420 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
421 }
422
423 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
424 $merged = $array1;
425
426 foreach ( $array2 as $key => &$value ) {
427 if ( is_array( $value )
428 && isset ( $merged [ $key ] )
429 && is_array( $merged [ $key ] )
430 ) {
431 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
432 } else {
433 $merged [ $key ] = $value;
434 }
435 }
436
437 return $merged;
438 }
439
440 public static function get_property( $source, $name, $if_not_exist = '' ) {
441 if ( is_object( $source ) ) {
442 return $source->{$name} ?? $if_not_exist;
443 }
444
445 return $source[ $name ] ?? $if_not_exist;
446 }
447
448 }