This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@ -0,0 +1,261 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Site option for Multisite installs only
*
* Overloads a number of methods of the abstract class to ensure the use of the correct site_option
* WP functions.
*/
class WPSEO_Option_MS extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_ms';
/**
* Option group name for use in settings forms.
*
* @var string
*/
public $group_name = 'yoast_wpseo_multisite_options';
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* @var bool
*/
public $include_in_all = false;
/**
* Whether this option is only for when the install is multisite.
*
* @var bool
*/
public $multisite_only = true;
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults = [];
/**
* Available options for the 'access' setting. Used for input validation.
*
* {@internal Important: Make sure the options added to the array here are in line
* with the keys for the options set for the select box in the
* admin/pages/network.php file.}}
*
* @var array
*/
public static $allowed_access_options = [
'admin',
'superadmin',
];
/**
* 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;
}
/**
* Only run parent constructor in multisite context.
*/
public function __construct() {
$allow_prefix = self::ALLOW_KEY_PREFIX;
$this->defaults = [
'access' => 'admin',
'defaultblog' => '', // Numeric blog ID or empty.
"{$allow_prefix}disableadvanced_meta" => true,
"{$allow_prefix}ryte_indexability" => false,
"{$allow_prefix}content_analysis_active" => true,
"{$allow_prefix}keyword_analysis_active" => true,
"{$allow_prefix}inclusive_language_analysis_active" => true,
"{$allow_prefix}enable_admin_bar_menu" => true,
"{$allow_prefix}enable_cornerstone_content" => true,
"{$allow_prefix}enable_xml_sitemap" => true,
"{$allow_prefix}enable_text_link_counter" => true,
"{$allow_prefix}enable_headless_rest_endpoints" => true,
"{$allow_prefix}enable_metabox_insights" => true,
"{$allow_prefix}enable_link_suggestions" => true,
"{$allow_prefix}tracking" => true,
"{$allow_prefix}enable_enhanced_slack_sharing" => true,
"{$allow_prefix}semrush_integration_active" => true,
"{$allow_prefix}zapier_integration_active" => true,
"{$allow_prefix}wincher_integration_active" => false,
"{$allow_prefix}remove_feed_global" => true,
"{$allow_prefix}remove_feed_global_comments" => true,
"{$allow_prefix}remove_feed_post_comments" => true,
"{$allow_prefix}enable_index_now" => true,
"{$allow_prefix}enable_ai_generator" => true,
"{$allow_prefix}remove_feed_authors" => true,
"{$allow_prefix}remove_feed_categories" => true,
"{$allow_prefix}remove_feed_tags" => true,
"{$allow_prefix}remove_feed_custom_taxonomies" => true,
"{$allow_prefix}remove_feed_post_types" => true,
"{$allow_prefix}remove_feed_search" => true,
"{$allow_prefix}remove_atom_rdf_feeds" => true,
"{$allow_prefix}remove_shortlinks" => true,
"{$allow_prefix}remove_rest_api_links" => true,
"{$allow_prefix}remove_rsd_wlw_links" => true,
"{$allow_prefix}remove_oembed_links" => true,
"{$allow_prefix}remove_generator" => true,
"{$allow_prefix}remove_emoji_scripts" => true,
"{$allow_prefix}remove_powered_by_header" => true,
"{$allow_prefix}remove_pingback_header" => true,
"{$allow_prefix}clean_campaign_tracking_urls" => true,
"{$allow_prefix}clean_permalinks" => true,
"{$allow_prefix}search_cleanup" => true,
"{$allow_prefix}search_cleanup_emoji" => true,
"{$allow_prefix}search_cleanup_patterns" => true,
"{$allow_prefix}redirect_search_pretty_urls" => true,
"{$allow_prefix}wordproof_integration_active" => false,
"{$allow_prefix}algolia_integration_active" => true,
];
if ( is_multisite() ) {
parent::__construct();
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
}
}
/**
* Add filters to make sure that the option default is returned if the option is not set
*
* @return void
*/
public function add_default_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
add_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
}
/**
* Remove the default filters.
* Called from the validate() method to prevent failure to add new options.
*
* @return void
*/
public function remove_default_filters() {
remove_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
/**
* Add filters to make sure that the option is merged with its defaults before being returned.
*
* @return void
*/
public function add_option_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
add_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
}
}
/**
* 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() {
remove_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
}
/* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
/**
* 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 'access':
if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$allowed_access_options, true ) ) {
$clean[ $key ] = $dirty[ $key ];
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
/* translators: %1$s expands to the option name and %2$sexpands to Yoast SEO */
sprintf( __( '%1$s is not a valid choice for who should be allowed access to the %2$s settings. Value reset to the default.', 'wordpress-seo' ), esc_html( sanitize_text_field( $dirty[ $key ] ) ), 'Yoast SEO' ), // The error message.
'error' // Message type.
);
}
break;
case 'defaultblog':
if ( isset( $dirty[ $key ] ) && ( $dirty[ $key ] !== '' && $dirty[ $key ] !== '-' ) ) {
$int = WPSEO_Utils::validate_int( $dirty[ $key ] );
if ( $int !== false && $int > 0 ) {
// Check if a valid blog number has been received.
$exists = get_blog_details( $int, false );
if ( $exists && $exists->deleted === '0' ) {
$clean[ $key ] = $int;
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' )
. '<br>'
. sprintf(
/* translators: %s is the ID number of a blog. */
esc_html__( 'This must be an existing blog. Blog %s does not exist or has been marked as deleted.', 'wordpress-seo' ),
'<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
), // The error message.
'error' // Message type.
);
}
unset( $exists );
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . esc_html__( 'No numeric value was received.', 'wordpress-seo' ), // The error message.
'error' // Message type.
);
}
unset( $int );
}
break;
default:
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
break;
}
}
return $clean;
}
}

View File

