1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_JSON_API_Get_Options_Endpoint extends Jetpack_JSON_API_Endpoint { |
4
|
|
|
|
5
|
|
|
protected $needed_capabilities = 'manage_options'; |
6
|
|
|
|
7
|
|
|
static $options = array( |
|
|
|
|
8
|
|
|
'blogname' => 'string' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Data that we want to sync. |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
static $mock_options = array( |
|
|
|
|
16
|
|
|
'admin_url' => array( |
17
|
|
|
'type' => 'url', |
18
|
|
|
'callback' => 'get_admin_url' |
19
|
|
|
) |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constants that we want to sync |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
static $constants = array( |
|
|
|
|
27
|
|
|
'EMPTY_TRASH_DAYS' => 'int', |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
function result() { |
31
|
|
|
$data = array(); |
32
|
|
|
if ( isset( $args['options'] ) && is_array( $args['options'] ) ) { |
33
|
|
|
$all_settings = $args['options']; |
34
|
|
|
} else { |
35
|
|
|
$type = isset( $args['options'] ) ? $args['options'] : 'all'; |
|
|
|
|
36
|
|
|
switch( $type ) { |
37
|
|
|
case 'options': |
38
|
|
|
$all_settings = array_keys( self::$options ); |
39
|
|
|
break; |
40
|
|
|
case 'mock_options': |
41
|
|
|
$all_settings = array_keys( self::$mock_options ); |
42
|
|
|
break; |
43
|
|
|
case 'constants': |
44
|
|
|
$all_settings = array_keys( self::$constants ); |
45
|
|
|
break; |
46
|
|
|
case 'all': |
47
|
|
|
default: |
48
|
|
|
$all_settings = array_merge( array_keys( self::$options ), array_keys( self::$mock_options ), array_keys( self::$constants ) ); |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
foreach ( $all_settings as $name ) { |
53
|
|
|
$data[ $name ] = self::get( $name ); |
54
|
|
|
} |
55
|
|
|
return array( 'options', $data ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get individual setting |
60
|
|
|
* |
61
|
|
|
* @param sting $name |
62
|
|
|
* @param string $type |
|
|
|
|
63
|
|
|
* @param string or array $callback |
64
|
|
|
* @param boolean $is_constant |
|
|
|
|
65
|
|
|
* @return value of the setting |
66
|
|
|
*/ |
67
|
|
|
static function get( $name ) { |
68
|
|
|
// Options |
69
|
|
|
if ( array_key_exists( $name, self::$options ) ) { |
70
|
|
|
return self::validate_option( get_option( $name ), self::$options[ $name ] ); |
71
|
|
|
// Mock options |
72
|
|
|
} elseif ( array_key_exists( $name, self::$mock_options ) ) { |
73
|
|
|
if ( is_callable( self::$mock_options[ $name ][ 'callback' ] ) ) { |
74
|
|
|
$args = array(); |
75
|
|
|
if ( isset( self::$mock_options[ $name ][ 'callback_args' ] ) && |
76
|
|
|
is_array( self::$mock_options[ $name ][ 'callback_args' ] ) ) { |
77
|
|
|
$args = self::$mock_options[ $name ][ 'callback_args' ]; |
78
|
|
|
} |
79
|
|
|
$data = call_user_func_array( self::$mock_options[ $name ][ 'callback' ], $args ); |
80
|
|
|
return self::validate_option( $data, self::$mock_options[ $name ][ 'type' ] ); |
81
|
|
|
} else { |
82
|
|
|
return new WP_Error( json_encode( self::$mock_options[ $name ][ 'callback' ] ) . ' can not be called' ); |
83
|
|
|
} |
84
|
|
|
// Constants |
85
|
|
|
} elseif ( array_key_exists( $name, self::$constants) ) { |
86
|
|
|
if ( defined( $name ) ) { |
87
|
|
|
return self::validate_option( constant( $name ) , self::$constants[ $name ] ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
static function validate( $data, $type = null ) { |
94
|
|
|
if ( is_null( $data ) ) { |
95
|
|
|
return $data; |
96
|
|
|
} |
97
|
|
|
switch( $type ) { |
98
|
|
|
case 'bool': |
99
|
|
|
return boolval( $data ); |
100
|
|
|
case 'url': |
101
|
|
|
return esc_url( $data ); |
102
|
|
|
case 'on': |
103
|
|
|
return ( 'on' == $data ? true : false ); |
104
|
|
|
break; |
|
|
|
|
105
|
|
|
case 'closed': |
106
|
|
|
return ( 'closed' != $data ? true : false ); |
107
|
|
|
case 'string': |
108
|
|
|
return strval( $data ); |
109
|
|
|
case 'int': |
110
|
|
|
return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
111
|
|
|
case 'float': |
112
|
|
|
return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
113
|
|
|
case 'array': |
114
|
|
|
return ( is_array( $data ) ? $data : array() ); |
115
|
|
|
case 'rtrim-slash': |
116
|
|
|
return strval( rtrim( $data, '/' ) ); |
117
|
|
|
} |
118
|
|
|
if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
119
|
|
|
return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
120
|
|
|
} elseif ( is_array( $type ) ) { |
121
|
|
|
// Is the array associative? |
122
|
|
|
if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
123
|
|
|
foreach ( $type as $item => $check ) { |
124
|
|
|
$data[ $item ] = self::validate( $data[ $item ], $check ); |
125
|
|
|
} |
126
|
|
|
return $data; |
127
|
|
|
} else { |
128
|
|
|
// check if the value exists in the array if not return the first value. |
129
|
|
|
// Ex $type = array( 'open', 'closed' ); defaults to 'open' |
130
|
|
|
return ( in_array( $data, $type ) ? $data: $type[0] ); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
// Don't check for validity here |
134
|
|
|
if ( 'no-validation' == $type ) { |
135
|
|
|
return $data; |
136
|
|
|
} |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
static function validate_option( $data, $type = null ) { |
141
|
|
|
if ( is_null( $data ) ) { |
142
|
|
|
return $data; |
143
|
|
|
} |
144
|
|
|
switch( $type ) { |
145
|
|
|
case 'bool': |
146
|
|
|
return boolval( $data ); |
147
|
|
|
case 'url': |
148
|
|
|
return esc_url( $data ); |
149
|
|
|
case 'on': |
150
|
|
|
return ( 'on' == $data ? true : false ); |
151
|
|
|
break; |
|
|
|
|
152
|
|
|
case 'closed': |
153
|
|
|
return ( 'closed' != $data ? true : false ); |
154
|
|
|
case 'string': |
155
|
|
|
return strval( $data ); |
156
|
|
|
case 'int': |
157
|
|
|
return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
158
|
|
|
case 'float': |
159
|
|
|
return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
160
|
|
|
case 'array': |
161
|
|
|
return ( is_array( $data ) ? $data : array() ); |
162
|
|
|
case 'rtrim-slash': |
163
|
|
|
return strval( rtrim( $data, '/' ) ); |
164
|
|
|
} |
165
|
|
|
if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
166
|
|
|
return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
167
|
|
|
} elseif ( is_array( $type ) ) { |
168
|
|
|
// Is the array associative? |
169
|
|
|
if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
170
|
|
|
foreach ( $type as $item => $check ) { |
171
|
|
|
$data[ $item ] = self::validate( $data[ $item ], $check ); |
172
|
|
|
} |
173
|
|
|
return $data; |
174
|
|
|
} else { |
175
|
|
|
// check if the value exists in the array if not return the first value. |
176
|
|
|
// Ex $type = array( 'open', 'closed' ); defaults to 'open' |
177
|
|
|
return ( in_array( $data, $type ) ? $data: $type[0] ); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
// Don't check for validity here |
181
|
|
|
if ( 'no-validation' == $type ) { |
182
|
|
|
return $data; |
183
|
|
|
} |
184
|
|
|
return null; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.