-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions-api.php
302 lines (254 loc) · 7.13 KB
/
options-api.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
<?php
/**
* Plugin_Name Options API.
*
* @link https://webberzone.com
*
* @package Plugin_Name
* @subpackage Admin
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Get Settings.
*
* Retrieves all plugin settings
*
* @since 1.0.0
* @return array AutoClose settings
*/
function plugin_name_get_settings() {
$settings = get_option( 'plugin_name_settings', plugin_name_settings_defaults() );
/**
* Settings array
*
* Retrieves all plugin settings
*
* @since 1.0.0
* @param array $settings Settings array
*/
return apply_filters( 'plugin_name_get_settings', $settings );
}
/**
* Get an option
*
* Looks to see if the specified setting exists, returns default if not
*
* @since 1.0.0
*
* @param string $key Option to fetch.
* @param mixed $default_value Default option.
* @return mixed
*/
function plugin_name_get_option( $key = '', $default_value = null ) {
global $plugin_name_settings;
if ( empty( $plugin_name_settings ) ) {
$plugin_name_settings = plugin_name_get_settings();
}
if ( is_null( $default_value ) ) {
$default_value = plugin_name_get_default_option( $key );
}
$value = isset( $plugin_name_settings[ $key ] ) ? $plugin_name_settings[ $key ] : $default_value;
/**
* Filter the value for the option being fetched.
*
* @since 1.0.0
*
* @param mixed $value Value of the option
* @param mixed $key Name of the option
* @param mixed $default_value Default value
*/
$value = apply_filters( 'plugin_name_get_option', $value, $key, $default_value );
/**
* Key specific filter for the value of the option being fetched.
*
* @since 1.0.0
*
* @param mixed $value Value of the option
* @param mixed $key Name of the option
* @param mixed $default_value Default value
*/
return apply_filters( 'plugin_name_get_option_' . $key, $value, $key, $default_value );
}
/**
* Update an option
*
* Updates a setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the plugin_name_options array.
*
* @since 1.0.0
*
* @param string $key The Key to update.
* @param string|bool|int $value The value to set the key to.
* @return boolean True if updated, false if not.
*/
function plugin_name_update_option( $key = '', $value = false ) {
// If no key, exit.
if ( empty( $key ) ) {
return false;
}
// If no value, delete.
if ( empty( $value ) ) {
$remove_option = plugin_name_delete_option( $key );
return $remove_option;
}
// First let's grab the current settings.
$options = get_option( 'plugin_name_settings' );
// Let's let devs alter that value coming in.
$value = apply_filters( 'plugin_name_update_option', $value, $key );
// Next let's try to update the value.
$options[ $key ] = $value;
$did_update = update_option( 'plugin_name_settings', $options );
// If it updated, let's update the global variable.
if ( $did_update ) {
global $plugin_name_settings;
$plugin_name_settings[ $key ] = $value;
}
return $did_update;
}
/**
* Remove an option
*
* Removes a setting value in both the db and the global variable.
*
* @since 1.0.0
*
* @param string $key The Key to update.
* @return boolean True if updated, false if not.
*/
function plugin_name_delete_option( $key = '' ) {
// If no key, exit.
if ( empty( $key ) ) {
return false;
}
// First let's grab the current settings.
$options = get_option( 'plugin_name_settings' );
// Next let's try to update the value.
if ( isset( $options[ $key ] ) ) {
unset( $options[ $key ] );
}
$did_update = update_option( 'plugin_name_settings', $options );
// If it updated, let's update the global variable.
if ( $did_update ) {
global $plugin_name_settings;
$plugin_name_settings = $options;
}
return $did_update;
}
/**
* Default settings.
*
* @since 1.0.0
*
* @return array Default settings
*/
function plugin_name_settings_defaults() {
$options = array();
// Populate some default values.
foreach ( Plugin_Name_Settings::get_registered_settings() as $tab => $settings ) {
foreach ( $settings as $option ) {
// When checkbox is set to true, set this to 1.
if ( 'checkbox' === $option['type'] && ! empty( $option['options'] ) ) {
$options[ $option['id'] ] = 1;
} else {
$options[ $option['id'] ] = 0;
}
// If an option is set.
if ( in_array( $option['type'], array( 'textarea', 'css', 'html', 'text', 'url', 'csv', 'color', 'numbercsv', 'postids', 'posttypes', 'number', 'wysiwyg', 'file', 'password' ), true ) && isset( $option['options'] ) ) {
$options[ $option['id'] ] = $option['options'];
}
if ( in_array( $option['type'], array( 'multicheck', 'radio', 'select', 'radiodesc', 'thumbsizes' ), true ) && isset( $option['default'] ) ) {
$options[ $option['id'] ] = $option['default'];
}
}
}
/**
* Filters the default settings array.
*
* @since 1.0.0
*
* @param array $options Default settings.
*/
return apply_filters( 'plugin_name_settings_defaults', $options );
}
/**
* Get the default option for a specific key
*
* @since 1.0.0
*
* @param string $key Key of the option to fetch.
* @return mixed
*/
function plugin_name_get_default_option( $key = '' ) {
$default_settings = plugin_name_settings_defaults();
if ( array_key_exists( $key, $default_settings ) ) {
return $default_settings[ $key ];
} else {
return false;
}
}
/**
* Reset settings.
*
* @since 1.0.0
*
* @return void
*/
function plugin_name_settings_reset() {
delete_option( 'plugin_name_settings' );
}
if ( ! function_exists( 'wz_tag_search' ) ) :
/**
* Function to add an action to search for tags using Ajax.
*
* @since 1.0.0
*
* @return void
*/
function wz_tag_search() {
if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
wp_die( 0 );
}
$taxonomy = sanitize_key( $_REQUEST['tax'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$tax = get_taxonomy( $taxonomy );
if ( ! $tax ) {
wp_die( 0 );
}
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
wp_die( -1 );
}
$s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$s = str_replace( $comma, ',', $s );
}
if ( false !== strpos( $s, ',' ) ) {
$s = explode( ',', $s );
$s = $s[ count( $s ) - 1 ];
}
$s = trim( $s );
/** This filter has been defined in /wp-admin/includes/ajax-actions.php */
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
/*
* Require $term_search_min_chars chars for matching (default: 2)
* ensure it's a non-negative, non-zero integer.
*/
if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
wp_die();
}
$results = get_terms(
array(
'taxonomy' => $taxonomy,
'name__like' => $s,
'fields' => 'names',
'hide_empty' => false,
)
);
echo wp_json_encode( $results );
wp_die();
}
add_action( 'wp_ajax_wz_tag_search', 'wz_tag_search' );
endif;