@ -0,0 +1,336 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Option: wpseo_social.
*/
class WPSEO_Option_Social extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_social';
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults = [
// Form fields.
'facebook_site' => '', // Text field.
'instagram_url' => '',
'linkedin_url' => '',
'myspace_url' => '',
'og_default_image' => '', // Text field.
'og_default_image_id' => '',
'og_frontpage_title' => '', // Text field.
'og_frontpage_desc' => '', // Text field.
'og_frontpage_image' => '', // Text field.
'og_frontpage_image_id' => '',
'opengraph' => true,
'pinterest_url' => '',
'pinterestverify' => '',
'twitter' => true,
'twitter_site' => '', // Text field.
'twitter_card_type' => 'summary_large_image',
'youtube_url' => '',
'wikipedia_url' => '',
'other_social_urls' => [],
'mastodon_url' => '',
];
/**
* Array of sub-options which should not be overloaded with multi-site defaults.
*
* @var array
*/
public $ms_exclude = [
/* Privacy. */
'pinterestverify',
];
/**
* Array of allowed twitter card types.
*
* While we only have the options summary and summary_large_image in the
* interface now, we might change that at some point.
*
* {@internal Uncomment any of these to allow them in validation *and* automatically
* add them as a choice in the options page.}}
*
* @var array
*/
public static $twitter_card_types = [
'summary_large_image' => '',
// 'summary' => '',
// 'photo' => '',
// 'gallery' => '',
// 'app' => '',
// 'player' => '',
// 'product' => '',
];
/**
* Add the actions and filters for the option.
*/
protected function __construct() {
parent::__construct();
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
}
/**
* 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;
}
/**
* Translate/set strings used in the option defaults.
*
* @return void
*/
public function translate_defaults() {
self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' );
}
/**
* 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 ) {
/* Text fields. */
case 'og_frontpage_desc':
case 'og_frontpage_title':
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
}
break;
case 'og_default_image_id':
case 'og_frontpage_image_id':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = (int) $dirty[ $key ];
if ( $dirty[ $key ] === '' ) {
$clean[ $key ] = $dirty[ $key ];
}
}
break;
/* URL text fields - no ftp allowed. */
case 'facebook_site':
case 'instagram_url':
case 'linkedin_url':
case 'myspace_url':
case 'pinterest_url':
case 'og_default_image':
case 'og_frontpage_image':
case 'youtube_url':
case 'wikipedia_url':
case 'mastodon_url':
$this->validate_url( $key, $dirty, $old, $clean );
break;
case 'pinterestverify':
$this->validate_verification_string( $key, $dirty, $old, $clean );
break;
/* Twitter user name. */
case 'twitter_site':
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$twitter_id = $this->validate_twitter_id( $dirty[ $key ] );
if ( $twitter_id ) {
$clean[ $key ] = $twitter_id;
}
else {
if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
$twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) );
if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
$clean[ $key ] = $twitter_id;
}
}
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
sprintf(
/* translators: %s expands to a twitter user name. */
__( '%s does not seem to be a valid Twitter Username. Please correct.', 'wordpress-seo' ),
'<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
), // The error message.
'error' // Message type.
);
}
}
unset( $twitter_id );
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] );
}
break;
case 'twitter_card_type':
if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
/* Boolean fields. */
case 'opengraph':
case 'twitter':
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
break;
/* Array fields. */
case 'other_social_urls':
if ( isset( $dirty[ $key ] ) ) {
$items = $dirty[ $key ];
if ( ! is_array( $items ) ) {
$items = json_decode( $dirty[ $key ], true );
}
if ( is_array( $items ) ) {
foreach ( $items as $item_key => $item ) {
$validated_url = $this->validate_social_url( $item );
if ( $validated_url === false ) {
// Restore the previous URL values, if any.
$old_urls = ( isset( $old[ $key ] ) ) ? $old[ $key ] : [];
foreach ( $old_urls as $old_item_key => $old_url ) {
if ( $old_url !== '' ) {
$url = WPSEO_Utils::sanitize_url( $old_url );
if ( $url !== '' ) {
$clean[ $key ][ $old_item_key ] = $url;
}
}
}
break;
}
// The URL format is valid, let's sanitize it.
$url = WPSEO_Utils::sanitize_url( $validated_url );
if ( $url !== '' ) {
$clean[ $key ][ $item_key ] = $url;
}
}
}
}
break;
}
}
return $clean;
}
/**
* Validates a social URL.
*
* @param string $url The url to be validated.
*
* @return string|false The validated URL or false if the URL is not valid.
*/
public function validate_social_url( $url ) {
$validated_url = filter_var( WPSEO_Utils::sanitize_url( trim( $url ) ), FILTER_VALIDATE_URL );
return $validated_url;
}
/**
* Validates a twitter id.
*
* @param string $twitter_id The twitter id to be validated.
* @param bool $strip_at_sign Whether or not to strip the `@` sign.
*
* @return string|false The validated twitter id or false if it is not valid.
*/
public function validate_twitter_id( $twitter_id, $strip_at_sign = true ) {
$twitter_id = ( $strip_at_sign ) ? sanitize_text_field( ltrim( $twitter_id, '@' ) ) : sanitize_text_field( $twitter_id );
/*
* From the Twitter documentation about twitter screen names:
* Typically a maximum of 15 characters long, but some historical accounts
* may exist with longer names.
* A username can only contain alphanumeric characters (letters A-Z, numbers 0-9)
* with the exception of underscores.
*
* @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames
*/
if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
return $twitter_id;
}
if ( preg_match( '`^http(?:s)?://(?:www\.)?twitter\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) {
return $matches['handle'];
}
return false;
}
/**
* 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 ) {
/* Move options from very old option to this one. */
$old_option = null;
if ( isset( $all_old_option_values ) ) {
// Ok, we have an import.
if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== [] ) {
$old_option = $all_old_option_values['wpseo_indexation'];
}
}
else {
$old_option = get_option( 'wpseo_indexation' );
}
if ( is_array( $old_option ) && $old_option !== [] ) {
$move = [
'opengraph',
];
foreach ( $move as $key ) {
if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) {
$option_value[ $key ] = $old_option[ $key ];
}
}
unset( $move, $key );
}
unset( $old_option );
return $option_value;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,656 @@
<?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,
'enable_ai_generator' => false,
'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,
'enable_ai_generator' => 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;
}
}

View File

