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

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

354 lines 7.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' );
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 if ( empty( $tptn_settings ) ) {
50 $tptn_settings = tptn_get_settings();
51 }
52
53 if ( is_null( $default_value ) ) {
54 $default_value = tptn_get_default_option( $key );
55 }
56
57 $value = isset( $tptn_settings[ $key ] ) ? $tptn_settings[ $key ] : $default_value;
58
59 /**
60 * Filter the value for the option being fetched.
61 *
62 * @since 2.5.0
63 *
64 * @param mixed $value Value of the option
65 * @param mixed $key Name of the option
66 * @param mixed $default_value Default value
67 */
68 $value = apply_filters( 'tptn_get_option', $value, $key, $default_value );
69
70 /**
71 * Key specific filter for the value of the option being fetched.
72 *
73 * @since 2.5.0
74 *
75 * @param mixed $value Value of the option
76 * @param mixed $key Name of the option
77 * @param mixed $default_value Default value
78 */
79 return apply_filters( 'tptn_get_option_' . $key, $value, $key, $default_value );
80 }
81
82
83 /**
84 * Update an option
85 *
86 * Updates an ata setting value in both the db and the global variable.
87 * Warning: Passing a null value will remove
88 * the key from the tptn_options array.
89 *
90 * @since 2.5.0
91 *
92 * @param string $key The Key to update.
93 * @param string|bool|int|null $value The value to set the key to.
94 * @return boolean True if updated, false if not.
95 */
96 function tptn_update_option( $key = '', $value = false ) {
97
98 // If no key, exit.
99 if ( empty( $key ) ) {
100 return false;
101 }
102
103 // If no value, delete.
104 if ( is_null( $value ) ) {
105 $remove_option = tptn_delete_option( $key );
106 return $remove_option;
107 }
108
109 // First let's grab the current settings.
110 $options = get_option( 'tptn_settings' );
111
112 // Let's let devs alter that value coming in.
113 $value = apply_filters( 'tptn_update_option', $value, $key );
114
115 // Next let's try to update the value.
116 $options[ $key ] = $value;
117 $did_update = update_option( 'tptn_settings', $options );
118
119 // If it updated, let's update the global variable.
120 if ( $did_update ) {
121 global $tptn_settings;
122 $tptn_settings[ $key ] = $value;
123 }
124 return $did_update;
125 }
126
127
128 /**
129 * Remove an option
130 *
131 * Removes an ata setting value in both the db and the global variable.
132 *
133 * @since 2.5.0
134 *
135 * @param string $key The Key to update.
136 * @return boolean True if updated, false if not.
137 */
138 function tptn_delete_option( $key = '' ) {
139
140 // If no key, exit.
141 if ( empty( $key ) ) {
142 return false;
143 }
144
145 // First let's grab the current settings.
146 $options = get_option( 'tptn_settings' );
147
148 // Next let's try to update the value.
149 if ( isset( $options[ $key ] ) ) {
150 unset( $options[ $key ] );
151 }
152
153 $did_update = update_option( 'tptn_settings', $options );
154
155 // If it updated, let's update the global variable.
156 if ( $did_update ) {
157 global $tptn_settings;
158 $tptn_settings = $options;
159 }
160 return $did_update;
161 }
162
163
164 /**
165 * Default settings.
166 *
167 * @since 2.5.0
168 *
169 * @return array Default settings
170 */
171 function tptn_settings_defaults() {
172
173 $options = array();
174 $default_types = array(
175 'color',
176 'css',
177 'csv',
178 'file',
179 'html',
180 'multicheck',
181 'number',
182 'numbercsv',
183 'password',
184 'postids',
185 'posttypes',
186 'radio',
187 'radiodesc',
188 'repeater',
189 'select',
190 'sensitive',
191 'taxonomies',
192 'text',
193 'textarea',
194 'thumbsizes',
195 'url',
196 'wysiwyg',
197 );
198
199 // Populate some default values.
200 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
201 foreach ( $settings as $option ) {
202 if ( ! isset( $option['id'] ) ) {
203 continue;
204 }
205
206 $setting_id = $option['id'];
207 $setting_type = $option['type'] ?? '';
208 $default_value = '';
209
210 // When checkbox is set to true, set this to 1.
211 if ( 'checkbox' === $setting_type ) {
212 $default_value = isset( $option['default'] ) ? (int) (bool) $option['default'] : 0;
213 } elseif ( isset( $option['default'] ) && in_array( $setting_type, $default_types, true ) ) {
214 $default_value = $option['default'];
215 }
216
217 $options[ $setting_id ] = $default_value;
218 }
219 }
220
221 /**
222 * Filters the default settings array.
223 *
224 * @since 2.5.0
225 *
226 * @param array $options Default settings.
227 */
228 return apply_filters( 'tptn_settings_defaults', $options );
229 }
230
231
232 /**
233 * Get the default option for a specific key
234 *
235 * @since 1.3.0
236 *
237 * @param string $key Key of the option to fetch.
238 * @return mixed
239 */
240 function tptn_get_default_option( $key = '' ) {
241
242 $default_settings = tptn_settings_defaults();
243
244 if ( array_key_exists( $key, $default_settings ) ) {
245 return $default_settings[ $key ];
246 } else {
247 return false;
248 }
249 }
250
251
252 /**
253 * Reset settings.
254 *
255 * @since 2.5.0
256 *
257 * @return void
258 */
259 function tptn_settings_reset() {
260 delete_option( 'tptn_settings' );
261 }
262
263
264 /**
265 * Get registered settings types for cache key generation.
266 *
267 * @since 4.2.0
268 *
269 * @return array Array of setting types keyed by setting ID.
270 */
271 function tptn_get_registered_settings_types() {
272 $options = array();
273
274 // Populate some default values.
275 foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) {
276 foreach ( $settings as $option ) {
277 $options[ $option['id'] ] = $option['type'];
278 }
279 }
280
281 /**
282 * Filter the settings types array for cache key generation.
283 *
284 * @since 4.2.0
285 *
286 * @param array $options Array of setting types keyed by setting ID.
287 */
288 return apply_filters( 'tptn_registered_settings_types', $options );
289 }
290
291
292 /**
293 * Function to add an action to search for tags using Ajax.
294 *
295 * @since 1.7.0
296 *
297 * @return void
298 */
299 function tptn_tags_search() {
300
301 if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
302 wp_die();
303 }
304
305 $taxonomy = sanitize_key( $_REQUEST['tax'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
306 $tax = get_taxonomy( $taxonomy );
307 if ( ! empty( $taxonomy ) ) {
308 $tax = get_taxonomy( $taxonomy );
309 if ( ! $tax ) {
310 wp_die();
311 }
312
313 if ( ! current_user_can( $tax->cap->assign_terms ) ) {
314 wp_die();
315 }
316 }
317
318 $s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
319
320 $comma = _x( ',', 'tag delimiter' );
321 if ( ',' !== $comma ) {
322 $s = str_replace( $comma, ',', $s );
323 }
324 if ( false !== strpos( $s, ',' ) ) {
325 $s = explode( ',', $s );
326 $s = $s[ count( $s ) - 1 ];
327 }
328 $s = trim( $s );
329
330 /** This filter has been defined in /wp-admin/includes/ajax-actions.php */
331 $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
332
333 /*
334 * Require $term_search_min_chars chars for matching (default: 2)
335 * ensure it's a non-negative, non-zero integer.
336 */
337 if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
338 wp_die();
339 }
340
341 $results = get_terms(
342 array(
343 'taxonomy' => $taxonomy,
344 'name__like' => $s,
345 'fields' => 'names',
346 'hide_empty' => false,
347 )
348 );
349
350 echo wp_json_encode( $results );
351 wp_die();
352 }
353 add_action( 'wp_ajax_tptn_tags_search', 'tptn_tags_search' );
354