PK &MFZ�t�
�� �� class-wpseo-replace-vars.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals
* @since 1.5.4
*/
// Avoid direct calls to this file.
if ( ! defined( 'WPSEO_VERSION' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
/**
* Class: WPSEO_Replace_Vars.
*
* This class implements the replacing of `%%variable_placeholders%%` with their real value based on the current
* requested page/post/cpt/etc in text strings.
*/
class WPSEO_Replace_Vars {
/**
* Default post/page/cpt information.
*
* @var array
*/
protected $defaults = [
'ID' => '',
'name' => '',
'post_author' => '',
'post_content' => '',
'post_date' => '',
'post_excerpt' => '',
'post_modified' => '',
'post_title' => '',
'taxonomy' => '',
'term_id' => '',
'term404' => '',
];
/**
* Current post/page/cpt information.
*
* @var stdClass
*/
protected $args;
/**
* Help texts for use in WPSEO -> Search appearance tabs.
*
* @var array
*/
protected static $help_texts = [];
/**
* Register of additional variable replacements registered by other plugins/themes.
*
* @var array
*/
protected static $external_replacements = [];
/**
* Setup the help texts and external replacements as statics so they will be available to all instances.
*
* @return void
*/
public static function setup_statics_once() {
if ( self::$help_texts === [] ) {
self::set_basic_help_texts();
self::set_advanced_help_texts();
}
if ( self::$external_replacements === [] ) {
/**
* Action: 'wpseo_register_extra_replacements' - Allows for registration of additional
* variables to replace.
*/
do_action( 'wpseo_register_extra_replacements' );
}
}
/**
* Register new replacement %%variables%%.
* For use by other plugins/themes to register extra variables.
*
* @see wpseo_register_var_replacement() for a usage example.
*
* @param string $var_to_replace The name of the variable to replace, i.e. '%%var%%'.
* Note: the surrounding %% are optional.
* @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
* Uses the same format as add_filter/add_action function parameter and
* should *return* the replacement value. DON'T echo it.
* @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
* @param string $help_text Help text to be added to the help tab for this variable.
*
* @return bool Whether the replacement function was succesfully registered.
*/
public static function register_replacement( $var_to_replace, $replace_function, $type = 'advanced', $help_text = '' ) {
$success = false;
if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
$var_to_replace = self::remove_var_delimiter( $var_to_replace );
if ( preg_match( '`^[A-Z0-9_-]+$`i', $var_to_replace ) === false ) {
trigger_error( esc_html__( 'A replacement variable can only contain alphanumeric characters, an underscore or a dash. Try renaming your variable.', 'wordpress-seo' ), E_USER_WARNING );
}
elseif ( strpos( $var_to_replace, 'cf_' ) === 0 || strpos( $var_to_replace, 'ct_' ) === 0 ) {
trigger_error( esc_html__( 'A replacement variable can not start with "%%cf_" or "%%ct_" as these are reserved for the WPSEO standard variable variables for custom fields and custom taxonomies. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
}
elseif ( ! method_exists( self::class, 'retrieve_' . $var_to_replace ) ) {
if ( $var_to_replace !== '' && ! isset( self::$external_replacements[ $var_to_replace ] ) ) {
self::$external_replacements[ $var_to_replace ] = $replace_function;
$replacement_variable = new WPSEO_Replacement_Variable( $var_to_replace, $var_to_replace, $help_text );
self::register_help_text( $type, $replacement_variable );
$success = true;
}
else {
trigger_error( esc_html__( 'A replacement variable with the same name has already been registered. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
}
}
else {
trigger_error( esc_html__( 'You cannot overrule a WPSEO standard variable replacement by registering a variable with the same name. Use the "wpseo_replacements" filter instead to adjust the replacement value.', 'wordpress-seo' ), E_USER_WARNING );
}
}
return $success;
}
/**
* Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt/etc.
*
* @param string $text The string to replace the variables in.
* @param array $args The object some of the replacement values might come from,
* could be a post, taxonomy or term.
* @param array $omit Variables that should not be replaced by this function.
*
* @return string
*/
public function replace( $text, $args, $omit = [] ) {
$text = wp_strip_all_tags( $text );
// Let's see if we can bail super early.
if ( strpos( $text, '%%' ) === false ) {
return YoastSEO()->helpers->string->standardize_whitespace( $text );
}
$args = (array) $args;
if ( isset( $args['post_content'] ) && ! empty( $args['post_content'] ) ) {
$args['post_content'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_content'] );
}
if ( isset( $args['post_excerpt'] ) && ! empty( $args['post_excerpt'] ) ) {
$args['post_excerpt'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_excerpt'] );
}
$this->args = (object) wp_parse_args( $args, $this->defaults );
// Clean $omit array.
if ( is_array( $omit ) && $omit !== [] ) {
$omit = array_map( [ self::class, 'remove_var_delimiter' ], $omit );
}
$replacements = [];
if ( preg_match_all( '`%%([^%]+(%%single)?)%%?`iu', $text, $matches ) ) {
$replacements = $this->set_up_replacements( $matches, $omit );
}
/**
* Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied.
*
* @param array $replacements The replacements.
* @param array $args The object some of the replacement values might come from,
* could be a post, taxonomy or term.
*/
$replacements = apply_filters( 'wpseo_replacements', $replacements, $this->args );
// Do the actual replacements.
if ( is_array( $replacements ) && $replacements !== [] ) {
$text = str_replace(
array_keys( $replacements ),
// Make sure to exclude replacement values that are arrays e.g. coming from a custom field serialized value.
array_filter( array_values( $replacements ), 'is_scalar' ),
$text
);
}
/**
* Filter: 'wpseo_replacements_final' - Allow overruling of whether or not to remove placeholders
* which didn't yield a replacement.
*
* @example <code>add_filter( 'wpseo_replacements_final', '__return_false' );</code>
*
* @param bool $final
*/
if ( apply_filters( 'wpseo_replacements_final', true ) === true && ( isset( $matches[1] ) && is_array( $matches[1] ) ) ) {
// Remove non-replaced variables.
$remove = array_diff( $matches[1], $omit ); // Make sure the $omit variables do not get removed.
$remove = array_map( [ self::class, 'add_var_delimiter' ], $remove );
$text = str_replace( $remove, '', $text );
}
// Undouble separators which have nothing between them, i.e. where a non-replaced variable was removed.
if ( isset( $replacements['%%sep%%'] ) && ( is_string( $replacements['%%sep%%'] ) && $replacements['%%sep%%'] !== '' ) ) {
$q_sep = preg_quote( $replacements['%%sep%%'], '`' );
$text = preg_replace( '`' . $q_sep . '(?:\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $text );
}
// Remove superfluous whitespace.
$text = YoastSEO()->helpers->string->standardize_whitespace( $text );
return $text;
}
/**
* Register a new replacement variable if it has not been registered already.
*
* @param string $var_to_replace The name of the variable to replace, i.e. '%%var%%'.
* Note: the surrounding %% are optional.
* @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
* Uses the same format as add_filter/add_action function parameter and
* should *return* the replacement value. DON'T echo it.
* @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
* @param string $help_text Help text to be added to the help tab for this variable.
*
* @return bool `true` if the replace var has been registered, `false` if not.
*/
public function safe_register_replacement( $var_to_replace, $replace_function, $type = 'advanced', $help_text = '' ) {
if ( ! $this->has_been_registered( $var_to_replace ) ) {
return self::register_replacement( $var_to_replace, $replace_function, $type, $help_text );
}
return false;
}
/**
* Checks whether the given replacement variable has already been registered or not.
*
* @param string $replacement_variable The replacement variable to check, including the variable delimiter (e.g. `%%var%%`).
*
* @return bool `true` if the replacement variable has already been registered.
*/
public function has_been_registered( $replacement_variable ) {
$replacement_variable = self::remove_var_delimiter( $replacement_variable );
return isset( self::$external_replacements[ $replacement_variable ] );
}
/**
* Returns the list of hidden replace vars.
*
* E.g. the replace vars that should work, but are not advertised.
*
* @return string[] The list of hidden replace vars.
*/
public function get_hidden_replace_vars() {
return [
'currentdate',
'currentyear',
'currentmonth',
'currentday',
'post_year',
'post_month',
'post_day',
'author_first_name',
'author_last_name',
'permalink',
'post_content',
'category_title',
];
}
/**
* Retrieve the replacements for the variables found.
*
* @param array $matches Variables found in the original string - regex result.
* @param array $omit Variables that should not be replaced by this function.
*
* @return array Retrieved replacements - this might be a smaller array as some variables
* may not yield a replacement in certain contexts.
*/
private function set_up_replacements( $matches, $omit ) {
$replacements = [];
// @todo Figure out a way to deal with external functions starting with cf_/ct_.
foreach ( $matches[1] as $k => $var ) {
// Don't set up replacements which should be omitted.
if ( in_array( $var, $omit, true ) ) {
continue;
}
// Deal with variable variable names first.
if ( strpos( $var, 'cf_' ) === 0 ) {
$replacement = $this->retrieve_cf_custom_field_name( $var );
}
elseif ( strpos( $var, 'ct_desc_' ) === 0 ) {
$replacement = $this->retrieve_ct_desc_custom_tax_name( $var );
}
elseif ( strpos( $var, 'ct_' ) === 0 ) {
$single = ( isset( $matches[2][ $k ] ) && $matches[2][ $k ] !== '' );
$replacement = $this->retrieve_ct_custom_tax_name( $var, $single );
}
// Deal with non-variable variable names.
elseif ( method_exists( $this, 'retrieve_' . $var ) ) {
$method_name = 'retrieve_' . $var;
$replacement = $this->$method_name();
}
// Deal with externally defined variable names.
elseif ( isset( self::$external_replacements[ $var ] ) && ! is_null( self::$external_replacements[ $var ] ) ) {
$replacement = call_user_func( self::$external_replacements[ $var ], $var, $this->args );
}
// Replacement retrievals can return null if no replacement can be determined, root those outs.
if ( isset( $replacement ) ) {
$var = self::add_var_delimiter( $var );
$replacements[ $var ] = $replacement;
}
unset( $replacement, $single, $method_name );
}
return $replacements;
}
/* *********************** BASIC VARIABLES ************************** */
/**
* Retrieve the post/cpt categories (comma separated) for use as replacement string.
*
* @return string|null
*/
private function retrieve_category() {
$replacement = null;
if ( ! empty( $this->args->ID ) ) {
$cat = $this->get_terms( $this->args->ID, 'category' );
if ( $cat !== '' ) {
return $cat;
}
}
if ( isset( $this->args->cat_name ) && ! empty( $this->args->cat_name ) ) {
$replacement = $this->args->cat_name;
}
return $replacement;
}
/**
* Retrieve the category description for use as replacement string.
*
* @return string|null
*/
private function retrieve_category_description() {
return $this->retrieve_term_description();
}
/**
* Retrieve the date of the post/page/cpt for use as replacement string.
*
* @return string|null
*/
private function retrieve_date() {
$replacement = null;
if ( $this->args->post_date !== '' ) {
// Returns a string.
$replacement = YoastSEO()->helpers->date->format_translated( $this->args->post_date, get_option( 'date_format' ) );
}
elseif ( get_query_var( 'day' ) && get_query_var( 'day' ) !== '' ) {
// Returns a string.
$replacement = get_the_date();
}
elseif ( single_month_title( ' ', false ) && single_month_title( ' ', false ) !== '' ) {
// Returns a string.
$replacement = single_month_title( ' ', false );
}
elseif ( get_query_var( 'year' ) !== '' ) {
// Returns an integer, let's cast to string.
$replacement = (string) get_query_var( 'year' );
}
return $replacement;
}
/**
* Retrieve the post/page/cpt excerpt for use as replacement string.
* The excerpt will be auto-generated if it does not exist.
*
* @return string|null
*/
private function retrieve_excerpt() {
$replacement = null;
$locale = get_locale();
// Japanese doesn't have a jp_JP variant in WP.
$limit = ( $locale === 'ja' ) ? 80 : 156;
// The check `post_password_required` is because excerpt must be hidden for a post with a password.
if ( ! empty( $this->args->ID ) && ! post_password_required( $this->args->ID ) ) {
if ( $this->args->post_excerpt !== '' ) {
$replacement = wp_strip_all_tags( $this->args->post_excerpt );
}
elseif ( $this->args->post_content !== '' ) {
$content = strip_shortcodes( $this->args->post_content );
$content = wp_strip_all_tags( $content );
if ( mb_strlen( $content ) <= $limit ) {
return $content;
}
$replacement = wp_html_excerpt( $content, $limit );
// Check if the description has space and trim the auto-generated string to a word boundary.
if ( strrpos( $replacement, ' ' ) ) {
$replacement = substr( $replacement, 0, strrpos( $replacement, ' ' ) );
}
}
}
return $replacement;
}
/**
* Retrieve the post/page/cpt excerpt for use as replacement string (without auto-generation).
*
* @return string|null
*/
private function retrieve_excerpt_only() {
$replacement = null;
// The check `post_password_required` is because excerpt must be hidden for a post with a password.
if ( ! empty( $this->args->ID ) && $this->args->post_excerpt !== '' && ! post_password_required( $this->args->ID ) ) {
$replacement = wp_strip_all_tags( $this->args->post_excerpt );
}
return $replacement;
}
/**
* Retrieve the title of the parent page of the current page/cpt for use as replacement string.
* Only applicable for hierarchical post types.
*
* @todo Check: shouldn't this use $this->args as well ?
*
* @return string|null
*/
private function retrieve_parent_title() {
$replacement = null;
if ( ! empty( $this->args->ID ) ) {
$parent_id = wp_get_post_parent_id( $this->args->ID );
if ( $parent_id ) {
$replacement = get_the_title( $parent_id );
}
}
return $replacement;
}
/**
* Retrieve the current search phrase for use as replacement string.
*
* @return string|null
*/
private function retrieve_searchphrase() {
$replacement = null;
$search = get_query_var( 's' );
if ( $search !== '' ) {
$replacement = esc_html( $search );
}
return $replacement;
}
/**
* Retrieve the separator for use as replacement string.
*
* @return string Retrieves the title separator.
*/
private function retrieve_sep() {
return YoastSEO()->helpers->options->get_title_separator();
}
/**
* Retrieve the site's tag line / description for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string|null
*/
private function retrieve_sitedesc() {
static $replacement;
if ( ! isset( $replacement ) ) {
$description = wp_strip_all_tags( get_bloginfo( 'description' ) );
if ( $description !== '' ) {
$replacement = $description;
}
}
return $replacement;
}
/**
* Retrieve the site's name for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string|null
*/
private function retrieve_sitename() {
static $replacement;
if ( ! isset( $replacement ) ) {
$sitename = YoastSEO()->helpers->site->get_site_name();
if ( $sitename !== '' ) {
$replacement = $sitename;
}
}
return $replacement;
}
/**
* Retrieve the current tag/tags for use as replacement string.
*
* @return string|null
*/
private function retrieve_tag() {
$replacement = null;
if ( ! empty( $this->args->ID ) ) {
$tags = $this->get_terms( $this->args->ID, 'post_tag' );
if ( $tags !== '' ) {
$replacement = $tags;
}
}
return $replacement;
}
/**
* Retrieve the tag description for use as replacement string.
*
* @return string|null
*/
private function retrieve_tag_description() {
return $this->retrieve_term_description();
}
/**
* Retrieve the term description for use as replacement string.
*
* @return string|null
*/
private function retrieve_term_description() {
$replacement = null;
if ( ! empty( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
$term_desc = get_term_field( 'description', $this->args->term_id, $this->args->taxonomy );
if ( $term_desc !== '' ) {
$replacement = wp_strip_all_tags( $term_desc );
}
}
return $replacement;
}
/**
* Retrieve the term name for use as replacement string.
*
* @return string|null
*/
private function retrieve_term_title() {
$replacement = null;
if ( ! empty( $this->args->taxonomy ) && ! empty( $this->args->name ) ) {
$replacement = $this->args->name;
}
return $replacement;
}
/**
* Retrieve the title of the post/page/cpt for use as replacement string.
*
* @return string|null
*/
private function retrieve_title() {
$replacement = null;
if ( is_string( $this->args->post_title ) && $this->args->post_title !== '' ) {
$replacement = $this->args->post_title;
}
return $replacement;
}
/**
* Retrieve primary category for use as replacement string.
*
* @return bool|int|null
*/
private function retrieve_primary_category() {
$primary_category = null;
if ( ! empty( $this->args->ID ) ) {
$wpseo_primary_category = new WPSEO_Primary_Term( 'category', $this->args->ID );
$term_id = $wpseo_primary_category->get_primary_term();
$term = get_term( $term_id );
if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
$primary_category = $term->name;
}
}
return $primary_category;
}
/**
* Retrieve the string generated by get_the_archive_title().
*
* @return string|null
*/
private function retrieve_archive_title() {
return get_the_archive_title();
}
/* *********************** ADVANCED VARIABLES ************************** */
/**
* Determine the page numbering of the current post/page/cpt.
*
* @param string $request Either 'nr'|'max' - whether to return the page number or the max number of pages.
*
* @return int|null
*/
private function determine_pagenumbering( $request = 'nr' ) {
global $wp_query, $post;
$max_num_pages = null;
$page_number = null;
$max_num_pages = 1;
if ( ! is_singular() ) {
$page_number = get_query_var( 'paged' );
if ( $page_number === 0 || $page_number === '' ) {
$page_number = 1;
}
if ( ! empty( $wp_query->max_num_pages ) ) {
$max_num_pages = $wp_query->max_num_pages;
}
}
else {
$page_number = get_query_var( 'page' );
if ( $page_number === 0 || $page_number === '' ) {
$page_number = 1;
}
if ( isset( $post->post_content ) ) {
$max_num_pages = ( substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
}
}
$return = null;
switch ( $request ) {
case 'nr':
$return = $page_number;
break;
case 'max':
$return = $max_num_pages;
break;
}
return $return;
}
/**
* Determine the post type names for the current post/page/cpt.
*
* @param string $request Either 'single'|'plural' - whether to return the single or plural form.
*
* @return string|null
*/
private function determine_pt_names( $request = 'single' ) {
global $wp_query;
$pt_single = null;
$pt_plural = null;
$post_type = '';
if ( isset( $wp_query->query_vars['post_type'] ) && ( ( is_string( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== '' ) || ( is_array( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== [] ) ) ) {
$post_type = $wp_query->query_vars['post_type'];
}
elseif ( isset( $this->args->post_type ) && ( is_string( $this->args->post_type ) && $this->args->post_type !== '' ) ) {
$post_type = $this->args->post_type;
}
else {
// Make it work in preview mode.
$post = $wp_query->get_queried_object();
if ( $post instanceof WP_Post ) {
$post_type = $post->post_type;
}
}
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
if ( $post_type !== '' ) {
$pt = get_post_type_object( $post_type );
$pt_single = $pt->name;
$pt_plural = $pt->name;
if ( isset( $pt->labels->singular_name ) ) {
$pt_single = $pt->labels->singular_name;
}
if ( isset( $pt->labels->name ) ) {
$pt_plural = $pt->labels->name;
}
}
$return = null;
switch ( $request ) {
case 'single':
$return = $pt_single;
break;
case 'plural':
$return = $pt_plural;
break;
}
return $return;
}
/**
* Retrieve the attachment caption for use as replacement string.
*
* @return string|null
*/
private function retrieve_caption() {
return $this->retrieve_excerpt_only();
}
/**
* Retrieve a post/page/cpt's custom field value for use as replacement string.
*
* @param string $var_to_replace The complete variable to replace which includes the name of
* the custom field which value is to be retrieved.
*
* @return string|null
*/
private function retrieve_cf_custom_field_name( $var_to_replace ) {
$replacement = null;
if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
$field = substr( $var_to_replace, 3 );
if ( ! empty( $this->args->ID ) ) {
// Post meta can be arrays and in this case we need to exclude them.
$name = get_post_meta( $this->args->ID, $field, true );
if ( $name !== '' && ! is_array( $name ) ) {
$replacement = $name;
}
}
elseif ( ! empty( $this->args->term_id ) ) {
$name = get_term_meta( $this->args->term_id, $field, true );
if ( $name !== '' ) {
$replacement = $name;
}
}
}
return $replacement;
}
/**
* Retrieve a post/page/cpt's custom taxonomies for use as replacement string.
*
* @param string $var_to_replace The complete variable to replace which includes the name of
* the custom taxonomy which value(s) is to be retrieved.
* @param bool $single Whether to retrieve only the first or all values for the taxonomy.
*
* @return string|null
*/
private function retrieve_ct_custom_tax_name( $var_to_replace, $single = false ) {
$replacement = null;
if ( ( is_string( $var_to_replace ) && $var_to_replace !== '' ) && ! empty( $this->args->ID ) ) {
$tax = substr( $var_to_replace, 3 );
$name = $this->get_terms( $this->args->ID, $tax, $single );
if ( $name !== '' ) {
$replacement = $name;
}
}
return $replacement;
}
/**
* Retrieve a post/page/cpt's custom taxonomies description for use as replacement string.
*
* @param string $var_to_replace The complete variable to replace which includes the name of
* the custom taxonomy which description is to be retrieved.
*
* @return string|null
*/
private function retrieve_ct_desc_custom_tax_name( $var_to_replace ) {
$replacement = null;
if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
$tax = substr( $var_to_replace, 8 );
if ( ! empty( $this->args->ID ) ) {
$terms = get_the_terms( $this->args->ID, $tax );
if ( is_array( $terms ) && $terms !== [] ) {
$term = current( $terms );
$term_desc = get_term_field( 'description', $term->term_id, $tax );
if ( $term_desc !== '' ) {
$replacement = wp_strip_all_tags( $term_desc );
}
}
}
}
return $replacement;
}
/**
* Retrieve the current date for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string The formatted current date.
*/
private function retrieve_currentdate() {
static $replacement;
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( get_option( 'date_format' ) );
}
return $replacement;
}
/**
* Retrieve the current day for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string The current day.
*/
private function retrieve_currentday() {
static $replacement;
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( 'j' );
}
return $replacement;
}
/**
* Retrieve the current month for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string The current month.
*/
private function retrieve_currentmonth() {
static $replacement;
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( 'F' );
}
return $replacement;
}
/**
* Retrieve the current time for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string The formatted current time.
*/
private function retrieve_currenttime() {
static $replacement;
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( get_option( 'time_format' ) );
}
return $replacement;
}
/**
* Retrieve the current year for use as replacement string.
*
* The `$replacement` variable is static because it doesn't change depending
* on the context. See https://github.com/Yoast/wordpress-seo/pull/1172#issuecomment-46019482.
*
* @return string The current year.
*/
private function retrieve_currentyear() {
static $replacement;
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( 'Y' );
}
return $replacement;
}
/**
* Retrieve the post/page/cpt's focus keyword for use as replacement string.
*
* @return string|null
*/
private function retrieve_focuskw() {
// Retrieve focuskw from a Post.
if ( ! empty( $this->args->ID ) ) {
$focus_kw = WPSEO_Meta::get_value( 'focuskw', $this->args->ID );
if ( $focus_kw !== '' ) {
return $focus_kw;
}
return null;
}
// Retrieve focuskw from a Term.
if ( ! empty( $this->args->term_id ) ) {
$focus_kw = WPSEO_Taxonomy_Meta::get_term_meta( $this->args->term_id, $this->args->taxonomy, 'focuskw' );
if ( $focus_kw !== '' ) {
return $focus_kw;
}
}
return null;
}
/**
* Retrieve the post/page/cpt ID for use as replacement string.
*
* @return string|null
*/
private function retrieve_id() {
$replacement = null;
if ( ! empty( $this->args->ID ) ) {
// The post/page/cpt ID is an integer, let's cast to string.
$replacement = (string) $this->args->ID;
}
return $replacement;
}
/**
* Retrieve the post/page/cpt modified time for use as replacement string.
*
* @return string|null
*/
private function retrieve_modified() {
$replacement = null;
if ( ! empty( $this->args->post_modified ) ) {
$replacement = YoastSEO()->helpers->date->format_translated( $this->args->post_modified, get_option( 'date_format' ) );
}
return $replacement;
}
/**
* Retrieve the post/page/cpt author's "nice name" for use as replacement string.
*
* @return string|null
*/
private function retrieve_name() {
$replacement = null;
$user_id = (int) $this->retrieve_userid();
$name = get_the_author_meta( 'display_name', $user_id );
if ( $name !== '' ) {
$replacement = $name;
}
return $replacement;
}
/**
* Retrieve the post/page/cpt author's users description for use as a replacement string.
*
* @return string|null
*/
private function retrieve_user_description() {
$replacement = null;
$user_id = (int) $this->retrieve_userid();
$description = get_the_author_meta( 'description', $user_id );
if ( $description !== '' ) {
$replacement = $description;
}
return $replacement;
}
/**
* Retrieve the current page number with context (i.e. 'page 2 of 4') for use as replacement string.
*
* @return string
*/
private function retrieve_page() {
$replacement = null;
$max = $this->determine_pagenumbering( 'max' );
$nr = $this->determine_pagenumbering( 'nr' );
$sep = $this->retrieve_sep();
if ( $max > 1 && $nr > 1 ) {
/* translators: 1: current page number, 2: total number of pages. */
$replacement = sprintf( $sep . ' ' . __( 'Page %1$d of %2$d', 'wordpress-seo' ), $nr, $max );
}
return $replacement;
}
/**
* Retrieve the current page number for use as replacement string.
*
* @return string|null
*/
private function retrieve_pagenumber() {
$replacement = null;
$nr = $this->determine_pagenumbering( 'nr' );
if ( isset( $nr ) && $nr > 0 ) {
$replacement = (string) $nr;
}
return $replacement;
}
/**
* Retrieve the current page total for use as replacement string.
*
* @return string|null
*/
private function retrieve_pagetotal() {
$replacement = null;
$max = $this->determine_pagenumbering( 'max' );
if ( isset( $max ) && $max > 0 ) {
$replacement = (string) $max;
}
return $replacement;
}
/**
* Retrieve the post type plural label for use as replacement string.
*
* @return string|null
*/
private function retrieve_pt_plural() {
$replacement = null;
$name = $this->determine_pt_names( 'plural' );
if ( isset( $name ) && $name !== '' ) {
$replacement = $name;
}
return $replacement;
}
/**
* Retrieve the post type single label for use as replacement string.
*
* @return string|null
*/
private function retrieve_pt_single() {
$replacement = null;
$name = $this->determine_pt_names( 'single' );
if ( isset( $name ) && $name !== '' ) {
$replacement = $name;
}
return $replacement;
}
/**
* Retrieve the slug which caused the 404 for use as replacement string.
*
* @return string|null
*/
private function retrieve_term404() {
$replacement = null;
if ( $this->args->term404 !== '' ) {
$replacement = sanitize_text_field( str_replace( '-', ' ', $this->args->term404 ) );
}
else {
$error_request = get_query_var( 'pagename' );
if ( $error_request !== '' ) {
$replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
}
else {
$error_request = get_query_var( 'name' );
if ( $error_request !== '' ) {
$replacement = sanitize_text_field( str_replace( '-', ' ', $error_request ) );
}
}
}
return $replacement;
}
/**
* Retrieve the post/page/cpt author's user id for use as replacement string.
*
* @return string
*/
private function retrieve_userid() {
// The user ID is an integer, let's cast to string.
$replacement = ! empty( $this->args->post_author ) ? (string) $this->args->post_author : (string) get_query_var( 'author' );
return $replacement;
}
/**
* Retrieve the post/page/cpt's published year for use as replacement string.
*
* @return string|null
*/
private function retrieve_post_year() {
if ( empty( $this->args->ID ) ) {
return null;
}
return get_the_date( 'Y', $this->args->ID );
}
/**
* Retrieve the post/page/cpt's published month for use as replacement string.
*
* @return string|null
*/
private function retrieve_post_month() {
if ( empty( $this->args->ID ) ) {
return null;
}
return get_the_date( 'F', $this->args->ID );
}
/**
* Retrieve the post/page/cpt's published day for use as replacement string.
*
* @return string|null
*/
private function retrieve_post_day() {
if ( empty( $this->args->ID ) ) {
return null;
}
return get_the_date( 'd', $this->args->ID );
}
/**
* Retrieve the post/page/cpt author's first name for use as replacement string.
*
* @return string|null
*/
private function retrieve_author_first_name() {
$replacement = null;
$user_id = (int) $this->retrieve_userid();
$name = get_the_author_meta( 'first_name', $user_id );
if ( $name !== '' ) {
$replacement = $name;
}
return $replacement;
}
/**
* Retrieve the post/page/cpt author's last name for use as replacement string.
*
* @return string|null
*/
private function retrieve_author_last_name() {
$replacement = null;
$user_id = (int) $this->retrieve_userid();
$name = get_the_author_meta( 'last_name', $user_id );
if ( $name !== '' ) {
$replacement = $name;
}
return $replacement;
}
/**
* Retrieve the post/page/cpt permalink for use as replacement string.
*
* @return string|null
*/
private function retrieve_permalink() {
if ( empty( $this->args->ID ) ) {
return null;
}
return get_permalink( $this->args->ID );
}
/**
* Retrieve the post/page/cpt content for use as replacement string.
*
* @return string|null
*/
private function retrieve_post_content() {
$replacement = null;
// The check `post_password_required` is because content must be hidden for a post with a password.
if ( ! empty( $this->args->ID ) && $this->args->post_content !== '' && ! post_password_required( $this->args->ID ) ) {
$content = strip_shortcodes( $this->args->post_content );
$replacement = wp_strip_all_tags( $content );
}
return $replacement;
}
/**
* Retrieve the current or first category title. To be used for import data from AIOSEO.
* The code derives from AIOSEO's way of dealing with that var, so we can ensure 100% seamless transition.
*
* @return string|null
*/
private function retrieve_category_title() {
if ( empty( $this->args ) || empty( $this->args->ID ) ) {
return null;
}
$post_id = $this->args->ID;
$post = get_post( $post_id );
$taxonomies = get_object_taxonomies( $post, 'objects' );
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
if ( ! $taxonomy->hierarchical ) {
continue;
}
$post_terms = get_the_terms( $post_id, $taxonomy_slug );
if ( is_array( $post_terms ) && count( $post_terms ) > 0 ) {
// AiOSEO takes the name of whatever the first hierarchical taxonomy is.
$term = $post_terms[0];
if ( $term ) {
return $term->name;
}
}
}
return null;
}
/* *********************** HELP TEXT RELATED ************************** */
/**
* Set the help text for a user/plugin/theme defined extra variable.
*
* @param string $type Type of variable: 'basic' or 'advanced'.
* @param WPSEO_Replacement_Variable $replacement_variable The replacement variable to register.
*
* @return void
*/
private static function register_help_text( $type, WPSEO_Replacement_Variable $replacement_variable ) {
$identifier = $replacement_variable->get_variable();
if ( ( is_string( $type ) && in_array( $type, [ 'basic', 'advanced' ], true ) )
&& ( $identifier !== '' && ! isset( self::$help_texts[ $type ][ $identifier ] ) )
) {
self::$help_texts[ $type ][ $identifier ] = $replacement_variable;
}
}
/**
* Generates a list of replacement variables based on the help texts.
*
* @return array List of replace vars.
*/
public function get_replacement_variables_with_labels() {
self::setup_statics_once();
$custom_variables = [];
foreach ( array_merge( WPSEO_Custom_Fields::get_custom_fields(), WPSEO_Custom_Taxonomies::get_custom_taxonomies() ) as $custom_variable ) {
$custom_variables[ $custom_variable ] = new WPSEO_Replacement_Variable( $custom_variable, $this->get_label( $custom_variable ), '' );
}
$replacement_variables = array_filter(
array_merge( self::$help_texts['basic'], self::$help_texts['advanced'] ),
[ $this, 'is_not_prefixed' ],
ARRAY_FILTER_USE_KEY
);
$hidden = $this->get_hidden_replace_vars();
return array_values(
array_map(
static function ( WPSEO_Replacement_Variable $replacement_variable ) use ( $hidden ) {
$name = $replacement_variable->get_variable();
return [
'name' => $name,
'value' => '',
'label' => $replacement_variable->get_label(),
'hidden' => in_array( $name, $hidden, true ),
];
},
array_merge( $replacement_variables, $custom_variables )
)
);
}
/**
* Generates a list of replacement variables based on the help texts.
*
* @return array List of replace vars.
*/
public function get_replacement_variables_list() {
self::setup_statics_once();
$replacement_variables = array_merge(
$this->get_replacement_variables(),
WPSEO_Custom_Fields::get_custom_fields(),
WPSEO_Custom_Taxonomies::get_custom_taxonomies()
);
return array_map( [ $this, 'format_replacement_variable' ], $replacement_variables );
}
/**
* Creates a merged associative array of both the basic and advanced help texts.
*
* @return array Array with the replacement variables.
*/
private function get_replacement_variables() {
$help_texts = array_merge( self::$help_texts['basic'], self::$help_texts['advanced'] );
return array_filter( array_keys( $help_texts ), [ $this, 'is_not_prefixed' ] );
}
/**
* Checks whether the replacement variable contains a `ct_` or `cf_` prefix, because they follow different logic.
*
* @param string $replacement_variable The replacement variable.
*
* @return bool True when the replacement variable is not prefixed.
*/
private function is_not_prefixed( $replacement_variable ) {
$prefixes = [ 'cf_', 'ct_' ];
$prefix = $this->get_prefix( $replacement_variable );
return ! in_array( $prefix, $prefixes, true );
}
/**
* Strip the prefix from a replacement variable name.
*
* @param string $replacement_variable The replacement variable.
*
* @return string The replacement variable name without the prefix.
*/
private function strip_prefix( $replacement_variable ) {
return substr( $replacement_variable, 3 );
}
/**
* Gets the prefix from a replacement variable name.
*
* @param string $replacement_variable The replacement variable.
*
* @return string The prefix of the replacement variable.
*/
private function get_prefix( $replacement_variable ) {
return substr( $replacement_variable, 0, 3 );
}
/**
* Strips 'desc_' if present, and appends ' description' at the end.
*
* @param string $label The replacement variable.
*
* @return string The altered replacement variable name.
*/
private function handle_description( $label ) {
if ( strpos( $label, 'desc_' ) === 0 ) {
return substr( $label, 5 ) . ' description';
}
return $label;
}
/**
* Creates a label for prefixed replacement variables that matches the format in the editors.
*
* @param string $replacement_variable The replacement variable.
*
* @return string The replacement variable label.
*/
private function get_label( $replacement_variable ) {
$prefix = $this->get_prefix( $replacement_variable );
if ( $prefix === 'cf_' ) {
return $this->strip_prefix( $replacement_variable ) . ' (custom field)';
}
if ( $prefix === 'ct_' ) {
$label = $this->strip_prefix( $replacement_variable );
$label = $this->handle_description( $label );
return ucfirst( $label . ' (custom taxonomy)' );
}
if ( $prefix === 'pt_' ) {
if ( $replacement_variable === 'pt_single' ) {
return 'Post type (singular)';
}
return 'Post type (plural)';
}
return '';
}
/**
* Formats the replacement variables.
*
* @param string $replacement_variable The replacement variable to format.
*
* @return array The formatted replacement variable.
*/
private function format_replacement_variable( $replacement_variable ) {
return [
'name' => $replacement_variable,
'value' => '',
'label' => $this->get_label( $replacement_variable ),
];
}
/**
* Set/translate the help texts for the WPSEO standard basic variables.
*
* @return void
*/
private static function set_basic_help_texts() {
/* translators: %s: wp_title() function. */
$separator_description = __( 'The separator defined in your theme\'s %s tag.', 'wordpress-seo' );
$separator_description = sprintf(
$separator_description,
// '<code>wp_title()</code>'
'wp_title()'
);
$replacement_variables = [
new WPSEO_Replacement_Variable( 'date', __( 'Date', 'wordpress-seo' ), __( 'Replaced with the date of the post/page', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'title', __( 'Title', 'wordpress-seo' ), __( 'Replaced with the title of the post/page', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'parent_title', __( 'Parent title', 'wordpress-seo' ), __( 'Replaced with the title of the parent page of the current page', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'archive_title', __( 'Archive title', 'wordpress-seo' ), __( 'Replaced with the normal title for an archive generated by WordPress', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'sitename', __( 'Site title', 'wordpress-seo' ), __( 'The site\'s name', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'sitedesc', __( 'Tagline', 'wordpress-seo' ), __( 'The site\'s tagline', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'excerpt', __( 'Excerpt', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (or auto-generated if it does not exist)', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'excerpt_only', __( 'Excerpt only', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (without auto-generation)', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'tag', __( 'Tag', 'wordpress-seo' ), __( 'Replaced with the current tag/tags', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'category', __( 'Category', 'wordpress-seo' ), __( 'Replaced with the post categories (comma separated)', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'primary_category', __( 'Primary category', 'wordpress-seo' ), __( 'Replaced with the primary category of the post/page', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'category_description', __( 'Category description', 'wordpress-seo' ), __( 'Replaced with the category description', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'tag_description', __( 'Tag description', 'wordpress-seo' ), __( 'Replaced with the tag description', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'term_description', __( 'Term description', 'wordpress-seo' ), __( 'Replaced with the term description', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'term_title', __( 'Term title', 'wordpress-seo' ), __( 'Replaced with the term name', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'searchphrase', __( 'Search phrase', 'wordpress-seo' ), __( 'Replaced with the current search phrase', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'term_hierarchy', __( 'Term hierarchy', 'wordpress-seo' ), __( 'Replaced with the term ancestors hierarchy', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'sep', __( 'Separator', 'wordpress-seo' ), $separator_description ),
new WPSEO_Replacement_Variable( 'currentdate', __( 'Current date', 'wordpress-seo' ), __( 'Replaced with the current date', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'currentyear', __( 'Current year', 'wordpress-seo' ), __( 'Replaced with the current year', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'currentmonth', __( 'Current month', 'wordpress-seo' ), __( 'Replaced with the current month', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'currentday', __( 'Current day', 'wordpress-seo' ), __( 'Replaced with the current day', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'post_year', __( 'Post year', 'wordpress-seo' ), __( 'Replaced with the year the post was published', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'post_month', __( 'Post month', 'wordpress-seo' ), __( 'Replaced with the month the post was published', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'post_day', __( 'Post day', 'wordpress-seo' ), __( 'Replaced with the day the post was published', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'author_first_name', __( 'Author first name', 'wordpress-seo' ), __( 'Replaced with the first name of the author', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'author_last_name', __( 'Author last name', 'wordpress-seo' ), __( 'Replaced with the last name of the author', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'permalink', __( 'Permalink', 'wordpress-seo' ), __( 'Replaced with the permalink', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'post_content', __( 'Post Content', 'wordpress-seo' ), __( 'Replaced with the post content', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'category_title', __( 'Category Title', 'wordpress-seo' ), __( 'Current or first category title', 'wordpress-seo' ) ),
];
foreach ( $replacement_variables as $replacement_variable ) {
self::register_help_text( 'basic', $replacement_variable );
}
}
/**
* Set/translate the help texts for the WPSEO standard advanced variables.
*
* @return void
*/
private static function set_advanced_help_texts() {
$replacement_variables = [
new WPSEO_Replacement_Variable( 'pt_single', __( 'Post type (singular)', 'wordpress-seo' ), __( 'Replaced with the content type single label', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'pt_plural', __( 'Post type (plural)', 'wordpress-seo' ), __( 'Replaced with the content type plural label', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'modified', __( 'Modified', 'wordpress-seo' ), __( 'Replaced with the post/page modified time', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'id', __( 'ID', 'wordpress-seo' ), __( 'Replaced with the post/page ID', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'name', __( 'Name', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'nicename\'', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'user_description', __( 'User description', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'Biographical Info\'', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'page', __( 'Page', 'wordpress-seo' ), __( 'Replaced with the current page number with context (i.e. page 2 of 4)', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'pagetotal', __( 'Pagetotal', 'wordpress-seo' ), __( 'Replaced with the current page total', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'pagenumber', __( 'Pagenumber', 'wordpress-seo' ), __( 'Replaced with the current page number', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'caption', __( 'Caption', 'wordpress-seo' ), __( 'Attachment caption', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'focuskw', __( 'Focus keyword', 'wordpress-seo' ), __( 'Replaced with the posts focus keyphrase', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'term404', __( 'Term404', 'wordpress-seo' ), __( 'Replaced with the slug which caused the 404', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'cf_<custom-field-name>', '<custom-field-name> ' . __( '(custom field)', 'wordpress-seo' ), __( 'Replaced with a posts custom field value', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'ct_<custom-tax-name>', '<custom-tax-name> ' . __( '(custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a posts custom taxonomies, comma separated.', 'wordpress-seo' ) ),
new WPSEO_Replacement_Variable( 'ct_desc_<custom-tax-name>', '<custom-tax-name> ' . __( 'description (custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a custom taxonomies description', 'wordpress-seo' ) ),
];
foreach ( $replacement_variables as $replacement_variable ) {
self::register_help_text( 'advanced', $replacement_variable );
}
}
/* *********************** GENERAL HELPER METHODS ************************** */
/**
* Remove the '%%' delimiters from a variable string.
*
* @param string $text Variable string to be cleaned.
*
* @return string
*/
private static function remove_var_delimiter( $text ) {
return trim( $text, '%' );
}
/**
* Add the '%%' delimiters to a variable string.
*
* @param string $text Variable string to be delimited.
*
* @return string
*/
private static function add_var_delimiter( $text ) {
return '%%' . $text . '%%';
}
/**
* Retrieve a post's terms, comma delimited.
*
* @param int $id ID of the post to get the terms for.
* @param string $taxonomy The taxonomy to get the terms for this post from.
* @param bool $return_single If true, return the first term.
*
* @return string Either a single term or a comma delimited string of terms.
*/
public function get_terms( $id, $taxonomy, $return_single = false ) {
$output = '';
// If we're on a specific tag, category or taxonomy page, use that.
if ( ! empty( $this->args->term_id ) ) {
$output = $this->args->name;
}
elseif ( ! empty( $id ) && ! empty( $taxonomy ) ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_array( $terms ) && $terms !== [] ) {
foreach ( $terms as $term ) {
if ( $return_single ) {
$output = $term->name;
break;
}
else {
$output .= $term->name . ', ';
}
}
$output = rtrim( trim( $output ), ',' );
}
}
unset( $terms, $term );
/**
* Allows filtering of the terms list used to replace %%category%%, %%tag%%
* and %%ct_<custom-tax-name>%% variables.
*
* @param string $output Comma-delimited string containing the terms.
* @param string $taxonomy The taxonomy of the terms.
*/
return apply_filters( 'wpseo_terms', $output, $taxonomy );
}
/**
* Gets a taxonomy term hierarchy including the term to get the parents for.
*
* @return string
*/
private function get_term_hierarchy() {
if ( ! is_taxonomy_hierarchical( $this->args->taxonomy ) ) {
return '';
}
$separator = ' ' . $this->retrieve_sep() . ' ';
$args = [
'format' => 'name',
'separator' => $separator,
'link' => false,
'inclusive' => true,
];
return rtrim(
get_term_parents_list( $this->args->term_id, $this->args->taxonomy, $args ),
$separator
);
}
/**
* Retrieves the term ancestors hierarchy.
*
* @return string|null The term ancestors hierarchy.
*/
private function retrieve_term_hierarchy() {
$replacement = null;
if ( ! empty( $this->args->term_id ) && ! empty( $this->args->taxonomy ) ) {
$hierarchy = $this->get_term_hierarchy();
if ( $hierarchy !== '' ) {
$replacement = esc_html( $hierarchy );
}
}
return $replacement;
}
}
PK &MFZ)���� � 3 exceptions/class-myyoast-invalid-json-exception.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals
*/
/**
* Class WPSEO_MyYoast_Invalid_JSON_Exception.
*/
class WPSEO_MyYoast_Invalid_JSON_Exception extends WPSEO_MyYoast_Bad_Request_Exception {
}
PK &MFZ�� � 2 exceptions/class-myyoast-bad-request-exception.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals
*/
/**
* Class WPSEO_MyYoast_Bad_Request_Exception.
*/
class WPSEO_MyYoast_Bad_Request_Exception extends Exception {
}
PK &MFZh����o �o options/class-wpseo-option.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* This abstract class and its 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 as the classes here
* are instantiated, validation for all options and their subkeys will be automatic.
* - On (successful) 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 existence.
* - 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
*/
public 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 conforms to the normal pattern 'yoast' . $option_name . 'options',
* only set in concrete class if it doesn't).
*
* @var string
*/
public $group_name;
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* Also determines which options are copied over for ms_(re)set_blog().
*
* @var bool
*/
public $include_in_all = true;
/**
* Whether this option is only for when the install is multisite.
*
* @var bool
*/
public $multisite_only = false;
/**
* Array of defaults for the option - MUST be set in concrete class.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults;
/**
* Array of variable option name patterns for the option - if any.
*
* Set this when the option contains array keys which vary based on post_type
* or taxonomy.
*
* @var array
*/
protected $variable_array_key_patterns;
/**
* Array of sub-options which should not be overloaded with multi-site defaults.
*
* @var array
*/
public $ms_exclude = [];
/**
* Name for an option higher in the hierarchy to override setting access.
*
* @var string
*/
protected $override_option_name;
/**
* Instance of this class.
*
* @var WPSEO_Option
*/
protected static $instance;
/* *********** INSTANTIATION METHODS *********** */
/**
* Add all the actions and filters for the option.
*/
protected function __construct() {
/* Add filters which get applied to the get_options() results. */
$this->add_default_filters(); // Return defaults if option not set.
$this->add_option_filters(); // Merge with defaults if option *is* set.
if ( $this->multisite_only !== true ) {
/**
* The option validation routines remove the default filters to prevent failing
* to insert an option if it's new. Let's add them back afterwards.
*/
add_action( 'add_option', [ $this, 'add_default_filters_if_same_option' ] ); // Adding back after INSERT.
add_action( 'update_option', [ $this, 'add_default_filters_if_same_option' ] );
add_filter( 'pre_update_option', [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
// Refills the cache when the option has been updated.
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 10 );
}
elseif ( is_multisite() ) {
/*
* The option validation routines remove the default filters to prevent failing
* to insert an option if it's new. Let's add them back afterwards.
*
* For site_options, this method is not foolproof as these actions are not fired
* on an insert/update failure. Please use the WPSEO_Options::update_site_option() method
* for updating site options to make sure the filters are in place.
*/
add_action( 'add_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
add_action( 'update_site_option_' . $this->option_name, [ $this, 'add_default_filters' ] );
add_filter( 'pre_update_site_option_' . $this->option_name, [ $this, 'add_default_filters_if_not_changed' ], PHP_INT_MAX, 3 );
// Refills the cache when the option has been updated.
add_action( 'update_site_option_' . $this->option_name, [ 'WPSEO_Options', 'clear_cache' ], 1, 0 );
}
/*
* Make sure the option will always get validated, independently of register_setting()
* (only available on back-end).
*/
add_filter( 'sanitize_option_' . $this->option_name, [ $this, 'validate' ] );
/* Register our option for the admin pages */
add_action( 'admin_init', [ $this, 'register_setting' ] );
/* Set option group name if not given */
if ( ! isset( $this->group_name ) || $this->group_name === '' ) {
$this->group_name = 'yoast_' . $this->option_name . '_options';
}
/* Translate some defaults as early as possible - textdomain is loaded in init on priority 1. */
if ( method_exists( $this, 'translate_defaults' ) ) {
add_action( 'init', [ $this, 'translate_defaults' ], 2 );
}
/**
* Enrich defaults once custom post types and taxonomies have been registered
* which is normally done on the init action.
*
* @todo [JRF/testers] Verify that none of the options which are only available after
* enrichment are used before the enriching.
*/
if ( method_exists( $this, 'enrich_defaults' ) ) {
add_action( 'init', [ $this, 'enrich_defaults' ], 99 );
}
}
/*
* All concrete classes *must* contain the get_instance method.
*
* {@internal Unfortunately I can't define it as an abstract as it also *has* to be static...}}
*
* ```
* abstract protected static function get_instance();
* ```
* ---------------
*
* Concrete classes *may* contain a translate_defaults method.
* ```
* abstract public function translate_defaults();
* ```
* ---------------
*
* Concrete classes *may* contain an enrich_defaults method to add additional defaults once
* all post_types and taxonomies have been registered.
*
* ```
* abstract public function enrich_defaults();
* ```
*/
/* *********** METHODS INFLUENCING get_option() *********** */
/**
* Add filters to make sure that the option default is returned if the option is not set.
*
* @return void
*/
public function add_default_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
add_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
}
/**
* Adds back the default filters that were removed during validation if the option was changed.
* Checks if this option was changed to prevent constantly checking if filters are present.
*
* @param string $option_name The option name.
*
* @return void
*/
public function add_default_filters_if_same_option( $option_name ) {
if ( $option_name === $this->option_name ) {
$this->add_default_filters();
}
}
/**
* Adds back the default filters that were removed during validation if the option was not changed.
* This is because in that case the latter actions are not called and thus the filters are never
* added back.
*
* @param mixed $value The current value.
* @param string $option_name The option name.
* @param mixed $old_value The old value.
*
* @return string The current value.
*/
public function add_default_filters_if_not_changed( $value, $option_name, $old_value ) {
if ( $option_name !== $this->option_name ) {
return $value;
}
if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
$this->add_default_filters();
}
return $value;
}
/**
* Validate webmaster tools & Pinterest verification strings.
*
* @param string $key Key to check, by type of service.
* @param array $dirty Dirty data with the new values.
* @param array $old Old data.
* @param array $clean Clean data by reference, normally the default values.
*
* @return void
*/
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_-]+$`';
switch ( $key ) {
case 'googleverify':
case 'baiduverify':
$regex = '`^[A-Za-z0-9_-]+$`';
break;
case 'msverify':
case 'pinterestverify':
case 'yandexverify':
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 ];
}
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.
*
* @return void
*/
public function validate_url( $key, $dirty, $old, &$clean ) {
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$submitted_url = trim( $dirty[ $key ] );
$validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );
if ( $validated_url === false ) {
// Restore the previous URL value, if any.
if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
$url = WPSEO_Utils::sanitize_url( $old[ $key ] );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
}
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $submitted_url );
return;
}
// The URL format is valid, let's sanitize it.
$url = WPSEO_Utils::sanitize_url( $validated_url );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
}
}
/**
* Remove the default filters.
* Called from the validate() method to prevent failure to add new options.
*
* @return void
*/
public function remove_default_filters() {
remove_filter( 'default_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
/**
* Get the enriched default value for an option.
*
* Checks if the concrete class contains an enrich_defaults() method and if so, runs it.
*
* {@internal The enrich_defaults method is used to set defaults for variable array keys
* in an option, such as array keys depending on post_types and/or taxonomies.}}
*
* @return array
*/
public function get_defaults() {
if ( method_exists( $this, 'translate_defaults' ) ) {
$this->translate_defaults();
}
if ( method_exists( $this, 'enrich_defaults' ) ) {
$this->enrich_defaults();
}
return apply_filters( 'wpseo_defaults', $this->defaults, $this->option_name );
}
/**
* Add filters to make sure that the option is merged with its defaults before being returned.
*
* @return void
*/
public function add_option_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
add_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
}
}
/**
* Remove the option filters.
* Called from the clean_up methods to make sure we retrieve the original old option.
*
* @return void
*/
public function remove_option_filters() {
remove_filter( 'option_' . $this->option_name, [ $this, 'get_option' ] );
}
/**
* Merge an option with its default values.
*
* This method should *not* be called directly!!! It is only meant to filter the get_option() results.
*
* @param mixed $options Option value.
*
* @return mixed Option merged with the defaults for that option.
*/
public function get_option( $options = null ) {
$filtered = $this->array_filter_merge( $options );
/*
* If the option contains variable option keys, make sure we don't remove those settings
* - even if the defaults are not complete yet.
* Unfortunately this means we also won't be removing the settings for post types or taxonomies
* which are no longer in the WP install, but rather that than the other way around.
*/
if ( isset( $this->variable_array_key_patterns ) ) {
$filtered = $this->retain_variable_keys( $options, $filtered );
}
return $filtered;
}
/* *********** METHODS influencing add_option(), 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 successful.
*/
public function update_site_option( $value ) {
if ( $this->multisite_only === true && is_multisite() ) {
$this->remove_default_filters();
$result = update_site_option( $this->option_name, $value );
$this->add_default_filters();
return $result;
}
else {
return false;
}
}
/**
* Retrieve the real old value (unmerged with defaults), clean and re-save the option.
*
* @uses WPSEO_Option::get_original_option()
* @uses WPSEO_Option::import()
*
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version-specific upgrades will be disregarded.
*
* @return void
*/
public function clean( $current_version = null ) {
$option_value = $this->get_original_option();
$this->import( $option_value, $current_version );
}
/**
* Clean and re-save the option.
*
* @uses clean_option() method from concrete class if it exists.
*
* @todo [JRF/whomever] Figure out a way to show settings error during/after the upgrade - maybe
* something along the lines of:
* -> add them to a property in this class
* -> if that property isset at the end of the routine and add_settings_error function does not exist,
* save as transient (or update the transient if one already exists)
* -> next time an admin is in the WP back-end, show the errors and delete the transient or only delete it
* once the admin has dismissed the message (add ajax function)
* Important: all validation routines which add_settings_errors would need to be changed for this to work
*
* @param array $option_value Option value to be imported.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version-specific upgrades will be disregarded.
* @param array|null $all_old_option_values Optional. Only used when importing old options to
* have access to the real old values, in contrast to
* the saved ones.
*
* @return void
*/
public function import( $option_value, $current_version = null, $all_old_option_values = null ) {
if ( $option_value === false ) {
$option_value = $this->get_defaults();
}
elseif ( is_array( $option_value ) && method_exists( $this, 'clean_option' ) ) {
$option_value = $this->clean_option( $option_value, $current_version, $all_old_option_values );
}
/*
* Save the cleaned value - validation will take care of cleaning out array keys which
* should no longer be there.
*/
if ( $this->multisite_only !== true ) {
update_option( $this->option_name, $option_value );
}
else {
$this->update_site_option( $this->option_name, $option_value );
}
}
/**
* Returns the variable array key patterns for an options class.
*
* @return array
*/
public function get_patterns() {
return (array) $this->variable_array_key_patterns;
}
/**
* Retrieves the option name.
*
* @return string The set option name.
*/
public function get_option_name() {
return $this->option_name;
}
/*
* Concrete classes *may* contain a clean_option method which will clean out old/renamed
* values within the option.
*
* ```
* abstract public function clean_option( $option_value, $current_version = null, $all_old_option_values = null );
* ```
*/
/* *********** HELPER METHODS for internal use. *********** */
/**
* Helper method - Combines a fixed array of default values with an options array
* while filtering out any keys which are not in the defaults array.
*
* @todo [JRF] - shouldn't this be a straight array merge ? at the end of the day, the validation
* removes any invalid keys on save.
*
* @param array|null $options Optional. Current options. If not set, the option defaults
* for the $option_key will be returned.
*
* @return array Combined and filtered options array.
*/
protected function array_filter_merge( $options = null ) {
$defaults = $this->get_defaults();
if ( ! isset( $options ) || $options === false || $options === [] ) {
return $defaults;
}
$options = (array) $options;
/*
$filtered = array();
if ( $defaults !== array() ) {
foreach ( $defaults as $key => $default_value ) {
// @todo should this walk through array subkeys ?
$filtered[ $key ] = ( isset( $options[ $key ] ) ? $options[ $key ] : $default_value );
}
}
*/
$filtered = array_merge( $defaults, $options );
return $filtered;
}
/**
* Sets updated values for variables that are disabled via the override option back to their previous values.
*
* @param array $updated Updated option value.
* @param array $old Old option value.
*
* @return array Updated option value, with all disabled variables set to their old values.
*/
protected function prevent_disabled_options_update( $updated, $old ) {
$override_option = $this->get_override_option();
if ( empty( $override_option ) ) {
return $updated;
}
/*
* This loop could as well call `is_disabled( $key )` for each iteration,
* however this would be worse performance-wise.
*/
foreach ( $old as $key => $value ) {
if ( isset( $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) && ! $override_option[ self::ALLOW_KEY_PREFIX . $key ] ) {
$updated[ $key ] = $old[ $key ];
}
}
return $updated;
}
/**
* Retrieves the value of the override option, if available.
*
* An override option contains values that may determine access to certain sub-variables
* of this option.
*
* Only regular options in multisite can have override options, which in that case
* would be network options.
*
* @return array Override option value, or empty array if unavailable.
*/
protected function get_override_option() {
if ( empty( $this->override_option_name ) || $this->multisite_only === true || ! is_multisite() ) {
return [];
}
return get_site_option( $this->override_option_name, [] );
}
/**
* Make sure that any set option values relating to post_types and/or taxonomies are retained,
* even when that post_type or taxonomy may not yet have been registered.
*
* {@internal The wpseo_titles concrete class overrules this method. Make sure that any
* changes applied here, also get ported to that version.}}
*
* @param array $dirty Original option as retrieved from the database.
* @param array $clean Filtered option where any options which shouldn't be in our option
* have already been removed and any options which weren't set
* have been set to their defaults.
*
* @return array
*/
protected function retain_variable_keys( $dirty, $clean ) {
if ( ( is_array( $this->variable_array_key_patterns ) && $this->variable_array_key_patterns !== [] ) && ( is_array( $dirty ) && $dirty !== [] ) ) {
foreach ( $dirty as $key => $value ) {
// Do nothing if already in filtered options.
if ( isset( $clean[ $key ] ) ) {
continue;
}
foreach ( $this->variable_array_key_patterns as $pattern ) {
if ( strpos( $key, $pattern ) === 0 ) {
$clean[ $key ] = $value;
break;
}
}
}
}
return $clean;
}
/**
* Check whether a given array key conforms to one of the variable array key patterns for this option.
*
* @used-by 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;
}
}
PK &MFZ� �#cE cE % options/class-wpseo-taxonomy-meta.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Option: wpseo_taxonomy_meta.
*/
class WPSEO_Taxonomy_Meta extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_taxonomy_meta';
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* @var bool
*/
public $include_in_all = false;
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* {@internal Important: in contrast to most defaults, the below array format is
* very bare. The real option is in the format [taxonomy_name][term_id][...]
* where [...] is any of the $defaults_per_term options shown below.
* This is of course taken into account in the below methods.}}
*
* @var array
*/
protected $defaults = [];
/**
* Option name - same as $option_name property, but now also available to static methods.
*
* @var string
*/
public static $name;
/**
* Array of defaults for individual taxonomy meta entries.
*
* @var array
*/
public static $defaults_per_term = [
'wpseo_title' => '',
'wpseo_desc' => '',
'wpseo_canonical' => '',
'wpseo_bctitle' => '',
'wpseo_noindex' => 'default',
'wpseo_focuskw' => '',
'wpseo_linkdex' => '',
'wpseo_content_score' => '',
'wpseo_inclusive_language_score' => '',
'wpseo_focuskeywords' => '[]',
'wpseo_keywordsynonyms' => '[]',
'wpseo_is_cornerstone' => '0',
// Social fields.
'wpseo_opengraph-title' => '',
'wpseo_opengraph-description' => '',
'wpseo_opengraph-image' => '',
'wpseo_opengraph-image-id' => '',
'wpseo_twitter-title' => '',
'wpseo_twitter-description' => '',
'wpseo_twitter-image' => '',
'wpseo_twitter-image-id' => '',
];
/**
* Available index options.
*
* Used for form generation and input validation.
*
* {@internal Labels (translation) added on admin_init via WPSEO_Taxonomy::translate_meta_options().}}
*
* @var array
*/
public static $no_index_options = [
'default' => '',
'index' => '',
'noindex' => '',
];
/**
* Add the actions and filters for the option.
*
* @todo [JRF => testers] Check if the extra actions below would run into problems if an option
* is updated early on and if so, change the call to schedule these for a later action on add/update
* instead of running them straight away.
*/
protected function __construct() {
parent::__construct();
self::$name = $this->option_name;
}
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
self::$name = self::$instance->option_name;
}
return self::$instance;
}
/**
* Add extra default options received from a filter.
*
* @return void
*/
public function enrich_defaults() {
$extra_defaults_per_term = apply_filters( 'wpseo_add_extra_taxmeta_term_defaults', [] );
if ( is_array( $extra_defaults_per_term ) ) {
self::$defaults_per_term = array_merge( $extra_defaults_per_term, self::$defaults_per_term );
}
}
/**
* Validate the option.
*
* @param array $dirty New value for the option.
* @param array $clean Clean value for the option, normally the defaults.
* @param array $old Old value of the option.
*
* @return array Validated clean value for the option to be saved to the database.
*/
protected function validate_option( $dirty, $clean, $old ) {
/*
* Prevent complete validation (which can be expensive when there are lots of terms)
* if only one item has changed and has already been validated.
*/
if ( isset( $dirty['wpseo_already_validated'] ) && $dirty['wpseo_already_validated'] === true ) {
unset( $dirty['wpseo_already_validated'] );
return $dirty;
}
foreach ( $dirty as $taxonomy => $terms ) {
/* Don't validate taxonomy - may not be registered yet and we don't want to remove valid ones. */
if ( is_array( $terms ) && $terms !== [] ) {
foreach ( $terms as $term_id => $meta_data ) {
/* Only validate term if the taxonomy exists. */
if ( taxonomy_exists( $taxonomy ) && get_term_by( 'id', $term_id, $taxonomy ) === false ) {
/* Is this term id a special case ? */
if ( has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
$clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
}
continue;
}
if ( is_array( $meta_data ) && $meta_data !== [] ) {
/* Validate meta data. */
$old_meta = self::get_term_meta( $term_id, $taxonomy );
$meta_data = self::validate_term_meta_data( $meta_data, $old_meta );
if ( $meta_data !== [] ) {
$clean[ $taxonomy ][ $term_id ] = $meta_data;
}
}
// Deal with special cases (for when taxonomy doesn't exist yet).
if ( ! isset( $clean[ $taxonomy ][ $term_id ] ) && has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
$clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
}
}
}
}
return $clean;
}
/**
* Validate the meta data for one individual term and removes default values (no need to save those).
*
* @param array $meta_data New values.
* @param array $old_meta The original values.
*
* @return array Validated and filtered value.
*/
public static function validate_term_meta_data( $meta_data, $old_meta ) {
$clean = self::$defaults_per_term;
$meta_data = array_map( [ 'WPSEO_Utils', 'trim_recursive' ], $meta_data );
if ( ! is_array( $meta_data ) || $meta_data === [] ) {
return $clean;
}
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'wpseo_noindex':
if ( isset( $meta_data[ $key ] ) ) {
if ( isset( self::$no_index_options[ $meta_data[ $key ] ] ) ) {
$clean[ $key ] = $meta_data[ $key ];
}
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_canonical':
if ( isset( $meta_data[ $key ] ) && $meta_data[ $key ] !== '' ) {
$url = WPSEO_Utils::sanitize_url( $meta_data[ $key ] );
if ( $url !== '' ) {
$clean[ $key ] = $url;
}
unset( $url );
}
break;
case 'wpseo_bctitle':
if ( isset( $meta_data[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $meta_data[ $key ] );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_keywordsynonyms':
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
// The data is stringified JSON. Use `json_decode` and `json_encode` around the sanitation.
$input = json_decode( $meta_data[ $key ], true );
$sanitized = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $input );
$clean[ $key ] = WPSEO_Utils::format_json_encode( $sanitized );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_focuskeywords':
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
// The data is stringified JSON. Use `json_decode` and `json_encode` around the sanitation.
$input = json_decode( $meta_data[ $key ], true );
// This data has two known keys: `keyword` and `score`.
$sanitized = [];
foreach ( $input as $entry ) {
$sanitized[] = [
'keyword' => WPSEO_Utils::sanitize_text_field( $entry['keyword'] ),
'score' => WPSEO_Utils::sanitize_text_field( $entry['score'] ),
];
}
$clean[ $key ] = WPSEO_Utils::format_json_encode( $sanitized );
}
elseif ( isset( $old_meta[ $key ] ) ) {
// Retain old value if field currently not in use.
$clean[ $key ] = $old_meta[ $key ];
}
break;
case 'wpseo_focuskw':
case 'wpseo_title':
case 'wpseo_desc':
case 'wpseo_linkdex':
default:
if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $meta_data[ $key ] );
}
if ( $key === 'wpseo_focuskw' ) {
$search = [
'<',
'>',
'`',
'<',
'>',
'`',
];
$clean[ $key ] = str_replace( $search, '', $clean[ $key ] );
}
break;
}
$clean[ $key ] = apply_filters( 'wpseo_sanitize_tax_meta_' . $key, $clean[ $key ], ( $meta_data[ $key ] ?? null ), ( $old_meta[ $key ] ?? null ) );
}
// Only save the non-default values.
return array_diff_assoc( $clean, self::$defaults_per_term );
}
/**
* Clean a given option value.
* - Convert old option values to new
* - Fixes strings which were escaped (should have been sanitized - escaping is for output)
*
* @param array $option_value Old (not merged with defaults or filtered) option value to
* clean according to the rules for this option.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
* @param array|null $all_old_option_values Optional. Only used when importing old options to have
* access to the real old values, in contrast to the saved ones.
*
* @return array Cleaned option.
*/
protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
/* Clean up old values and remove empty arrays. */
if ( is_array( $option_value ) && $option_value !== [] ) {
foreach ( $option_value as $taxonomy => $terms ) {
if ( is_array( $terms ) && $terms !== [] ) {
foreach ( $terms as $term_id => $meta_data ) {
if ( ! is_array( $meta_data ) || $meta_data === [] ) {
// Remove empty term arrays.
unset( $option_value[ $taxonomy ][ $term_id ] );
}
else {
foreach ( $meta_data as $key => $value ) {
switch ( $key ) {
case 'noindex':
if ( $value === 'on' ) {
// Convert 'on' to 'noindex'.
$option_value[ $taxonomy ][ $term_id ][ $key ] = 'noindex';
}
break;
case 'canonical':
case 'wpseo_bctitle':
case 'wpseo_title':
case 'wpseo_desc':
case 'wpseo_linkdex':
// @todo [JRF => whomever] Needs checking, I don't have example data [JRF].
if ( $value !== '' ) {
// Fix incorrectly saved (encoded) canonical urls and texts.
$option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( stripslashes( $value ), ENT_QUOTES );
}
break;
default:
// @todo [JRF => whomever] Needs checking, I don't have example data [JRF].
if ( $value !== '' ) {
// Fix incorrectly saved (escaped) text strings.
$option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( $value, ENT_QUOTES );
}
break;
}
}
}
}
}
else {
// Remove empty taxonomy arrays.
unset( $option_value[ $taxonomy ] );
}
}
}
return $option_value;
}
/**
* Retrieve a taxonomy term's meta value(s).
*
* @param mixed $term Term to get the meta value for
* either (string) term name, (int) term id or (object) term.
* @param string $taxonomy Name of the taxonomy to which the term is attached.
* @param string|null $meta Optional. Meta value to get (without prefix).
*
* @return mixed Value for the $meta if one is given, might be the default.
* If no meta is given, an array of all the meta data for the term.
* False if the term does not exist or the $meta provided is invalid.
*/
public static function get_term_meta( $term, $taxonomy, $meta = null ) {
/* Figure out the term id. */
if ( is_int( $term ) ) {
$term = get_term_by( 'id', $term, $taxonomy );
}
elseif ( is_string( $term ) ) {
$term = get_term_by( 'slug', $term, $taxonomy );
}
if ( is_object( $term ) && isset( $term->term_id ) ) {
$term_id = $term->term_id;
}
else {
return false;
}
$tax_meta = self::get_term_tax_meta( $term_id, $taxonomy );
/*
* Either return the complete array or a single value from it or false if the value does not exist
* (shouldn't happen after merge with defaults, indicates typo in request).
*/
if ( ! isset( $meta ) ) {
return $tax_meta;
}
if ( isset( $tax_meta[ 'wpseo_' . $meta ] ) ) {
return $tax_meta[ 'wpseo_' . $meta ];
}
return false;
}
/**
* Get the current queried object and return the meta value.
*
* @param string $meta The meta field that is needed.
*
* @return mixed
*/
public static function get_meta_without_term( $meta ) {
$term = $GLOBALS['wp_query']->get_queried_object();
if ( ! $term || empty( $term->taxonomy ) ) {
return false;
}
return self::get_term_meta( $term, $term->taxonomy, $meta );
}
/**
* Saving the values for the given term_id.
*
* @param int $term_id ID of the term to save data for.
* @param string $taxonomy The taxonomy the term belongs to.
* @param array $meta_values The values that will be saved.
*
* @return void
*/
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.
*
* @return void
*/
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.
*
* @return void
*/
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.
*
* @return void
*/
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;
}
}
PK &MFZ��g�k$ k$ % options/class-wpseo-option-social.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Option: wpseo_social.
*/
class WPSEO_Option_Social extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_social';
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults = [
// Form fields.
'facebook_site' => '', // Text field.
'instagram_url' => '',
'linkedin_url' => '',
'myspace_url' => '',
'og_default_image' => '', // Text field.
'og_default_image_id' => '',
'og_frontpage_title' => '', // Text field.
'og_frontpage_desc' => '', // Text field.
'og_frontpage_image' => '', // Text field.
'og_frontpage_image_id' => '',
'opengraph' => true,
'pinterest_url' => '',
'pinterestverify' => '',
'twitter' => true,
'twitter_site' => '', // Text field.
'twitter_card_type' => 'summary_large_image',
'youtube_url' => '',
'wikipedia_url' => '',
'other_social_urls' => [],
'mastodon_url' => '',
];
/**
* Array of sub-options which should not be overloaded with multi-site defaults.
*
* @var array
*/
public $ms_exclude = [
/* Privacy. */
'pinterestverify',
];
/**
* Array of allowed twitter card types.
*
* While we only have the options summary and summary_large_image in the
* interface now, we might change that at some point.
*
* {@internal Uncomment any of these to allow them in validation *and* automatically
* add them as a choice in the options page.}}
*
* @var array
*/
public static $twitter_card_types = [
'summary_large_image' => '',
// 'summary' => '',
// 'photo' => '',
// 'gallery' => '',
// 'app' => '',
// 'player' => '',
// 'product' => '',
];
/**
* Add the actions and filters for the option.
*/
protected function __construct() {
parent::__construct();
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
}
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Translate/set strings used in the option defaults.
*
* @return void
*/
public function translate_defaults() {
self::$twitter_card_types['summary_large_image'] = 'Summary with large image';
}
/**
* Validate the option.
*
* @param array $dirty New value for the option.
* @param array $clean Clean value for the option, normally the defaults.
* @param array $old Old value of the option.
*
* @return array Validated clean value for the option to be saved to the database.
*/
protected function validate_option( $dirty, $clean, $old ) {
foreach ( $clean as $key => $value ) {
switch ( $key ) {
/* Text fields. */
case 'og_frontpage_desc':
case 'og_frontpage_title':
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
}
break;
case 'og_default_image_id':
case 'og_frontpage_image_id':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = (int) $dirty[ $key ];
if ( $dirty[ $key ] === '' ) {
$clean[ $key ] = $dirty[ $key ];
}
}
break;
/* URL text fields - no ftp allowed. */
case 'facebook_site':
case 'instagram_url':
case 'linkedin_url':
case 'myspace_url':
case 'pinterest_url':
case 'og_default_image':
case 'og_frontpage_image':
case 'youtube_url':
case 'wikipedia_url':
case 'mastodon_url':
$this->validate_url( $key, $dirty, $old, $clean );
break;
case 'pinterestverify':
$this->validate_verification_string( $key, $dirty, $old, $clean );
break;
/* Twitter user name. */
case 'twitter_site':
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$twitter_id = $this->validate_twitter_id( $dirty[ $key ] );
if ( $twitter_id ) {
$clean[ $key ] = $twitter_id;
}
elseif ( 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;
}
}
unset( $twitter_id );
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] );
}
break;
case 'twitter_card_type':
if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
/* Boolean fields. */
case 'opengraph':
case 'twitter':
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
break;
/* Array fields. */
case 'other_social_urls':
if ( isset( $dirty[ $key ] ) ) {
$items = $dirty[ $key ];
if ( ! is_array( $items ) ) {
$items = json_decode( $dirty[ $key ], true );
}
if ( is_array( $items ) ) {
foreach ( $items as $item_key => $item ) {
$validated_url = $this->validate_social_url( $item );
if ( $validated_url === false ) {
// Restore the previous URL values, if any.
$old_urls = ( isset( $old[ $key ] ) ) ? $old[ $key ] : [];
foreach ( $old_urls as $old_item_key => $old_url ) {
if ( $old_url !== '' ) {
$url = WPSEO_Utils::sanitize_url( $old_url );
if ( $url !== '' ) {
$clean[ $key ][ $old_item_key ] = $url;
}
}
}
break;
}
// The URL format is valid, let's sanitize it.
$url = WPSEO_Utils::sanitize_url( $validated_url );
if ( $url !== '' ) {
$clean[ $key ][ $item_key ] = $url;
}
}
}
}
break;
}
}
return $clean;
}
/**
* Validates a social URL.
*
* @param string $url The url to be validated.
*
* @return string|false The validated URL or false if the URL is not valid.
*/
public function validate_social_url( $url ) {
$validated_url = filter_var( WPSEO_Utils::sanitize_url( trim( $url ) ), FILTER_VALIDATE_URL );
return $validated_url;
}
/**
* Validates a twitter id.
*
* @param string $twitter_id The twitter id to be validated.
* @param bool $strip_at_sign Whether or not to strip the `@` sign.
*
* @return string|false The validated twitter id or false if it is not valid.
*/
public function validate_twitter_id( $twitter_id, $strip_at_sign = true ) {
$twitter_id = ( $strip_at_sign ) ? sanitize_text_field( ltrim( $twitter_id, '@' ) ) : sanitize_text_field( $twitter_id );
/*
* From the Twitter documentation about twitter screen names:
* Typically a maximum of 15 characters long, but some historical accounts
* may exist with longer names.
* A username can only contain alphanumeric characters (letters A-Z, numbers 0-9)
* with the exception of underscores.
*
* @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames
*/
if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
return $twitter_id;
}
if ( preg_match( '`^http(?:s)?://(?:www\.)?(?:twitter|x)\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) {
return $matches['handle'];
}
return false;
}
/**
* Clean a given option value.
*
* @param array $option_value Old (not merged with defaults or filtered) option value to
* clean according to the rules for this option.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
* @param array|null $all_old_option_values Optional. Only used when importing old options to have
* access to the real old values, in contrast to the saved ones.
*
* @return array Cleaned option.
*/
protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
/* Move options from very old option to this one. */
$old_option = null;
if ( isset( $all_old_option_values ) ) {
// Ok, we have an import.
if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== [] ) {
$old_option = $all_old_option_values['wpseo_indexation'];
}
}
else {
$old_option = get_option( 'wpseo_indexation' );
}
if ( is_array( $old_option ) && $old_option !== [] ) {
$move = [
'opengraph',
];
foreach ( $move as $key ) {
if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) {
$option_value[ $key ] = $old_option[ $key ];
}
}
unset( $move, $key );
}
unset( $old_option );
return $option_value;
}
}
PK &MFZNvA �$ �$ ! options/class-wpseo-option-ms.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Site option for Multisite installs only
*
* Overloads a number of methods of the abstract class to ensure the use of the correct site_option
* WP functions.
*/
class WPSEO_Option_MS extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_ms';
/**
* Option group name for use in settings forms.
*
* @var string
*/
public $group_name = 'yoast_wpseo_multisite_options';
/**
* Whether to include the option in the return for WPSEO_Options::get_all().
*
* @var bool
*/
public $include_in_all = false;
/**
* Whether this option is only for when the install is multisite.
*
* @var bool
*/
public $multisite_only = true;
/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array
*/
protected $defaults = [];
/**
* Available options for the 'access' setting. Used for input validation.
*
* {@internal Important: Make sure the options added to the array here are in line
* with the keys for the options set for the select box in the
* admin/pages/network.php file.}}
*
* @var array
*/
public static $allowed_access_options = [
'admin',
'superadmin',
];
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Only run parent constructor in multisite context.
*/
public function __construct() {
$allow_prefix = self::ALLOW_KEY_PREFIX;
$this->defaults = [
'access' => 'admin',
'defaultblog' => '', // Numeric blog ID or empty.
"{$allow_prefix}disableadvanced_meta" => true,
"{$allow_prefix}ryte_indexability" => false,
"{$allow_prefix}content_analysis_active" => true,
"{$allow_prefix}keyword_analysis_active" => true,
"{$allow_prefix}inclusive_language_analysis_active" => true,
"{$allow_prefix}enable_admin_bar_menu" => true,
"{$allow_prefix}enable_cornerstone_content" => true,
"{$allow_prefix}enable_xml_sitemap" => true,
"{$allow_prefix}enable_text_link_counter" => true,
"{$allow_prefix}enable_headless_rest_endpoints" => true,
"{$allow_prefix}enable_metabox_insights" => true,
"{$allow_prefix}enable_link_suggestions" => true,
"{$allow_prefix}tracking" => true,
"{$allow_prefix}enable_enhanced_slack_sharing" => true,
"{$allow_prefix}semrush_integration_active" => true,
"{$allow_prefix}wincher_integration_active" => false,
"{$allow_prefix}remove_feed_global" => true,
"{$allow_prefix}remove_feed_global_comments" => true,
"{$allow_prefix}remove_feed_post_comments" => true,
"{$allow_prefix}enable_index_now" => true,
"{$allow_prefix}enable_ai_generator" => true,
"{$allow_prefix}remove_feed_authors" => true,
"{$allow_prefix}remove_feed_categories" => true,
"{$allow_prefix}remove_feed_tags" => true,
"{$allow_prefix}remove_feed_custom_taxonomies" => true,
"{$allow_prefix}remove_feed_post_types" => true,
"{$allow_prefix}remove_feed_search" => true,
"{$allow_prefix}remove_atom_rdf_feeds" => true,
"{$allow_prefix}remove_shortlinks" => true,
"{$allow_prefix}remove_rest_api_links" => true,
"{$allow_prefix}remove_rsd_wlw_links" => true,
"{$allow_prefix}remove_oembed_links" => true,
"{$allow_prefix}remove_generator" => true,
"{$allow_prefix}remove_emoji_scripts" => true,
"{$allow_prefix}remove_powered_by_header" => true,
"{$allow_prefix}remove_pingback_header" => true,
"{$allow_prefix}clean_campaign_tracking_urls" => true,
"{$allow_prefix}clean_permalinks" => true,
"{$allow_prefix}search_cleanup" => true,
"{$allow_prefix}search_cleanup_emoji" => true,
"{$allow_prefix}search_cleanup_patterns" => true,
"{$allow_prefix}redirect_search_pretty_urls" => true,
"{$allow_prefix}algolia_integration_active" => true,
];
if ( is_multisite() ) {
parent::__construct();
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
}
}
/**
* Add filters to make sure that the option default is returned if the option is not set
*
* @return void
*/
public function add_default_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
add_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
}
/**
* Remove the default filters.
* Called from the validate() method to prevent failure to add new options.
*
* @return void
*/
public function remove_default_filters() {
remove_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
}
/**
* Add filters to make sure that the option is merged with its defaults before being returned.
*
* @return void
*/
public function add_option_filters() {
// Don't change, needs to check for false as could return prio 0 which would evaluate to false.
if ( has_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
add_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
}
}
/**
* Remove the option filters.
* Called from the clean_up methods to make sure we retrieve the original old option.
*
* @return void
*/
public function remove_option_filters() {
remove_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
}
/* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
/**
* Validate the option.
*
* @param array $dirty New value for the option.
* @param array $clean Clean value for the option, normally the defaults.
* @param array $old Old value of the option.
*
* @return array Validated clean value for the option to be saved to the database.
*/
protected function validate_option( $dirty, $clean, $old ) {
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'access':
if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$allowed_access_options, true ) ) {
$clean[ $key ] = $dirty[ $key ];
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
/* translators: %1$s expands to the option name and %2$sexpands to Yoast SEO */
sprintf( __( '%1$s is not a valid choice for who should be allowed access to the %2$s settings. Value reset to the default.', 'wordpress-seo' ), esc_html( sanitize_text_field( $dirty[ $key ] ) ), 'Yoast SEO' ), // The error message.
'error' // Message type.
);
}
break;
case 'defaultblog':
if ( isset( $dirty[ $key ] ) && ( $dirty[ $key ] !== '' && $dirty[ $key ] !== '-' ) ) {
$int = WPSEO_Utils::validate_int( $dirty[ $key ] );
if ( $int !== false && $int > 0 ) {
// Check if a valid blog number has been received.
$exists = get_blog_details( $int, false );
if ( $exists && $exists->deleted === '0' ) {
$clean[ $key ] = $int;
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' )
. '<br>'
. sprintf(
/* translators: %s is the ID number of a blog. */
esc_html__( 'This must be an existing blog. Blog %s does not exist or has been marked as deleted.', 'wordpress-seo' ),
'<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
), // The error message.
'error' // Message type.
);
}
unset( $exists );
}
elseif ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
$this->group_name, // Slug title of the setting.
$key, // Suffix-ID for the error message box.
esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . esc_html__( 'No numeric value was received.', 'wordpress-seo' ), // The error message.
'error' // Message type.
);
}
unset( $int );
}
break;
default:
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
break;
}
}
return $clean;
}
}
PK &MFZ�U� D D options/class-wpseo-options.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Overall Option Management class.
*
* Instantiates all the options and offers a number of utility methods to work with the options.
*/
class WPSEO_Options {
/**
* The option values.
*
* @var array|null
*/
protected static $option_values = null;
/**
* Options this class uses.
*
* @var array Array format: (string) option_name => (string) name of concrete class for the option.
*/
public static $options = [
'wpseo' => 'WPSEO_Option_Wpseo',
'wpseo_titles' => 'WPSEO_Option_Titles',
'wpseo_social' => 'WPSEO_Option_Social',
'wpseo_ms' => 'WPSEO_Option_MS',
'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
];
/**
* Array of instantiated option objects.
*
* @var array
*/
protected static $option_instances = [];
/**
* Array with the option names.
*
* @var array
*/
protected static $option_names = [];
/**
* Instance of this class.
*
* @var WPSEO_Options
*/
protected static $instance;
/**
* Instantiate all the WPSEO option management classes.
*/
protected function __construct() {
$this->register_hooks();
foreach ( static::$options as $option_class ) {
static::register_option( call_user_func( [ $option_class, 'get_instance' ] ) );
}
}
/**
* Register our hooks.
*
* @return void
*/
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.
*
* @return void
*/
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.
*
* @param array $option_names 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.
*
* @param array<string> $specific_options The option groups of the option you want to get.
*
* @return array Array combining the values of all the options.
*/
public static function get_all( $specific_options = [] ) {
$option_names = ( empty( $specific_options ) ) ? static::get_option_names() : $specific_options;
static::$option_values = static::get_options( $option_names );
return static::$option_values;
}
/**
* Retrieve one or more options for the SEO plugin.
*
* @param array $option_names An array of option names of the options you want to get.
*
* @return array Array combining the values of the requested options.
*/
public static function get_options( array $option_names ) {
$options = [];
$option_names = array_filter( $option_names, 'is_string' );
foreach ( $option_names as $option_name ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
$option = static::get_option( $option_name );
if ( $option !== null ) {
$options = array_merge( $options, $option );
}
}
}
return $options;
}
/**
* Retrieve a single option for the SEO plugin.
*
* @param string $option_name The name of the option you want to get.
*
* @return array Array containing the requested option.
*/
public static function get_option( $option_name ) {
$option = null;
if ( is_string( $option_name ) && ! empty( $option_name ) ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
if ( static::$option_instances[ $option_name ]->multisite_only !== true ) {
$option = get_option( $option_name );
}
else {
$option = get_site_option( $option_name );
}
}
}
return $option;
}
/**
* Retrieve a single field from any option for the SEO plugin. Keys are always unique.
*
* @param string $key The key it should return.
* @param mixed $default_value The default value that should be returned if the key isn't set.
* @param array<string> $option_groups The option groups to retrieve the option from.
*
* @return mixed Returns value if found, $default_value if not.
*/
public static function get( $key, $default_value = null, $option_groups = [] ) {
if ( ! isset( static::$option_values[ $key ] ) ) {
static::prime_cache( $option_groups );
}
if ( isset( static::$option_values[ $key ] ) ) {
return static::$option_values[ $key ];
}
return $default_value;
}
/**
* Resets the cache to null.
*
* @return void
*/
public static function clear_cache() {
static::$option_values = null;
}
/**
* Primes our cache.
*
* @param array<string> $option_groups The option groups to prime the cache with.
*
* @return void
*/
private static function prime_cache( $option_groups = [] ) {
static::$option_values = static::get_all( $option_groups );
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.
* @param string $option_group The lookup table which represents the option_group where the key is stored.
*
* @return mixed|null Returns value if found, $default if not.
*/
public static function set( $key, $value, $option_group = '' ) {
$lookup_table = static::get_lookup_table( $option_group );
if ( isset( $lookup_table[ $key ] ) ) {
return static::save_option( $lookup_table[ $key ], $key, $value );
}
$patterns = static::get_pattern_table();
foreach ( $patterns as $pattern => $option ) {
if ( strpos( $key, $pattern ) === 0 ) {
return static::save_option( $option, $key, $value );
}
}
static::$option_values[ $key ] = $value;
}
/**
* Get an option only if it's been auto-loaded.
*
* @param string $option The option to retrieve.
* @param mixed $default_value A default value to return.
*
* @return mixed
*/
public static function get_autoloaded_option( $option, $default_value = false ) {
$value = wp_cache_get( $option, 'options' );
if ( $value === false ) {
$passed_default = func_num_args() > 1;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
}
/**
* Run the clean up routine for one or all options.
*
* @param array|string|null $option_name Optional. the option you want to clean or an array of
* option names for the options you want to clean.
* If not set, all options will be cleaned.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
*
* @return void
*/
public static function clean_up( $option_name = null, $current_version = null ) {
if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
if ( isset( static::$option_instances[ $option_name ] ) ) {
static::$option_instances[ $option_name ]->clean( $current_version );
}
}
elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== [] ) {
foreach ( $option_name as $option ) {
if ( isset( static::$option_instances[ $option ] ) ) {
static::$option_instances[ $option ]->clean( $current_version );
}
}
unset( $option );
}
else {
foreach ( static::$option_instances as $instance ) {
$instance->clean( $current_version );
}
unset( $instance );
// If we've done a full clean-up, we can safely remove this really old option.
delete_option( 'wpseo_indexation' );
}
}
/**
* Check that all options exist in the database and add any which don't.
*
* @return void
*/
public static function ensure_options_exist() {
foreach ( static::$option_instances as $instance ) {
$instance->maybe_add_option();
}
}
/**
* Initialize some options on first install/activate/reset.
*
* @return void
*/
public static function initialize() {
/* Force WooThemes to use Yoast SEO data. */
if ( function_exists( 'woo_version_init' ) ) {
update_option( 'seo_woo_use_third_party_data', 'true' );
}
}
/**
* Reset all options to their default values and rerun some tests.
*
* @return void
*/
public static function reset() {
if ( ! is_multisite() ) {
$option_names = static::get_option_names();
if ( is_array( $option_names ) && $option_names !== [] ) {
foreach ( $option_names as $option_name ) {
delete_option( $option_name );
update_option( $option_name, get_option( $option_name ) );
}
}
unset( $option_names );
}
else {
// Reset MS blog based on network default blog setting.
static::reset_ms_blog( get_current_blog_id() );
}
static::initialize();
}
/**
* Initialize default values for a new multisite blog.
*
* @param bool $force_init Whether to always do the initialization routine (title/desc test).
*
* @return void
*/
public static function maybe_set_multisite_defaults( $force_init = false ) {
$option = get_option( 'wpseo' );
if ( is_multisite() ) {
if ( $option['ms_defaults_set'] === false ) {
static::reset_ms_blog( get_current_blog_id() );
static::initialize();
}
elseif ( $force_init === true ) {
static::initialize();
}
}
}
/**
* Reset all options for a specific multisite blog to their default values based upon a
* specified default blog if one was chosen on the network page or the plugin defaults if it was not.
*
* @param int|string $blog_id Blog id of the blog for which to reset the options.
*
* @return void
*/
public static function reset_ms_blog( $blog_id ) {
if ( is_multisite() ) {
$options = get_site_option( 'wpseo_ms' );
$option_names = static::get_option_names();
if ( is_array( $option_names ) && $option_names !== [] ) {
$base_blog_id = $blog_id;
if ( $options['defaultblog'] !== '' && $options['defaultblog'] !== 0 ) {
$base_blog_id = $options['defaultblog'];
}
foreach ( $option_names as $option_name ) {
delete_blog_option( $blog_id, $option_name );
$new_option = get_blog_option( $base_blog_id, $option_name );
/* Remove sensitive, theme dependent and site dependent info. */
if ( isset( static::$option_instances[ $option_name ] ) && static::$option_instances[ $option_name ]->ms_exclude !== [] ) {
foreach ( static::$option_instances[ $option_name ]->ms_exclude as $key ) {
unset( $new_option[ $key ] );
}
}
if ( $option_name === 'wpseo' ) {
$new_option['ms_defaults_set'] = true;
}
update_blog_option( $blog_id, $option_name, $new_option );
}
}
}
}
/**
* Saves the option to the database.
*
* @param string $wpseo_options_group_name The name for the wpseo option group in the database.
* @param string $option_name The name for the option to set.
* @param mixed $option_value The value for the option.
*
* @return bool Returns true if the option is successfully saved in the database.
*/
public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) {
$options = static::get_option( $wpseo_options_group_name );
$options[ $option_name ] = $option_value;
if ( isset( static::$option_instances[ $wpseo_options_group_name ] ) && static::$option_instances[ $wpseo_options_group_name ]->multisite_only === true ) {
static::update_site_option( $wpseo_options_group_name, $options );
}
else {
update_option( $wpseo_options_group_name, $options );
}
// Check if everything got saved properly.
$saved_option = static::get_option( $wpseo_options_group_name );
// Clear our cache.
static::clear_cache();
return $saved_option[ $option_name ] === $options[ $option_name ];
}
/**
* Adds the multisite options to the option stack if relevant.
*
* @param array $option The currently present options settings.
*
* @return array Options possibly including multisite.
*/
protected static function add_ms_option( $option ) {
if ( ! is_multisite() ) {
return $option;
}
$ms_option = static::get_option( 'wpseo_ms' );
if ( $ms_option === null ) {
return $option;
}
return array_merge( $option, $ms_option );
}
/**
* Checks if installation is multisite.
*
* @return bool True when is multisite.
*/
protected static function is_multisite() {
static $is_multisite;
if ( $is_multisite === null ) {
$is_multisite = is_multisite();
}
return $is_multisite;
}
/**
* Retrieves a lookup table to find in which option_group a key is stored.
*
* @param string $option_group The option_group where the key is stored.
*
* @return array The lookup table.
*/
private static function get_lookup_table( $option_group = '' ) {
$lookup_table = [];
$option_groups = ( $option_group === '' ) ? static::$options : [ $option_group => static::$options[ $option_group ] ];
foreach ( array_keys( $option_groups ) 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;
}
}
PK &MFZ����"T "T $ options/class-wpseo-option-wpseo.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
/**
* Option: wpseo.
*/
class WPSEO_Option_Wpseo extends WPSEO_Option {
/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo';
/**
* Array of defaults for the option.
*
* {@internal Shouldn't be requested directly, use $this->get_defaults();}}
*
* @var array
*/
protected $defaults = [
// Non-form fields, set via (ajax) function.
'tracking' => null,
'toggled_tracking' => false,
'license_server_version' => false,
'ms_defaults_set' => false,
'ignore_search_engines_discouraged_notice' => false,
'indexing_first_time' => true,
'indexing_started' => null,
'indexing_reason' => '',
'indexables_indexing_completed' => false,
'index_now_key' => '',
// Non-form field, should only be set via validation routine.
'version' => '', // Leave default as empty to ensure activation/upgrade works.
'previous_version' => '',
// Form fields.
'disableadvanced_meta' => true,
'enable_headless_rest_endpoints' => true,
'ryte_indexability' => false,
'baiduverify' => '', // Text field.
'googleverify' => '', // Text field.
'msverify' => '', // Text field.
'yandexverify' => '',
'site_type' => '', // List of options.
'has_multiple_authors' => '',
'environment_type' => '',
'content_analysis_active' => true,
'keyword_analysis_active' => true,
'inclusive_language_analysis_active' => false,
'enable_admin_bar_menu' => true,
'enable_cornerstone_content' => true,
'enable_xml_sitemap' => true,
'enable_text_link_counter' => true,
'enable_index_now' => true,
'enable_ai_generator' => true,
'ai_enabled_pre_default' => false,
'show_onboarding_notice' => false,
'first_activated_on' => false,
'myyoast-oauth' => [
'config' => [
'clientId' => null,
'secret' => null,
],
'access_tokens' => [],
],
'semrush_integration_active' => true,
'semrush_tokens' => [],
'semrush_country_code' => 'us',
'permalink_structure' => '',
'home_url' => '',
'dynamic_permalinks' => false,
'category_base_url' => '',
'tag_base_url' => '',
'custom_taxonomy_slugs' => [],
'enable_enhanced_slack_sharing' => true,
'enable_metabox_insights' => true,
'enable_link_suggestions' => true,
'algolia_integration_active' => false,
'import_cursors' => [],
'workouts_data' => [ 'configuration' => [ 'finishedSteps' => [] ] ],
'configuration_finished_steps' => [],
'dismiss_configuration_workout_notice' => false,
'dismiss_premium_deactivated_notice' => false,
'importing_completed' => [],
'wincher_integration_active' => true,
'wincher_tokens' => [],
'wincher_automatically_add_keyphrases' => false,
'wincher_website_id' => '',
'first_time_install' => false,
'should_redirect_after_install_free' => false,
'activation_redirect_timestamp_free' => 0,
'remove_feed_global' => false,
'remove_feed_global_comments' => false,
'remove_feed_post_comments' => false,
'remove_feed_authors' => false,
'remove_feed_categories' => false,
'remove_feed_tags' => false,
'remove_feed_custom_taxonomies' => false,
'remove_feed_post_types' => false,
'remove_feed_search' => false,
'remove_atom_rdf_feeds' => false,
'remove_shortlinks' => false,
'remove_rest_api_links' => false,
'remove_rsd_wlw_links' => false,
'remove_oembed_links' => false,
'remove_generator' => false,
'remove_emoji_scripts' => false,
'remove_powered_by_header' => false,
'remove_pingback_header' => false,
'clean_campaign_tracking_urls' => false,
'clean_permalinks' => false,
'clean_permalinks_extra_variables' => '',
'search_cleanup' => false,
'search_cleanup_emoji' => false,
'search_cleanup_patterns' => false,
'search_character_limit' => 50,
'deny_search_crawling' => false,
'deny_wp_json_crawling' => false,
'deny_adsbot_crawling' => false,
'deny_ccbot_crawling' => false,
'deny_google_extended_crawling' => false,
'deny_gptbot_crawling' => false,
'redirect_search_pretty_urls' => false,
'least_readability_ignore_list' => [],
'least_seo_score_ignore_list' => [],
'most_linked_ignore_list' => [],
'least_linked_ignore_list' => [],
'indexables_page_reading_list' => [ false, false, false, false, false ],
'indexables_overview_state' => 'dashboard-not-visited',
'last_known_public_post_types' => [],
'last_known_public_taxonomies' => [],
'last_known_no_unindexed' => [],
'new_post_types' => [],
'new_taxonomies' => [],
'show_new_content_type_notification' => false,
];
/**
* Sub-options which should not be overloaded with multi-site defaults.
*
* @var array
*/
public $ms_exclude = [
'ignore_search_engines_discouraged_notice',
/* Privacy. */
'baiduverify',
'googleverify',
'msverify',
'yandexverify',
];
/**
* Possible values for the site_type option.
*
* @var array
*/
protected $site_types = [
'',
'blog',
'shop',
'news',
'smallBusiness',
'corporateOther',
'personalOther',
];
/**
* Possible environment types.
*
* @var array
*/
protected $environment_types = [
'',
'local',
'production',
'staging',
'development',
];
/**
* Possible has_multiple_authors options.
*
* @var array
*/
protected $has_multiple_authors_options = [
'',
true,
false,
];
/**
* Name for an option higher in the hierarchy to override setting access.
*
* @var string
*/
protected $override_option_name = 'wpseo_ms';
/**
* Add the actions and filters for the option.
*
* @todo [JRF => testers] Check if the extra actions below would run into problems if an option
* is updated early on and if so, change the call to schedule these for a later action on add/update
* instead of running them straight away.
*/
protected function __construct() {
parent::__construct();
/**
* Filter: 'wpseo_enable_tracking' - Enables the data tracking of Yoast SEO Premium.
*
* @param string|false $is_enabled The enabled state. Default is false.
*/
$this->defaults['tracking'] = apply_filters( 'wpseo_enable_tracking', false );
/* Clear the cache on update/add. */
add_action( 'add_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_cache' ] );
add_action( 'update_option_' . $this->option_name, [ 'WPSEO_Utils', 'clear_cache' ] );
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
/**
* Filter the `wpseo` option defaults.
*
* @param array $defaults Array the defaults for the `wpseo` option attributes.
*/
$this->defaults = apply_filters( 'wpseo_option_wpseo_defaults', $this->defaults );
}
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Add filters to make sure that the option is merged with its defaults before being returned.
*
* @return void
*/
public function add_option_filters() {
parent::add_option_filters();
list( $hookname, $callback, $priority ) = $this->get_verify_features_option_filter_hook();
if ( has_filter( $hookname, $callback ) === false ) {
add_filter( $hookname, $callback, $priority );
}
}
/**
* Remove the option filters.
* Called from the clean_up methods to make sure we retrieve the original old option.
*
* @return void
*/
public function remove_option_filters() {
parent::remove_option_filters();
list( $hookname, $callback, $priority ) = $this->get_verify_features_option_filter_hook();
remove_filter( $hookname, $callback, $priority );
}
/**
* Add filters to make sure that the option default is returned if the option is not set.
*
* @return void
*/
public function add_default_filters() {
parent::add_default_filters();
list( $hookname, $callback, $priority ) = $this->get_verify_features_default_option_filter_hook();
if ( has_filter( $hookname, $callback ) === false ) {
add_filter( $hookname, $callback, $priority );
}
}
/**
* Remove the default filters.
* Called from the validate() method to prevent failure to add new options.
*
* @return void
*/
public function remove_default_filters() {
parent::remove_default_filters();
list( $hookname, $callback, $priority ) = $this->get_verify_features_default_option_filter_hook();
remove_filter( $hookname, $callback, $priority );
}
/**
* Validate the option.
*
* @param array $dirty New value for the option.
* @param array $clean Clean value for the option, normally the defaults.
* @param array $old Old value of the option.
*
* @return array Validated clean value for the option to be saved to the database.
*/
protected function validate_option( $dirty, $clean, $old ) {
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'version':
$clean[ $key ] = WPSEO_VERSION;
break;
case 'previous_version':
case 'semrush_country_code':
case 'license_server_version':
case 'home_url':
case 'index_now_key':
case 'wincher_website_id':
case 'clean_permalinks_extra_variables':
case 'indexables_overview_state':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
case 'indexing_reason':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = sanitize_text_field( $dirty[ $key ] );
}
break;
/* Verification strings. */
case 'baiduverify':
case 'googleverify':
case 'msverify':
case 'yandexverify':
$this->validate_verification_string( $key, $dirty, $old, $clean );
break;
/*
* Boolean dismiss warnings - not fields - may not be in form
* (and don't need to be either as long as the default is false).
*/
case 'ignore_search_engines_discouraged_notice':
case 'ms_defaults_set':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::validate_bool( $dirty[ $key ] );
}
elseif ( isset( $old[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::validate_bool( $old[ $key ] );
}
break;
case 'site_type':
$clean[ $key ] = $old[ $key ];
if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->site_types, true ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
case 'environment_type':
$clean[ $key ] = $old[ $key ];
if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->environment_types, true ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
case 'has_multiple_authors':
$clean[ $key ] = $old[ $key ];
if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->has_multiple_authors_options, true ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
case 'first_activated_on':
case 'indexing_started':
case 'activation_redirect_timestamp_free':
$clean[ $key ] = false;
if ( isset( $dirty[ $key ] ) ) {
if ( $dirty[ $key ] === false || WPSEO_Utils::validate_int( $dirty[ $key ] ) ) {
$clean[ $key ] = $dirty[ $key ];
}
}
break;
case 'tracking':
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : null );
break;
case 'myyoast_oauth':
case 'semrush_tokens':
case 'custom_taxonomy_slugs':
case 'wincher_tokens':
case 'workouts_data':
case 'configuration_finished_steps':
case 'least_readability_ignore_list':
case 'least_seo_score_ignore_list':
case 'most_linked_ignore_list':
case 'least_linked_ignore_list':
case 'indexables_page_reading_list':
case 'last_known_public_post_types':
case 'last_known_public_taxonomies':
case 'new_post_types':
case 'new_taxonomies':
$clean[ $key ] = $old[ $key ];
if ( isset( $dirty[ $key ] ) ) {
$items = $dirty[ $key ];
if ( ! is_array( $items ) ) {
$items = json_decode( $dirty[ $key ], true );
}
if ( is_array( $items ) ) {
$clean[ $key ] = $dirty[ $key ];
}
}
break;
case 'permalink_structure':
case 'category_base_url':
case 'tag_base_url':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = sanitize_option( $key, $dirty[ $key ] );
}
break;
case 'search_character_limit':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = (int) $dirty[ $key ];
}
break;
case 'import_cursors':
case 'importing_completed':
if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
case 'last_known_no_unindexed':
$clean[ $key ] = $old[ $key ];
if ( isset( $dirty[ $key ] ) ) {
$items = $dirty[ $key ];
if ( is_array( $items ) ) {
foreach ( $items as $item_key => $item ) {
if ( ! is_string( $item_key ) || ! is_numeric( $item ) ) {
unset( $items[ $item_key ] );
}
}
$clean[ $key ] = $items;
}
}
break;
/*
* Boolean (checkbox) fields.
*
* Covers:
* 'disableadvanced_meta'
* 'enable_headless_rest_endpoints'
* 'yoast_tracking'
* 'dynamic_permalinks'
* 'indexing_first_time'
* 'first_time_install'
* 'remove_feed_global'
* 'remove_feed_global_comments'
* 'remove_feed_post_comments'
* 'remove_feed_authors'
* 'remove_feed_categories'
* 'remove_feed_tags'
* 'remove_feed_custom_taxonomies'
* 'remove_feed_post_types'
* 'remove_feed_search'
* 'remove_atom_rdf_feeds'
* 'remove_shortlinks'
* 'remove_rest_api_links'
* 'remove_rsd_wlw_links'
* 'remove_oembed_links'
* 'remove_generator'
* 'remove_emoji_scripts'
* 'remove_powered_by_header'
* 'remove_pingback_header'
* 'clean_campaign_tracking_urls'
* 'clean_permalinks'
* 'clean_permalinks_extra_variables'
* 'search_cleanup'
* 'search_cleanup_emoji'
* 'search_cleanup_patterns'
* 'deny_wp_json_crawling'
* 'deny_adsbot_crawling'
* 'deny_ccbot_crawling'
* 'deny_google_extended_crawling'
* 'deny_gptbot_crawling'
* 'redirect_search_pretty_urls'
* 'should_redirect_after_install_free'
* 'show_new_content_type_notification'
* and most of the feature variables.
*/
default:
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
break;
}
}
return $clean;
}
/**
* Verifies that the feature variables are turned off if the network is configured so.
*
* @param mixed $options Value of the option to be returned. Typically an array.
*
* @return mixed Filtered $options value.
*/
public function verify_features_against_network( $options = [] ) {
if ( ! is_array( $options ) || empty( $options ) ) {
return $options;
}
// For the feature variables, set their values to off in case they are disabled.
$feature_vars = [
'disableadvanced_meta' => false,
'ryte_indexability' => false,
'content_analysis_active' => false,
'keyword_analysis_active' => false,
'inclusive_language_analysis_active' => false,
'enable_admin_bar_menu' => false,
'enable_cornerstone_content' => false,
'enable_xml_sitemap' => false,
'enable_text_link_counter' => false,
'enable_metabox_insights' => false,
'enable_link_suggestions' => false,
'enable_headless_rest_endpoints' => false,
'tracking' => false,
'enable_enhanced_slack_sharing' => false,
'semrush_integration_active' => false,
'wincher_integration_active' => false,
'remove_feed_global' => false,
'remove_feed_global_comments' => false,
'remove_feed_post_comments' => false,
'enable_index_now' => false,
'enable_ai_generator' => false,
'remove_feed_authors' => false,
'remove_feed_categories' => false,
'remove_feed_tags' => false,
'remove_feed_custom_taxonomies' => false,
'remove_feed_post_types' => false,
'remove_feed_search' => false,
'remove_atom_rdf_feeds' => false,
'remove_shortlinks' => false,
'remove_rest_api_links' => false,
'remove_rsd_wlw_links' => false,
'remove_oembed_links' => false,
'remove_generator' => false,
'remove_emoji_scripts' => false,
'remove_powered_by_header' => false,
'remove_pingback_header' => false,
'clean_campaign_tracking_urls' => false,
'clean_permalinks' => false,
'search_cleanup' => false,
'search_cleanup_emoji' => false,
'search_cleanup_patterns' => false,
'redirect_search_pretty_urls' => false,
'algolia_integration_active' => false,
];
// We can reuse this logic from the base class with the above defaults to parse with the correct feature values.
$options = $this->prevent_disabled_options_update( $options, $feature_vars );
return $options;
}
/**
* Gets the filter hook name and callback for adjusting the retrieved option value
* against the network-allowed features.
*
* @return array Array where the first item is the hook name, the second is the hook callback,
* and the third is the hook priority.
*/
protected function get_verify_features_option_filter_hook() {
return [
"option_{$this->option_name}",
[ $this, 'verify_features_against_network' ],
11,
];
}
/**
* Gets the filter hook name and callback for adjusting the default option value against the network-allowed features.
*
* @return array Array where the first item is the hook name, the second is the hook callback,
* and the third is the hook priority.
*/
protected function get_verify_features_default_option_filter_hook() {
return [
"default_option_{$this->option_name}",
[ $this, 'verify_features_against_network' ],
11,
];
}
/**
* Clean a given option value.
*
* @param array $option_value Old (not merged with defaults or filtered) option value to
* clean according to the rules for this option.
* @param string|null $current_version Optional. Version from which to upgrade, if not set,
* version specific upgrades will be disregarded.
* @param array|null $all_old_option_values Optional. Only used when importing old options to have
* access to the real old values, in contrast to the saved ones.
*
* @return array Cleaned option.
*/
protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
// Deal with value change from text string to boolean.
$value_change = [
'ignore_search_engines_discouraged_notice',
];
$target_values = [
'ignore',
'done',
];
foreach ( $value_change as $key ) {
if ( isset( $option_value[ $key ] )
&& in_array( $option_value[ $key ], $target_values, true )
) {
$option_value[ $key ] = true;
}
}
return $option_value;
}
}
PK &MFZ�+�/F� F� % options/class-wpseo-option-titles.phpnu �[��� <?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/
use Yoast\WP\SEO\Config\Schema_Types;
/**
* 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 string[]
*/
protected $defaults = [
// 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.
'social-title-author-wpseo' => '%%name%%', // Text field.
'social-title-archive-wpseo' => '%%date%%', // Text field.
'social-description-author-wpseo' => '', // Text area.
'social-description-archive-wpseo' => '', // Text area.
'social-image-url-author-wpseo' => '', // Hidden input field.
'social-image-url-archive-wpseo' => '', // Hidden input field.
'social-image-id-author-wpseo' => 0, // Hidden input field.
'social-image-id-archive-wpseo' => 0, // Hidden input 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,
'breadcrumbs-404crumb' => '', // Text field.
'breadcrumbs-display-blog-page' => true,
'breadcrumbs-boldlast' => false,
'breadcrumbs-archiveprefix' => '', // Text field.
'breadcrumbs-enable' => true,
'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_logo_meta' => false,
'person_logo_meta' => false,
'company_name' => '',
'company_alternate_name' => '',
'company_or_person' => 'company',
'company_or_person_user_id' => false,
'stripcategorybase' => false,
'open_graph_frontpage_title' => '%%sitename%%', // Text field.
'open_graph_frontpage_desc' => '', // Text field.
'open_graph_frontpage_image' => '', // Text field.
'open_graph_frontpage_image_id' => 0,
'publishing_principles_id' => 0,
'ownership_funding_info_id' => 0,
'actionable_feedback_policy_id' => 0,
'corrections_policy_id' => 0,
'ethics_policy_id' => 0,
'diversity_policy_id' => 0,
'diversity_staffing_report_id' => 0,
'org-description' => '',
'org-email' => '',
'org-phone' => '',
'org-legal-name' => '',
'org-founding-date' => '',
'org-number-employees' => '',
'org-vat-id' => '',
'org-tax-id' => '',
'org-iso' => '',
'org-duns' => '',
'org-leicode' => '',
'org-naics' => '',
/*
* Uses enrich_defaults to add more along the lines of:
* - 'title-' . $pt->name => ''; // Text field.
* - 'metadesc-' . $pt->name => ''; // Text field.
* - 'noindex-' . $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;
*
* - 'schema-page-type-' . $pt->name => 'WebPage';
* - 'schema-article-type-' . $pt->name => 'Article';
*/
];
/**
* Used for "caching" during pageload.
*
* @var string[]
*/
protected $enriched_defaults = null;
/**
* Array of variable option name patterns for the option.
*
* @var string[]
*/
protected $variable_array_key_patterns = [
'title-',
'metadesc-',
'noindex-',
'display-metabox-pt-',
'bctitle-ptarchive-',
'post_types-',
'taxonomy-',
'schema-page-type-',
'schema-article-type-',
'social-title-',
'social-description-',
'social-image-url-',
'social-image-id-',
'org-',
];
/**
* Array of sub-options which should not be overloaded with multi-site defaults.
*
* @var string[]
*/
public $ms_exclude = [
'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.
*
* @return void
*/
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 string[]
*/
public function get_separator_options() {
$separators = wp_list_pluck( self::get_separator_option_list(), 'option' );
/**
* Allow altering the array with separator options.
*
* @param 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 string[] 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 = ( $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[ 'display-metabox-pt-' . $pt->name ] = true;
$enriched_defaults[ 'post_types-' . $pt->name . '-maintax' ] = 0; // Select box.
$enriched_defaults[ 'schema-page-type-' . $pt->name ] = 'WebPage';
$enriched_defaults[ 'schema-article-type-' . $pt->name ] = ( $pt->name === 'post' ) ? 'Article' : 'None';
if ( $pt->name !== 'attachment' ) {
$enriched_defaults[ 'social-title-' . $pt->name ] = '%%title%%'; // Text field.
$enriched_defaults[ 'social-description-' . $pt->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-' . $pt->name ] = 0; // Hidden input field.
}
// Custom post types that have archives.
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;
$enriched_defaults[ 'social-title-ptarchive-' . $pt->name ] = $archive; // Text field.
$enriched_defaults[ 'social-description-ptarchive-' . $pt->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-ptarchive-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-ptarchive-' . $pt->name ] = 0; // Hidden input field.
}
}
}
$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' );
$enriched_defaults[ 'social-title-tax-' . $tax->name ] = $archives; // Text field.
$enriched_defaults[ 'social-description-tax-' . $tax->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-tax-' . $tax->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-tax-' . $tax->name ] = 0; // Hidden input field.
$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 string[] $dirty New value for the option.
* @param string[] $clean Clean value for the option, normally the defaults.
* @param string[] $old Old value of the option.
*
* @return string[] 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 ) {
// Only ever set programmatically, so no reason for intense validation.
case 'company_logo_meta':
case 'person_logo_meta':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
/* 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', // phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- This isn't commented out code.
* 'title-search-wpseo', 'title-404-wpseo'
* 'title-' . $pt->name
* 'title-ptarchive-' . $pt->name
* 'title-tax-' . $tax->name
* 'social-title-' . $pt->name
* 'social-title-ptarchive-' . $pt->name
* 'social-title-tax-' . $tax->name
* 'social-title-author-wpseo', 'social-title-archive-wpseo'
* 'open_graph_frontpage_title'
*/
case 'org-':
case 'website_name':
case 'alternate_website_name':
case 'title-':
case 'social-title-':
case 'open_graph_frontpage_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;
/*
* Covers:
* 'company_logo', 'person_logo' // phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- This isn't commented out code.
*/
case 'company_logo':
case 'person_logo':
case 'open_graph_frontpage_image':
// When a logo changes, we need to ditch the caches we have for it.
unset( $clean[ $switch_key . '_id' ] );
unset( $clean[ $switch_key . '_meta' ] );
$this->validate_url( $key, $dirty, $old, $clean );
break;
/*
* Covers:
* 'social-image-url-' . $pt->name
* 'social-image-url-ptarchive-' . $pt->name
* 'social-image-url-tax-' . $tax->name
* 'social-image-url-author-wpseo', 'social-image-url-archive-wpseo'
*/
case 'social-image-url-':
$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
* 'social-description-' . $pt->name
* 'social-description-ptarchive-' . $pt->name
* 'social-description-tax-' . $tax->name
* 'social-description-author-wpseo', 'social-description-archive-wpseo'
* 'open_graph_frontpage_desc'
*/
case 'metadesc-':
case 'bctitle-ptarchive-':
case 'company_name':
case 'company_alternate_name':
case 'person_name':
case 'social-description-':
case 'open_graph_frontpage_desc':
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
}
break;
/*
* Covers: 'rssbefore', 'rssafter' // phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- This isn't commented out code.
*/
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;
/*
* Covers:
* 'company_or_person_user_id'
* 'company_logo_id', 'person_logo_id', 'open_graph_frontpage_image_id'
* 'social-image-id-' . $pt->name
* 'social-image-id-ptarchive-' . $pt->name
* 'social-image-id-tax-' . $tax->name
* 'social-image-id-author-wpseo', 'social-image-id-archive-wpseo'
*/
case 'company_or_person_user_id':
case 'company_logo_id':
case 'person_logo_id':
case 'social-image-id-':
case 'open_graph_frontpage_image_id':
case 'publishing_principles_id':
case 'ownership_funding_info_id':
case 'actionable_feedback_policy_id':
case 'corrections_policy_id':
case 'ethics_policy_id':
case 'diversity_policy_id':
case 'diversity_staffing_report_id':
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;
case 'schema-page-type-':
if ( isset( $dirty[ $key ] ) && is_string( $dirty[ $key ] ) ) {
if ( array_key_exists( $dirty[ $key ], Schema_Types::PAGE_TYPES ) ) {
$clean[ $key ] = $dirty[ $key ];
}
else {
$defaults = $this->get_defaults();
$post_type = str_replace( $switch_key, '', $key );
$clean[ $key ] = $defaults[ $switch_key . $post_type ];
}
}
break;
case 'schema-article-type-':
if ( isset( $dirty[ $key ] ) && is_string( $dirty[ $key ] ) ) {
/**
* Filter: 'wpseo_schema_article_types' - Allow developers to filter the available article types.
*
* Make sure when you filter this to also filter `wpseo_schema_article_types_labels`.
*
* @param array $schema_article_types The available schema article types.
*/
if ( array_key_exists( $dirty[ $key ], apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES ) ) ) {
$clean[ $key ] = $dirty[ $key ];
}
else {
$defaults = $this->get_defaults();
$post_type = str_replace( $switch_key, '', $key );
$clean[ $key ] = $defaults[ $switch_key . $post_type ];
}
}
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-'
* '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'
*/
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 string[]
*/
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 string[] $option_value Old (not merged with defaults or filtered) option value to clean according to the rules for this option.
* @param string[]|null $current_version Optional. Version from which to upgrade, if not set, version specific upgrades will be disregarded.
* @param string[]|null $all_old_option_values Optional. Only used when importing old options to have access to the real old values, in contrast to the saved ones.
*
* @return string[] 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 ] );
}
elseif ( 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 );
}
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 string[] $dirty Original option as retrieved from the database.
* @param string[] $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 string[]
*/
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.
*
* @param 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 string[] 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.
*
* @param 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;
}
}
PK &MFZ�Yd�� � date-helper.phpnu �[��� <?php
/**
* Date helper class.
*
* @package WPSEO\Internals
*/
/**
* Class WPSEO_Date_Helper
*
* Note: Move this class with namespace to the src/helpers directory and add a class_alias for BC.
*/
class WPSEO_Date_Helper {
/**
* Formats a given date in UTC TimeZone format.
*
* @param string $date String representing the date / time.
* @param string $format The format that the passed date should be in.
*
* @return string The formatted date.
*/
public function format( $date, $format = DATE_W3C ) {
return YoastSEO()->helpers->date->format( $date, $format );
}
/**
* Formats the given timestamp to the needed format.
*
* @param int $timestamp The timestamp to use for the formatting.
* @param string $format The format that the passed date should be in.
*
* @return string The formatted date.
*/
public function format_timestamp( $timestamp, $format = DATE_W3C ) {
return YoastSEO()->helpers->date->format_timestamp( $timestamp, $format );
}
/**
* Formats a given date in UTC TimeZone format and translate it to the set language.
*
* @param string $date String representing the date / time.
* @param string $format The format that the passed date should be in.
*
* @return string The formatted and translated date.
*/
public function format_translated( $date, $format = DATE_W3C ) {
return YoastSEO()->helpers->date->format_translated( $date, $format );
}
/**
* Check if a string is a valid datetime.
*
* @param string $datetime String input to check as valid input for DateTime class.
*
* @return bool True when datatime is valid.
*/
public function is_valid_datetime( $datetime ) {
return YoastSEO()->helpers->date->is_valid_datetime( $datetime );
}
}
PK &MFZ��]<