PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.2
WebberZone Top 10 — Popular Posts v4.5.2
4.5.2 4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 All 118 releases
top-10 / includes / options-api.php

options-api.php in WebberZone Top 10 — Popular Posts 4.5.2, at includes/options-api.php

385 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Options API functions.
4 *
5 * @package WebberZone\Top_Ten
6 */
7
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12 /**
13 * Get Settings.
14 *
15 * Retrieves all plugin settings
16 *
17 * @since 2.5.0
18 * @return array Top 10 settings
19 */
20 function tptn_get_settings() {
21
22 $settings = get_option( 'tptn_settings', array() );
23
24 /**
25 * Settings array
26 *
27 * Retrieves all plugin settings
28 *
29 * @since 2.5.0
30 * @param array $settings Settings array
31 */
32 return apply_filters( 'tptn_get_settings', $settings );
33 }
34
35 /**
36 * Get an option
37 *
38 * Looks to see if the specified setting exists, returns default if not
39 *
40 * @since 2.5.0
41 *
42 * @param string $key Option to fetch.
43 * @param mixed $default_value Default option.
44 * @return mixed
45 */
46 function tptn_get_option( $key = '', $default_value = null ) {
47 global $tptn_settings;
48
49 $tptn_settings = tptn_get_settings();
50
51 if ( is_null( $default_value ) ) {
52 $default_value = tptn_get_default_option( $key );
53 }
54
55 $value = isset( $tptn_settings[ $key ] ) ? $tptn_settings[ $key ] : $default_value;
56
57 /**
58 * Filter the value for the option being fetched.
59 *
60 * @since 2.5.0
61 *
62 * @param mixed $value Value of the option
63 * @param mixed $key Name of the option
64 * @param mixed $default_value Default value
65 */
66 $value = apply_filters( 'tptn_get_option', $value, $key, $default_value );
67
68 /**
69 * Key specific filter for the value of the option being fetched.
70 *
71 * @since 2.5.0
72 *
73 * @param mixed $value Value of the option
74 * @param mixed $key Name of the option
75 * @param mixed $default_value Default value
76 */
77 return apply_filters( 'tptn_get_option_' . $key, $value, $key, $default_value );
78 }
79
80
81 /**
82 * Update an option
83 *
84 * Updates an ata setting value in both the db and the global variable.
85 * Warning: Passing a null value will remove
86 * the key from the tptn_options array.
87 *
88 * @since 2.5.0
89 *
90 * @param string $key The Key to update.
91 * @param string|bool|int|null $value The value to set the key to.
92 * @return boolean True if updated, false if not.
93 */
94 function tptn_update_option( $key = '', $value = false ) {
95
96 // If no key, exit.
97 if ( empty( $key ) ) {
98 return false;
99 }
100
101 // If no value, delete.
102 if ( is_null( $value ) ) {
103 $remove_option = tptn_delete_option( $key );
104 return $remove_option;
105 }
106
107 // First let's grab the current settings.
108 $options = get_option( 'tptn_settings', array() );
109 $options = is_array( $options ) ? $options : array();
110
111 // Let's let devs alter that value coming in.
112 /**
113 * Filters a setting value before it is written to the options table.
114 *
115 * @since 2.5.0
116 *
117 * @param mixed $value The value being saved.
118 * @param string $key The settings key being updated.
119 */
120 $value = apply_filters( 'tptn_update_option', $value, $key );
121
122 // Next let's try to update the value.
123 $options[ $key ] = $value;
124 $did_update = update_option( 'tptn_settings', $options );
125
126 // If it updated, let's update the global variable.
127 if ( $did_update ) {
128 global $tptn_settings;
129 $tptn_settings[ $key ] = $value;
130 }
131 return $did_update;
132 }
133
134
135 /**
136 * Remove an option
137 *
138 * Removes an ata setting value in both the db and the global variable.
139 *
140 * @since 2.5.0
141 *
142 * @param string $key The Key to update.
143 * @return boolean True if updated, false if not.
144 */
145 function tptn_delete_option( $key = '' ) {
146
147 // If no key, exit.
148 if ( empty( $key ) ) {
149 return false;
150 }
151
152 // First let's grab the current settings.
153 $options = get_option( 'tptn_settings', array() );
154 $options = is_array( $options ) ? $options : array();
155
156 // Next let's try to update the value.
157 if ( isset( $options[ $key ] ) ) {
158 unset( $options[ $key ] );
159 }
160
161 $did_update = update_option( 'tptn_settings', $options );
162
163 // If it updated, let's update the global variable.
164 if ( $did_update ) {
165 global $tptn_settings;
166 $tptn_settings = $options;
167 }
168 return $did_update;
169 }
170
171
172 /**
173 * Default settings.
174 *
175 * @since 2.5.0
176 *
177 * @return array Default settings
178 */
179 function tptn_settings_defaults() {
180
181 static $options = null;
182
183 if ( null !== $options ) {
184 return $options;
185 }
186
187 $options = array();
188 $default_types = array(
189 'color',
190 'css',
191 'csv',
192 'file',
193 'html',
194 'multicheck',
195 'number',
196 'numbercsv',
197 'password',
198 'postids',
199 'posttypes',
200 'radio',
201 'radiodesc',
202 'repeater',
203 'select',
204 'sensitive',
205 'taxonomies',
206 'text',
207 'textarea',
208 'thumbsizes',
209 'url',
210 'wysiwyg',
211 );
212
213 // Populate some default values.
214 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
215 foreach ( $settings as $option ) {
216 if ( ! isset( $option['id'] ) ) {
217 continue;
218 }
219
220 $setting_id = $option['id'];
221 $setting_type = $option['type'] ?? '';
222 $default_value = '';
223
224 // When checkbox is set to true, set this to 1.
225 if ( 'checkbox' === $setting_type ) {
226 $default_value = isset( $option['default'] ) ? (int) (bool) $option['default'] : 0;
227 } elseif ( isset( $option['default'] ) && in_array( $setting_type, $default_types, true ) ) {
228 $default_value = $option['default'];
229 }
230
231 $options[ $setting_id ] = $default_value;
232 }
233 }
234
235 /**
236 * Filters the default settings array.
237 *
238 * @since 2.5.0
239 *
240 * @param array $options Default settings.
241 */
242 $options = apply_filters( 'tptn_settings_defaults', $options );
243
244 return $options;
245 }
246
247 /**
248 * Get the saved settings merged with the default settings, so newly-registered settings
249 * that are not yet present in the saved `tptn_settings` option still resolve to a value.
250 *
251 * @since 4.4.0
252 *
253 * @return array Settings merged with defaults.
254 */
255 function tptn_get_settings_with_defaults() {
256 return array_merge( tptn_settings_defaults(), tptn_get_settings() );
257 }
258
259
260 /**
261 * Get the default option for a specific key
262 *
263 * @since 1.3.0
264 *
265 * @param string $key Key of the option to fetch.
266 * @return mixed
267 */
268 function tptn_get_default_option( $key = '' ) {
269
270 /** This filter is documented in includes/options-api.php */
271 $default_settings = apply_filters( 'tptn_settings_defaults', \WebberZone\Top_Ten\Admin\Settings::get_defaults() );
272
273 if ( array_key_exists( $key, $default_settings ) ) {
274 return $default_settings[ $key ];
275 } else {
276 return false;
277 }
278 }
279
280
281 /**
282 * Reset settings.
283 *
284 * @since 2.5.0
285 *
286 * @return void
287 */
288 function tptn_settings_reset() {
289 delete_option( 'tptn_settings' );
290 }
291
292
293 /**
294 * Get registered settings types for cache key generation.
295 *
296 * @since 4.2.0
297 *
298 * @return array Array of setting types keyed by setting ID.
299 */
300 function tptn_get_registered_settings_types() {
301 $options = array();
302
303 // Populate some default values.
304 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
305 foreach ( $settings as $option ) {
306 $options[ $option['id'] ] = $option['type'];
307 }
308 }
309
310 /**
311 * Filter the settings types array for cache key generation.
312 *
313 * @since 4.2.0
314 *
315 * @param array $options Array of setting types keyed by setting ID.
316 */
317 return apply_filters( 'tptn_registered_settings_types', $options );
318 }
319
320
321 /**
322 * Handles the AJAX request to search for tags in the Top 10 settings.
323 *
324 * @since 1.7.0
325 *
326 * @return void
327 */
328 function tptn_tags_search() {
329
330 if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
331 wp_die();
332 }
333
334 $taxonomy = sanitize_key( $_REQUEST['tax'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
335 $tax = get_taxonomy( $taxonomy );
336 if ( ! empty( $taxonomy ) ) {
337 $tax = get_taxonomy( $taxonomy );
338 if ( ! $tax ) {
339 wp_die();
340 }
341
342 if ( ! current_user_can( $tax->cap->assign_terms ) ) {
343 wp_die();
344 }
345 }
346
347 $s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
348
349 $comma = _x( ',', 'tag delimiter' );
350 if ( ',' !== $comma ) {
351 $s = str_replace( $comma, ',', $s );
352 }
353 if ( false !== strpos( $s, ',' ) ) {
354 $s = explode( ',', $s );
355 $s = $s[ count( $s ) - 1 ];
356 }
357 $s = trim( $s, " \t\n\r\0\x0B" );
358
359 /**
360 * This filter has been defined in /wp-admin/includes/ajax-actions.php
361 */
362 $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
363
364 /*
365 * Require $term_search_min_chars chars for matching (default: 2)
366 * ensure it's a non-negative, non-zero integer.
367 */
368 if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
369 wp_die();
370 }
371
372 $results = get_terms(
373 array(
374 'taxonomy' => $taxonomy,
375 'name__like' => $s,
376 'fields' => 'names',
377 'hide_empty' => false,
378 )
379 );
380
381 echo wp_json_encode( $results );
382 wp_die();
383 }
384 add_action( 'wp_ajax_tptn_tags_search', 'tptn_tags_search' );
385