|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* LSX sanitize callbacks for the customizer options. |
|
4
|
|
|
* |
|
5
|
|
|
* @package lsx |
|
6
|
|
|
* @subpackage sanitize |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
10
|
|
|
exit; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
if ( ! function_exists( 'lsx_sanitize_choices' ) ) : |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Sanitize a value from a list of allowed values. |
|
17
|
|
|
* |
|
18
|
|
|
* @package lsx |
|
19
|
|
|
* @subpackage sanitize |
|
20
|
|
|
* |
|
21
|
|
|
* @param mixed $value The value to sanitize. |
|
22
|
|
|
* @param mixed $setting The setting for which the sanitizing is occurring. |
|
23
|
|
|
* @return mixed The sanitized value. |
|
24
|
|
|
*/ |
|
25
|
|
|
function lsx_sanitize_choices( $value, $setting ) { |
|
26
|
|
|
if ( is_object( $setting ) ) { |
|
27
|
|
|
$setting = $setting->id; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$choices = lsx_customizer_sanitize_get_choices( $setting ); |
|
31
|
|
|
|
|
32
|
|
|
if ( ! is_wp_error( $choices ) && ! empty( $choices ) ) { |
|
33
|
|
|
$allowed_choices = array_keys( $choices ); |
|
34
|
|
|
|
|
35
|
|
|
if ( ! in_array( $value, $allowed_choices ) ) { |
|
36
|
|
|
$value = lsx_customizer_sanitize_get_default( $setting ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $value; |
|
40
|
|
|
} else { |
|
41
|
|
|
return $choices; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
endif; |
|
46
|
|
|
|
|
47
|
|
|
if ( ! function_exists( 'lsx_customizer_sanitize_get_choices' ) ) : |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Helper function to return the choices for a field. |
|
51
|
|
|
* |
|
52
|
|
|
* @package lsx |
|
53
|
|
|
* @subpackage sanitize |
|
54
|
|
|
* |
|
55
|
|
|
* @param string |
|
56
|
|
|
* @return mixed $field |
|
57
|
|
|
*/ |
|
58
|
|
|
function lsx_customizer_sanitize_get_choices( $id ) { |
|
59
|
|
|
global $lsx_customizer; |
|
60
|
|
|
|
|
61
|
|
|
$can_validate = method_exists( 'WP_Customize_Setting', 'validate' ); |
|
62
|
|
|
$field = $lsx_customizer->get_control( $id ); |
|
63
|
|
|
|
|
64
|
|
|
if ( ! isset( $field['choices'] ) ) { |
|
65
|
|
|
return $can_validate ? new WP_Error( 'notexists', esc_html__( 'Choice doesn\'t exist', 'lsx' ) ) : false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $field['choices']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
endif; |
|
72
|
|
|
|
|
73
|
|
|
if ( ! function_exists( 'lsx_customizer_sanitize_get_default' ) ) : |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Helper function to return defaults. |
|
77
|
|
|
* |
|
78
|
|
|
* @package lsx |
|
79
|
|
|
* @subpackage sanitize |
|
80
|
|
|
* |
|
81
|
|
|
* @param string |
|
82
|
|
|
* @return mixed $default |
|
83
|
|
|
*/ |
|
84
|
|
|
function lsx_customizer_sanitize_get_default( $id ) { |
|
85
|
|
|
global $lsx_customizer; |
|
86
|
|
|
$setting = $lsx_customizer->get_setting( $id ); |
|
87
|
|
|
|
|
88
|
|
|
if ( isset( $setting['default'] ) ) { |
|
89
|
|
|
return $setting['default']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
endif; |
|
96
|
|
|
|
|
97
|
|
|
if ( ! function_exists( 'lsx_sanitize_checkbox' ) ) : |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sanitizes an single or multiple checkbox input. |
|
101
|
|
|
* |
|
102
|
|
|
* @package lsx |
|
103
|
|
|
* @subpackage sanitize |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $input |
|
106
|
|
|
* @return array $output |
|
107
|
|
|
*/ |
|
108
|
|
|
function lsx_sanitize_checkbox( $input ) { |
|
109
|
|
|
$can_validate = method_exists( 'WP_Customize_Setting', 'validate' ); |
|
110
|
|
|
|
|
111
|
|
|
if ( ! is_bool( $input ) ) { |
|
112
|
|
|
return $can_validate ? new WP_Error( 'notboolean', esc_html__( 'Not a boolean', 'lsx' ) ) : false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $input; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
endif; |
|
119
|
|
|
|