class-wpseo-option-wpseo.php 21.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Internals\Options
 */

/**
 * Option: wpseo.
 */
class WPSEO_Option_Wpseo extends WPSEO_Option {

	/**
	 * Option name.
	 *
	 * @var string
	 */
	public $option_name = 'wpseo';

	/**
	 * Array of defaults for the option.
	 *
	 * {@internal Shouldn't be requested directly, use $this->get_defaults();}}
	 *
	 * @var array
	 */
	protected $defaults = [
		// Non-form fields, set via (ajax) function.
		'tracking'                                 => null,
		'toggled_tracking'                         => false,
		'license_server_version'                   => false,
		'ms_defaults_set'                          => false,
		'ignore_search_engines_discouraged_notice' => false,
		'indexing_first_time'                      => true,
		'indexing_started'                         => null,
		'indexing_reason'                          => '',
		'indexables_indexing_completed'            => false,
		'index_now_key'                            => '',
		// Non-form field, should only be set via validation routine.
		'version'                                  => '', // Leave default as empty to ensure activation/upgrade works.
		'previous_version'                         => '',
		// Form fields.
		'disableadvanced_meta'                     => true,
		'enable_headless_rest_endpoints'           => true,
		'ryte_indexability'                        => false,
		'baiduverify'                              => '', // Text field.
		'googleverify'                             => '', // Text field.
		'msverify'                                 => '', // Text field.
		'yandexverify'                             => '',
		'site_type'                                => '', // List of options.
		'has_multiple_authors'                     => '',
		'environment_type'                         => '',
		'content_analysis_active'                  => true,
		'keyword_analysis_active'                  => true,
		'inclusive_language_analysis_active'       => false,
		'enable_admin_bar_menu'                    => true,
		'enable_cornerstone_content'               => true,
		'enable_xml_sitemap'                       => true,
		'enable_text_link_counter'                 => true,
		'enable_index_now'                         => true,
		'show_onboarding_notice'                   => false,
		'first_activated_on'                       => false,
		'myyoast-oauth'                            => [
			'config'        => [
				'clientId' => null,
				'secret'   => null,
			],
			'access_tokens' => [],
		],
		'semrush_integration_active'               => true,
		'semrush_tokens'                           => [],
		'semrush_country_code'                     => 'us',
		'permalink_structure'                      => '',
		'home_url'                                 => '',
		'dynamic_permalinks'                       => false,
		'category_base_url'                        => '',
		'tag_base_url'                             => '',
		'custom_taxonomy_slugs'                    => [],
		'enable_enhanced_slack_sharing'            => true,
		'zapier_integration_active'                => false,
		'zapier_subscription'                      => [],
		'zapier_api_key'                           => '',
		'enable_metabox_insights'                  => true,
		'enable_link_suggestions'                  => true,
		'algolia_integration_active'               => false,
		'import_cursors'                           => [],
		'workouts_data'                            => [ 'configuration' => [ 'finishedSteps' => [] ] ],
		'configuration_finished_steps'             => [],
		'dismiss_configuration_workout_notice'     => false,
		'dismiss_premium_deactivated_notice'       => false,
		'importing_completed'                      => [],
		'wincher_integration_active'               => true,
		'wincher_tokens'                           => [],
		'wincher_automatically_add_keyphrases'     => false,
		'wincher_website_id'                       => '',
		'wordproof_integration_active'             => false,
		'wordproof_integration_changed'            => false,
		'first_time_install'                       => false,
		'should_redirect_after_install_free'       => false,
		'activation_redirect_timestamp_free'       => 0,
		'remove_feed_global'                       => false,
		'remove_feed_global_comments'              => false,
		'remove_feed_post_comments'                => false,
		'remove_feed_authors'                      => false,
		'remove_feed_categories'                   => false,
		'remove_feed_tags'                         => false,
		'remove_feed_custom_taxonomies'            => false,
		'remove_feed_post_types'                   => false,
		'remove_feed_search'                       => false,
		'remove_atom_rdf_feeds'                    => false,
		'remove_shortlinks'                        => false,
		'remove_rest_api_links'                    => false,
		'remove_rsd_wlw_links'                     => false,
		'remove_oembed_links'                      => false,
		'remove_generator'                         => false,
		'remove_emoji_scripts'                     => false,
		'remove_powered_by_header'                 => false,
		'remove_pingback_header'                   => false,
		'clean_campaign_tracking_urls'             => false,
		'clean_permalinks'                         => false,
		'clean_permalinks_extra_variables'         => '',
		'search_cleanup'                           => false,
		'search_cleanup_emoji'                     => false,
		'search_cleanup_patterns'                  => false,
		'search_character_limit'                   => 50,
		'deny_search_crawling'                     => false,
		'deny_wp_json_crawling'                    => false,
		'deny_adsbot_crawling'                     => false,
		'redirect_search_pretty_urls'              => false,
		'least_readability_ignore_list'            => [],
		'least_seo_score_ignore_list'              => [],
		'most_linked_ignore_list'                  => [],
		'least_linked_ignore_list'                 => [],
		'indexables_page_reading_list'             => [ false, false, false, false, false ],
		'indexables_overview_state'                => 'dashboard-not-visited',
		'last_known_public_post_types'             => [],
		'last_known_public_taxonomies'             => [],
		'last_known_no_unindexed'                  => [],
		'new_post_types'                           => [],
		'new_taxonomies'                           => [],
		'show_new_content_type_notification'       => false,
	];

