PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.0
JetFormBuilder — Dynamic Blocks Form Builder v1.3.0
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
422 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 * @return [type] [description]
99 */
100 public static function get_taxonomies_for_js() {
101 $taxonomies = get_taxonomies( array(), 'objects' );
102
103 return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) );
104 }
105
106 public static function get_generators_list_for_js() {
107 $generators = Plugin::instance()->form->get_generators_list();
108
109 return self::prepare_list_for_js( $generators );
110 }
111
112 public static function get_allowed_mimes_list_for_js() {
113 $mimes = get_allowed_mime_types();
114
115 $mimes_list = array();
116 foreach ( $mimes as $mime ) {
117 $mimes_list[] = array(
118 'label' => $mime,
119 'value' => $mime
120 );
121 }
122
123 return $mimes_list;
124 }
125
126 /**
127 * Returns all registeredroles for JS
128 */
129 public static function get_user_roles_for_js( $exclude = array( 'administrator' ) ) {
130
131 $roles = self::get_user_roles( $exclude );
132 $result = array();
133
134 foreach ( $roles as $role => $label ) {
135 $result[] = array(
136 'value' => $role,
137 'label' => $label,
138 );
139 }
140
141 return self::with_placeholder( $result );
142 }
143
144 public static function get_options_pages_for_js() {
145 $pages = array();
146
147 if ( function_exists( 'jet_engine' ) ) {
148 $pages = jet_engine()->options_pages->get_options_pages_for_select();
149 }
150
151 return self::prepare_list_for_js( $pages );
152 }
153
154 /**
155 * Returns pages list
156 * @return [type] [description]
157 */
158 public static function get_pages_list_for_js() {
159 $pages = get_pages();
160
161 return self::prepare_list_for_js( $pages, 'ID', 'post_title' );
162 }
163
164 /**
165 * Returns pages list
166 *
167 * @param bool $for_elementor
168 *
169 * @return array [description]
170 */
171 public static function get_forms_list_for_js( $for_elementor = false ) {
172 $posts = get_posts( array(
173 'post_status' => 'publish',
174 'posts_per_page' => - 1,
175 'post_type' => jet_form_builder()->post_type->slug(),
176 ) );
177
178 return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor );
179 }
180
181 public static function get_form_settings_options( $for_elementor = false ) {
182 $submit_type = array(
183 'reload' => 'Page Reload',
184 'ajax' => 'AJAX',
185 );
186 $fields_layout = array(
187 'column' => 'Column',
188 'row' => 'Row'
189 );
190
191 if ( ! $for_elementor ) {
192 $submit_type = self::prepare_list_for_js( $submit_type );
193 $fields_layout = self::prepare_list_for_js( $fields_layout );
194 }
195
196 return array(
197 'submit_type' => $submit_type,
198 'fields_layout' => $fields_layout
199 );
200 }
201
202 /**
203 * Returns all registered user roles
204 *
205 * @param string[] $exclude
206 *
207 * @return array [type] [description]
208 */
209 public static function get_user_roles( $exclude = array( 'administrator' ) ) {
210
211 if ( ! function_exists( 'get_editable_roles' ) ) {
212 return array();
213 } else {
214 $roles = get_editable_roles();
215 $result = array();
216
217 foreach ( $roles as $role => $data ) {
218 if ( ! in_array( $role, $exclude ) ) {
219 $result[ $role ] = $data['name'];
220 }
221 }
222
223 return $result;
224 }
225 }
226
227 /**
228 * Prepare passed array for using in JS options
229 *
230 * @return [type] [description]
231 */
232 public static function prepare_list_for_js( $array = array(), $value_key = null, $label_key = null, $for_elementor = false ) {
233
234 $result = array();
235
236 if ( ! is_array( $array ) || empty( $array ) ) {
237 return $result;
238 }
239
240 foreach ( $array as $key => $item ) {
241
242 $value = null;
243 $label = null;
244
245 if ( is_object( $item ) ) {
246 $value = $item->$value_key;
247 $label = $item->$label_key;
248 } elseif ( is_array( $item ) ) {
249 $value = $item[ $value_key ];
250 $label = $item[ $label_key ];
251 } else {
252 $value = $key;
253 $label = $item;
254 }
255
256 if ( $for_elementor ) {
257 $result[ $value ] = $label;
258 } else {
259 $result[] = array(
260 'value' => $value,
261 'label' => $label,
262 );
263 }
264 }
265
266 return $result;
267
268 }
269
270 public static function with_placeholder( $array, $label = '--' ) {
271 return array_merge(
272 array(
273 array( 'label' => $label, 'value' => '' ),
274 ),
275 $array
276 );
277 }
278
279 /**
280 * Check if is valid timestamp
281 *
282 * @param mixed $timestamp
283 *
284 * @return boolean
285 */
286 public static function is_valid_timestamp( $timestamp ) {
287 return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp )
288 && ( $timestamp <= PHP_INT_MAX )
289 && ( $timestamp >= ~PHP_INT_MAX );
290 }
291
292 public static function array_merge_intersect_key( $source, $arrays ) {
293 foreach ( $source as $index => $path ) {
294 $name = isset( $path['path'] ) ? $path['path'] : $index;
295
296 $deep_value = self::getDeepValue( $name, $arrays );
297
298 if ( self::EMPTY_DEEP_VALUE === $deep_value ) {
299 unset( $source[ $index ] );
300 } else {
301 $source[ $index ] = $deep_value;
302 }
303 }
304
305 return $source;
306 }
307
308 public static function getDeepValue( $key, $source ) {
309 $keys = explode( '/', $key );
310 $last = end( $keys );
311 reset( $keys );
312
313 return self::deep( $keys, current( $keys ), $last, $source );
314 }
315
316 private static function deep( $array, $key, $last, $source ) {
317
318 if ( isset( $source[ $key ] ) ) {
319 if ( $last !== $key ) {
320 return self::deep( $array, next( $array ), $last, $source[ $key ] );
321 }
322
323 return $source[ $key ];
324 }
325
326 return self::EMPTY_DEEP_VALUE;
327 }
328
329 public static function run_callbacks( $callbacks = array(), ...$params ) {
330 if ( ! $callbacks ) {
331 return;
332 }
333
334 foreach ( $callbacks as $callback ) {
335 if ( ! is_callable( $callback ) ) {
336 continue;
337 }
338 call_user_func( $callback, ...$params );
339 }
340 }
341
342 public static function decode_unserializable( $value ) {
343 $data = json_decode( $value, true );
344
345 return $data ? $data : maybe_unserialize( $value );
346 }
347
348 public static function maybe_recursive_sanitize( $source = null ) {
349 if ( ! is_array( $source ) ) {
350 return esc_attr( $source );
351 }
352
353 $result = array();
354 foreach ( $source as $key => $value ) {
355 $result[ $key ] = self::maybe_recursive_sanitize( $value );
356 }
357
358 return $result;
359 }
360
361 public static function sanitize_files( $source ) {
362 if ( ! is_array( $source ) ) {
363 return false;
364 }
365
366 $response = array();
367
368 foreach ( $source as $index => $item ) {
369 foreach ( $item as $key => $value ) {
370 switch ( $key ) {
371 case 'error':
372 case 'size':
373 $response[ $index ][ $key ] = absint( $value );
374 break;
375 default:
376 $response[ $index ][ $key ] = esc_attr( $value );
377 }
378 }
379 }
380
381 return $response;
382 }
383
384 public static function get_jet_engine_version() {
385 return function_exists( 'jet_engine' )
386 ? jet_engine()->get_version()
387 : false;
388 }
389
390 public static function is_readable( string $filename ) {
391 return strlen( $filename ) <= PHP_MAXPATHLEN && is_readable( $filename );
392 }
393
394 /**
395 * Returns template path
396 *
397 * @param [type] $path [description]
398 *
399 * @return [type] [description]
400 */
401 public static function get_global_template( $path = '' ) {
402 return JET_FORM_BUILDER_PATH . 'templates/' . $path;
403 }
404
405 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
406 $merged = $array1;
407
408 foreach ( $array2 as $key => &$value ) {
409 if ( is_array( $value )
410 && isset ( $merged [ $key ] )
411 && is_array( $merged [ $key ] )
412 ) {
413 $merged [ $key ] = self::array_merge_recursive_distinct( $merged [ $key ], $value );
414 } else {
415 $merged [ $key ] = $value;
416 }
417 }
418
419 return $merged;
420 }
421
422 }