PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.1
WebberZone Top 10 — Popular Posts v4.5.1
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 1.6.1 All 117 releases
top-10 / includes / options-api.php

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

383 lines 8.3 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' );
109
110 // Let's let devs alter that value coming in.
111 /**
112 * Filters a setting value before it is written to the options table.
113 *
114 * @since 2.5.0
115 *
116 * @param mixed $value The value being saved.
117 * @param string $key The settings key being updated.
118 */
119 $value = apply_filters( 'tptn_update_option', $value, $key );
120
121 // Next let's try to update the value.
122 $options[ $key ] = $value;
123 $did_update = update_option( 'tptn_settings', $options );
124
125 // If it updated, let's update the global variable.
126 if ( $did_update ) {
127 global $tptn_settings;
128 $tptn_settings[ $key ] = $value;
129 }
130 return $did_update;
131 }
132
133
134 /**
135 * Remove an option
136 *
137 * Removes an ata setting value in both the db and the global variable.
138 *
139 * @since 2.5.0
140 *
141 * @param string $key The Key to update.
142 * @return boolean True if updated, false if not.
143 */
144 function tptn_delete_option( $key = '' ) {
145
146 // If no key, exit.
147 if ( empty( $key ) ) {
148 return false;
149 }
150
151 // First let's grab the current settings.
152 $options = get_option( 'tptn_settings' );
153
154 // Next let's try to update the value.
155 if ( isset( $options[ $key ] ) ) {
156 unset( $options[ $key ] );
157 }
158
159 $did_update = update_option( 'tptn_settings', $options );
160
161 // If it updated, let's update the global variable.
162 if ( $did_update ) {
163 global $tptn_settings;
164 $tptn_settings = $options;
165 }
166 return $did_update;
167 }
168
169
170 /**
171 * Default settings.
172 *
173 * @since 2.5.0
174 *
175 * @return array Default settings
176 */
177 function tptn_settings_defaults() {
178
179 static $options = null;
180
181 if ( null !== $options ) {
182 return $options;
183 }
184
185 $options = array();
186 $default_types = array(
187 'color',
188 'css',
189 'csv',
190 'file',
191 'html',
192 'multicheck',
193 'number',
194 'numbercsv',
195 'password',
196 'postids',
197 'posttypes',
198 'radio',
199 'radiodesc',
200 'repeater',
201 'select',
202 'sensitive',
203 'taxonomies',
204 'text',
205 'textarea',
206 'thumbsizes',
207 'url',
208 'wysiwyg',
209 );
210
211 // Populate some default values.
212 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
213 foreach ( $settings as $option ) {
214 if ( ! isset( $option['id'] ) ) {
215 continue;
216 }
217
218 $setting_id = $option['id'];
219 $setting_type = $option['type'] ?? '';
220 $default_value = '';
221
222 // When checkbox is set to true, set this to 1.
223 if ( 'checkbox' === $setting_type ) {
224 $default_value = isset( $option['default'] ) ? (int) (bool) $option['default'] : 0;
225 } elseif ( isset( $option['default'] ) && in_array( $setting_type, $default_types, true ) ) {
226 $default_value = $option['default'];
227 }
228
229 $options[ $setting_id ] = $default_value;
230 }
231 }
232
233 /**
234 * Filters the default settings array.
235 *
236 * @since 2.5.0
237 *
238 * @param array $options Default settings.
239 */
240 $options = apply_filters( 'tptn_settings_defaults', $options );
241
242 return $options;
243 }
244
245 /**
246 * Get the saved settings merged with the default settings, so newly-registered settings
247 * that are not yet present in the saved `tptn_settings` option still resolve to a value.
248 *
249 * @since 4.4.0
250 *
251 * @return array Settings merged with defaults.
252 */
253 function tptn_get_settings_with_defaults() {
254 return array_merge( tptn_settings_defaults(), tptn_get_settings() );
255 }
256
257
258 /**
259 * Get the default option for a specific key
260 *
261 * @since 1.3.0
262 *
263 * @param string $key Key of the option to fetch.
264 * @return mixed
265 */
266 function tptn_get_default_option( $key = '' ) {
267
268 /** This filter is documented in includes/options-api.php */
269 $default_settings = apply_filters( 'tptn_settings_defaults', \WebberZone\Top_Ten\Admin\Settings::get_defaults() );
270
271 if ( array_key_exists( $key, $default_settings ) ) {
272 return $default_settings[ $key ];
273 } else {
274 return false;
275 }
276 }
277
278
279 /**
280 * Reset settings.
281 *
282 * @since 2.5.0
283 *
284 * @return void
285 */
286 function tptn_settings_reset() {
287 delete_option( 'tptn_settings' );
288 }
289
290
291 /**
292 * Get registered settings types for cache key generation.
293 *
294 * @since 4.2.0
295 *
296 * @return array Array of setting types keyed by setting ID.
297 */
298 function tptn_get_registered_settings_types() {
299 $options = array();
300
301 // Populate some default values.
302 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
303 foreach ( $settings as $option ) {
304 $options[ $option['id'] ] = $option['type'];
305 }
306 }
307
308 /**
309 * Filter the settings types array for cache key generation.
310 *
311 * @since 4.2.0
312 *
313 * @param array $options Array of setting types keyed by setting ID.
314 */
315 return apply_filters( 'tptn_registered_settings_types', $options );
316 }
317
318
319 /**
320 * Handles the AJAX request to search for tags in the Top 10 settings.
321 *
322 * @since 1.7.0
323 *
324 * @return void
325 */
326 function tptn_tags_search() {
327
328 if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
329 wp_die();
330 }
331
332 $taxonomy = sanitize_key( $_REQUEST['tax'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
333 $tax = get_taxonomy( $taxonomy );
334 if ( ! empty( $taxonomy ) ) {
335 $tax = get_taxonomy( $taxonomy );
336 if ( ! $tax ) {
337 wp_die();
338 }
339
340 if ( ! current_user_can( $tax->cap->assign_terms ) ) {
341 wp_die();
342 }
343 }
344
345 $s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
346
347 $comma = _x( ',', 'tag delimiter' );
348 if ( ',' !== $comma ) {
349 $s = str_replace( $comma, ',', $s );
350 }
351 if ( false !== strpos( $s, ',' ) ) {
352 $s = explode( ',', $s );
353 $s = $s[ count( $s ) - 1 ];
354 }
355 $s = trim( $s, " \t\n\r\0\x0B" );
356
357 /**
358 * This filter has been defined in /wp-admin/includes/ajax-actions.php
359 */
360 $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
361
362 /*
363 * Require $term_search_min_chars chars for matching (default: 2)
364 * ensure it's a non-negative, non-zero integer.
365 */
366 if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
367 wp_die();
368 }
369
370 $results = get_terms(
371 array(
372 'taxonomy' => $taxonomy,
373 'name__like' => $s,
374 'fields' => 'names',
375 'hide_empty' => false,
376 )
377 );
378
379 echo wp_json_encode( $results );
380 wp_die();
381 }
382 add_action( 'wp_ajax_tptn_tags_search', 'tptn_tags_search' );
383