@ -0,0 +1,893 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* This abstract class and it's concrete classes implement defaults and value validation for
* all WPSEO options and subkeys within options.
*
* Some guidelines:
* [Retrieving options]
* - Use the normal get_option() to retrieve an option. You will receive a complete array for the option.
* Any subkeys which were not set, will have their default values in place.
* - In other words, you will normally not have to check whether a subkey isset() as they will *always* be set.
* They will also *always* be of the correct variable type.
* The only exception to this are the options with variable option names based on post_type or taxonomy
* as those will not always be available before the taxonomy/post_type is registered.
* (they will be available if a value was set, they won't be if it wasn't as the class won't know
* that a default needs to be injected).
*
* [Updating/Adding options]
* - For multisite site_options, please use the WPSEO_Options::update_site_option() method.
* - For normal options, use the normal add/update_option() functions. As long a the classes here
* are instantiated, validation for all options and their subkeys will be automatic.
* - On (succesfull) update of a couple of options, certain related actions will be run automatically.
* Some examples:
* - on change of wpseo[yoast_tracking], the cron schedule will be adjusted accordingly
* - on change of wpseo and wpseo_title, some caches will be cleared
*
* [Important information about add/updating/changing these classes]
* - Make sure that option array key names are unique across options. The WPSEO_Options::get_all()
* method merges most options together. If any of them have non-unique names, even if they
* are in a different option, they *will* overwrite each other.
* - When you add a new array key in an option: make sure you add proper defaults and add the key
* to the validation routine in the proper place or add a new validation case.
* You don't need to do any upgrading as any option returned will always be merged with the
* defaults, so new options will automatically be available.
* If the default value is a string which need translating, add this to the concrete class
* translate_defaults() method.
* - When you remove an array key from an option: if it's important that the option is really removed,
* add the WPSEO_Option::clean_up( $option_name ) method to the upgrade run.
* This will re-save the option and automatically remove the array key no longer in existance.
* - When you rename a sub-option: add it to the clean_option() routine and run that in the upgrade run.
* - When you change the default for an option sub-key, make sure you verify that the validation routine will
* still work the way it should.
* Example: changing a default from '' (empty string) to 'text' with a validation routine with tests
* for an empty string will prevent a user from saving an empty string as the real value. So the
* test for '' with the validation routine would have to be removed in that case.
* - If an option needs specific actions different from defined in this abstract class, you can just overrule
* a method by defining it in the concrete class.
*
* @todo [JRF => testers] Double check that validation will not cause errors when called
* from upgrade routine (some of the WP functions may not yet be available).
*/
abstract class WPSEO_Option {
/**
* Prefix for override option keys that allow or disallow the option key of the same name.
*
* @var string
*/
const ALLOW_KEY_PREFIX = 'allow_';
/**
* Option name - MUST be set in concrete class and set to public.
*
* @var string
*/
protected $option_name;
/**
* Option group name for use in settings forms.
*
* Will be set automagically if not set in concrete class (i.e.
* if it confirm to the normal pattern 'yoast' . $option_name . 'options',
* only set in conrete class if it doesn't).
*
* @var string
*/
public $group_name;
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* Also determines which options are copied over for ms_(re)set_blog().
*
* @var bool
*/
public $include_in_all = true;
/**
* Whether this option is only for when the install is multisite.
*
* @var bool
*/
public $multisite_only = false;
/**
* Array of defaults for the option - MUST be set in concrete class.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults;
/**
* Array of variable option name patterns for the option - if any -.
*
* Set this when the option contains array keys which vary based on post_type
* or taxonomy.
*
* @var array
*/
protected $variable_array_key_patterns;
/**
* Array of sub-options which should not be overloaded with multi-site defaults.
*
* @var array
*/
public $ms_exclude = [];
/**
* Name for an option higher in the hierarchy to override setting access.
*
* @var string
*/
protected $override_option_name;
/**
* Instance of this class.
*
* @var WPSEO_Option
*/
protected static $instance;
/* *********** INSTANTIATION METHODS *********** */
/**
* Add all the actions and filters for the option.
*/
protected function __construct() {
/* Add filters which get applied to the get_options() results. */
$this->add_default_filters(); // Return defaults if option not set.
$this->add_option_filters(); // Merge with defaults if option *is* set.
if ( $this->multisite_only !== true ) {
/**
* The option validation routines remove the default filters to prevent failing
* to insert an option if it's new. Let's add them back afterwards.
*/
add_action( 'add_option', [ $this, 'add_default_filters_if_same_option' ] ); // Adding back after INSERT.
add_action( 'update_option', [ $this, 'add_default_filters_if_same_option' ] );
add_filter( 'pre_update_option', [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
// Refills the cache when the option has been updated.
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 10 );
}
elseif ( is_multisite() ) {
/*
* The option validation routines remove the default filters to prevent failing
* to insert an option if it's new. Let's add them back afterwards.
*
* For site_options, this method is not foolproof as these actions are not fired
* on an insert/update failure. Please use the WPSEO_Options::update_site_option() method
* for updating site options to make sure the filters are in place.
*/
add_action( 'add_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
add_action( 'update_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
add_filter( 'pre_update_site_option_' . $this->option_name, [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
// Refills the cache when the option has been updated.
add_action( 'update_site_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 1, 0 );
}
/*
* Make sure the option will always get validated, independently of register_setting()
* (only available on back-end).
*/
add_filter( 'sanitize_option_' . $this->option_name, [ $this, 'validate' ] );
// Flushes the rewrite rules when option is updated.
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_rewrites' ] );
/* Register our option for the admin pages */
add_action( 'admin_init', [ $this, 'register_setting' ] );
/* Set option group name if not given */
if ( ! isset( $this->group_name ) || $this->group_name === '' ) {
$this->group_name = 'yoast_' . $this->option_name . '_options';
}
/* Translate some defaults as early as possible - textdomain is loaded in init on priority 1. */
if ( method_exists( $this, 'translate_defaults' ) ) {
add_action( 'init', [ $this, 'translate_defaults' ], 2 );
}
/**
* Enrich defaults once custom post types and taxonomies have been registered
* which is normally done on the init action.
*
* @todo [JRF/testers] Verify that none of the options which are only available after
* enrichment are used before the enriching.
*/
if ( method_exists( $this, 'enrich_defaults' ) ) {
add_action( 'init', [ $this, 'enrich_defaults' ], 99 );
}
}
/*
* All concrete classes *must* contain the get_instance method.
*
* {@internal Unfortunately I can't define it as an abstract as it also *has* to be static...}}
*
* ```
* abstract protected static function get_instance();
* ```
* ---------------
*
* Concrete classes *may* contain a translate_defaults method.
* ```
* abstract public function translate_defaults();
* ```
* ---------------
*
* Concrete classes *may* contain a enrich_defaults method to add additional defaults once
* all post_types and taxonomies have been registered.
*
* ```
* abstract public function enrich_defaults();
* ```
*/
/* *********** METHODS INFLUENCING get_option() *********** */
/**
* Add filters to make sure that the option default is returned if the option is not set.
*
* @return void
*/
public function add_default_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
add_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
}
/**
* Adds back the default filters that were removed during validation if the option was changed.
* Checks if this option was changed to prevent constantly checking if filters are present.
*
* @param string $option_name The option name.
*
* @return void
*/
public function add_default_filters_if_same_option( $option_name ) {
if ( $option_name === $this->option_name ) {
$this->add_default_filters();
}
}
/**
* Adds back the default filters that were removed during validation if the option was not changed.
* This is because in that case the latter actions are not called and thus the filters are never
* added back.
*
* @param mixed $value The current value.
* @param string $option_name The option name.
* @param mixed $old_value The old value.
*
* @return string The current value.
*/
public function add_default_filters_if_not_changed( $value, $option_name, $old_value ) {
if ( $option_name !== $this->option_name ) {
return $value;
}
if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
$this->add_default_filters();
}
return $value;
}
/**
* Validate webmaster tools & Pinterest verification strings.
*
* @param string $key Key to check, by type of service.
* @param array $dirty Dirty data with the new values.
* @param array $old Old data.
* @param array $clean Clean data by reference, normally the default values.
*/
public function validate_verification_string( $key, $dirty, $old, &$clean ) {
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$meta = $dirty[ $key ];
if ( strpos( $meta, 'content=' ) ) {
// Make sure we only have the real key, not a complete meta tag.
preg_match( '`content=([\'"])?([^\'"> ]+)(?:\1|[ />])`', $meta, $match );
if ( isset( $match[2] ) ) {
$meta = $match[2];
}
unset( $match );
}
$meta = sanitize_text_field( $meta );
if ( $meta !== '' ) {
$regex = '`^[A-Fa-f0-9_-]+$`';
$service = '';
switch ( $key ) {
case 'baiduverify':
$regex = '`^[A-Za-z0-9_-]+$`';
$service = 'Baidu Webmaster tools';
break;
case 'googleverify':
$regex = '`^[A-Za-z0-9_-]+$`';
$service = 'Google Webmaster tools';
break;
case 'msverify':
$service = 'Bing Webmaster tools';
break;
case 'pinterestverify':
$service = 'Pinterest';
break;
case 'yandexverify':
$service = 'Yandex Webmaster tools';
break;
}
if ( preg_match( $regex, $meta ) ) {
$clean[ $key ] = $meta;
}
else {
// Restore the previous value, if any.
if ( isset( $old[ $key ] ) && preg_match( $regex, $old[ $key ] ) ) {
$clean[ $key ] = $old[ $key ];
}
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box. WordPress prepends `setting-error-`.
/* translators: 1: Verification string from user input; 2: Service name. */
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.
'error' // CSS class for the WP notice, either the legacy 'error' / 'updated' or the new `notice-*` ones.
);
}
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $meta );
}
}
}
}
/**
* Validates an option as a valid URL. Prints out a WordPress settings error
* notice if the URL is invalid.
*
* @param string $key Key to check, by type of URL setting.
* @param array $dirty Dirty data with the new values.
* @param array $old Old data.
* @param array $clean Clean data by reference, normally the default values.
*/
public function validate_url( $key, $dirty, $old, &$clean ) {
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$submitted_url = trim( $dirty[ $key ] );
$validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );
if ( $validated_url === false ) {
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
// Slug title of the setting.
$this->group_name,
// Suffix-ID for the error message box. WordPress prepends `setting-error-`.
$key,
// The error message.
sprintf(
/* translators: %s expands to an invalid URL. */
__( '%s does not seem to be a valid url. Please correct.', 'wordpress-seo' ),
'<strong>' . esc_url( $submitted_url ) . '</strong>'
),
// Message type.
'error'
);
}
// Restore the previous URL value, if any.
if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
$url = WPSEO_Utils::sanitize_url( $old[ $key ] );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
}
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $submitted_url );
return;
}
// The URL format is valid, let's sanitize it.
$url = WPSEO_Utils::sanitize_url( $validated_url );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
}
}
/**
* Remove the default filters.
* Called from the validate() method to prevent failure to add new options.
*
* @return void
*/
public function remove_default_filters() {
remove_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
/**
* Get the enriched default value for an option.
*
* Checks if the concrete class contains an enrich_defaults() method and if so, runs it.
*
* {@internal The enrich_defaults method is used to set defaults for variable array keys
* in an option, such as array keys depending on post_types and/or taxonomies.}}
*
* @return array
*/
public function get_defaults() {
if ( method_exists( $this, 'translate_defaults' ) ) {
$this->translate_defaults();
}
if ( method_exists( $this, 'enrich_defaults' ) ) {
$this->enrich_defaults();
}
return apply_filters( 'wpseo_defaults', $this->defaults, $this->option_name );
}
/**
* Add filters to make sure that the option is merged with its defaults before being returned.
*
* @return void
*/
public function add_option_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
add_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
}
}
/**
* 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() {
remove_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
}
/**
* Merge an option with its default values.
*
* This method should *not* be called directly!!! It is only meant to filter the get_option() results.
*
* @param mixed $options Option value.
*
* @return mixed Option merged with the defaults for that option.
*/
public function get_option( $options = null ) {
$filtered = $this->array_filter_merge( $options );
/*
* If the option contains variable option keys, make sure we don't remove those settings
* - even if the defaults are not complete yet.
* Unfortunately this means we also won't be removing the settings for post types or taxonomies
* which are no longer in the WP install, but rather that than the other way around.
*/
if ( isset( $this->variable_array_key_patterns ) ) {
$filtered = $this->retain_variable_keys( $options, $filtered );
}
return $filtered;
}
/* *********** METHODS influencing add_uption(), update_option() and saving from admin pages. *********** */
/**
* Register (whitelist) the option for the configuration pages.
* The validation callback is already registered separately on the sanitize_option hook,
* so no need to double register.
*
* @return void
*/
public function register_setting() {
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
return;
}
if ( $this->multisite_only === true ) {
$network_settings_api = Yoast_Network_Settings_API::get();
if ( $network_settings_api->meets_requirements() ) {
$network_settings_api->register_setting( $this->group_name, $this->option_name );
}
return;
}
register_setting( $this->group_name, $this->option_name );
}
/**
* Validate the option
*
* @param mixed $option_value The unvalidated new value for the option.
*
* @return array Validated new value for the option.
*/
public function validate( $option_value ) {
$clean = $this->get_defaults();
/* Return the defaults if the new value is empty. */
if ( ! is_array( $option_value ) || $option_value === [] ) {
return $clean;
}
$option_value = array_map( [ 'WPSEO_Utils', 'trim_recursive' ], $option_value );
$old = $this->get_original_option();
if ( ! is_array( $old ) ) {
$old = [];
}
$old = array_merge( $clean, $old );
$clean = $this->validate_option( $option_value, $clean, $old );
// Prevent updates to variables that are disabled via the override option.
$clean = $this->prevent_disabled_options_update( $clean, $old );
/* Retain the values for variable array keys even when the post type/taxonomy is not yet registered. */
if ( isset( $this->variable_array_key_patterns ) ) {
$clean = $this->retain_variable_keys( $option_value, $clean );
}
$this->remove_default_filters();
return $clean;
}
/**
* Checks whether a specific option key is disabled.
*
* This is determined by whether an override option is available with a key that equals the given key prefixed
* with 'allow_'.
*
* @param string $key Option key.
*
* @return bool True if option key is disabled, false otherwise.
*/
public function is_disabled( $key ) {
$override_option = $this->get_override_option();
if ( empty( $override_option ) ) {
return false;
}
return isset( $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) && ! $override_option[ self::ALLOW_KEY_PREFIX . $key ];
}
/**
* All concrete classes must contain a validate_option() method which validates all
* values within 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.
*/
abstract protected function validate_option( $dirty, $clean, $old );
/* *********** METHODS for ADDING/UPDATING/UPGRADING the option. *********** */
/**
* Retrieve the real old value (unmerged with defaults).
*
* @return array|bool The original option value (which can be false if the option doesn't exist).
*/
protected function get_original_option() {
$this->remove_default_filters();
$this->remove_option_filters();
// Get (unvalidated) array, NOT merged with defaults.
if ( $this->multisite_only !== true ) {
$option_value = get_option( $this->option_name );
}
else {
$option_value = get_site_option( $this->option_name );
}
$this->add_option_filters();
$this->add_default_filters();
return $option_value;
}
/**
* Add the option if it doesn't exist for some strange reason.
*
* @uses WPSEO_Option::get_original_option()
*
* @return void
*/
public function maybe_add_option() {
if ( $this->get_original_option() === false ) {
if ( $this->multisite_only !== true ) {
update_option( $this->option_name, $this->get_defaults() );
}
else {
$this->update_site_option( $this->get_defaults() );
}
}
}
/**
* Update a site_option.
*
* {@internal This special method is only needed for multisite options, but very needed indeed there.
* The order in which certain functions and hooks are run is different between
* get_option() and get_site_option() which means in practice that the removing
* of the default filters would be done too late and the re-adding of the default
* filters might not be done at all.
* Aka: use the WPSEO_Options::update_site_option() method (which calls this method)
* for safely adding/updating multisite options.}}
*
* @param mixed $value The new value for the option.
*
* @return bool Whether the update was succesfull.
*/
public function update_site_option( $value ) {
if ( $this->multisite_only === true && is_multisite() ) {
$this->remove_default_filters();
$result = update_site_option( $this->option_name, $value );
$this->add_default_filters();
return $result;
}
else {
return false;
}
}
/**
* Retrieve the real old value (unmerged with defaults), clean and re-save the option.
*
* @uses WPSEO_Option::get_original_option()
* @uses WPSEO_Option::import()
*
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
*
* @return void
*/
public function clean( $current_version = null ) {
$option_value = $this->get_original_option();
$this->import( $option_value, $current_version );
}
/**
* Clean and re-save the option.
*
* @uses clean_option() method from concrete class if it exists.
*
* @todo [JRF/whomever] Figure out a way to show settings error during/after the upgrade - maybe
* something along the lines of:
* -> add them to a property in this class
* -> if that property isset at the end of the routine and add_settings_error function does not exist,
* save as transient (or update the transient if one already exists)
* -> next time an admin is in the WP back-end, show the errors and delete the transient or only delete it
* once the admin has dismissed the message (add ajax function)
* Important: all validation routines which add_settings_errors would need to be changed for this to work
*
* @param array $option_value Option value to be imported.
* @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 void
*/
public function import( $option_value, $current_version = null, $all_old_option_values = null ) {
if ( $option_value === false ) {
$option_value = $this->get_defaults();
}
elseif ( is_array( $option_value ) && method_exists( $this, 'clean_option' ) ) {
$option_value = $this->clean_option( $option_value, $current_version, $all_old_option_values );
}
/*
* Save the cleaned value - validation will take care of cleaning out array keys which
* should no longer be there.
*/
if ( $this->multisite_only !== true ) {
update_option( $this->option_name, $option_value );
}
else {
$this->update_site_option( $this->option_name, $option_value );
}
}
/**
* Returns the variable array key patterns for an options class.
*
* @return array
*/
public function get_patterns() {
return (array) $this->variable_array_key_patterns;
}
/**
* Retrieves the option name.
*
* @return string The set option name.
*/
public function get_option_name() {
return $this->option_name;
}
/*
* Concrete classes *may* contain a clean_option method which will clean out old/renamed
* values within the option.
*
* ```
* abstract public function clean_option( $option_value, $current_version = null, $all_old_option_values = null );
* ```
*/
/* *********** HELPER METHODS for internal use. *********** */
/**
* Helper method - Combines a fixed array of default values with an options array
* while filtering out any keys which are not in the defaults array.
*
* @todo [JRF] - shouldn't this be a straight array merge ? at the end of the day, the validation
* removes any invalid keys on save.
*
* @param array|null $options Optional. Current options. If not set, the option defaults
* for the $option_key will be returned.
*
* @return array Combined and filtered options array.
*/
protected function array_filter_merge( $options = null ) {
$defaults = $this->get_defaults();
if ( ! isset( $options ) || $options === false || $options === [] ) {
return $defaults;
}
$options = (array) $options;
/*
$filtered = array();
if ( $defaults !== array() ) {
foreach ( $defaults as $key => $default_value ) {
// @todo should this walk through array subkeys ?
$filtered[ $key ] = ( isset( $options[ $key ] ) ? $options[ $key ] : $default_value );
}
}
*/
$filtered = array_merge( $defaults, $options );
return $filtered;
}
/**
* Sets updated values for variables that are disabled via the override option back to their previous values.
*
* @param array $updated Updated option value.
* @param array $old Old option value.
*
* @return array Updated option value, with all disabled variables set to their old values.
*/
protected function prevent_disabled_options_update( $updated, $old ) {
$override_option = $this->get_override_option();
if ( empty( $override_option ) ) {
return $updated;
}
/*
* This loop could as well call `is_disabled( $key )` for each iteration,
* however this would be worse performance-wise.
*/
foreach ( $old as $key => $value ) {
if ( isset( $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) && ! $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) {
$updated[ $key ] = $old[ $key ];
}
}
return $updated;
}
/**
* Retrieves the value of the override option, if available.
*
* An override option contains values that may determine access to certain sub-variables
* of this option.
*
* Only regular options in multisite can have override options, which in that case
* would be network options.
*
* @return array Override option value, or empty array if unavailable.
*/
protected function get_override_option() {
if ( empty( $this->override_option_name ) || $this->multisite_only === true || ! is_multisite() ) {
return [];
}
return get_site_option( $this->override_option_name, [] );
}
/**
* Make sure that any set option values relating to post_types and/or taxonomies are retained,
* even when that post_type or taxonomy may not yet have been registered.
*
* {@internal The wpseo_titles concrete class overrules this method. Make sure that any
* changes applied here, also get ported to that version.}}
*
* @param array $dirty Original option as retrieved from the database.
* @param array $clean Filtered option where any options which shouldn't be in our option
* have already been removed and any options which weren't set
* have been set to their defaults.
*
* @return array
*/
protected function retain_variable_keys( $dirty, $clean ) {
if ( ( is_array( $this->variable_array_key_patterns ) && $this->variable_array_key_patterns !== [] ) && ( is_array( $dirty ) && $dirty !== [] ) ) {
foreach ( $dirty as $key => $value ) {
// Do nothing if already in filtered options.
if ( isset( $clean[ $key ] ) ) {
continue;
}
foreach ( $this->variable_array_key_patterns as $pattern ) {
if ( strpos( $key, $pattern ) === 0 ) {
$clean[ $key ] = $value;
break;
}
}
}
}
return $clean;
}
/**
* Check whether a given array key conforms to one of the variable array key patterns for this option.
*
* @usedby validate_option() methods for options with variable array keys.
*
* @param string $key Array key to check.
*
* @return string Pattern if it conforms, original array key if it doesn't or if the option
* does not have variable array keys.
*/
protected function get_switch_key( $key ) {
if ( ! isset( $this->variable_array_key_patterns ) || ( ! is_array( $this->variable_array_key_patterns ) || $this->variable_array_key_patterns === [] ) ) {
return $key;
}
foreach ( $this->variable_array_key_patterns as $pattern ) {
if ( strpos( $key, $pattern ) === 0 ) {
return $pattern;
}
}
return $key;
}
}

