| 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 |
static $options = null; |
| 174 |
|
| 175 |
if ( null !== $options ) { |
| 176 |
return $options; |
| 177 |
} |
| 178 |
|
| 179 |
$options = array(); |
| 180 |
$default_types = array( |
| 181 |
'color', |
| 182 |
'css', |
| 183 |
'csv', |
| 184 |
'file', |
| 185 |
'html', |
| 186 |
'multicheck', |
| 187 |
'number', |
| 188 |
'numbercsv', |
| 189 |
'password', |
| 190 |
'postids', |
| 191 |
'posttypes', |
| 192 |
'radio', |
| 193 |
'radiodesc', |
| 194 |
'repeater', |
| 195 |
'select', |
| 196 |
'sensitive', |
| 197 |
'taxonomies', |
| 198 |
'text', |
| 199 |
'textarea', |
| 200 |
'thumbsizes', |
| 201 |
'url', |
| 202 |
'wysiwyg', |
| 203 |
); |
| 204 |
|
| 205 |
// Populate some default values. |
| 206 |
foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) { |
| 207 |
foreach ( $settings as $option ) { |
| 208 |
if ( ! isset( $option['id'] ) ) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
$setting_id = $option['id']; |
| 213 |
$setting_type = $option['type'] ?? ''; |
| 214 |
$default_value = ''; |
| 215 |
|
| 216 |
// When checkbox is set to true, set this to 1. |
| 217 |
if ( 'checkbox' === $setting_type ) { |
| 218 |
$default_value = isset( $option['default'] ) ? (int) (bool) $option['default'] : 0; |
| 219 |
} elseif ( isset( $option['default'] ) && in_array( $setting_type, $default_types, true ) ) { |
| 220 |
$default_value = $option['default']; |
| 221 |
} |
| 222 |
|
| 223 |
$options[ $setting_id ] = $default_value; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Filters the default settings array. |
| 229 |
* |
| 230 |
* @since 2.5.0 |
| 231 |
* |
| 232 |
* @param array $options Default settings. |
| 233 |
*/ |
| 234 |
$options = apply_filters( 'tptn_settings_defaults', $options ); |
| 235 |
|
| 236 |
return $options; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the saved settings merged with the default settings, so newly-registered settings |
| 241 |
* that are not yet present in the saved `tptn_settings` option still resolve to a value. |
| 242 |
* |
| 243 |
* @since 4.4.0 |
| 244 |
* |
| 245 |
* @return array Settings merged with defaults. |
| 246 |
*/ |
| 247 |
function tptn_get_settings_with_defaults() { |
| 248 |
return array_merge( tptn_settings_defaults(), tptn_get_settings() ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* Get the default option for a specific key |
| 254 |
* |
| 255 |
* @since 1.3.0 |
| 256 |
* |
| 257 |
* @param string $key Key of the option to fetch. |
| 258 |
* @return mixed |
| 259 |
*/ |
| 260 |
function tptn_get_default_option( $key = '' ) { |
| 261 |
|
| 262 |
$default_settings = tptn_settings_defaults(); |
| 263 |
|
| 264 |
if ( array_key_exists( $key, $default_settings ) ) { |
| 265 |
return $default_settings[ $key ]; |
| 266 |
} else { |
| 267 |
return false; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* Reset settings. |
| 274 |
* |
| 275 |
* @since 2.5.0 |
| 276 |
* |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
function tptn_settings_reset() { |
| 280 |
delete_option( 'tptn_settings' ); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Get registered settings types for cache key generation. |
| 286 |
* |
| 287 |
* @since 4.2.0 |
| 288 |
* |
| 289 |
* @return array Array of setting types keyed by setting ID. |
| 290 |
*/ |
| 291 |
function tptn_get_registered_settings_types() { |
| 292 |
$options = array(); |
| 293 |
|
| 294 |
// Populate some default values. |
| 295 |
foreach ( \WebberZone\Top_Ten\Admin\Settings::get_registered_settings() as $tab => $settings ) { |
| 296 |
foreach ( $settings as $option ) { |
| 297 |
$options[ $option['id'] ] = $option['type']; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Filter the settings types array for cache key generation. |
| 303 |
* |
| 304 |
* @since 4.2.0 |
| 305 |
* |
| 306 |
* @param array $options Array of setting types keyed by setting ID. |
| 307 |
*/ |
| 308 |
return apply_filters( 'tptn_registered_settings_types', $options ); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* Handles the AJAX request to search for tags in the Top 10 settings. |
| 314 |
* |
| 315 |
* @since 1.7.0 |
| 316 |
* |
| 317 |
* @return void |
| 318 |
*/ |
| 319 |
function tptn_tags_search() { |
| 320 |
|
| 321 |
if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 322 |
wp_die(); |
| 323 |
} |
| 324 |
|
| 325 |
$taxonomy = sanitize_key( $_REQUEST['tax'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 326 |
$tax = get_taxonomy( $taxonomy ); |
| 327 |
if ( ! empty( $taxonomy ) ) { |
| 328 |
$tax = get_taxonomy( $taxonomy ); |
| 329 |
if ( ! $tax ) { |
| 330 |
wp_die(); |
| 331 |
} |
| 332 |
|
| 333 |
if ( ! current_user_can( $tax->cap->assign_terms ) ) { |
| 334 |
wp_die(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
$s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 339 |
|
| 340 |
$comma = _x( ',', 'tag delimiter' ); |
| 341 |
if ( ',' !== $comma ) { |
| 342 |
$s = str_replace( $comma, ',', $s ); |
| 343 |
} |
| 344 |
if ( false !== strpos( $s, ',' ) ) { |
| 345 |
$s = explode( ',', $s ); |
| 346 |
$s = $s[ count( $s ) - 1 ]; |
| 347 |
} |
| 348 |
$s = trim( $s ); |
| 349 |
|
| 350 |
/** This filter has been defined in /wp-admin/includes/ajax-actions.php */ |
| 351 |
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s ); |
| 352 |
|
| 353 |
/* |
| 354 |
* Require $term_search_min_chars chars for matching (default: 2) |
| 355 |
* ensure it's a non-negative, non-zero integer. |
| 356 |
*/ |
| 357 |
if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) { |
| 358 |
wp_die(); |
| 359 |
} |
| 360 |
|
| 361 |
$results = get_terms( |
| 362 |
array( |
| 363 |
'taxonomy' => $taxonomy, |
| 364 |
'name__like' => $s, |
| 365 |
'fields' => 'names', |
| 366 |
'hide_empty' => false, |
| 367 |
) |
| 368 |
); |
| 369 |
|
| 370 |
echo wp_json_encode( $results ); |
| 371 |
wp_die(); |
| 372 |
} |
| 373 |
add_action( 'wp_ajax_tptn_tags_search', 'tptn_tags_search' ); |
| 374 |
|