khaihihi
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
<?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}onpage_indexability" => true,
|
||||
"{$allow_prefix}content_analysis_active" => true,
|
||||
"{$allow_prefix}keyword_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,
|
||||
];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $current_version (optional) Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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 ) {
|
||||
|
||||
return $option_value;
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
<?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' => '',
|
||||
// Form field, but not always available.
|
||||
'fbadminapp' => '', // Facebook app ID.
|
||||
];
|
||||
|
||||
/**
|
||||
* Array of sub-options which should not be overloaded with multi-site defaults.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $ms_exclude = [
|
||||
/* Privacy. */
|
||||
'pinterestverify',
|
||||
'fbadminapp',
|
||||
];
|
||||
|
||||
/**
|
||||
* 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' => '',
|
||||
'summary_large_image' => '',
|
||||
// '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'] = __( 'Summary', 'wordpress-seo' );
|
||||
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':
|
||||
$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 = sanitize_text_field( ltrim( $dirty[ $key ], '@' ) );
|
||||
|
||||
/*
|
||||
* 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
|
||||
* @link https://dev.twitter.com/docs/platform-objects/users
|
||||
*/
|
||||
if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
|
||||
$clean[ $key ] = $twitter_id;
|
||||
}
|
||||
elseif ( preg_match( '`^http(?:s)?://(?:www\.)?twitter\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) {
|
||||
$clean[ $key ] = $matches['handle'];
|
||||
}
|
||||
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;
|
||||
|
||||
case 'fbadminapp':
|
||||
$this->validate_facebook_app_id( $key, $dirty, $old, $clean );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $current_version Optional. Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,905 @@
|
||||
<?php
|
||||
/**
|
||||
* WPSEO plugin file.
|
||||
*
|
||||
* @package WPSEO\Internals\Options
|
||||
*/
|
||||
|
||||
/**
|
||||
* Option: wpseo_titles.
|
||||
*/
|
||||
class WPSEO_Option_Titles extends WPSEO_Option {
|
||||
|
||||
/**
|
||||
* Option name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $option_name = 'wpseo_titles';
|
||||
|
||||
/**
|
||||
* Array of defaults for the option.
|
||||
*
|
||||
* Shouldn't be requested directly, use $this->get_defaults();
|
||||
*
|
||||
* {@internal Note: Some of the default values are added via the translate_defaults() method.}}
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaults = [
|
||||
// Non-form fields, set via (ajax) function.
|
||||
'title_test' => 0,
|
||||
// Form fields.
|
||||
'forcerewritetitle' => false,
|
||||
'separator' => 'sc-dash',
|
||||
'title-home-wpseo' => '%%sitename%% %%page%% %%sep%% %%sitedesc%%', // Text field.
|
||||
'title-author-wpseo' => '', // Text field.
|
||||
'title-archive-wpseo' => '%%date%% %%page%% %%sep%% %%sitename%%', // Text field.
|
||||
'title-search-wpseo' => '', // Text field.
|
||||
'title-404-wpseo' => '', // Text field.
|
||||
|
||||
'metadesc-home-wpseo' => '', // Text area.
|
||||
'metadesc-author-wpseo' => '', // Text area.
|
||||
'metadesc-archive-wpseo' => '', // Text area.
|
||||
'rssbefore' => '', // Text area.
|
||||
'rssafter' => '', // Text area.
|
||||
|
||||
'noindex-author-wpseo' => false,
|
||||
'noindex-author-noposts-wpseo' => true,
|
||||
'noindex-archive-wpseo' => true,
|
||||
|
||||
'disable-author' => false,
|
||||
'disable-date' => false,
|
||||
'disable-post_format' => false,
|
||||
'disable-attachment' => true,
|
||||
'is-media-purge-relevant' => false,
|
||||
|
||||
'breadcrumbs-404crumb' => '', // Text field.
|
||||
'breadcrumbs-display-blog-page' => true,
|
||||
'breadcrumbs-boldlast' => false,
|
||||
'breadcrumbs-archiveprefix' => '', // Text field.
|
||||
'breadcrumbs-enable' => false,
|
||||
'breadcrumbs-home' => '', // Text field.
|
||||
'breadcrumbs-prefix' => '', // Text field.
|
||||
'breadcrumbs-searchprefix' => '', // Text field.
|
||||
'breadcrumbs-sep' => '»', // Text field.
|
||||
|
||||
'website_name' => '',
|
||||
'person_name' => '',
|
||||
'person_logo' => '',
|
||||
'person_logo_id' => 0,
|
||||
'alternate_website_name' => '',
|
||||
'company_logo' => '',
|
||||
'company_logo_id' => 0,
|
||||
'company_name' => '',
|
||||
'company_or_person' => 'company',
|
||||
'company_or_person_user_id' => false,
|
||||
|
||||
'stripcategorybase' => false,
|
||||
|
||||
/**
|
||||
* Uses enrich_defaults to add more along the lines of:
|
||||
* - 'title-' . $pt->name => ''; // Text field.
|
||||
* - 'metadesc-' . $pt->name => ''; // Text field.
|
||||
* - 'noindex-' . $pt->name => false;
|
||||
* - 'showdate-' . $pt->name => false;
|
||||
* - 'display-metabox-pt-' . $pt->name => false;
|
||||
*
|
||||
* - 'title-ptarchive-' . $pt->name => ''; // Text field.
|
||||
* - 'metadesc-ptarchive-' . $pt->name => ''; // Text field.
|
||||
* - 'bctitle-ptarchive-' . $pt->name => ''; // Text field.
|
||||
* - 'noindex-ptarchive-' . $pt->name => false;
|
||||
*
|
||||
* - 'title-tax-' . $tax->name => '''; // Text field.
|
||||
* - 'metadesc-tax-' . $tax->name => ''; // Text field.
|
||||
* - 'noindex-tax-' . $tax->name => false;
|
||||
* - 'display-metabox-tax-' . $tax->name => false;
|
||||
*/
|
||||
];
|
||||
|
||||
/**
|
||||
* Used for "caching" during pageload.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $enriched_defaults = null;
|
||||
|
||||
/**
|
||||
* Array of variable option name patterns for the option.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $variable_array_key_patterns = [
|
||||
'title-',
|
||||
'metadesc-',
|
||||
'noindex-',
|
||||
'showdate-',
|
||||
'display-metabox-pt-',
|
||||
'bctitle-ptarchive-',
|
||||
'post_types-',
|
||||
'taxonomy-',
|
||||
];
|
||||
|
||||
/**
|
||||
* Array of sub-options which should not be overloaded with multi-site defaults.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $ms_exclude = [
|
||||
/* Theme dependent. */
|
||||
'title_test',
|
||||
'forcerewritetitle',
|
||||
];
|
||||
|
||||
/**
|
||||
* 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();
|
||||
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_cache' ] );
|
||||
add_action( 'init', [ $this, 'end_of_init' ], 999 );
|
||||
|
||||
add_action( 'registered_post_type', [ $this, 'invalidate_enrich_defaults_cache' ] );
|
||||
add_action( 'unregistered_post_type', [ $this, 'invalidate_enrich_defaults_cache' ] );
|
||||
add_action( 'registered_taxonomy', [ $this, 'invalidate_enrich_defaults_cache' ] );
|
||||
add_action( 'unregistered_taxonomy', [ $this, 'invalidate_enrich_defaults_cache' ] );
|
||||
|
||||
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure we can recognize the right action for the double cleaning.
|
||||
*/
|
||||
public function end_of_init() {
|
||||
do_action( 'wpseo_double_clean_titles' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of this class.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( ! ( self::$instance instanceof self ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available separator options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_separator_options() {
|
||||
$separators = wp_list_pluck( self::get_separator_option_list(), 'option' );
|
||||
|
||||
/**
|
||||
* Allow altering the array with separator options.
|
||||
*
|
||||
* @api array $separator_options Array with the separator options.
|
||||
*/
|
||||
$filtered_separators = apply_filters( 'wpseo_separator_options', $separators );
|
||||
|
||||
if ( is_array( $filtered_separators ) && $filtered_separators !== [] ) {
|
||||
$separators = array_merge( $separators, $filtered_separators );
|
||||
}
|
||||
|
||||
return $separators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available separator options aria-labels.
|
||||
*
|
||||
* @return array Array with the separator options aria-labels.
|
||||
*/
|
||||
public function get_separator_options_for_display() {
|
||||
$separators = $this->get_separator_options();
|
||||
$separator_list = self::get_separator_option_list();
|
||||
|
||||
$separator_options = [];
|
||||
|
||||
foreach ( $separators as $key => $label ) {
|
||||
$aria_label = isset( $separator_list[ $key ]['label'] ) ? $separator_list[ $key ]['label'] : '';
|
||||
|
||||
$separator_options[ $key ] = [
|
||||
'label' => $label,
|
||||
'aria_label' => $aria_label,
|
||||
];
|
||||
}
|
||||
|
||||
return $separator_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate strings used in the option defaults.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function translate_defaults() {
|
||||
/* translators: 1: Author name; 2: Site name. */
|
||||
$this->defaults['title-author-wpseo'] = sprintf( __( '%1$s, Author at %2$s', 'wordpress-seo' ), '%%name%%', '%%sitename%%' ) . ' %%page%% ';
|
||||
/* translators: %s expands to the search phrase. */
|
||||
$this->defaults['title-search-wpseo'] = sprintf( __( 'You searched for %s', 'wordpress-seo' ), '%%searchphrase%%' ) . ' %%page%% %%sep%% %%sitename%%';
|
||||
$this->defaults['title-404-wpseo'] = __( 'Page not found', 'wordpress-seo' ) . ' %%sep%% %%sitename%%';
|
||||
/* translators: 1: link to post; 2: link to blog. */
|
||||
$this->defaults['rssafter'] = sprintf( __( 'The post %1$s appeared first on %2$s.', 'wordpress-seo' ), '%%POSTLINK%%', '%%BLOGLINK%%' );
|
||||
|
||||
$this->defaults['breadcrumbs-404crumb'] = __( 'Error 404: Page not found', 'wordpress-seo' );
|
||||
$this->defaults['breadcrumbs-archiveprefix'] = __( 'Archives for', 'wordpress-seo' );
|
||||
$this->defaults['breadcrumbs-home'] = __( 'Home', 'wordpress-seo' );
|
||||
$this->defaults['breadcrumbs-searchprefix'] = __( 'You searched for', 'wordpress-seo' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add dynamically created default options based on available post types and taxonomies.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enrich_defaults() {
|
||||
$enriched_defaults = $this->enriched_defaults;
|
||||
if ( $enriched_defaults !== null ) {
|
||||
$this->defaults += $enriched_defaults;
|
||||
return;
|
||||
}
|
||||
|
||||
$enriched_defaults = [];
|
||||
|
||||
/*
|
||||
* Retrieve all the relevant post type and taxonomy arrays.
|
||||
*
|
||||
* WPSEO_Post_Type::get_accessible_post_types() should *not* be used here.
|
||||
* These are the defaults and can be prepared for any public post type.
|
||||
*/
|
||||
$post_type_objects = get_post_types( [ 'public' => true ], 'objects' );
|
||||
|
||||
if ( $post_type_objects ) {
|
||||
/* translators: %s expands to the name of a post type (plural). */
|
||||
$archive = sprintf( __( '%s Archive', 'wordpress-seo' ), '%%pt_plural%%' );
|
||||
|
||||
foreach ( $post_type_objects as $pt ) {
|
||||
$enriched_defaults[ 'title-' . $pt->name ] = '%%title%% %%page%% %%sep%% %%sitename%%'; // Text field.
|
||||
$enriched_defaults[ 'metadesc-' . $pt->name ] = ''; // Text area.
|
||||
$enriched_defaults[ 'noindex-' . $pt->name ] = false;
|
||||
$enriched_defaults[ 'showdate-' . $pt->name ] = false;
|
||||
$enriched_defaults[ 'display-metabox-pt-' . $pt->name ] = true;
|
||||
$enriched_defaults[ 'post_types-' . $pt->name . '-maintax' ] = 0; // Select box.
|
||||
|
||||
if ( ! $pt->_builtin && WPSEO_Post_Type::has_archive( $pt ) ) {
|
||||
$enriched_defaults[ 'title-ptarchive-' . $pt->name ] = $archive . ' %%page%% %%sep%% %%sitename%%'; // Text field.
|
||||
$enriched_defaults[ 'metadesc-ptarchive-' . $pt->name ] = ''; // Text area.
|
||||
$enriched_defaults[ 'bctitle-ptarchive-' . $pt->name ] = ''; // Text field.
|
||||
$enriched_defaults[ 'noindex-ptarchive-' . $pt->name ] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$taxonomy_objects = get_taxonomies( [ 'public' => true ], 'object' );
|
||||
|
||||
if ( $taxonomy_objects ) {
|
||||
/* translators: %s expands to the variable used for term title. */
|
||||
$archives = sprintf( __( '%s Archives', 'wordpress-seo' ), '%%term_title%%' );
|
||||
|
||||
foreach ( $taxonomy_objects as $tax ) {
|
||||
$enriched_defaults[ 'title-tax-' . $tax->name ] = $archives . ' %%page%% %%sep%% %%sitename%%'; // Text field.
|
||||
$enriched_defaults[ 'metadesc-tax-' . $tax->name ] = ''; // Text area.
|
||||
$enriched_defaults[ 'display-metabox-tax-' . $tax->name ] = true;
|
||||
|
||||
$enriched_defaults[ 'noindex-tax-' . $tax->name ] = ( $tax->name === 'post_format' );
|
||||
|
||||
if ( ! $tax->_builtin ) {
|
||||
$enriched_defaults[ 'taxonomy-' . $tax->name . '-ptparent' ] = 0; // Select box;.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->enriched_defaults = $enriched_defaults;
|
||||
$this->defaults += $enriched_defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates enrich_defaults() cache.
|
||||
*
|
||||
* Called from actions:
|
||||
* - (un)registered_post_type
|
||||
* - (un)registered_taxonomy
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function invalidate_enrich_defaults_cache() {
|
||||
$this->enriched_defaults = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
$allowed_post_types = $this->get_allowed_post_types();
|
||||
|
||||
foreach ( $clean as $key => $value ) {
|
||||
$switch_key = $this->get_switch_key( $key );
|
||||
|
||||
switch ( $switch_key ) {
|
||||
/* Breadcrumbs text fields. */
|
||||
case 'breadcrumbs-404crumb':
|
||||
case 'breadcrumbs-archiveprefix':
|
||||
case 'breadcrumbs-home':
|
||||
case 'breadcrumbs-prefix':
|
||||
case 'breadcrumbs-searchprefix':
|
||||
case 'breadcrumbs-sep':
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
$clean[ $key ] = wp_kses_post( $dirty[ $key ] );
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Text fields.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Covers:
|
||||
* 'title-home-wpseo', 'title-author-wpseo', 'title-archive-wpseo',
|
||||
* 'title-search-wpseo', 'title-404-wpseo'
|
||||
* 'title-' . $pt->name
|
||||
* 'title-ptarchive-' . $pt->name
|
||||
* 'title-tax-' . $tax->name
|
||||
*/
|
||||
case 'website_name':
|
||||
case 'alternate_website_name':
|
||||
case 'title-':
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
|
||||
}
|
||||
break;
|
||||
|
||||
case 'company_or_person':
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
if ( in_array( $dirty[ $key ], [ 'company', 'person' ], true ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
else {
|
||||
$defaults = $this->get_defaults();
|
||||
$clean[ $key ] = $defaults['company_or_person'];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'company_logo':
|
||||
case 'person_logo':
|
||||
$this->validate_url( $key, $dirty, $old, $clean );
|
||||
break;
|
||||
|
||||
/*
|
||||
* Covers:
|
||||
* 'metadesc-home-wpseo', 'metadesc-author-wpseo', 'metadesc-archive-wpseo'
|
||||
* 'metadesc-' . $pt->name
|
||||
* 'metadesc-ptarchive-' . $pt->name
|
||||
* 'metadesc-tax-' . $tax->name
|
||||
* and also:
|
||||
* 'bctitle-ptarchive-' . $pt->name
|
||||
*/
|
||||
case 'metadesc-':
|
||||
case 'bctitle-ptarchive-':
|
||||
case 'company_name':
|
||||
case 'person_name':
|
||||
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
|
||||
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Covers: 'rssbefore', 'rssafter'
|
||||
*/
|
||||
case 'rssbefore':
|
||||
case 'rssafter':
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
$clean[ $key ] = wp_kses_post( $dirty[ $key ] );
|
||||
}
|
||||
break;
|
||||
|
||||
/* 'post_types-' . $pt->name . '-maintax' fields. */
|
||||
case 'post_types-':
|
||||
$post_type = str_replace( [ 'post_types-', '-maintax' ], '', $key );
|
||||
$taxonomies = get_object_taxonomies( $post_type, 'names' );
|
||||
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
if ( $taxonomies !== [] && in_array( $dirty[ $key ], $taxonomies, true ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) {
|
||||
$clean[ $key ] = 0;
|
||||
}
|
||||
elseif ( sanitize_title_with_dashes( $dirty[ $key ] ) === $dirty[ $key ] ) {
|
||||
// Allow taxonomies which may not be registered yet.
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
else {
|
||||
if ( isset( $old[ $key ] ) ) {
|
||||
$clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] );
|
||||
}
|
||||
|
||||
/*
|
||||
* @todo [JRF => whomever] Maybe change the untranslated $pt name in the
|
||||
* error message to the nicely translated label ?
|
||||
*/
|
||||
add_settings_error(
|
||||
$this->group_name, // Slug title of the setting.
|
||||
$key, // Suffix-id for the error message box.
|
||||
/* translators: %s expands to a post type. */
|
||||
sprintf( __( 'Please select a valid taxonomy for post type "%s"', 'wordpress-seo' ), $post_type ), // The error message.
|
||||
'error' // Message type.
|
||||
);
|
||||
}
|
||||
}
|
||||
elseif ( isset( $old[ $key ] ) ) {
|
||||
$clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] );
|
||||
}
|
||||
unset( $taxonomies, $post_type );
|
||||
break;
|
||||
|
||||
/* 'taxonomy-' . $tax->name . '-ptparent' fields. */
|
||||
case 'taxonomy-':
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
if ( $allowed_post_types !== [] && in_array( $dirty[ $key ], $allowed_post_types, true ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) {
|
||||
$clean[ $key ] = 0;
|
||||
}
|
||||
elseif ( sanitize_key( $dirty[ $key ] ) === $dirty[ $key ] ) {
|
||||
// Allow taxonomies which may not be registered yet.
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
else {
|
||||
if ( isset( $old[ $key ] ) ) {
|
||||
$clean[ $key ] = sanitize_key( $old[ $key ] );
|
||||
}
|
||||
|
||||
/*
|
||||
* @todo [JRF =? whomever] Maybe change the untranslated $tax name in the
|
||||
* error message to the nicely translated label ?
|
||||
*/
|
||||
$tax = str_replace( [ 'taxonomy-', '-ptparent' ], '', $key );
|
||||
add_settings_error(
|
||||
$this->group_name, // Slug title of the setting.
|
||||
'_' . $tax, // Suffix-ID for the error message box.
|
||||
/* translators: %s expands to a taxonomy slug. */
|
||||
sprintf( __( 'Please select a valid post type for taxonomy "%s"', 'wordpress-seo' ), $tax ), // The error message.
|
||||
'error' // Message type.
|
||||
);
|
||||
unset( $tax );
|
||||
}
|
||||
}
|
||||
elseif ( isset( $old[ $key ] ) ) {
|
||||
$clean[ $key ] = sanitize_key( $old[ $key ] );
|
||||
}
|
||||
break;
|
||||
|
||||
case 'company_or_person_user_id':
|
||||
case 'company_logo_id':
|
||||
case 'person_logo_id':
|
||||
case 'title_test': /* Integer field - not in form. */
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
$int = WPSEO_Utils::validate_int( $dirty[ $key ] );
|
||||
if ( $int !== false && $int >= 0 ) {
|
||||
$clean[ $key ] = $int;
|
||||
}
|
||||
}
|
||||
elseif ( isset( $old[ $key ] ) ) {
|
||||
$int = WPSEO_Utils::validate_int( $old[ $key ] );
|
||||
if ( $int !== false && $int >= 0 ) {
|
||||
$clean[ $key ] = $int;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* Separator field - Radio. */
|
||||
case 'separator':
|
||||
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
|
||||
|
||||
// Get separator fields.
|
||||
$separator_fields = $this->get_separator_options();
|
||||
|
||||
// Check if the given separator exists.
|
||||
if ( isset( $separator_fields[ $dirty[ $key ] ] ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Boolean fields.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Covers:
|
||||
* 'noindex-author-wpseo', 'noindex-author-noposts-wpseo', 'noindex-archive-wpseo'
|
||||
* 'noindex-' . $pt->name
|
||||
* 'noindex-ptarchive-' . $pt->name
|
||||
* 'noindex-tax-' . $tax->name
|
||||
* 'forcerewritetitle':
|
||||
* 'noodp':
|
||||
* 'noydir':
|
||||
* 'disable-author':
|
||||
* 'disable-date':
|
||||
* 'disable-post_format';
|
||||
* 'noindex-'
|
||||
* 'showdate-'
|
||||
* 'showdate-'. $pt->name
|
||||
* 'display-metabox-pt-'
|
||||
* 'display-metabox-pt-'. $pt->name
|
||||
* 'display-metabox-tax-'
|
||||
* 'display-metabox-tax-' . $tax->name
|
||||
* 'breadcrumbs-display-blog-page'
|
||||
* 'breadcrumbs-boldlast'
|
||||
* 'breadcrumbs-enable'
|
||||
* 'stripcategorybase'
|
||||
* 'is-media-purge-relevant'
|
||||
*/
|
||||
default:
|
||||
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of the allowed post types as breadcrumb parent for a taxonomy.
|
||||
* Helper method for validation.
|
||||
*
|
||||
* {@internal Don't make static as new types may still be registered.}}
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_post_types() {
|
||||
$allowed_post_types = [];
|
||||
|
||||
/*
|
||||
* WPSEO_Post_Type::get_accessible_post_types() should *not* be used here.
|
||||
*/
|
||||
$post_types = get_post_types( [ 'public' => true ], 'objects' );
|
||||
|
||||
if ( get_option( 'show_on_front' ) === 'page' && get_option( 'page_for_posts' ) > 0 ) {
|
||||
$allowed_post_types[] = 'post';
|
||||
}
|
||||
|
||||
if ( is_array( $post_types ) && $post_types !== [] ) {
|
||||
foreach ( $post_types as $type ) {
|
||||
if ( WPSEO_Post_Type::has_archive( $type ) ) {
|
||||
$allowed_post_types[] = $type->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $allowed_post_types;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $current_version Optional. Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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 ) {
|
||||
static $original = null;
|
||||
|
||||
// Double-run this function to ensure renaming of the taxonomy options will work.
|
||||
if ( ! isset( $original )
|
||||
&& has_action( 'wpseo_double_clean_titles', [ $this, 'clean' ] ) === false
|
||||
) {
|
||||
add_action( 'wpseo_double_clean_titles', [ $this, 'clean' ] );
|
||||
$original = $option_value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Move options from very old option to this one.
|
||||
*
|
||||
* {@internal Don't rename to the 'current' names straight away as that would prevent
|
||||
* the rename/unset combi below from working.}}
|
||||
*
|
||||
* @todo [JRF] Maybe figure out a smarter way to deal with this.
|
||||
*/
|
||||
$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 = [
|
||||
'noindexauthor' => 'noindex-author',
|
||||
'disableauthor' => 'disable-author',
|
||||
'noindexdate' => 'noindex-archive',
|
||||
'noindexcat' => 'noindex-category',
|
||||
'noindextag' => 'noindex-post_tag',
|
||||
'noindexpostformat' => 'noindex-post_format',
|
||||
];
|
||||
foreach ( $move as $old => $new ) {
|
||||
if ( isset( $old_option[ $old ] ) && ! isset( $option_value[ $new ] ) ) {
|
||||
$option_value[ $new ] = $old_option[ $old ];
|
||||
}
|
||||
}
|
||||
unset( $move, $old, $new );
|
||||
}
|
||||
unset( $old_option );
|
||||
|
||||
|
||||
// Fix wrongness created by buggy version 1.2.2.
|
||||
if ( isset( $option_value['title-home'] ) && $option_value['title-home'] === '%%sitename%% - %%sitedesc%% - 12345' ) {
|
||||
$option_value['title-home-wpseo'] = '%%sitename%% - %%sitedesc%%';
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Renaming these options to avoid ever overwritting these if a (bloody stupid) user /
|
||||
* programmer would use any of the following as a custom post type or custom taxonomy:
|
||||
* 'home', 'author', 'archive', 'search', '404', 'subpages'.
|
||||
*
|
||||
* Similarly, renaming the tax options to avoid a custom post type and a taxonomy
|
||||
* with the same name occupying the same option.
|
||||
*/
|
||||
$rename = [
|
||||
'title-home' => 'title-home-wpseo',
|
||||
'title-author' => 'title-author-wpseo',
|
||||
'title-archive' => 'title-archive-wpseo',
|
||||
'title-search' => 'title-search-wpseo',
|
||||
'title-404' => 'title-404-wpseo',
|
||||
'metadesc-home' => 'metadesc-home-wpseo',
|
||||
'metadesc-author' => 'metadesc-author-wpseo',
|
||||
'metadesc-archive' => 'metadesc-archive-wpseo',
|
||||
'noindex-author' => 'noindex-author-wpseo',
|
||||
'noindex-archive' => 'noindex-archive-wpseo',
|
||||
];
|
||||
foreach ( $rename as $old => $new ) {
|
||||
if ( isset( $option_value[ $old ] ) && ! isset( $option_value[ $new ] ) ) {
|
||||
$option_value[ $new ] = $option_value[ $old ];
|
||||
unset( $option_value[ $old ] );
|
||||
}
|
||||
}
|
||||
unset( $rename, $old, $new );
|
||||
|
||||
|
||||
/*
|
||||
* {@internal This clean-up action can only be done effectively once the taxonomies
|
||||
* and post_types have been registered, i.e. at the end of the init action.}}
|
||||
*/
|
||||
if ( isset( $original ) && current_filter() === 'wpseo_double_clean_titles' || did_action( 'wpseo_double_clean_titles' ) > 0 ) {
|
||||
$rename = [
|
||||
'title-' => 'title-tax-',
|
||||
'metadesc-' => 'metadesc-tax-',
|
||||
'noindex-' => 'noindex-tax-',
|
||||
'tax-hideeditbox-' => 'hideeditbox-tax-',
|
||||
|
||||
];
|
||||
|
||||
$taxonomy_names = get_taxonomies( [ 'public' => true ], 'names' );
|
||||
$post_type_names = get_post_types( [ 'public' => true ], 'names' );
|
||||
$defaults = $this->get_defaults();
|
||||
if ( $taxonomy_names !== [] ) {
|
||||
foreach ( $taxonomy_names as $tax ) {
|
||||
foreach ( $rename as $old_prefix => $new_prefix ) {
|
||||
if (
|
||||
( isset( $original[ $old_prefix . $tax ] ) && ! isset( $original[ $new_prefix . $tax ] ) )
|
||||
&& ( ! isset( $option_value[ $new_prefix . $tax ] )
|
||||
|| ( isset( $option_value[ $new_prefix . $tax ] )
|
||||
&& $option_value[ $new_prefix . $tax ] === $defaults[ $new_prefix . $tax ] ) )
|
||||
) {
|
||||
$option_value[ $new_prefix . $tax ] = $original[ $old_prefix . $tax ];
|
||||
|
||||
/*
|
||||
* Check if there is a cpt with the same name as the tax,
|
||||
* if so, we should make sure that the old setting hasn't been removed.
|
||||
*/
|
||||
if ( ! isset( $post_type_names[ $tax ] ) && isset( $option_value[ $old_prefix . $tax ] ) ) {
|
||||
unset( $option_value[ $old_prefix . $tax ] );
|
||||
}
|
||||
else {
|
||||
if ( isset( $post_type_names[ $tax ] ) && ! isset( $option_value[ $old_prefix . $tax ] ) ) {
|
||||
$option_value[ $old_prefix . $tax ] = $original[ $old_prefix . $tax ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( $old_prefix === 'tax-hideeditbox-' ) {
|
||||
unset( $option_value[ $old_prefix . $tax ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset( $rename, $taxonomy_names, $post_type_names, $defaults, $tax, $old_prefix, $new_prefix );
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure the values of the variable option key options are cleaned as they
|
||||
* may be retained and would not be cleaned/validated then.
|
||||
*/
|
||||
if ( is_array( $option_value ) && $option_value !== [] ) {
|
||||
foreach ( $option_value as $key => $value ) {
|
||||
$switch_key = $this->get_switch_key( $key );
|
||||
|
||||
// Similar to validation routine - any changes made there should be made here too.
|
||||
switch ( $switch_key ) {
|
||||
/* Text fields. */
|
||||
case 'title-':
|
||||
case 'metadesc-':
|
||||
case 'bctitle-ptarchive-':
|
||||
$option_value[ $key ] = WPSEO_Utils::sanitize_text_field( $value );
|
||||
break;
|
||||
|
||||
case 'separator':
|
||||
if ( ! array_key_exists( $value, $this->get_separator_options() ) ) {
|
||||
$option_value[ $key ] = false;
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Boolean fields.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Covers:
|
||||
* 'noindex-'
|
||||
* 'showdate-'
|
||||
* 'hideeditbox-'
|
||||
*/
|
||||
default:
|
||||
$option_value[ $key ] = WPSEO_Utils::validate_bool( $value );
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset( $key, $value, $switch_key );
|
||||
}
|
||||
|
||||
return $option_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Overrule the abstract class version of this to make sure one extra renamed
|
||||
* variable key does not get removed. IMPORTANT: keep this method in line with
|
||||
* the parent on which it is based!}}
|
||||
*
|
||||
* @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 !== [] ) ) {
|
||||
|
||||
// Add the extra pattern.
|
||||
$patterns = $this->variable_array_key_patterns;
|
||||
$patterns[] = 'tax-hideeditbox-';
|
||||
|
||||
/**
|
||||
* Allow altering the array with variable array key patterns.
|
||||
*
|
||||
* @api array $patterns Array with the variable array key patterns.
|
||||
*/
|
||||
$patterns = apply_filters( 'wpseo_option_titles_variable_array_key_patterns', $patterns );
|
||||
|
||||
foreach ( $dirty as $key => $value ) {
|
||||
|
||||
// Do nothing if already in filtered option array.
|
||||
if ( isset( $clean[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $patterns as $pattern ) {
|
||||
if ( strpos( $key, $pattern ) === 0 ) {
|
||||
$clean[ $key ] = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a list of separator options.
|
||||
*
|
||||
* @return array An array of the separator options.
|
||||
*/
|
||||
protected static function get_separator_option_list() {
|
||||
$separators = [
|
||||
'sc-dash' => [
|
||||
'option' => '-',
|
||||
'label' => __( 'Dash', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-ndash' => [
|
||||
'option' => '–',
|
||||
'label' => __( 'En dash', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-mdash' => [
|
||||
'option' => '—',
|
||||
'label' => __( 'Em dash', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-colon' => [
|
||||
'option' => ':',
|
||||
'label' => __( 'Colon', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-middot' => [
|
||||
'option' => '·',
|
||||
'label' => __( 'Middle dot', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-bull' => [
|
||||
'option' => '•',
|
||||
'label' => __( 'Bullet', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-star' => [
|
||||
'option' => '*',
|
||||
'label' => __( 'Asterisk', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-smstar' => [
|
||||
'option' => '⋆',
|
||||
'label' => __( 'Low asterisk', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-pipe' => [
|
||||
'option' => '|',
|
||||
'label' => __( 'Vertical bar', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-tilde' => [
|
||||
'option' => '~',
|
||||
'label' => __( 'Small tilde', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-laquo' => [
|
||||
'option' => '«',
|
||||
'label' => __( 'Left angle quotation mark', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-raquo' => [
|
||||
'option' => '»',
|
||||
'label' => __( 'Right angle quotation mark', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-lt' => [
|
||||
'option' => '<',
|
||||
'label' => __( 'Less than sign', 'wordpress-seo' ),
|
||||
],
|
||||
'sc-gt' => [
|
||||
'option' => '>',
|
||||
'label' => __( 'Greater than sign', 'wordpress-seo' ),
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Allows altering the separator options array.
|
||||
*
|
||||
* @api array $separators Array with the separator options.
|
||||
*/
|
||||
$separator_list = apply_filters( 'wpseo_separator_option_list', $separators );
|
||||
|
||||
if ( ! is_array( $separator_list ) ) {
|
||||
return $separators;
|
||||
}
|
||||
|
||||
return $separator_list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
<?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.
|
||||
'ms_defaults_set' => false,
|
||||
// Non-form field, should only be set via validation routine.
|
||||
'version' => '', // Leave default as empty to ensure activation/upgrade works.
|
||||
|
||||
// Form fields.
|
||||
'disableadvanced_meta' => true,
|
||||
'onpage_indexability' => true,
|
||||
'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,
|
||||
'enable_admin_bar_menu' => true,
|
||||
'enable_cornerstone_content' => true,
|
||||
'enable_xml_sitemap' => true,
|
||||
'enable_text_link_counter' => true,
|
||||
'show_onboarding_notice' => false,
|
||||
'first_activated_on' => false,
|
||||
'myyoast-oauth' => [
|
||||
'config' => [
|
||||
'clientId' => null,
|
||||
'secret' => null,
|
||||
],
|
||||
'access_tokens' => [],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Sub-options which should not be overloaded with multi-site defaults.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $ms_exclude = [
|
||||
/* 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 = [
|
||||
'',
|
||||
'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.
|
||||
*
|
||||
* @return \WPSEO_Option_Wpseo
|
||||
*/
|
||||
protected function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
/* 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;
|
||||
|
||||
/* 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 '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':
|
||||
$clean[ $key ] = false;
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
if ( $dirty[ $key ] === false || WPSEO_Utils::validate_int( $dirty[ $key ] ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'myyoast_oauth':
|
||||
$clean[ $key ] = $old[ $key ];
|
||||
|
||||
if ( isset( $dirty[ $key ] ) ) {
|
||||
$myyoast_oauth = $dirty[ $key ];
|
||||
if ( ! is_array( $myyoast_oauth ) ) {
|
||||
$myyoast_oauth = json_decode( $dirty[ $key ], true );
|
||||
}
|
||||
|
||||
if ( is_array( $myyoast_oauth ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
/*
|
||||
* Boolean (checkbox) fields.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Covers:
|
||||
* 'disableadvanced_meta'
|
||||
* 'yoast_tracking'
|
||||
*/
|
||||
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,
|
||||
'onpage_indexability' => false,
|
||||
'content_analysis_active' => false,
|
||||
'keyword_analysis_active' => false,
|
||||
'enable_admin_bar_menu' => false,
|
||||
'enable_cornerstone_content' => false,
|
||||
'enable_xml_sitemap' => false,
|
||||
'enable_text_link_counter' => 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 $current_version Optional. Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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 ) {
|
||||
return $option_value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,911 @@
|
||||
<?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 object
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
|
||||
/* *********** INSTANTIATION METHODS *********** */
|
||||
|
||||
/**
|
||||
* Add all the actions and filters for the option.
|
||||
*
|
||||
* @return \WPSEO_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' ] ); // Adding back after INSERT.
|
||||
|
||||
add_action( 'update_option', [ $this, 'add_default_filters' ] );
|
||||
|
||||
// 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' ] );
|
||||
|
||||
// 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 );
|
||||
}
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
/**
|
||||
* 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' ] );
|
||||
}
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
/**
|
||||
* 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( htmlspecialchars( $dirty[ $key ], ENT_COMPAT, get_bloginfo( 'charset' ), true ) );
|
||||
$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_html( $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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a Facebook App ID.
|
||||
*
|
||||
* @param string $key Key to check, in this case: the Facebook App ID field name.
|
||||
* @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_facebook_app_id( $key, $dirty, $old, &$clean ) {
|
||||
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
|
||||
$url = 'https://graph.facebook.com/' . $dirty[ $key ];
|
||||
|
||||
$response = wp_remote_get( $url );
|
||||
// These filters are used in the tests.
|
||||
/**
|
||||
* Filter: 'validate_facebook_app_id_api_response_code' - Allows to filter the Faceboook API response code.
|
||||
*
|
||||
* @api int $response_code The Facebook API response header code.
|
||||
*/
|
||||
$response_code = apply_filters( 'validate_facebook_app_id_api_response_code', wp_remote_retrieve_response_code( $response ) );
|
||||
/**
|
||||
* Filter: 'validate_facebook_app_id_api_response_body' - Allows to filter the Faceboook API response body.
|
||||
*
|
||||
* @api string $response_body The Facebook API JSON response body.
|
||||
*/
|
||||
$response_body = apply_filters( 'validate_facebook_app_id_api_response_body', wp_remote_retrieve_body( $response ) );
|
||||
$response_object = json_decode( $response_body );
|
||||
|
||||
/*
|
||||
* When the request is successful the response code will be 200 and
|
||||
* the response object will contain an `id` property.
|
||||
*/
|
||||
if ( $response_code === 200 && isset( $response_object->id ) ) {
|
||||
$clean[ $key ] = $dirty[ $key ];
|
||||
return;
|
||||
}
|
||||
|
||||
// Restore the previous value, if any.
|
||||
if ( isset( $old[ $key ] ) && $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-`.
|
||||
sprintf(
|
||||
/* translators: %s expands to an invalid Facebook App ID. */
|
||||
__( '%s does not seem to be a valid Facebook App ID. Please correct.', 'wordpress-seo' ),
|
||||
'<strong>' . esc_html( $dirty[ $key ] ) . '</strong>'
|
||||
), // 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, $dirty[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $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 $current_version Optional. Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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 $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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,609 @@
|
||||
<?php
|
||||
/**
|
||||
* WPSEO plugin file.
|
||||
*
|
||||
* @package WPSEO\Internals\Options
|
||||
*/
|
||||
|
||||
/**
|
||||
* Overal 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 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 object
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Instantiate all the WPSEO option management classes.
|
||||
*/
|
||||
protected function __construct() {
|
||||
$this->register_hooks();
|
||||
|
||||
foreach ( static::$options as $option_name => $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 );
|
||||
$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 The default value that should be returned if the key isn't set.
|
||||
*
|
||||
* @return mixed|null Returns value if found, $default if not.
|
||||
*/
|
||||
public static function get( $key, $default = null ) {
|
||||
if ( static::$option_values === null ) {
|
||||
static::prime_cache();
|
||||
}
|
||||
if ( isset( static::$option_values[ $key ] ) ) {
|
||||
return static::$option_values[ $key ];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bool|mixed $default A default value to return.
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function get_autoloaded_option( $option, $default = false ) {
|
||||
$value = wp_cache_get( $option, 'options' );
|
||||
if ( $value === false ) {
|
||||
$passed_default = func_num_args() > 1;
|
||||
|
||||
return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
|
||||
}
|
||||
|
||||
return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the clean up routine for one or all options.
|
||||
*
|
||||
* @param array|string $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 $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 boolean 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;
|
||||
}
|
||||
|
||||
/* ********************* DEPRECATED METHODS ********************* */
|
||||
|
||||
/**
|
||||
* Fills our option cache.
|
||||
*
|
||||
* @deprecated 12.8.1
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function fill_cache() {
|
||||
_deprecated_function( __METHOD__, 'WPSEO 12.8.1', '::clear_cache' );
|
||||
static::clear_cache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct the inadvertent removal of the fallback to default values from the breadcrumbs.
|
||||
*
|
||||
* @since 1.5.2.3
|
||||
*
|
||||
* @deprecated 7.0
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function bring_back_breadcrumb_defaults() {
|
||||
_deprecated_function( __METHOD__, 'WPSEO 7.0' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,614 @@
|
||||
<?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_focuskeywords' => '[]',
|
||||
'wpseo_keywordsynonyms' => '[]',
|
||||
|
||||
// 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;
|
||||
|
||||
/* On succesfull update/add of the option, flush the W3TC cache. */
|
||||
add_action( 'add_option_' . $this->option_name, [ 'WPSEO_Utils', 'flush_w3tc_cache' ] );
|
||||
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'flush_w3tc_cache' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $option_key Option name of the option we're doing the merge for.
|
||||
* @param array $options Optional. Current options. If not set, the option defaults
|
||||
* for the $option_key will be returned.
|
||||
*
|
||||
* @return array Combined and filtered options array.
|
||||
*/
|
||||
|
||||
/*
|
||||
Public function array_filter_merge( $option_key, $options = null ) {
|
||||
|
||||
$defaults = $this->get_defaults( $option_key );
|
||||
|
||||
if ( ! isset( $options ) || $options === false ) {
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
/ *
|
||||
{@internal Adding the defaults to all taxonomy terms each time the option is retrieved
|
||||
will be quite inefficient if there are a lot of taxonomy terms.
|
||||
As long as taxonomy_meta is only retrieved via methods in this class, we shouldn't need this.}}
|
||||
|
||||
$options = (array) $options;
|
||||
$filtered = array();
|
||||
|
||||
if ( $options !== array() ) {
|
||||
foreach ( $options as $taxonomy => $terms ) {
|
||||
if ( is_array( $terms ) && $terms !== array() ) {
|
||||
foreach ( $terms as $id => $term_meta ) {
|
||||
foreach ( self::$defaults_per_term as $name => $default ) {
|
||||
if ( isset( $options[ $taxonomy ][ $id ][ $name ] ) ) {
|
||||
$filtered[ $taxonomy ][ $id ][ $name ] = $options[ $taxonomy ][ $id ][ $name ];
|
||||
}
|
||||
else {
|
||||
$filtered[ $name ] = $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset( $taxonomy, $terms, $id, $term_meta, $name, $default );
|
||||
}
|
||||
// end of may be remove.
|
||||
|
||||
return $filtered;
|
||||
* /
|
||||
|
||||
return (array) $options;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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 = [
|
||||
'<',
|
||||
'>',
|
||||
'`',
|
||||
'<',
|
||||
'>',
|
||||
'`',
|
||||
];
|
||||
|
||||
$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 $current_version Optional. Version from which to upgrade, if not set,
|
||||
* version specific upgrades will be disregarded.
|
||||
* @param array $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 $meta Optional. Meta value to get (without prefix).
|
||||
*
|
||||
* @return mixed|bool 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 bool|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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user