View File

@ -0,0 +1,588 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Overall Option Management class.
*
* Instantiates all the options and offers a number of utility methods to work with the options.
*/
class WPSEO_Options {
/**
* The option values.
*
* @var array|null
*/
protected static $option_values = null;
/**
* Options this class uses.
*
* @var array Array format: (string) option_name => (string) name of concrete class for the option.
*/
public static $options = [
'wpseo' => 'WPSEO_Option_Wpseo',
'wpseo_titles' => 'WPSEO_Option_Titles',
'wpseo_social' => 'WPSEO_Option_Social',
'wpseo_ms' => 'WPSEO_Option_MS',
'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
];
/**
* Array of instantiated option objects.
*
* @var array
*/
protected static $option_instances = [];
/**
* Array with the option names.
*
* @var array
*/
protected static $option_names = [];
/**
* Instance of this class.
*
* @var WPSEO_Options
*/
protected static $instance;
/**
* Instantiate all the WPSEO option management classes.
*/
protected function __construct() {
$this->register_hooks();
foreach ( static::$options as $option_class ) {
static::register_option( call_user_func( [ $option_class, 'get_instance' ] ) );
}
}
/**
* Register our hooks.
*/
public function register_hooks() {
add_action( 'registered_taxonomy', [ $this, 'clear_cache' ] );
add_action( 'unregistered_taxonomy', [ $this, 'clear_cache' ] );
add_action( 'registered_post_type', [ $this, 'clear_cache' ] );
add_action( 'unregistered_post_type', [ $this, 'clear_cache' ] );
}
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( static::$instance instanceof self ) ) {
static::$instance = new self();
}
return static::$instance;
}
/**
* Registers an option to the options list.
*
* @param WPSEO_Option $option_instance Instance of the option.
*/
public static function register_option( WPSEO_Option $option_instance ) {
$option_name = $option_instance->get_option_name();
if ( $option_instance->multisite_only && ! static::is_multisite() ) {
unset( static::$options[ $option_name ], static::$option_names[ $option_name ] );
return;
}
$is_already_registered = array_key_exists( $option_name, static::$options );
if ( ! $is_already_registered ) {
static::$options[ $option_name ] = get_class( $option_instance );
}
if ( $option_instance->include_in_all === true ) {
static::$option_names[ $option_name ] = $option_name;
}
static::$option_instances[ $option_name ] = $option_instance;
if ( ! $is_already_registered ) {
static::clear_cache();
}
}
/**
* Get the group name of an option for use in the settings form.
*
* @param string $option_name The option for which you want to retrieve the option group name.
*
* @return string|bool
*/
public static function get_group_name( $option_name ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
return static::$option_instances[ $option_name ]->group_name;
}
return false;
}
/**
* Get a specific default value for an option.
*
* @param string $option_name The option for which you want to retrieve a default.
* @param string $key The key within the option who's default you want.
*
* @return mixed
*/
public static function get_default( $option_name, $key ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
$defaults = static::$option_instances[ $option_name ]->get_defaults();
if ( isset( $defaults[ $key ] ) ) {
return $defaults[ $key ];
}
}
return null;
}
/**
* Update a site_option.
*
* @param string $option_name The option name of the option to save.
* @param mixed $value The new value for the option.
*
* @return bool
*/
public static function update_site_option( $option_name, $value ) {
if ( is_multisite() && isset( static::$option_instances[ $option_name ] ) ) {
return static::$option_instances[ $option_name ]->update_site_option( $value );
}
return false;
}
/**
* Get the instantiated option instance.
*
* @param string $option_name The option for which you want to retrieve the instance.
*
* @return object|bool
*/
public static function get_option_instance( $option_name ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
return static::$option_instances[ $option_name ];
}
return false;
}
/**
* Retrieve an array of the options which should be included in get_all() and reset().
*
* @return array Array of option names.
*/
public static function get_option_names() {
$option_names = array_values( static::$option_names );
if ( $option_names === [] ) {
foreach ( static::$option_instances as $option_name => $option_object ) {
if ( $option_object->include_in_all === true ) {
$option_names[] = $option_name;
}
}
}
/**
* Filter: wpseo_options - Allow developers to change the option name to include.
*
* @api array The option names to include in get_all and reset().
*/
return apply_filters( 'wpseo_options', $option_names );
}
/**
* Retrieve all the options for the SEO plugin in one go.
*
* @return array Array combining the values of all the options.
*/
public static function get_all() {
static::$option_values = static::get_options( static::get_option_names() );
return static::$option_values;
}
/**
* Retrieve one or more options for the SEO plugin.
*
* @param array $option_names An array of option names of the options you want to get.
*
* @return array Array combining the values of the requested options.
*/
public static function get_options( array $option_names ) {
$options = [];
$option_names = array_filter( $option_names, 'is_string' );
foreach ( $option_names as $option_name ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
$option = static::get_option( $option_name );
if ( $option !== null ) {
$options = array_merge( $options, $option );
}
}
}
return $options;
}
/**
* Retrieve a single option for the SEO plugin.
*
* @param string $option_name The name of the option you want to get.
*
* @return array Array containing the requested option.
*/
public static function get_option( $option_name ) {
$option = null;
if ( is_string( $option_name ) && ! empty( $option_name ) ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
if ( static::$option_instances[ $option_name ]->multisite_only !== true ) {
$option = get_option( $option_name );
}
else {
$option = get_site_option( $option_name );
}
}
}
return $option;
}
/**
* Retrieve a single field from any option for the SEO plugin. Keys are always unique.
*
* @param string $key The key it should return.
* @param mixed $default_value The default value that should be returned if the key isn't set.
*
* @return mixed Returns value if found, $default_value if not.
*/
public static function get( $key, $default_value = null ) {
if ( static::$option_values === null ) {
static::prime_cache();
}
if ( isset( static::$option_values[ $key ] ) ) {
return static::$option_values[ $key ];
}
return $default_value;
}
/**
* Resets the cache to null.
*/
public static function clear_cache() {
static::$option_values = null;
}
/**
* Primes our cache.
*/
private static function prime_cache() {
static::$option_values = static::get_all();
static::$option_values = static::add_ms_option( static::$option_values );
}
/**
* Retrieve a single field from an option for the SEO plugin.
*
* @param string $key The key to set.
* @param mixed $value The value to set.
*
* @return mixed|null Returns value if found, $default if not.
*/
public static function set( $key, $value ) {
$lookup_table = static::get_lookup_table();
if ( isset( $lookup_table[ $key ] ) ) {
return static::save_option( $lookup_table[ $key ], $key, $value );
}
$patterns = static::get_pattern_table();
foreach ( $patterns as $pattern => $option ) {
if ( strpos( $key, $pattern ) === 0 ) {
return static::save_option( $option, $key, $value );
}
}
static::$option_values[ $key ] = $value;
}
/**
* Get an option only if it's been auto-loaded.
*
* @param string $option The option to retrieve.
* @param mixed $default_value A default value to return.
*
* @return mixed
*/
public static function get_autoloaded_option( $option, $default_value = false ) {
$value = wp_cache_get( $option, 'options' );
if ( $value === false ) {
$passed_default = func_num_args() > 1;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
}
/**
* Run the clean up routine for one or all options.
*
* @param array|string|null $option_name Optional. the option you want to clean or an array of
* option names for the options you want to clean.
* If not set, all options will be cleaned.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
*
* @return void
*/
public static function clean_up( $option_name = null, $current_version = null ) {
if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
static::$option_instances[ $option_name ]->clean( $current_version );
}
}
elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== [] ) {
foreach ( $option_name as $option ) {
if ( isset( static::$option_instances[ $option ] ) ) {
static::$option_instances[ $option ]->clean( $current_version );
}
}
unset( $option );
}
else {
foreach ( static::$option_instances as $instance ) {
$instance->clean( $current_version );
}
unset( $instance );
// If we've done a full clean-up, we can safely remove this really old option.
delete_option( 'wpseo_indexation' );
}
}
/**
* Check that all options exist in the database and add any which don't.
*
* @return void
*/
public static function ensure_options_exist() {
foreach ( static::$option_instances as $instance ) {
$instance->maybe_add_option();
}
}
/**
* Initialize some options on first install/activate/reset.
*
* @return void
*/
public static function initialize() {
/* Force WooThemes to use Yoast SEO data. */
if ( function_exists( 'woo_version_init' ) ) {
update_option( 'seo_woo_use_third_party_data', 'true' );
}
}
/**
* Reset all options to their default values and rerun some tests.
*
* @return void
*/
public static function reset() {
if ( ! is_multisite() ) {
$option_names = static::get_option_names();
if ( is_array( $option_names ) && $option_names !== [] ) {
foreach ( $option_names as $option_name ) {
delete_option( $option_name );
update_option( $option_name, get_option( $option_name ) );
}
}
unset( $option_names );
}
else {
// Reset MS blog based on network default blog setting.
static::reset_ms_blog( get_current_blog_id() );
}
static::initialize();
}
/**
* Initialize default values for a new multisite blog.
*
* @param bool $force_init Whether to always do the initialization routine (title/desc test).
*
* @return void
*/
public static function maybe_set_multisite_defaults( $force_init = false ) {
$option = get_option( 'wpseo' );
if ( is_multisite() ) {
if ( $option['ms_defaults_set'] === false ) {
static::reset_ms_blog( get_current_blog_id() );
static::initialize();
}
elseif ( $force_init === true ) {
static::initialize();
}
}
}
/**
* Reset all options for a specific multisite blog to their default values based upon a
* specified default blog if one was chosen on the network page or the plugin defaults if it was not.
*
* @param int|string $blog_id Blog id of the blog for which to reset the options.
*
* @return void
*/
public static function reset_ms_blog( $blog_id ) {
if ( is_multisite() ) {
$options = get_site_option( 'wpseo_ms' );
$option_names = static::get_option_names();
if ( is_array( $option_names ) && $option_names !== [] ) {
$base_blog_id = $blog_id;
if ( $options['defaultblog'] !== '' && $options['defaultblog'] !== 0 ) {
$base_blog_id = $options['defaultblog'];
}
foreach ( $option_names as $option_name ) {
delete_blog_option( $blog_id, $option_name );
$new_option = get_blog_option( $base_blog_id, $option_name );
/* Remove sensitive, theme dependent and site dependent info. */
if ( isset( static::$option_instances[ $option_name ] ) && static::$option_instances[ $option_name ]->ms_exclude !== [] ) {
foreach ( static::$option_instances[ $option_name ]->ms_exclude as $key ) {
unset( $new_option[ $key ] );
}
}
if ( $option_name === 'wpseo' ) {
$new_option['ms_defaults_set'] = true;
}
update_blog_option( $blog_id, $option_name, $new_option );
}
}
}
}
/**
* Saves the option to the database.
*
* @param string $wpseo_options_group_name The name for the wpseo option group in the database.
* @param string $option_name The name for the option to set.
* @param mixed $option_value The value for the option.
*
* @return bool Returns true if the option is successfully saved in the database.
*/
public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) {
$options = static::get_option( $wpseo_options_group_name );
$options[ $option_name ] = $option_value;
if ( isset( static::$option_instances[ $wpseo_options_group_name ] ) && static::$option_instances[ $wpseo_options_group_name ]->multisite_only === true ) {
static::update_site_option( $wpseo_options_group_name, $options );
}
else {
update_option( $wpseo_options_group_name, $options );
}
// Check if everything got saved properly.
$saved_option = static::get_option( $wpseo_options_group_name );
// Clear our cache.
static::clear_cache();
return $saved_option[ $option_name ] === $options[ $option_name ];
}
/**
* Adds the multisite options to the option stack if relevant.
*
* @param array $option The currently present options settings.
*
* @return array Options possibly including multisite.
*/
protected static function add_ms_option( $option ) {
if ( ! is_multisite() ) {
return $option;
}
$ms_option = static::get_option( 'wpseo_ms' );
if ( $ms_option === null ) {
return $option;
}
return array_merge( $option, $ms_option );
}
/**
* Checks if installation is multisite.
*
* @return bool True when is multisite.
*/
protected static function is_multisite() {
static $is_multisite;
if ( $is_multisite === null ) {
$is_multisite = is_multisite();
}
return $is_multisite;
}
/**
* Retrieves a lookup table to find in which option_group a key is stored.
*
* @return array The lookup table.
*/
private static function get_lookup_table() {
$lookup_table = [];
foreach ( array_keys( static::$options ) as $option_name ) {
$full_option = static::get_option( $option_name );
foreach ( $full_option as $key => $value ) {
$lookup_table[ $key ] = $option_name;
}
}
return $lookup_table;
}
/**
* Retrieves a lookup table to find in which option_group a key is stored.
*
* @return array The lookup table.
*/
private static function get_pattern_table() {
$pattern_table = [];
foreach ( static::$options as $option_name => $option_class ) {
$instance = call_user_func( [ $option_class, 'get_instance' ] );
foreach ( $instance->get_patterns() as $key ) {
$pattern_table[ $key ] = $option_name;
}
}
return $pattern_table;
}
}

