-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelpers.php
431 lines (389 loc) · 11.1 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/*-------------------------------------------------------------------------------------------------
- This file is part of the WPSF package. -
- This package is Open Source Software. For the full copyright and license -
- information, please view the LICENSE file which was distributed with this -
- source code. -
- -
- @package WPSF -
- @author Varun Sridharan <[email protected]> -
-------------------------------------------------------------------------------------------------*/
global $wpsf_errors;
$wpsf_errors = array();
if ( ! defined( 'ABSPATH' ) ) {
die();
} // Cannot access pages directly.
if ( ! function_exists( 'wpsf_init_element' ) ) {
/**
* Creates A Fields Instance
*
* @param array $field
* @param string $value
* @param string $unique
*
* @return array
*/
function wpsf_init_element( $field = array(), $value = '', $unique = '' ) {
$class = 'WPSFramework_Option_' . $field ['type'];
wpsf_autoloader( $class );
if ( class_exists( $class ) ) {
$element = new $class( $field, $value, $unique );
$instance_id = $element->id;
$field['instance_id'] = $instance_id;
}
return $field;
}
}
if ( ! function_exists( 'wpsf_add_element' ) ) {
/**
* Adds A WPSF Field & Renders it.
*
* @param array $field
* @param string $value
* @param string $unique
* @param bool $force
*
* @return string
*/
function wpsf_add_element( $field = array(), $value = '', $unique = '', $force = false ) {
$output = '';
if ( isset( $field['instance_id'] ) && false === $force ) {
$_instance = wpsf_field_registry()->get( $field['instance_id'] );
if ( $_instance instanceof WPSFramework_Options ) {
ob_start();
$_instance->final_output();
return ob_get_clean();
}
return wpsf_add_element( $field, $value, $unique, true );
} else {
$class = 'WPSFramework_Option_' . $field ['type'];
if ( isset( $field['clone'] ) && true === $field['clone'] ) {
$class = 'WPSFramework_Field_Cloner';
}
wpsf_autoloader( $class );
if ( class_exists( $class ) ) {
ob_start();
$element = new $class( $field, $value, $unique );
$element->final_output();
$output .= ob_get_clean();
} else {
$output .= '<p>' . sprintf( esc_html__( 'This field class is not available! %s', 'wpsf-framework' ), '<strong>' . $class . '</strong>' ) . ' </p > ';
}
}
return $output;
}
}
if ( ! function_exists( 'wpsf_unarray_fields' ) ) {
/**
* Returns all field types that can be unarrayed.
*
* @return array
*/
function wpsf_unarray_fields() {
return apply_filters( 'wpsf_unarray_fields_types', array( 'tab', 'group', 'fieldset', 'accordion' ) );
}
}
if ( ! function_exists( 'wpsf_is_unarray_field' ) ) {
/**
* Checks if field type is unarray.
*
* @param mixed $type .
*
* @return bool
*/
function wpsf_is_unarray_field( $type ) {
if ( is_array( $type ) && isset( $type['clone'] ) && true === $type['clone'] ) {
return true;
} elseif ( is_array( $type ) && isset( $type['type'] ) ) {
return in_array( $type['type'], wpsf_unarray_fields() );
}
return in_array( $type, wpsf_unarray_fields() );
}
}
if ( ! function_exists( 'wpsf_is_unarrayed' ) ) {
/**
* Checks if field is unarray.
*
* @param mixed $field .
*
* @return bool
*/
function wpsf_is_unarrayed( $field = array() ) {
if ( wpsf_is_unarray_field( $field ) ) {
if ( isset( $field['un_array'] ) && true === $field['un_array'] ) {
return true;
}
}
return false;
}
}
if ( ! function_exists( 'wpsf_encode_string' ) ) {
/**
* Encode string for backup options
*
* @since 1.0.0
* @version 1.0.0
*
* @param $string
*
* @return string
*/
function wpsf_encode_string( $string ) {
return serialize( $string );
}
}
if ( ! function_exists( 'wpsf_decode_string' ) ) {
/**
* Decode string for backup options
*
* @since 1.0.0
* @version 1.0.0
*
* @param $string
*
* @return mixed
*/
function wpsf_decode_string( $string ) {
return unserialize( $string );
}
}
if ( ! function_exists( 'wpsf_get_google_fonts' ) ) {
/**
* Get google font from json file
*
* @since 1.0.0
* @version 1.0.0
* @return array|mixed|object
*/
function wpsf_get_google_fonts() {
global $wpsf_google_fonts;
if ( ! empty( $wpsf_google_fonts ) ) {
return $wpsf_google_fonts;
} else {
ob_start();
wpsf_locate_template( 'fields/typography/google-fonts.json' );
$json = ob_get_clean();
$wpsf_google_fonts = json_decode( $json );
return $wpsf_google_fonts;
}
}
}
if ( ! function_exists( 'wpsf_get_icon_fonts' ) ) {
/**
* Get icon fonts from json file
*
* @since 1.0.0
* @version 1.0.0
*
* @param $file
*
* @return array|mixed|object
*/
function wpsf_get_icon_fonts( $file ) {
ob_start();
wpsf_locate_template( $file );
$json = ob_get_clean();
return json_decode( $json );
}
}
if ( ! function_exists( 'wpsf_array_search' ) ) {
/**
* Array search key & value
*
* @since 1.0.0
* @version 1.0.0
*
* @param $array
* @param $key
* @param $value
*
* @return array
*/
function wpsf_array_search( $array, $key, $value ) {
$results = array();
if ( is_array( $array ) ) {
if ( isset( $array[ $key ] ) && $value === $array[ $key ] ) {
$results [] = $array;
}
foreach ( $array as $sub_array ) {
$results = array_merge( $results, wpsf_array_search( $sub_array, $key, $value ) );
}
}
return $results;
}
}
if ( ! function_exists( 'wpsf_get_var' ) ) {
/**
* Getting POST Var
*
* @since 1.0.0
* @version 1.0.0
*
* @param $var
* @param string $default
*
* @return string
*/
function wpsf_get_var( $var, $default = '' ) {
if ( isset( $_POST[ $var ] ) ) {
return $_POST[ $var ];
}
if ( isset( $_GET[ $var ] ) ) {
return $_GET[ $var ];
}
return $default;
}
}
if ( ! function_exists( 'wpsf_get_vars' ) ) {
/**
* Getting POST Vars
*
* @since 1.0.0
* @version 1.0.0
*
* @param $var
* @param $depth
* @param string $default
*
* @return string
*/
function wpsf_get_vars( $var, $depth, $default = '' ) {
if ( isset( $_POST[ $var ][ $depth ] ) ) {
return $_POST[ $var ][ $depth ];
}
if ( isset( $_GET[ $var ][ $depth ] ) ) {
return $_GET[ $var ][ $depth ];
}
return $default;
}
}
if ( ! function_exists( 'wpsf_js_vars' ) ) {
/**
* Converts PHP Array into JS JSON String with script tag and returns it.
*
* @param $object_name
* @param $l10n
* @param bool $with_script_tag
*
* @return string
*/
function wpsf_js_vars( $object_name = '', $l10n, $with_script_tag = true ) {
foreach ( (array) $l10n as $key => $value ) {
if ( ! is_scalar( $value ) ) {
continue;
}
$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
}
$script = null;
if ( ! empty( $object_name ) ) {
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
} else {
$script = wp_json_encode( $l10n );
}
if ( ! empty( $after ) ) {
$script .= "\n$after;";
}
if ( $with_script_tag ) {
return '<script type="text/javascript" >' . $script . '</script>';
}
return $script;
}
}
if ( ! function_exists( 'wpsf_add_errors' ) ) {
/**
* Adds Error to global $wpsf_error array.
*
* @param $errs
*
*/
function wpsf_add_errors( $errs ) {
global $wpsf_errors;
if ( is_array( $wpsf_errors ) && is_array( $errs ) ) {
$wpsf_errors = array_merge( $wpsf_errors, $errs );
} else {
$wpsf_errors = $errs;
}
}
}
if ( ! function_exists( 'wpsf_get_errors' ) ) {
/**
* Returns gloabl $wpsf_errors.
*
* @return array
*/
function wpsf_get_errors() {
global $wpsf_errors;
return $wpsf_errors;
}
}
if ( ! function_exists( 'wpsf_modern_navs' ) ) {
/**
* Renders Modern Theme Menu
*
* @param $navs
* @param $class
* @param null $parent
*/
function wpsf_modern_navs( $navs, $class, $parent = null ) {
$parent = ( null === $parent ) ? '' : 'data-parent-section="' . $parent . '"';
foreach ( $navs as $i => $nav ) :
$title = ( isset( $nav['title'] ) ) ? $nav['title'] : '';
$href = ( isset( $nav['href'] ) && false !== $nav['href'] ) ? $nav['href'] : '#';
if ( ! empty( $nav['submenus'] ) ) {
$is_active = ( isset( $nav['is_active'] ) && true === $nav['is_active'] ) ? ' style="display: block;"' : '';
$is_active_li = ( isset( $nav['is_active'] ) && true === $nav['is_active'] ) ? ' wpsf-tab-active ' : '';
echo '<li class="wpsf-sub ' . $is_active_li . '">';
echo '<a href="#" class="wpsf-arrow">' . $class->icon( $nav ) . ' ' . $title . '</a>';
echo '<ul ' . $is_active . '>';
wpsf_modern_navs( $nav['submenus'], $class, $nav['name'] );
echo '</ul>';
echo '</li>';
} else {
if ( isset( $nav['is_separator'] ) && true === $nav['is_separator'] ) {
echo '<li><div class="wpsf-seperator">' . $class->icon( $nav ) . ' ' . $title . '</div></li>';
} else {
$is_active = ( isset( $nav['is_active'] ) && true === $nav['is_active'] ) ? "class='wpsf-section-active'" : '';
echo '<li>';
echo '<a ' . $is_active . ' href="' . $href . '" ' . $parent . ' data-section="' . $nav['name'] . '">' . $class->icon( $nav ) . ' ' . $title . '</a>';
echo '</li>';
}
}
endforeach;
}
}
if ( ! function_exists( 'wpsf_simple_render_submenus' ) ) {
/**
* @param array $menus
* @param null $parent_name
* @param array $class
*/
function wpsf_simple_render_submenus( $menus = array(), $parent_name = null, $class = array() ) {
global $wpsf_submenus;
$return = array();
$first = current( $menus );
$first = isset( $first['name'] ) ? $first['name'] : false;
foreach ( $menus as $nav ) {
if ( isset( $nav['is_separator'] ) && true === $nav['is_separator'] ) {
continue;
}
$title = ( isset( $nav['title'] ) ) ? $nav['title'] : '';
$is_active = ( isset( $nav['is_active'] ) && true === $nav['is_active'] ) ? ' current ' : '';
if ( empty( $is_active ) ) {
$is_active = ( $parent_name !== $class->active() && $first === $nav['name'] ) ? 'current' : $is_active;
}
$href = '#';
if ( isset( $nav['href'] ) && ( false !== $nav['href'] && '#' !== $nav['href'] && true !== $nav['is_internal_url'] ) ) {
$href = $nav['href'];
$is_active .= ' has-link ';
}
if ( isset( $nav['query_args'] ) && is_array( $nav['query_args'] ) ) {
$url = remove_query_arg( array_keys( $nav['query_args'] ) );
$href = add_query_arg( array_filter( $nav['query_args'] ), $url );
$is_active .= ' has-link ';
}
$icon = $class->icon( $nav );
$return[] = '<li> <a href="' . $href . '" class="' . $is_active . '" data-parent-section="' . $parent_name . '" data-section="' . $nav['name'] . '">' . $icon . ' ' . $title . '</a>';
}
$wpsf_submenus[ $parent_name ] = implode( '|</li>', $return );
}
}