PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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
← All changes | inc/options/class-wpseo-option.php +24 -135 18.8 → trunk View file →
@@ -5,9 +5,9 @@
5 5 * @package WPSEO\Internals\Options
6 6 */
7 7
8 8 /**
9 - * This abstract class and it's concrete classes implement defaults and value validation for
9 + * This abstract class and its concrete classes implement defaults and value validation for
10 10 * all WPSEO options and subkeys within options.
11 11 *
12 12 * Some guidelines:
13 13 * [Retrieving options]
@@ -21,11 +21,11 @@
21 21 * that a default needs to be injected).
22 22 *
23 23 * [Updating/Adding options]
24 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 a the classes here
25 + * - For normal options, use the normal add/update_option() functions. As long as the classes here
26 26 * are instantiated, validation for all options and their subkeys will be automatic.
27 - * - On (succesfull) update of a couple of options, certain related actions will be run automatically.
27 + * - On (successful) update of a couple of options, certain related actions will be run automatically.
28 28 * Some examples:
29 29 * - on change of wpseo[yoast_tracking], the cron schedule will be adjusted accordingly
30 30 * - on change of wpseo and wpseo_title, some caches will be cleared
31 31 *
@@ -40,9 +40,9 @@
40 40 * If the default value is a string which need translating, add this to the concrete class
41 41 * translate_defaults() method.
42 42 * - When you remove an array key from an option: if it's important that the option is really removed,
43 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 existance.
44 + * This will re-save the option and automatically remove the array key no longer in existence.
45 45 * - When you rename a sub-option: add it to the clean_option() routine and run that in the upgrade run.
46 46 * - When you change the default for an option sub-key, make sure you verify that the validation routine will
47 47 * still work the way it should.
48 48 * Example: changing a default from '' (empty string) to 'text' with a validation routine with tests
@@ -60,9 +60,9 @@
60 60 * Prefix for override option keys that allow or disallow the option key of the same name.
61 61 *
62 62 * @var string
63 63 */
64 - const ALLOW_KEY_PREFIX = 'allow_';
64 + public const ALLOW_KEY_PREFIX = 'allow_';
65 65
66 66 /**
67 67 * Option name - MUST be set in concrete class and set to public.
68 68 *
@@ -73,10 +73,10 @@
73 73 /**
74 74 * Option group name for use in settings forms.
75 75 *
76 76 * Will be set automagically if not set in concrete class (i.e.
77 - * if it confirm to the normal pattern 'yoast' . $option_name . 'options',
78 - * only set in conrete class if it doesn't).
77 + * if it conforms to the normal pattern 'yoast' . $option_name . 'options',
78 + * only set in concrete class if it doesn't).
79 79 *
80 80 * @var string
81 81 */
82 82 public $group_name;
@@ -106,9 +106,9 @@
106 106 */
107 107 protected $defaults;
108 108
109 109 /**
110 - * Array of variable option name patterns for the option - if any -.
110 + * Array of variable option name patterns for the option - if any.
111 111 *
112 112 * Set this when the option contains array keys which vary based on post_type
113 113 * or taxonomy.
114 114 *
@@ -136,9 +136,8 @@
136 136 * @var WPSEO_Option
137 137 */
138 138 protected static $instance;
139 139
140 -
141 140 /* *********** INSTANTIATION METHODS *********** */
142 141
143 142 /**
144 143 * Add all the actions and filters for the option.
@@ -148,9 +147,8 @@
148 147 /* Add filters which get applied to the get_options() results. */
149 148 $this->add_default_filters(); // Return defaults if option not set.
150 149 $this->add_option_filters(); // Merge with defaults if option *is* set.
151 150
152 -
153 151 if ( $this->multisite_only !== true ) {
154 152 /**
155 153 * The option validation routines remove the default filters to prevent failing
156 154 * to insert an option if it's new. Let's add them back afterwards.
@@ -180,9 +178,8 @@
180 178 // Refills the cache when the option has been updated.
181 179 add_action( 'update_site_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 1, 0 );
182 180 }
183 181
184 -
185 182 /*
186 183 * Make sure the option will always get validated, independently of register_setting()
187 184 * (only available on back-end).
188 185 */
@@ -187,15 +184,11 @@
187 184 * (only available on back-end).
188 185 */
189 186 add_filter( 'sanitize_option_' . $this->option_name, [ $this, 'validate' ] );
190 187
191 - // Flushes the rewrite rules when option is updated.
192 - add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_rewrites' ] );
193 -
194 188 /* Register our option for the admin pages */
195 189 add_action( 'admin_init', [ $this, 'register_setting' ] );
196 190
197 -
198 191 /* Set option group name if not given */
199 192 if ( ! isset( $this->group_name ) || $this->group_name === '' ) {
200 193 $this->group_name = 'yoast_' . $this->option_name . '_options';
201 194 }
@@ -232,9 +225,9 @@
232 225 * abstract public function translate_defaults();
233 226 * ```
234 227 * ---------------
235 228 *
236 - * Concrete classes *may* contain a enrich_defaults method to add additional defaults once
229 + * Concrete classes *may* contain an enrich_defaults method to add additional defaults once
237 230 * all post_types and taxonomies have been registered.
238 231 *
239 232 * ```
240 233 * abstract public function enrich_defaults();
@@ -298,8 +291,10 @@
298 291 * @param string $key Key to check, by type of service.
299 292 * @param array $dirty Dirty data with the new values.
300 293 * @param array $old Old data.
301 294 * @param array $clean Clean data by reference, normally the default values.
295 + *
296 + * @return void
302 297 */
303 298 public function validate_verification_string( $key, $dirty, $old, &$clean ) {
304 299 if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
305 300 $meta = $dirty[ $key ];
@@ -313,32 +308,20 @@
313 308 }
314 309
315 310 $meta = sanitize_text_field( $meta );
316 311 if ( $meta !== '' ) {
317 - $regex = '`^[A-Fa-f0-9_-]+$`';
318 - $service = '';
312 + $regex = '`^[A-Fa-f0-9_-]+$`';
319 313
320 314 switch ( $key ) {
315 + case 'googleverify':
316 + case 'ahrefsverify':
321 317 case 'baiduverify':
322 - $regex = '`^[A-Za-z0-9_-]+$`';
323 - $service = 'Baidu Webmaster tools';
318 + $regex = '`^[A-Za-z0-9_-]+$`';
324 319 break;
325 320
326 - case 'googleverify':
327 - $regex = '`^[A-Za-z0-9_-]+$`';
328 - $service = 'Google Webmaster tools';
329 - break;
330 -
331 321 case 'msverify':
332 - $service = 'Bing Webmaster tools';
333 - break;
334 -
335 322 case 'pinterestverify':
336 - $service = 'Pinterest';
337 - break;
338 -
339 323 case 'yandexverify':
340 - $service = 'Yandex Webmaster tools';
341 324 break;
342 325 }
343 326
344 327 if ( preg_match( $regex, $meta ) ) {
@@ -349,18 +332,8 @@
349 332 if ( isset( $old[ $key ] ) && preg_match( $regex, $old[ $key ] ) ) {
350 333 $clean[ $key ] = $old[ $key ];
351 334 }
352 335
353 - if ( function_exists( 'add_settings_error' ) ) {
354 - add_settings_error(
355 - $this->group_name, // Slug title of the setting.
356 - $key, // Suffix-ID for the error message box. WordPress prepends `setting-error-`.
357 - /* translators: 1: Verification string from user input; 2: Service name. */
358 - sprintf( __( '%1$s does not seem to be a valid %2$s verification string. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( $meta ) . '</strong>', $service ), // The error message.
359 - 'error' // CSS class for the WP notice, either the legacy 'error' / 'updated' or the new `notice-*` ones.
360 - );
361 - }
362 -
363 336 Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $meta );
364 337 }
365 338 }
366 339 }
@@ -373,33 +346,18 @@
373 346 * @param string $key Key to check, by type of URL setting.
374 347 * @param array $dirty Dirty data with the new values.
375 348 * @param array $old Old data.
376 349 * @param array $clean Clean data by reference, normally the default values.
350 + *
351 + * @return void
377 352 */
378 353 public function validate_url( $key, $dirty, $old, &$clean ) {
379 354 if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
380 355
381 - $submitted_url = trim( htmlspecialchars( $dirty[ $key ], ENT_COMPAT, get_bloginfo( 'charset' ), true ) );
356 + $submitted_url = trim( $dirty[ $key ] );
382 357 $validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );
383 358
384 359 if ( $validated_url === false ) {
385 - if ( function_exists( 'add_settings_error' ) ) {
386 - add_settings_error(
387 - // Slug title of the setting.
388 - $this->group_name,
389 - // Suffix-ID for the error message box. WordPress prepends `setting-error-`.
390 - $key,
391 - // The error message.
392 - sprintf(
393 - /* translators: %s expands to an invalid URL. */
394 - __( '%s does not seem to be a valid url. Please correct.', 'wordpress-seo' ),
395 - '<strong>' . esc_html( $submitted_url ) . '</strong>'
396 - ),
397 - // Message type.
398 - 'error'
399 - );
400 - }
401 -
402 360 // Restore the previous URL value, if any.
403 361 if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
404 362 $url = WPSEO_Utils::sanitize_url( $old[ $key ] );
405 363 if ( $url !== '' ) {
@@ -421,77 +379,8 @@
421 379 }
422 380 }
423 381
424 382 /**
425 - * Validates a Facebook App ID.
426 - *
427 - * @deprecated 15.5
428 - * @codeCoverageIgnore
429 - *
430 - * @param string $key Key to check, in this case: the Facebook App ID field name.
431 - * @param array $dirty Dirty data with the new values.
432 - * @param array $old Old data.
433 - * @param array $clean Clean data by reference, normally the default values.
434 - */
435 - public function validate_facebook_app_id( $key, $dirty, $old, &$clean ) {
436 - _deprecated_function( __METHOD__, 'WPSEO 15.5' );
437 -
438 - if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
439 - $url = 'https://graph.facebook.com/' . $dirty[ $key ];
440 -
441 - $response = wp_remote_get( $url );
442 -
443 - /**
444 - * Filter: 'validate_facebook_app_id_api_response_code' - Allows to filter the Faceboook API response code.
445 - *
446 - * @deprecated 15.5
447 - *
448 - * @api int $response_code The Facebook API response header code.
449 - */
450 - $response_code = apply_filters_deprecated( 'validate_facebook_app_id_api_response_code', wp_remote_retrieve_response_code( $response ), 'WPSEO 15.5' );
451 -
452 - /**
453 - * Filter: 'validate_facebook_app_id_api_response_body' - Allows to filter the Faceboook API response body.
454 - *
455 - * @deprecated 15.5
456 - *
457 - * @api string $response_body The Facebook API JSON response body.
458 - */
459 - $response_body = apply_filters_deprecated( 'validate_facebook_app_id_api_response_body', wp_remote_retrieve_body( $response ), 'WPSEO 15.5' );
460 - $response_object = json_decode( $response_body );
461 -
462 - /*
463 - * When the request is successful the response code will be 200 and
464 - * the response object will contain an `id` property.
465 - */
466 - if ( $response_code === 200 && isset( $response_object->id ) ) {
467 - $clean[ $key ] = $dirty[ $key ];
468 - return;
469 - }
470 -
471 - // Restore the previous value, if any.
472 - if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
473 - $clean[ $key ] = $old[ $key ];
474 - }
475 -
476 - if ( function_exists( 'add_settings_error' ) ) {
477 - add_settings_error(
478 - $this->group_name, // Slug title of the setting.
479 - $key, // Suffix-ID for the error message box. WordPress prepends `setting-error-`.
480 - sprintf(
481 - /* translators: %s expands to an invalid Facebook App ID. */
482 - __( '%s does not seem to be a valid Facebook App ID. Please correct.', 'wordpress-seo' ),
483 - '<strong>' . esc_html( $dirty[ $key ] ) . '</strong>'
484 - ), // The error message.
485 - 'error' // CSS class for the WP notice, either the legacy 'error' / 'updated' or the new `notice-*` ones.
486 - );
487 - }
488 -
489 - Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] );
490 - }
491 - }
492 -
493 - /**
494 383 * Remove the default filters.
495 384 * Called from the validate() method to prevent failure to add new options.
496 385 *
497 386 * @return void
@@ -568,9 +457,9 @@
568 457
569 458 return $filtered;
570 459 }
571 460
572 - /* *********** METHODS influencing add_uption(), update_option() and saving from admin pages. *********** */
461 + /* *********** METHODS influencing add_option(), update_option() and saving from admin pages. *********** */
573 462
574 463 /**
575 464 * Register (whitelist) the option for the configuration pages.
576 465 * The validation callback is already registered separately on the sanitize_option hook,
@@ -594,9 +483,9 @@
594 483 register_setting( $this->group_name, $this->option_name );
595 484 }
596 485
597 486 /**
598 - * Validate the option
487 + * Validate the option.
599 488 *
600 489 * @param mixed $option_value The unvalidated new value for the option.
601 490 *
602 491 * @return array Validated new value for the option.
@@ -716,9 +605,9 @@
716 605 * for safely adding/updating multisite options.}}
717 606 *
718 607 * @param mixed $value The new value for the option.
719 608 *
720 - * @return bool Whether the update was succesfull.
609 + * @return bool Whether the update was successful.
721 610 */
722 611 public function update_site_option( $value ) {
723 612 if ( $this->multisite_only === true && is_multisite() ) {
724 613 $this->remove_default_filters();
@@ -738,9 +627,9 @@
738 627 * @uses WPSEO_Option::get_original_option()
739 628 * @uses WPSEO_Option::import()
740 629 *
741 630 * @param string|null $current_version Optional. Version from which to upgrade, if not set,
742 - * version specific upgrades will be disregarded.
631 + * version-specific upgrades will be disregarded.
743 632 *
744 633 * @return void
745 634 */
746 635 public function clean( $current_version = null ) {
@@ -763,9 +652,9 @@
763 652 * Important: all validation routines which add_settings_errors would need to be changed for this to work
764 653 *
765 654 * @param array $option_value Option value to be imported.
766 655 * @param string|null $current_version Optional. Version from which to upgrade, if not set,
767 - * version specific upgrades will be disregarded.
656 + * version-specific upgrades will be disregarded.
768 657 * @param array|null $all_old_option_values Optional. Only used when importing old options to
769 658 * have access to the real old values, in contrast to
770 659 * the saved ones.
771 660 *
@@ -941,9 +830,9 @@
941 830
942 831 /**
943 832 * Check whether a given array key conforms to one of the variable array key patterns for this option.
944 833 *
945 - * @usedby validate_option() methods for options with variable array keys.
834 + * @used-by validate_option() methods for options with variable array keys.
946 835 *
947 836 * @param string $key Array key to check.
948 837 *
949 838 * @return string Pattern if it conforms, original array key if it doesn't or if the option