View File

@ -0,0 +1,556 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Option: wpseo_taxonomy_meta.
*/
class WPSEO_Taxonomy_Meta extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_taxonomy_meta';
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* @var bool
*/
public $include_in_all = false;
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* {@internal Important: in contrast to most defaults, the below array format is
* very bare. The real option is in the format [taxonomy_name][term_id][...]
* where [...] is any of the $defaults_per_term options shown below.
* This is of course taken into account in the below methods.}}
*
* @var array
*/
protected $defaults = [];
/**
* Option name - same as $option_name property, but now also available to static methods.
*
* @var string
*/
public static $name;
/**
* Array of defaults for individual taxonomy meta entries.
*
* @var array
*/
public static $defaults_per_term = [
'wpseo_title' => '',
'wpseo_desc' => '',
'wpseo_canonical' => '',
'wpseo_bctitle' => '',
'wpseo_noindex' => 'default',
'wpseo_focuskw' => '',
'wpseo_linkdex' => '',
'wpseo_content_score' => '',
'wpseo_inclusive_language_score' => '',
'wpseo_focuskeywords' => '[]',
'wpseo_keywordsynonyms' => '[]',
'wpseo_is_cornerstone' => '0',
// Social fields.
'wpseo_opengraph-title' => '',
'wpseo_opengraph-description' => '',
'wpseo_opengraph-image' => '',
'wpseo_opengraph-image-id' => '',
'wpseo_twitter-title' => '',
'wpseo_twitter-description' => '',
'wpseo_twitter-image' => '',
'wpseo_twitter-image-id' => '',
];
/**
* Available index options.
*
* Used for form generation and input validation.
*
* {@internal Labels (translation) added on admin_init via WPSEO_Taxonomy::translate_meta_options().}}
*
* @var array
*/
public static $no_index_options = [
'default' => '',
'index' => '',
'noindex' => '',
];
/**
* 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();
self::$name = $this->option_name;
}
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
self::$name = self::$instance->option_name;
}
return self::$instance;
}
/**
* Add extra default options received from a filter.
*/
public function enrich_defaults() {
$extra_defaults_per_term = apply_filters( 'wpseo_add_extra_taxmeta_term_defaults', [] );
if ( is_array( $extra_defaults_per_term ) ) {
self::$defaults_per_term = array_merge( $extra_defaults_per_term, self::$defaults_per_term );
}
}
/**
* 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 ) {
/*
* Prevent complete validation (which can be expensive when there are lots of terms)
* if only one item has changed and has already been validated.
*/
if ( isset( $dirty['wpseo_already_validated'] ) && $dirty['wpseo_already_validated'] === true ) {
unset( $dirty['wpseo_already_validated'] );
return $dirty;
}
foreach ( $dirty as $taxonomy => $terms ) {
/* Don't validate taxonomy - may not be registered yet and we don't want to remove valid ones. */
if ( is_array( $terms ) && $terms !== [] ) {
foreach ( $terms as $term_id => $meta_data ) {
/* Only validate term if the taxonomy exists. */
if ( taxonomy_exists( $taxonomy ) && get_term_by( 'id', $term_id, $taxonomy ) === false ) {
/* Is this term id a special case ? */
if ( has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
$clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
}
continue;
}
if ( is_array( $meta_data ) && $meta_data !== [] ) {
/* Validate meta data. */
$old_meta = self::get_term_meta( $term_id, $taxonomy );
$meta_data = self::validate_term_meta_data( $meta_data, $old_meta );
if ( $meta_data !== [] ) {
$clean[ $taxonomy ][ $term_id ] = $meta_data;
}
}
// Deal with special cases (for when taxonomy doesn't exist yet).
if ( ! isset( $clean[ $taxonomy ][ $term_id ] ) && has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
$clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
}
}
}
}
return $clean;
}
/**
* Validate the meta data for one individual term and removes default values (no need to save those).
*
* @param array $meta_data New values.
* @param array $old_meta The original values.
*
* @return array Validated and filtered value.
*/
public static function validate_term_meta_data( $meta_data, $old_meta ) {
$clean = self::$defaults_per_term;
$meta_data = array_map( [ 'WPSEO_Utils', 'trim_recursive' ], $meta_data );
if ( ! is_array( $meta_data ) || $meta_data === [] ) {
return $clean;
}
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'wpseo_noindex':
if ( isset( $meta_data[ $key ] ) ) {
if ( isset( self::$no_index_options[ $meta_data[ $key ] ] ) ) {
$clean[ $key ] = $meta_data[ $key ];
}
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_canonical':
if ( isset( $meta_data[ $key ] ) && $meta_data[ $key ] !== '' ) {
$url = WPSEO_Utils::sanitize_url( $meta_data[ $key ] );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
unset( $url );
}
break;
case 'wpseo_bctitle':
if ( isset( $meta_data[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $meta_data[ $key ] );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_keywordsynonyms':
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
// The data is stringified JSON. Use `json_decode` and `json_encode` around the sanitation.
$input = json_decode( $meta_data[ $key ], true );
$sanitized = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $input );
$clean[ $key ] = WPSEO_Utils::format_json_encode( $sanitized );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_focuskeywords':
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
// The data is stringified JSON. Use `json_decode` and `json_encode` around the sanitation.
$input = json_decode( $meta_data[ $key ], true );
// This data has two known keys: `keyword` and `score`.
$sanitized = [];
foreach ( $input as $entry ) {
$sanitized[] = [
'keyword' => WPSEO_Utils::sanitize_text_field( $entry['keyword'] ),
'score' => WPSEO_Utils::sanitize_text_field( $entry['score'] ),
];
}
$clean[ $key ] = WPSEO_Utils::format_json_encode( $sanitized );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_focuskw':
case 'wpseo_title':
case 'wpseo_desc':
case 'wpseo_linkdex':
default:
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $meta_data[ $key ] );
}
if ( $key === 'wpseo_focuskw' ) {
$search = [
'&lt;',
'&gt;',
'&#96',
'<',
'>',
'`',
];
$clean[ $key ] = str_replace( $search, '', $clean[ $key ] );
}
break;
}
$clean[ $key ] = apply_filters( 'wpseo_sanitize_tax_meta_' . $key, $clean[ $key ], ( isset( $meta_data[ $key ] ) ? $meta_data[ $key ] : null ), ( isset( $old_meta[ $key ] ) ? $old_meta[ $key ] : null ) );
}
// Only save the non-default values.
return array_diff_assoc( $clean, self::$defaults_per_term );
}
/**
* Clean a given option value.
* - Convert old option values to new
* - Fixes strings which were escaped (should have been sanitized - escaping is for output)
*
* @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 ) {
/* Clean up old values and remove empty arrays. */
if ( is_array( $option_value ) && $option_value !== [] ) {
foreach ( $option_value as $taxonomy => $terms ) {
if ( is_array( $terms ) && $terms !== [] ) {
foreach ( $terms as $term_id => $meta_data ) {
if ( ! is_array( $meta_data ) || $meta_data === [] ) {
// Remove empty term arrays.
unset( $option_value[ $taxonomy ][ $term_id ] );
}
else {
foreach ( $meta_data as $key => $value ) {
switch ( $key ) {
case 'noindex':
if ( $value === 'on' ) {
// Convert 'on' to 'noindex'.
$option_value[ $taxonomy ][ $term_id ][ $key ] = 'noindex';
}
break;
case 'canonical':
case 'wpseo_bctitle':
case 'wpseo_title':
case 'wpseo_desc':
case 'wpseo_linkdex':
// @todo [JRF => whomever] Needs checking, I don't have example data [JRF].
if ( $value !== '' ) {
// Fix incorrectly saved (encoded) canonical urls and texts.
$option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( stripslashes( $value ), ENT_QUOTES );
}
break;
default:
// @todo [JRF => whomever] Needs checking, I don't have example data [JRF].
if ( $value !== '' ) {
// Fix incorrectly saved (escaped) text strings.
$option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( $value, ENT_QUOTES );
}
break;
}
}
}
}
}
else {
// Remove empty taxonomy arrays.
unset( $option_value[ $taxonomy ] );
}
}
}
return $option_value;
}
/**
* Retrieve a taxonomy term's meta value(s).
*
* @param mixed $term Term to get the meta value for
* either (string) term name, (int) term id or (object) term.
* @param string $taxonomy Name of the taxonomy to which the term is attached.
* @param string|null $meta Optional. Meta value to get (without prefix).
*
* @return mixed Value for the $meta if one is given, might be the default.
* If no meta is given, an array of all the meta data for the term.
* False if the term does not exist or the $meta provided is invalid.
*/
public static function get_term_meta( $term, $taxonomy, $meta = null ) {
/* Figure out the term id. */
if ( is_int( $term ) ) {
$term = get_term_by( 'id', $term, $taxonomy );
}
elseif ( is_string( $term ) ) {
$term = get_term_by( 'slug', $term, $taxonomy );
}
if ( is_object( $term ) && isset( $term->term_id ) ) {
$term_id = $term->term_id;
}
else {
return false;
}
$tax_meta = self::get_term_tax_meta( $term_id, $taxonomy );
/*
* Either return the complete array or a single value from it or false if the value does not exist
* (shouldn't happen after merge with defaults, indicates typo in request).
*/
if ( ! isset( $meta ) ) {
return $tax_meta;
}
if ( isset( $tax_meta[ 'wpseo_' . $meta ] ) ) {
return $tax_meta[ 'wpseo_' . $meta ];
}
return false;
}
/**
* Get the current queried object and return the meta value.
*
* @param string $meta The meta field that is needed.
*
* @return mixed
*/
public static function get_meta_without_term( $meta ) {
$term = $GLOBALS['wp_query']->get_queried_object();
if ( ! $term || empty( $term->taxonomy ) ) {
return false;
}
return self::get_term_meta( $term, $term->taxonomy, $meta );
}
/**
* Saving the values for the given term_id.
*
* @param int $term_id ID of the term to save data for.
* @param string $taxonomy The taxonomy the term belongs to.
* @param array $meta_values The values that will be saved.
*/
public static function set_values( $term_id, $taxonomy, array $meta_values ) {
/* Validate the post values */
$old = self::get_term_meta( $term_id, $taxonomy );
$clean = self::validate_term_meta_data( $meta_values, $old );
self::save_clean_values( $term_id, $taxonomy, $clean );
}
/**
* Setting a single value to the term meta.
*
* @param int $term_id ID of the term to save data for.
* @param string $taxonomy The taxonomy the term belongs to.
* @param string $meta_key The target meta key to store the value in.
* @param string $meta_value The value of the target meta key.
*/
public static function set_value( $term_id, $taxonomy, $meta_key, $meta_value ) {
if ( substr( strtolower( $meta_key ), 0, 6 ) !== 'wpseo_' ) {
$meta_key = 'wpseo_' . $meta_key;
}
self::set_values( $term_id, $taxonomy, [ $meta_key => $meta_value ] );
}
/**
* Find the keyword usages in the metas for the taxonomies/terms.
*
* @param string $keyword The keyword to look for.
* @param string $current_term_id The current term id.
* @param string $current_taxonomy The current taxonomy name.
*
* @return array
*/
public static function get_keyword_usage( $keyword, $current_term_id, $current_taxonomy ) {
$tax_meta = self::get_tax_meta();
$found = [];
// @todo Check for terms of all taxonomies, not only the current taxonomy.
foreach ( $tax_meta as $taxonomy_name => $terms ) {
foreach ( $terms as $term_id => $meta_values ) {
$is_current = ( $current_taxonomy === $taxonomy_name && (string) $current_term_id === (string) $term_id );
if ( ! $is_current && ! empty( $meta_values['wpseo_focuskw'] ) && $meta_values['wpseo_focuskw'] === $keyword ) {
$found[] = $term_id;
}
}
}
return [ $keyword => $found ];
}
/**
* Saving the values for the given term_id.
*
* @param int $term_id ID of the term to save data for.
* @param string $taxonomy The taxonomy the term belongs to.
* @param array $clean Array with clean values.
*/
private static function save_clean_values( $term_id, $taxonomy, array $clean ) {
$tax_meta = self::get_tax_meta();
/* Add/remove the result to/from the original option value. */
if ( $clean !== [] ) {
$tax_meta[ $taxonomy ][ $term_id ] = $clean;
}
else {
unset( $tax_meta[ $taxonomy ][ $term_id ] );
if ( isset( $tax_meta[ $taxonomy ] ) && $tax_meta[ $taxonomy ] === [] ) {
unset( $tax_meta[ $taxonomy ] );
}
}
// Prevent complete array validation.
$tax_meta['wpseo_already_validated'] = true;
self::save_tax_meta( $tax_meta );
}
/**
* Getting the meta from the options.
*
* @return void|array
*/
private static function get_tax_meta() {
return get_option( self::$name );
}
/**
* Saving the tax meta values to the database.
*
* @param array $tax_meta Array with the meta values for taxonomy.
*/
private static function save_tax_meta( $tax_meta ) {
update_option( self::$name, $tax_meta );
}
/**
* Getting the taxonomy meta for the given term_id and taxonomy.
*
* @param int $term_id The id of the term.
* @param string $taxonomy Name of the taxonomy to which the term is attached.
*
* @return array
*/
private static function get_term_tax_meta( $term_id, $taxonomy ) {
$tax_meta = self::get_tax_meta();
/* If we have data for the term, merge with defaults for complete array, otherwise set defaults. */
if ( isset( $tax_meta[ $taxonomy ][ $term_id ] ) ) {
return array_merge( self::$defaults_per_term, $tax_meta[ $taxonomy ][ $term_id ] );
}
return self::$defaults_per_term;
}
}