PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / options / class-wpseo-option.php

class-wpseo-option.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at inc/options/class-wpseo-option.php

855 lines 28.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Internals\Options
6 */
7
8 /**
9 * This abstract class and its concrete classes implement defaults and value validation for
10 * all WPSEO options and subkeys within options.
11 *
12 * Some guidelines:
13 * [Retrieving options]
14 * - Use the normal get_option() to retrieve an option. You will receive a complete array for the option.
15 * Any subkeys which were not set, will have their default values in place.
16 * - In other words, you will normally not have to check whether a subkey isset() as they will *always* be set.
17 * They will also *always* be of the correct variable type.
18 * The only exception to this are the options with variable option names based on post_type or taxonomy
19 * as those will not always be available before the taxonomy/post_type is registered.
20 * (they will be available if a value was set, they won't be if it wasn't as the class won't know
21 * that a default needs to be injected).
22 *
23 * [Updating/Adding options]
24 * - For multisite site_options, please use the WPSEO_Options::update_site_option() method.
25 * - For normal options, use the normal add/update_option() functions. As long as the classes here
26 * are instantiated, validation for all options and their subkeys will be automatic.
27 * - On (successful) update of a couple of options, certain related actions will be run automatically.
28 * Some examples:
29 * - on change of wpseo[yoast_tracking], the cron schedule will be adjusted accordingly
30 * - on change of wpseo and wpseo_title, some caches will be cleared
31 *
32 * [Important information about add/updating/changing these classes]
33 * - Make sure that option array key names are unique across options. The WPSEO_Options::get_all()
34 * method merges most options together. If any of them have non-unique names, even if they
35 * are in a different option, they *will* overwrite each other.
36 * - When you add a new array key in an option: make sure you add proper defaults and add the key
37 * to the validation routine in the proper place or add a new validation case.
38 * You don't need to do any upgrading as any option returned will always be merged with the
39 * defaults, so new options will automatically be available.
40 * If the default value is a string which need translating, add this to the concrete class
41 * translate_defaults() method.
42 * - When you remove an array key from an option: if it's important that the option is really removed,
43 * add the WPSEO_Option::clean_up( $option_name ) method to the upgrade run.
44 * This will re-save the option and automatically remove the array key no longer in existence.
45 * - When you rename a sub-option: add it to the clean_option() routine and run that in the upgrade run.
46 * - When you change the default for an option sub-key, make sure you verify that the validation routine will
47 * still work the way it should.
48 * Example: changing a default from '' (empty string) to 'text' with a validation routine with tests
49 * for an empty string will prevent a user from saving an empty string as the real value. So the
50 * test for '' with the validation routine would have to be removed in that case.
51 * - If an option needs specific actions different from defined in this abstract class, you can just overrule
52 * a method by defining it in the concrete class.
53 *
54 * @todo [JRF => testers] Double check that validation will not cause errors when called
55 * from upgrade routine (some of the WP functions may not yet be available).
56 */
57 abstract class WPSEO_Option {
58
59 /**
60 * Prefix for override option keys that allow or disallow the option key of the same name.
61 *
62 * @var string
63 */
64 public const ALLOW_KEY_PREFIX = 'allow_';
65
66 /**
67 * Option name - MUST be set in concrete class and set to public.
68 *
69 * @var string
70 */
71 protected $option_name;
72
73 /**
74 * Option group name for use in settings forms.
75 *
76 * Will be set automagically if not set in concrete class (i.e.
77 * if it conforms to the normal pattern 'yoast' . $option_name . 'options',
78 * only set in concrete class if it doesn't).
79 *
80 * @var string
81 */
82 public $group_name;
83
84 /**
85 * Whether to include the option in the return for WPSEO_Options::get_all().
86 *
87 * Also determines which options are copied over for ms_(re)set_blog().
88 *
89 * @var bool
90 */
91 public $include_in_all = true;
92
93 /**
94 * Whether this option is only for when the install is multisite.
95 *
96 * @var bool
97 */
98 public $multisite_only = false;
99
100 /**
101 * Array of defaults for the option - MUST be set in concrete class.
102 *
103 * Shouldn't be requested directly, use $this->get_defaults();
104 *
105 * @var array
106 */
107 protected $defaults;
108
109 /**
110 * Array of variable option name patterns for the option - if any.
111 *
112 * Set this when the option contains array keys which vary based on post_type
113 * or taxonomy.
114 *
115 * @var array
116 */
117 protected $variable_array_key_patterns;
118
119 /**
120 * Array of sub-options which should not be overloaded with multi-site defaults.
121 *
122 * @var array
123 */
124 public $ms_exclude = [];
125
126 /**
127 * Name for an option higher in the hierarchy to override setting access.
128 *
129 * @var string
130 */
131 protected $override_option_name;
132
133 /**
134 * Instance of this class.
135 *
136 * @var WPSEO_Option
137 */
138 protected static $instance;
139
140 /* *********** INSTANTIATION METHODS *********** */
141
142 /**
143 * Add all the actions and filters for the option.
144 */
145 protected function __construct() {
146
147 /* Add filters which get applied to the get_options() results. */
148 $this->add_default_filters(); // Return defaults if option not set.
149 $this->add_option_filters(); // Merge with defaults if option *is* set.
150
151 if ( $this->multisite_only !== true ) {
152 /**
153 * The option validation routines remove the default filters to prevent failing
154 * to insert an option if it's new. Let's add them back afterwards.
155 */
156 add_action( 'add_option', [ $this, 'add_default_filters_if_same_option' ] ); // Adding back after INSERT.
157
158 add_action( 'update_option', [ $this, 'add_default_filters_if_same_option' ] );
159
160 add_filter( 'pre_update_option', [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
161
162 // Refills the cache when the option has been updated.
163 add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 10 );
164 }
165 elseif ( is_multisite() ) {
166 /*
167 * The option validation routines remove the default filters to prevent failing
168 * to insert an option if it's new. Let's add them back afterwards.
169 *
170 * For site_options, this method is not foolproof as these actions are not fired
171 * on an insert/update failure. Please use the WPSEO_Options::update_site_option() method
172 * for updating site options to make sure the filters are in place.
173 */
174 add_action( 'add_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
175 add_action( 'update_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
176 add_filter( 'pre_update_site_option_' . $this->option_name, [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
177
178 // Refills the cache when the option has been updated.
179 add_action( 'update_site_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 1, 0 );
180 }
181
182 /*
183 * Make sure the option will always get validated, independently of register_setting()
184 * (only available on back-end).
185 */
186 add_filter( 'sanitize_option_' . $this->option_name, [ $this, 'validate' ] );
187
188 /* Register our option for the admin pages */
189 add_action( 'admin_init', [ $this, 'register_setting' ] );
190
191 /* Set option group name if not given */
192 if ( ! isset( $this->group_name ) || $this->group_name === '' ) {
193 $this->group_name = 'yoast_' . $this->option_name . '_options';
194 }
195
196 /* Translate some defaults as early as possible - textdomain is loaded in init on priority 1. */
197 if ( method_exists( $this, 'translate_defaults' ) ) {
198 add_action( 'init', [ $this, 'translate_defaults' ], 2 );
199 }
200
201 /**
202 * Enrich defaults once custom post types and taxonomies have been registered
203 * which is normally done on the init action.
204 *
205 * @todo [JRF/testers] Verify that none of the options which are only available after
206 * enrichment are used before the enriching.
207 */
208 if ( method_exists( $this, 'enrich_defaults' ) ) {
209 add_action( 'init', [ $this, 'enrich_defaults' ], 99 );
210 }
211 }
212
213 /*
214 * All concrete classes *must* contain the get_instance method.
215 *
216 * {@internal Unfortunately I can't define it as an abstract as it also *has* to be static...}}
217 *
218 * ```
219 * abstract protected static function get_instance();
220 * ```
221 * ---------------
222 *
223 * Concrete classes *may* contain a translate_defaults method.
224 * ```
225 * abstract public function translate_defaults();
226 * ```
227 * ---------------
228 *
229 * Concrete classes *may* contain an enrich_defaults method to add additional defaults once
230 * all post_types and taxonomies have been registered.
231 *
232 * ```
233 * abstract public function enrich_defaults();
234 * ```
235 */
236
237 /* *********** METHODS INFLUENCING get_option() *********** */
238
239 /**
240 * Add filters to make sure that the option default is returned if the option is not set.
241 *
242 * @return void
243 */
244 public function add_default_filters() {
245 // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
246 if ( has_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
247 add_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
248 }
249 }
250
251 /**
252 * Adds back the default filters that were removed during validation if the option was changed.
253 * Checks if this option was changed to prevent constantly checking if filters are present.
254 *
255 * @param string $option_name The option name.
256 *
257 * @return void
258 */
259 public function add_default_filters_if_same_option( $option_name ) {
260 if ( $option_name === $this->option_name ) {
261 $this->add_default_filters();
262 }
263 }
264
265 /**
266 * Adds back the default filters that were removed during validation if the option was not changed.
267 * This is because in that case the latter actions are not called and thus the filters are never
268 * added back.
269 *
270 * @param mixed $value The current value.
271 * @param string $option_name The option name.
272 * @param mixed $old_value The old value.
273 *
274 * @return string The current value.
275 */
276 public function add_default_filters_if_not_changed( $value, $option_name, $old_value ) {
277 if ( $option_name !== $this->option_name ) {
278 return $value;
279 }
280
281 if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
282 $this->add_default_filters();
283 }
284
285 return $value;
286 }
287
288 /**
289 * Validate webmaster tools & Pinterest verification strings.
290 *
291 * @param string $key Key to check, by type of service.
292 * @param array $dirty Dirty data with the new values.
293 * @param array $old Old data.
294 * @param array $clean Clean data by reference, normally the default values.
295 *
296 * @return void
297 */
298 public function validate_verification_string( $key, $dirty, $old, &$clean ) {
299 if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
300 $meta = $dirty[ $key ];
301 if ( strpos( $meta, 'content=' ) ) {
302 // Make sure we only have the real key, not a complete meta tag.
303 preg_match( '`content=([\'"])?([^\'"> ]+)(?:\1|[ />])`', $meta, $match );
304 if ( isset( $match[2] ) ) {
305 $meta = $match[2];
306 }
307 unset( $match );
308 }
309
310 $meta = sanitize_text_field( $meta );
311 if ( $meta !== '' ) {
312 $regex = '`^[A-Fa-f0-9_-]+$`';
313
314 switch ( $key ) {
315 case 'googleverify':
316 case 'ahrefsverify':
317 case 'baiduverify':
318 $regex = '`^[A-Za-z0-9_-]+$`';
319 break;
320
321 case 'msverify':
322 case 'pinterestverify':
323 case 'yandexverify':
324 break;
325 }
326
327 if ( preg_match( $regex, $meta ) ) {
328 $clean[ $key ] = $meta;
329 }
330 else {
331 // Restore the previous value, if any.
332 if ( isset( $old[ $key ] ) && preg_match( $regex, $old[ $key ] ) ) {
333 $clean[ $key ] = $old[ $key ];
334 }
335
336 Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $meta );
337 }
338 }
339 }
340 }
341
342 /**
343 * Validates an option as a valid URL. Prints out a WordPress settings error
344 * notice if the URL is invalid.
345 *
346 * @param string $key Key to check, by type of URL setting.
347 * @param array $dirty Dirty data with the new values.
348 * @param array $old Old data.
349 * @param array $clean Clean data by reference, normally the default values.
350 *
351 * @return void
352 */
353 public function validate_url( $key, $dirty, $old, &$clean ) {
354 if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
355
356 $submitted_url = trim( $dirty[ $key ] );
357 $validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );
358
359 if ( $validated_url === false ) {
360 // Restore the previous URL value, if any.
361 if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
362 $url = WPSEO_Utils::sanitize_url( $old[ $key ] );
363 if ( $url !== '' ) {
364 $clean[ $key ] = $url;
365 }
366 }
367
368 Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $submitted_url );
369
370 return;
371 }
372
373 // The URL format is valid, let's sanitize it.
374 $url = WPSEO_Utils::sanitize_url( $validated_url );
375
376 if ( $url !== '' ) {
377 $clean[ $key ] = $url;
378 }
379 }
380 }
381
382 /**
383 * Remove the default filters.
384 * Called from the validate() method to prevent failure to add new options.
385 *
386 * @return void
387 */
388 public function remove_default_filters() {
389 remove_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
390 }
391
392 /**
393 * Get the enriched default value for an option.
394 *
395 * Checks if the concrete class contains an enrich_defaults() method and if so, runs it.
396 *
397 * {@internal The enrich_defaults method is used to set defaults for variable array keys
398 * in an option, such as array keys depending on post_types and/or taxonomies.}}
399 *
400 * @return array
401 */
402 public function get_defaults() {
403 if ( method_exists( $this, 'translate_defaults' ) ) {
404 $this->translate_defaults();
405 }
406
407 if ( method_exists( $this, 'enrich_defaults' ) ) {
408 $this->enrich_defaults();
409 }
410
411 return apply_filters( 'wpseo_defaults', $this->defaults, $this->option_name );
412 }
413
414 /**
415 * Add filters to make sure that the option is merged with its defaults before being returned.
416 *
417 * @return void
418 */
419 public function add_option_filters() {
420 // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
421 if ( has_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
422 add_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
423 }
424 }
425
426 /**
427 * Remove the option filters.
428 * Called from the clean_up methods to make sure we retrieve the original old option.
429 *
430 * @return void
431 */
432 public function remove_option_filters() {
433 remove_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
434 }
435
436 /**
437 * Merge an option with its default values.
438 *
439 * This method should *not* be called directly!!! It is only meant to filter the get_option() results.
440 *
441 * @param mixed $options Option value.
442 *
443 * @return mixed Option merged with the defaults for that option.
444 */
445 public function get_option( $options = null ) {
446 $filtered = $this->array_filter_merge( $options );
447
448 /*
449 * If the option contains variable option keys, make sure we don't remove those settings
450 * - even if the defaults are not complete yet.
451 * Unfortunately this means we also won't be removing the settings for post types or taxonomies
452 * which are no longer in the WP install, but rather that than the other way around.
453 */
454 if ( isset( $this->variable_array_key_patterns ) ) {
455 $filtered = $this->retain_variable_keys( $options, $filtered );
456 }
457
458 return $filtered;
459 }
460
461 /* *********** METHODS influencing add_option(), update_option() and saving from admin pages. *********** */
462
463 /**
464 * Register (whitelist) the option for the configuration pages.
465 * The validation callback is already registered separately on the sanitize_option hook,
466 * so no need to double register.
467 *
468 * @return void
469 */
470 public function register_setting() {
471 if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
472 return;
473 }
474
475 if ( $this->multisite_only === true ) {
476 $network_settings_api = Yoast_Network_Settings_API::get();
477 if ( $network_settings_api->meets_requirements() ) {
478 $network_settings_api->register_setting( $this->group_name, $this->option_name );
479 }
480 return;
481 }
482
483 register_setting( $this->group_name, $this->option_name );
484 }
485
486 /**
487 * Validate the option.
488 *
489 * @param mixed $option_value The unvalidated new value for the option.
490 *
491 * @return array Validated new value for the option.
492 */
493 public function validate( $option_value ) {
494 $clean = $this->get_defaults();
495
496 /* Return the defaults if the new value is empty. */
497 if ( ! is_array( $option_value ) || $option_value === [] ) {
498 return $clean;
499 }
500
501 $option_value = array_map( [ 'WPSEO_Utils', 'trim_recursive' ], $option_value );
502
503 $old = $this->get_original_option();
504 if ( ! is_array( $old ) ) {
505 $old = [];
506 }
507 $old = array_merge( $clean, $old );
508
509 $clean = $this->validate_option( $option_value, $clean, $old );
510
511 // Prevent updates to variables that are disabled via the override option.
512 $clean = $this->prevent_disabled_options_update( $clean, $old );
513
514 /* Retain the values for variable array keys even when the post type/taxonomy is not yet registered. */
515 if ( isset( $this->variable_array_key_patterns ) ) {
516 $clean = $this->retain_variable_keys( $option_value, $clean );
517 }
518
519 $this->remove_default_filters();
520
521 return $clean;
522 }
523
524 /**
525 * Checks whether a specific option key is disabled.
526 *
527 * This is determined by whether an override option is available with a key that equals the given key prefixed
528 * with 'allow_'.
529 *
530 * @param string $key Option key.
531 *
532 * @return bool True if option key is disabled, false otherwise.
533 */
534 public function is_disabled( $key ) {
535 $override_option = $this->get_override_option();
536 if ( empty( $override_option ) ) {
537 return false;
538 }
539
540 return isset( $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) && ! $override_option[ self::ALLOW_KEY_PREFIX . $key ];
541 }
542
543 /**
544 * All concrete classes must contain a validate_option() method which validates all
545 * values within the option.
546 *
547 * @param array $dirty New value for the option.
548 * @param array $clean Clean value for the option, normally the defaults.
549 * @param array $old Old value of the option.
550 */
551 abstract protected function validate_option( $dirty, $clean, $old );
552
553 /* *********** METHODS for ADDING/UPDATING/UPGRADING the option. *********** */
554
555 /**
556 * Retrieve the real old value (unmerged with defaults).
557 *
558 * @return array|bool The original option value (which can be false if the option doesn't exist).
559 */
560 protected function get_original_option() {
561 $this->remove_default_filters();
562 $this->remove_option_filters();
563
564 // Get (unvalidated) array, NOT merged with defaults.
565 if ( $this->multisite_only !== true ) {
566 $option_value = get_option( $this->option_name );
567 }
568 else {
569 $option_value = get_site_option( $this->option_name );
570 }
571
572 $this->add_option_filters();
573 $this->add_default_filters();
574
575 return $option_value;
576 }
577
578 /**
579 * Add the option if it doesn't exist for some strange reason.
580 *
581 * @uses WPSEO_Option::get_original_option()
582 *
583 * @return void
584 */
585 public function maybe_add_option() {
586 if ( $this->get_original_option() === false ) {
587 if ( $this->multisite_only !== true ) {
588 update_option( $this->option_name, $this->get_defaults() );
589 }
590 else {
591 $this->update_site_option( $this->get_defaults() );
592 }
593 }
594 }
595
596 /**
597 * Update a site_option.
598 *
599 * {@internal This special method is only needed for multisite options, but very needed indeed there.
600 * The order in which certain functions and hooks are run is different between
601 * get_option() and get_site_option() which means in practice that the removing
602 * of the default filters would be done too late and the re-adding of the default
603 * filters might not be done at all.
604 * Aka: use the WPSEO_Options::update_site_option() method (which calls this method)
605 * for safely adding/updating multisite options.}}
606 *
607 * @param mixed $value The new value for the option.
608 *
609 * @return bool Whether the update was successful.
610 */
611 public function update_site_option( $value ) {
612 if ( $this->multisite_only === true && is_multisite() ) {
613 $this->remove_default_filters();
614 $result = update_site_option( $this->option_name, $value );
615 $this->add_default_filters();
616
617 return $result;
618 }
619 else {
620 return false;
621 }
622 }
623
624 /**
625 * Retrieve the real old value (unmerged with defaults), clean and re-save the option.
626 *
627 * @uses WPSEO_Option::get_original_option()
628 * @uses WPSEO_Option::import()
629 *
630 * @param string|null $current_version Optional. Version from which to upgrade, if not set,
631 * version-specific upgrades will be disregarded.
632 *
633 * @return void
634 */
635 public function clean( $current_version = null ) {
636 $option_value = $this->get_original_option();
637 $this->import( $option_value, $current_version );
638 }
639
640 /**
641 * Clean and re-save the option.
642 *
643 * @uses clean_option() method from concrete class if it exists.
644 *
645 * @todo [JRF/whomever] Figure out a way to show settings error during/after the upgrade - maybe
646 * something along the lines of:
647 * -> add them to a property in this class
648 * -> if that property isset at the end of the routine and add_settings_error function does not exist,
649 * save as transient (or update the transient if one already exists)
650 * -> next time an admin is in the WP back-end, show the errors and delete the transient or only delete it
651 * once the admin has dismissed the message (add ajax function)
652 * Important: all validation routines which add_settings_errors would need to be changed for this to work
653 *
654 * @param array $option_value Option value to be imported.
655 * @param string|null $current_version Optional. Version from which to upgrade, if not set,
656 * version-specific upgrades will be disregarded.
657 * @param array|null $all_old_option_values Optional. Only used when importing old options to
658 * have access to the real old values, in contrast to
659 * the saved ones.
660 *
661 * @return void
662 */
663 public function import( $option_value, $current_version = null, $all_old_option_values = null ) {
664 if ( $option_value === false ) {
665 $option_value = $this->get_defaults();
666 }
667 elseif ( is_array( $option_value ) && method_exists( $this, 'clean_option' ) ) {
668 $option_value = $this->clean_option( $option_value, $current_version, $all_old_option_values );
669 }
670
671 /*
672 * Save the cleaned value - validation will take care of cleaning out array keys which
673 * should no longer be there.
674 */
675 if ( $this->multisite_only !== true ) {
676 update_option( $this->option_name, $option_value );
677 }
678 else {
679 $this->update_site_option( $this->option_name, $option_value );
680 }
681 }
682
683 /**
684 * Returns the variable array key patterns for an options class.
685 *
686 * @return array
687 */
688 public function get_patterns() {
689 return (array) $this->variable_array_key_patterns;
690 }
691
692 /**
693 * Retrieves the option name.
694 *
695 * @return string The set option name.
696 */
697 public function get_option_name() {
698 return $this->option_name;
699 }
700
701 /*
702 * Concrete classes *may* contain a clean_option method which will clean out old/renamed
703 * values within the option.
704 *
705 * ```
706 * abstract public function clean_option( $option_value, $current_version = null, $all_old_option_values = null );
707 * ```
708 */
709
710 /* *********** HELPER METHODS for internal use. *********** */
711
712 /**
713 * Helper method - Combines a fixed array of default values with an options array
714 * while filtering out any keys which are not in the defaults array.
715 *
716 * @todo [JRF] - shouldn't this be a straight array merge ? at the end of the day, the validation
717 * removes any invalid keys on save.
718 *
719 * @param array|null $options Optional. Current options. If not set, the option defaults
720 * for the $option_key will be returned.
721 *
722 * @return array Combined and filtered options array.
723 */
724 protected function array_filter_merge( $options = null ) {
725
726 $defaults = $this->get_defaults();
727
728 if ( ! isset( $options ) || $options === false || $options === [] ) {
729 return $defaults;
730 }
731
732 $options = (array) $options;
733
734 /*
735 $filtered = array();
736
737 if ( $defaults !== array() ) {
738 foreach ( $defaults as $key => $default_value ) {
739 // @todo should this walk through array subkeys ?
740 $filtered[ $key ] = ( isset( $options[ $key ] ) ? $options[ $key ] : $default_value );
741 }
742 }
743 */
744 $filtered = array_merge( $defaults, $options );
745
746 return $filtered;
747 }
748
749 /**
750 * Sets updated values for variables that are disabled via the override option back to their previous values.
751 *
752 * @param array $updated Updated option value.
753 * @param array $old Old option value.
754 *
755 * @return array Updated option value, with all disabled variables set to their old values.
756 */
757 protected function prevent_disabled_options_update( $updated, $old ) {
758 $override_option = $this->get_override_option();
759 if ( empty( $override_option ) ) {
760 return $updated;
761 }
762
763 /*
764 * This loop could as well call `is_disabled( $key )` for each iteration,
765 * however this would be worse performance-wise.
766 */
767 foreach ( $old as $key => $value ) {
768 if ( isset( $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) && ! $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) {
769 $updated[ $key ] = $old[ $key ];
770 }
771 }
772
773 return $updated;
774 }
775
776 /**
777 * Retrieves the value of the override option, if available.
778 *
779 * An override option contains values that may determine access to certain sub-variables
780 * of this option.
781 *
782 * Only regular options in multisite can have override options, which in that case
783 * would be network options.
784 *
785 * @return array Override option value, or empty array if unavailable.
786 */
787 protected function get_override_option() {
788 if ( empty( $this->override_option_name ) || $this->multisite_only === true || ! is_multisite() ) {
789 return [];
790 }
791
792 return get_site_option( $this->override_option_name, [] );
793 }
794
795 /**
796 * Make sure that any set option values relating to post_types and/or taxonomies are retained,
797 * even when that post_type or taxonomy may not yet have been registered.
798 *
799 * {@internal The wpseo_titles concrete class overrules this method. Make sure that any
800 * changes applied here, also get ported to that version.}}
801 *
802 * @param array $dirty Original option as retrieved from the database.
803 * @param array $clean Filtered option where any options which shouldn't be in our option
804 * have already been removed and any options which weren't set
805 * have been set to their defaults.
806 *
807 * @return array
808 */
809 protected function retain_variable_keys( $dirty, $clean ) {
810 if ( ( is_array( $this->variable_array_key_patterns ) && $this->variable_array_key_patterns !== [] ) && ( is_array( $dirty ) && $dirty !== [] ) ) {
811 foreach ( $dirty as $key => $value ) {
812
813 // Do nothing if already in filtered options.
814 if ( isset( $clean[ $key ] ) ) {
815 continue;
816 }
817
818 foreach ( $this->variable_array_key_patterns as $pattern ) {
819
820 if ( strpos( $key, $pattern ) === 0 ) {
821 $clean[ $key ] = $value;
822 break;
823 }
824 }
825 }
826 }
827
828 return $clean;
829 }
830
831 /**
832 * Check whether a given array key conforms to one of the variable array key patterns for this option.
833 *
834 * @used-by validate_option() methods for options with variable array keys.
835 *
836 * @param string $key Array key to check.
837 *
838 * @return string Pattern if it conforms, original array key if it doesn't or if the option
839 * does not have variable array keys.
840 */
841 protected function get_switch_key( $key ) {
842 if ( ! isset( $this->variable_array_key_patterns ) || ( ! is_array( $this->variable_array_key_patterns ) || $this->variable_array_key_patterns === [] ) ) {
843 return $key;
844 }
845
846 foreach ( $this->variable_array_key_patterns as $pattern ) {
847 if ( strpos( $key, $pattern ) === 0 ) {
848 return $pattern;
849 }
850 }
851
852 return $key;
853 }
854 }
855