| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | * @return array Top 10 settings |
| 19 | 19 | */ |
| 20 | 20 | function tptn_get_settings() { |
| 21 | 21 | |
| 22 | - $settings = get_option( 'tptn_settings' ); | |
| 22 | + $settings = get_option( 'tptn_settings', array() ); | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * Settings array |
| 26 | 26 | * |
| @@ -36,20 +36,18 @@ | ||
| 36 | 36 | * Get an option |
| 37 | 37 | * |
| 38 | 38 | * Looks to see if the specified setting exists, returns default if not |
| 39 | 39 | * |
| 40 | - * @since 2.5.0 | |
| 40 | + * @since 2.5.0 | |
| 41 | 41 | * |
| 42 | - * @param string $key Option to fetch. | |
| 43 | - * @param mixed $default_value Default option. | |
| 42 | + * @param string $key Option to fetch. | |
| 43 | + * @param mixed $default_value Default option. | |
| 44 | 44 | * @return mixed |
| 45 | 45 | */ |
| 46 | 46 | function tptn_get_option( $key = '', $default_value = null ) { |
| 47 | 47 | global $tptn_settings; |
| 48 | 48 | |
| 49 | - if ( empty( $tptn_settings ) ) { | |
| 50 | - $tptn_settings = tptn_get_settings(); | |
| 51 | - } | |
| 49 | + $tptn_settings = tptn_get_settings(); | |
| 52 | 50 | |
| 53 | 51 | if ( is_null( $default_value ) ) { |
| 54 | 52 | $default_value = tptn_get_default_option( $key ); |
| 55 | 53 | } |
| @@ -106,11 +104,20 @@ | ||
| 106 | 104 | return $remove_option; |
| 107 | 105 | } |
| 108 | 106 | |
| 109 | 107 | // First let's grab the current settings. |
| 110 | - $options = get_option( 'tptn_settings' ); | |
| 108 | + $options = get_option( 'tptn_settings', array() ); | |
| 109 | + $options = is_array( $options ) ? $options : array(); | |
| 111 | 110 | |
| 112 | 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 | + */ | |
| 113 | 120 | $value = apply_filters( 'tptn_update_option', $value, $key ); |
| 114 | 121 | |
| 115 | 122 | // Next let's try to update the value. |
| 116 | 123 | $options[ $key ] = $value; |
| @@ -142,9 +149,10 @@ | ||
| 142 | 149 | return false; |
| 143 | 150 | } |
| 144 | 151 | |
| 145 | 152 | // First let's grab the current settings. |
| 146 | - $options = get_option( 'tptn_settings' ); | |
| 153 | + $options = get_option( 'tptn_settings', array() ); | |
| 154 | + $options = is_array( $options ) ? $options : array(); | |
| 147 | 155 | |
| 148 | 156 | // Next let's try to update the value. |
| 149 | 157 | if ( isset( $options[ $key ] ) ) { |
| 150 | 158 | unset( $options[ $key ] ); |
| @@ -169,8 +177,14 @@ | ||
| 169 | 177 | * @return array Default settings |
| 170 | 178 | */ |
| 171 | 179 | function tptn_settings_defaults() { |
| 172 | 180 | |
| 181 | + static $options = null; | |
| 182 | + | |
| 183 | + if ( null !== $options ) { | |
| 184 | + return $options; | |
| 185 | + } | |
| 186 | + | |
| 173 | 187 | $options = array(); |
| 174 | 188 | $default_types = array( |
| 175 | 189 | 'color', |
| 176 | 190 | 'css', |
| @@ -224,23 +238,38 @@ | ||
| 224 | 238 | * @since 2.5.0 |
| 225 | 239 | * |
| 226 | 240 | * @param array $options Default settings. |
| 227 | 241 | */ |
| 228 | - return apply_filters( 'tptn_settings_defaults', $options ); | |
| 242 | + $options = apply_filters( 'tptn_settings_defaults', $options ); | |
| 243 | + | |
| 244 | + return $options; | |
| 229 | 245 | } |
| 230 | 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 | +} | |
| 231 | 258 | |
| 259 | + | |
| 232 | 260 | /** |
| 233 | 261 | * Get the default option for a specific key |
| 234 | 262 | * |
| 235 | 263 | * @since 1.3.0 |
| 236 | 264 | * |
| 237 | - * @param string $key Key of the option to fetch. | |
| 265 | + * @param string $key Key of the option to fetch. | |
| 238 | 266 | * @return mixed |
| 239 | 267 | */ |
| 240 | 268 | function tptn_get_default_option( $key = '' ) { |
| 241 | 269 | |
| 242 | - $default_settings = tptn_settings_defaults(); | |
| 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() ); | |
| 243 | 272 | |
| 244 | 273 | if ( array_key_exists( $key, $default_settings ) ) { |
| 245 | 274 | return $default_settings[ $key ]; |
| 246 | 275 | } else { |
| @@ -289,9 +318,9 @@ | ||
| 289 | 318 | } |
| 290 | 319 | |
| 291 | 320 | |
| 292 | 321 | /** |
| 293 | - * Function to add an action to search for tags using Ajax. | |
| 322 | + * Handles the AJAX request to search for tags in the Top 10 settings. | |
| 294 | 323 | * |
| 295 | 324 | * @since 1.7.0 |
| 296 | 325 | * |
| 297 | 326 | * @return void |
| @@ -324,17 +353,19 @@ | ||
| 324 | 353 | if ( false !== strpos( $s, ',' ) ) { |
| 325 | 354 | $s = explode( ',', $s ); |
| 326 | 355 | $s = $s[ count( $s ) - 1 ]; |
| 327 | 356 | } |
| 328 | - $s = trim( $s ); | |
| 357 | + $s = trim( $s, " \t\n\r\0\x0B" ); | |
| 329 | 358 | |
| 330 | - /** This filter has been defined in /wp-admin/includes/ajax-actions.php */ | |
| 359 | + /** | |
| 360 | + * This filter has been defined in /wp-admin/includes/ajax-actions.php | |
| 361 | +*/ | |
| 331 | 362 | $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s ); |
| 332 | 363 | |
| 333 | 364 | /* |
| 334 | - * Require $term_search_min_chars chars for matching (default: 2) | |
| 335 | - * ensure it's a non-negative, non-zero integer. | |
| 336 | - */ | |
| 365 | + * Require $term_search_min_chars chars for matching (default: 2) | |
| 366 | + * ensure it's a non-negative, non-zero integer. | |
| 367 | + */ | |
| 337 | 368 | if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) { |
| 338 | 369 | wp_die(); |
| 339 | 370 | } |
| 340 | 371 | |