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