	/**
	 * Sub-options which should not be overloaded with multi-site defaults.
	 *
	 * @var array
	 */
	public $ms_exclude = [
		'ignore_search_engines_discouraged_notice',
		/* Privacy. */
		'baiduverify',
		'googleverify',
		'msverify',
		'yandexverify',
	];

	/**
	 * Possible values for the site_type option.
	 *
	 * @var array
	 */
	protected $site_types = [
		'',
		'blog',
		'shop',
		'news',
		'smallBusiness',
		'corporateOther',
		'personalOther',
	];

	/**
	 * Possible environment types.
	 *
	 * @var array
	 */
	protected $environment_types = [
		'',
		'local',
		'production',
		'staging',
		'development',
	];

	/**
	 * Possible has_multiple_authors options.
	 *
	 * @var array
	 */
	protected $has_multiple_authors_options = [
		'',
		true,
		false,
	];

	/**
	 * Name for an option higher in the hierarchy to override setting access.
	 *
	 * @var string
	 */
	protected $override_option_name = 'wpseo_ms';

	/**
	 * Add the actions and filters for the option.
	 *
	 * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
	 *       is updated early on and if so, change the call to schedule these for a later action on add/update
	 *       instead of running them straight away.
	 */
	protected function __construct() {
		parent::__construct();

		/**
		 * Filter: 'wpseo_enable_tracking' - Enables the data tracking of Yoast SEO Premium.
		 *
		 * @api string $is_enabled The enabled state. Default is false.
		 */
		$this->defaults['tracking'] = apply_filters( 'wpseo_enable_tracking', false );

		/* Clear the cache on update/add. */
		add_action( 'add_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_cache' ] );
		add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_cache' ] );

		add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );

		/**
		 * Filter the `wpseo` option defaults.
		 *
		 * @param array $defaults Array the defaults for the `wpseo` option attributes.
		 */
		$this->defaults = apply_filters( 'wpseo_option_wpseo_defaults', $this->defaults );
	}

	/**
	 * Get the singleton instance of this class.
	 *
	 * @return object
	 */
	public static function get_instance() {
		if ( ! ( self::$instance instanceof self ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Add filters to make sure that the option is merged with its defaults before being returned.
	 *
	 * @return void
	 */
	public function add_option_filters() {
		parent::add_option_filters();

		list( $hookname, $callback, $priority ) = $this->get_verify_features_option_filter_hook();

		if ( has_filter( $hookname, $callback ) === false ) {
			add_filter( $hookname, $callback, $priority );
		}
	}

	/**
	 * Remove the option filters.
	 * Called from the clean_up methods to make sure we retrieve the original old option.
	 *
	 * @return void
	 */
	public function remove_option_filters() {
		parent::remove_option_filters();

		list( $hookname, $callback, $priority ) = $this->get_verify_features_option_filter_hook();

		remove_filter( $hookname, $callback, $priority );
	}

	/**
	 * Add filters to make sure that the option default is returned if the option is not set.
	 *
	 * @return void
	 */
	public function add_default_filters() {
		parent::add_default_filters();

		list( $hookname, $callback, $priority ) = $this->get_verify_features_default_option_filter_hook();

		if ( has_filter( $hookname, $callback ) === false ) {
			add_filter( $hookname, $callback, $priority );
		}
	}

	/**
	 * Remove the default filters.
	 * Called from the validate() method to prevent failure to add new options.
	 *
	 * @return void
	 */
	public function remove_default_filters() {
		parent::remove_default_filters();

		list( $hookname, $callback, $priority ) = $this->get_verify_features_default_option_filter_hook();

		remove_filter( $hookname, $callback, $priority );
	}

	/**
	 * Validate the option.
	 *
	 * @param array $dirty New value for the option.
	 * @param array $clean Clean value for the option, normally the defaults.
	 * @param array $old   Old value of the option.
	 *
	 * @return array Validated clean value for the option to be saved to the database.
	 */
	protected function validate_option( $dirty, $clean, $old ) {

		foreach ( $clean as $key => $value ) {
			switch ( $key ) {
				case 'version':
					$clean[ $key ] = WPSEO_VERSION;
					break;
				case 'previous_version':
				case 'semrush_country_code':
				case 'license_server_version':
				case 'home_url':
				case 'zapier_api_key':
				case 'index_now_key':
				case 'wincher_website_id':
				case 'clean_permalinks_extra_variables':
				case 'indexables_overview_state':
					if ( isset( $dirty[ $key ] ) ) {
						$clean[ $key ] = $dirty[ $key ];
					}
					break;
				case 'indexing_reason':
					if ( isset( $dirty[ $key ] ) ) {
						$clean[ $key ] = sanitize_text_field( $dirty[ $key ] );
					}
					break;

				/* Verification strings. */
				case 'baiduverify':
				case 'googleverify':
				case 'msverify':
				case 'yandexverify':
					$this->validate_verification_string( $key, $dirty, $old, $clean );
					break;

				/*
				 * Boolean dismiss warnings - not fields - may not be in form
				 * (and don't need to be either as long as the default is false).
				 */
				case 'ignore_search_engines_discouraged_notice':
				case 'ms_defaults_set':
					if ( isset( $dirty[ $key ] ) ) {
						$clean[ $key ] = WPSEO_Utils::validate_bool( $dirty[ $key ] );
					}
					elseif ( isset( $old[ $key ] ) ) {
						$clean[ $key ] = WPSEO_Utils::validate_bool( $old[ $key ] );
					}
					break;

				case 'site_type':
					$clean[ $key ] = $old[ $key ];
					if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->site_types, true ) ) {
						$clean[ $key ] = $dirty[ $key ];
					}
					break;

				case 'environment_type':
					$clean[ $key ] = $old[ $key ];
					if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->environment_types, true ) ) {
						$clean[ $key ] = $dirty[ $key ];
					}
					break;

				case 'has_multiple_authors':
					$clean[ $key ] = $old[ $key ];
					if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->has_multiple_authors_options, true ) ) {
						$clean[ $key ] = $dirty[ $key ];
					}

					break;

				case 'first_activated_on':
				case 'indexing_started':
				case 'activation_redirect_timestamp_free':
					$clean[ $key ] = false;
					if ( isset( $dirty[ $key ] ) ) {
						if ( $dirty[ $key ] === false || WPSEO_Utils::validate_int( $dirty[ $key ] ) ) {
							$clean[ $key ] = $dirty[ $key ];
						}
					}
					break;

				case 'tracking':
					$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : null );
					break;

				case 'myyoast_oauth':
				case 'semrush_tokens':
				case 'custom_taxonomy_slugs':
				case 'zapier_subscription':
				case 'wincher_tokens':
				case 'workouts_data':
				case 'configuration_finished_steps':
				case 'least_readability_ignore_list':
				case 'least_seo_score_ignore_list':
				case 'most_linked_ignore_list':
				case 'least_linked_ignore_list':
				case 'indexables_page_reading_list':
				case 'last_known_public_post_types':
				case 'last_known_public_taxonomies':
				case 'new_post_types':
				case 'new_taxonomies':
					$clean[ $key ] = $old[ $key ];

					if ( isset( $dirty[ $key ] ) ) {
						$items = $dirty[ $key ];
						if ( ! is_array( $items ) ) {
							$items = json_decode( $dirty[ $key ], true );
						}

						if ( is_array( $items ) ) {
							$clean[ $key ] = $dirty[ $key ];
						}
					}

					break;

				case 'permalink_structure':
				case 'category_base_url':
				case 'tag_base_url':
					if ( isset( $dirty[ $key ] ) ) {
						$clean[ $key ] = sanitize_option( $key, $dirty[ $key ] );
					}
					break;

				case 'search_character_limit':
					if ( isset( $dirty[ $key ] ) ) {
						$clean[ $key ] = (int) $dirty[ $key ];
					}
					break;

				case 'import_cursors':
				case 'importing_completed':
					if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
						$clean[ $key ] = $dirty[ $key ];
					}
					break;

				case 'wordproof_integration_active':
					$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
					// If the setting has changed, record it.
					if ( $old[ $key ] !== $clean[ $key ] ) {
						$clean['wordproof_integration_changed'] = true;
					}
					break;
				case 'last_known_no_unindexed':
					$clean[ $key ] = $old[ $key ];

					if ( isset( $dirty[ $key ] ) ) {
						$items = $dirty[ $key ];

						if ( is_array( $items ) ) {
							foreach ( $items as $item_key => $item ) {
								if ( ! \is_string( $item_key ) || ! \is_numeric( $item ) ) {
									unset( $items[ $item_key ] );
								}
							}
							$clean[ $key ] = $items;
						}
					}

					break;

				/*
				 * Boolean (checkbox) fields.
				 *
				 * Covers:
				 *  'disableadvanced_meta'
				 *  'enable_headless_rest_endpoints'
				 *  'yoast_tracking'
				 *  'dynamic_permalinks'
				 *  'indexing_first_time'
				 *  'first_time_install'
				 *  'remove_feed_global'
				 *  'remove_feed_global_comments'
				 *  'remove_feed_post_comments'
				 *  'remove_feed_authors'
				 *  'remove_feed_categories'
				 *  'remove_feed_tags'
				 *  'remove_feed_custom_taxonomies'
				 *  'remove_feed_post_types'
				 *  'remove_feed_search'
				 *  'remove_atom_rdf_feeds'
				 *  'remove_shortlinks'
				 *  'remove_rest_api_links'
				 *  'remove_rsd_wlw_links'
				 *  'remove_oembed_links'
				 *  'remove_generator'
				 *  'remove_emoji_scripts'
				 *  'remove_powered_by_header'
				 *  'remove_pingback_header'
				 *  'clean_campaign_tracking_urls'
				 *  'clean_permalinks'
				 *  'clean_permalinks_extra_variables'
				 *  'search_cleanup'
				 *  'search_cleanup_emoji'
				 *  'search_cleanup_patterns'
				 *  'deny_wp_json_crawling'
				 *  'deny_adsbot_crawling'
				 *  'redirect_search_pretty_urls'
				 *  'should_redirect_after_install_free'
				 *  'show_new_content_type_notification'
				 *  and most of the feature variables.
				 */
				default:
					$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
					break;
			}
		}

		return $clean;
	}

	/**
	 * Verifies that the feature variables are turned off if the network is configured so.
	 *
	 * @param mixed $options Value of the option to be returned. Typically an array.
	 *
	 * @return mixed Filtered $options value.
	 */
	public function verify_features_against_network( $options = [] ) {
		if ( ! is_array( $options ) || empty( $options ) ) {
			return $options;
		}

		// For the feature variables, set their values to off in case they are disabled.
		$feature_vars = [
			'disableadvanced_meta'               => false,
			'ryte_indexability'                  => false,
			'content_analysis_active'            => false,
			'keyword_analysis_active'            => false,
			'inclusive_language_analysis_active' => false,
			'enable_admin_bar_menu'              => false,
			'enable_cornerstone_content'         => false,
			'enable_xml_sitemap'                 => false,
			'enable_text_link_counter'           => false,
			'enable_metabox_insights'            => false,
			'enable_link_suggestions'            => false,
			'enable_headless_rest_endpoints'     => false,
			'tracking'                           => false,
			'enable_enhanced_slack_sharing'      => false,
			'semrush_integration_active'         => false,
			'zapier_integration_active'          => false,
			'wincher_integration_active'         => false,
			'remove_feed_global'                 => false,
			'remove_feed_global_comments'        => false,
			'remove_feed_post_comments'          => false,
			'enable_index_now'                   => false,
			'remove_feed_authors'                => false,
			'remove_feed_categories'             => false,
			'remove_feed_tags'                   => false,
			'remove_feed_custom_taxonomies'      => false,
			'remove_feed_post_types'             => false,
			'remove_feed_search'                 => false,
			'remove_atom_rdf_feeds'              => false,
			'remove_shortlinks'                  => false,
			'remove_rest_api_links'              => false,
			'remove_rsd_wlw_links'               => false,
			'remove_oembed_links'                => false,
			'remove_generator'                   => false,
			'remove_emoji_scripts'               => false,
			'remove_powered_by_header'           => false,
			'remove_pingback_header'             => false,
			'clean_campaign_tracking_urls'       => false,
			'clean_permalinks'                   => false,
			'search_cleanup'                     => false,
			'search_cleanup_emoji'               => false,
			'search_cleanup_patterns'            => false,
			'redirect_search_pretty_urls'        => false,
			'algolia_integration_active'         => false,
		];

		// We can reuse this logic from the base class with the above defaults to parse with the correct feature values.
		$options = $this->prevent_disabled_options_update( $options, $feature_vars );

		return $options;
	}

	/**
	 * Gets the filter hook name and callback for adjusting the retrieved option value
	 * against the network-allowed features.
	 *
	 * @return array Array where the first item is the hook name, the second is the hook callback,
	 *               and the third is the hook priority.
	 */
	protected function get_verify_features_option_filter_hook() {
		return [
			"option_{$this->option_name}",
			[ $this, 'verify_features_against_network' ],
			11,
		];
	}

	/**
	 * Gets the filter hook name and callback for adjusting the default option value against the network-allowed features.
	 *
	 * @return array Array where the first item is the hook name, the second is the hook callback,
	 *               and the third is the hook priority.
	 */
	protected function get_verify_features_default_option_filter_hook() {
		return [
			"default_option_{$this->option_name}",
			[ $this, 'verify_features_against_network' ],
			11,
		];
	}

	/**
	 * Clean a given option value.
	 *
	 * @param array       $option_value          Old (not merged with defaults or filtered) option value to
	 *                                           clean according to the rules for this option.
	 * @param string|null $current_version       Optional. Version from which to upgrade, if not set,
	 *                                           version specific upgrades will be disregarded.
	 * @param array|null  $all_old_option_values Optional. Only used when importing old options to have
	 *                                           access to the real old values, in contrast to the saved ones.
	 *
	 * @return array Cleaned option.
	 */
	protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
		// Deal with value change from text string to boolean.
		$value_change = [
			'ignore_search_engines_discouraged_notice',
		];

		$target_values = [
			'ignore',
			'done',
		];

		foreach ( $value_change as $key ) {
			if ( isset( $option_value[ $key ] )
				&& in_array( $option_value[ $key ], $target_values, true )
			) {
				$option_value[ $key ] = true;
			}
		}

		return $option_value;
	}
}