: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @package WPSEO\Internals
use Yoast\WP\SEO\Config\Schema_Types;
use Yoast\WP\SEO\Helpers\Schema\Article_Helper;
use Yoast\WP\SEO\Repositories\Indexable_Repository;
* This class implements defaults and value validation for all WPSEO Post Meta values.
* - To update a meta value, you can just use update_post_meta() with the full (prefixed) meta key
* or the convenience method WPSEO_Meta::set_value() with the internal key.
* All updates will be automatically validated.
* Meta values will only be saved to the database if they are *not* the same as the default to
* keep database load low.
* - To retrieve a WPSEO meta value, you **must** use WPSEO_Meta::get_value() which will always return a
* string value, either the saved value or the default.
* This method can also retrieve a complete set of WPSEO meta values for one specific post, see
* the method documentation for the parameters.
* {@internal Unfortunately there isn't a filter available to hook into before returning the results
* for get_post_meta(), get_post_custom() and the likes. That would have been the
* {@internal All WP native get_meta() results get cached internally, so no need to cache locally.}}
* {@internal Use $key when the key is the WPSEO internal name (without prefix), $meta_key when it
* Prefix for all WPSEO meta values in the database.
* {@internal If at any point this would change, quite apart from an upgrade routine,
* this also will need to be changed in the wpml-config.xml file.}}
public static $meta_prefix = '_yoast_wpseo_';
* Prefix for all WPSEO meta value form field names and ids.
public static $form_prefix = 'yoast_wpseo_';
* Allowed length of the meta description.
public static $meta_length = 156;
* Reason the meta description is not the default length.
public static $meta_length_reason = '';
* Meta box field definitions for the meta box form.
* - Titles, help texts, description text and option labels are added via a translate_meta_boxes() method
* in the relevant child classes (WPSEO_Metabox and WPSEO_Social_admin) as they are only needed there.
* - Beware: even though the meta keys are divided into subsets, they still have to be uniquely named!}}
* (required) 'type' => (string) field type. i.e. text / textarea / checkbox /
* radio / select / multiselect / upload etc.
* (required) 'title' => (string) table row title.
* (recommended) 'default_value' => (string|array) default value for the field.
* - if the field has options, the default has to be the
* key of one of the options.
* - if the field is a text field, the default **has** to be
* an empty string as otherwise the user can't save
* an empty value/delete the meta value.
* - if the field is a checkbox, the only valid values
* (semi-required) 'options' => (array) options for used with (multi-)select and radio
* fields, required if that's the field type.
* key = (string) value which will be saved to db.
* value = (string) text label for the option.
* (optional) 'autocomplete' => (bool) whether autocomplete is on for text fields,
* (optional) 'class' => (string) classname(s) to add to the actual <input> tag.
* (optional) 'description' => (string) description to show underneath the field.
* (optional) 'expl' => (string) label for a checkbox.
* (optional) 'help' => (string) help text to show on mouse over ? image.
* (optional) 'rows' => (int) number of rows for a textarea, defaults to 3.
* (optional) 'placeholder' => (string) Currently only used by add-on plugins.
* (optional) 'serialized' => (bool) whether the value is expected to be serialized,
* i.e. an array or object, defaults to false.
* Currently only used by add-on plugins.
public static $meta_fields = [
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'help' => '', // Translation added later.
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'help' => '', // Translation added later.
'title' => 'content_score',
'inclusive_language_score' => [
'title' => 'inclusive_language_score',
'title' => 'is_cornerstone',
'default_value' => 'false',
'meta-robots-noindex' => [
'title' => '', // Translation added later.
'default_value' => '0', // = post-type default.
'0' => '', // Post type default - translation added later.
'2' => '', // Index - translation added later.
'1' => '', // No-index - translation added later.
'meta-robots-nofollow' => [
'title' => '', // Translation added later.
'default_value' => '0', // = follow.
'0' => '', // Follow - translation added later.
'1' => '', // No-follow - translation added later.
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'noimageindex' => '', // Translation added later.
'noarchive' => '', // Translation added later.
'nosnippet' => '', // Translation added later.
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'title' => '', // Translation added later.
'description' => '', // Translation added later.
'options' => Schema_Types::PAGE_TYPES,
'schema_article_type' => [
'options' => Schema_Types::ARTICLE_TYPES,
/* Fields we should validate & save, but not show on any form. */
'zapier_trigger_sent' => [
* Helper property - reverse index of the definition array.
* Format: [full meta key including prefix] => array
* ['subset'] => (string) primary index
* ['key'] => (string) internal key
public static $fields_index = [];
* Helper property - array containing only the defaults in the format:
* [full meta key including prefix] => (string) default value
public static $defaults = [];
* Helper property to define the social network meta field definitions - networks.
private static $social_networks = [
'opengraph' => 'opengraph',
* Helper property to define the social network meta field definitions - fields and their type.
private static $social_fields = [
'description' => 'hidden',
* Register our actions and filters.
public static function init() {
foreach ( self::$social_networks as $option => $network ) {
if ( WPSEO_Options::get( $option, false ) === true ) {
foreach ( self::$social_fields as $box => $type ) {
self::$meta_fields['social'][ $network . '-' . $box ] = [
'title' => '', // Translation added later.
'description' => '', // Translation added later.
unset( $option, $network, $box, $type );
* Allow add-on plugins to register their meta fields for management by this class.
* Calls to add_filter() must be made before plugins_loaded prio 14.
$extra_fields = apply_filters( 'add_extra_wpseo_meta_fields', [] );
if ( is_array( $extra_fields ) ) {
self::$meta_fields = self::array_merge_recursive_distinct( $extra_fields, self::$meta_fields );
foreach ( self::$meta_fields as $subset => $field_group ) {
foreach ( $field_group as $key => $field_def ) {
self::$meta_prefix . $key,
[ 'sanitize_callback' => [ self::class, 'sanitize_post_meta' ] ]
// Set the $fields_index property for efficiency.
self::$fields_index[ self::$meta_prefix . $key ] = [
// Set the $defaults property for efficiency.
if ( isset( $field_def['default_value'] ) ) {
self::$defaults[ self::$meta_prefix . $key ] = $field_def['default_value'];
// Meta will always be a string, so let's make the meta meta default also a string.
self::$defaults[ self::$meta_prefix . $key ] = '';
unset( $subset, $field_group, $key, $field_def );
self::filter_schema_article_types();
add_filter( 'update_post_metadata', [ self::class, 'remove_meta_if_default' ], 10, 5 );
add_filter( 'add_post_metadata', [ self::class, 'dont_save_meta_if_default' ], 10, 4 );
* Retrieve the meta box form field definitions for the given tab and post type.
* @param string $tab Tab for which to retrieve the field definitions.
* @param string $post_type Post type of the current post.
* @return array Array containing the meta box field definitions.
public static function get_meta_field_defs( $tab, $post_type = 'post' ) {
if ( ! isset( self::$meta_fields[ $tab ] ) ) {
$field_defs = self::$meta_fields[ $tab ];
// Prevent non-form fields from being passed to forms.
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) {
if ( isset( $post->post_type ) ) {
$post_type = $post->post_type;
elseif ( ! isset( $post->post_type ) && isset( $_GET['post_type'] ) ) {
$post_type = sanitize_text_field( $_GET['post_type'] );
if ( $post_type === '' ) {
/* Adjust the no-index text strings based on the post type. */
$post_type_object = get_post_type_object( $post_type );
$field_defs['meta-robots-noindex']['title'] = sprintf( $field_defs['meta-robots-noindex']['title'], $post_type_object->labels->singular_name );
$field_defs['meta-robots-noindex']['options']['0'] = sprintf( $field_defs['meta-robots-noindex']['options']['0'], ( ( WPSEO_Options::get( 'noindex-' . $post_type, false ) === true ) ? $field_defs['meta-robots-noindex']['options']['1'] : $field_defs['meta-robots-noindex']['options']['2'] ), $post_type_object->label );
$field_defs['meta-robots-nofollow']['title'] = sprintf( $field_defs['meta-robots-nofollow']['title'], $post_type_object->labels->singular_name );
/* Don't show the breadcrumb title field if breadcrumbs aren't enabled. */
if ( WPSEO_Options::get( 'breadcrumbs-enable', false ) !== true && ! current_theme_supports( 'yoast-seo-breadcrumbs' ) ) {
unset( $field_defs['bctitle'] );
if ( empty( $post->ID ) || ( ! empty( $post->ID ) && self::get_value( 'redirect', $post->ID ) === '' ) ) {
unset( $field_defs['redirect'] );
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) {
$field_defs['schema_page_type']['default'] = WPSEO_Options::get( 'schema-page-type-' . $post_type );
$article_helper = new Article_Helper();
if ( $article_helper->is_article_post_type( $post_type ) ) {
$default_schema_article_type = WPSEO_Options::get( 'schema-article-type-' . $post_type );
/** This filter is documented in inc/options/class-wpseo-option-titles.php */
$allowed_article_types = apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES );
if ( ! array_key_exists( $default_schema_article_type, $allowed_article_types ) ) {
$default_schema_article_type = WPSEO_Options::get_default( 'wpseo_titles', 'schema-article-type-' . $post_type );
$field_defs['schema_article_type']['default'] = $default_schema_article_type;
unset( $field_defs['schema_article_type'] );
* Filter the WPSEO metabox form field definitions for a tab.
* {tab} can be 'general', 'advanced' or 'social'.
* @param array $field_defs Metabox form field definitions.
* @param string $post_type Post type of the post the metabox is for, defaults to 'post'.
return apply_filters( 'wpseo_metabox_entries_' . $tab, $field_defs, $post_type );
* Validate the post meta values.
* @param mixed $meta_value The new value.
* @param string $meta_key The full meta key (including prefix).
* @return string Validated meta value.
public static function sanitize_post_meta( $meta_value, $meta_key ) {
$field_def = self::$meta_fields[ self::$fields_index[ $meta_key ]['subset'] ][ self::$fields_index[ $meta_key ]['key'] ];
$clean = self::$defaults[ $meta_key ];
case ( $meta_key === self::$meta_prefix . 'linkdex' ):
$int = WPSEO_Utils::validate_int( $meta_value );
if ( $int !== false && $int >= 0 ) {
$clean = strval( $int ); // Convert to string to make sure default check works.
case ( $field_def['type'] === 'checkbox' ):
// Only allow value if it's one of the predefined options.
if ( in_array( $meta_value, [ 'on', 'off' ], true ) ) {
case ( $field_def['type'] === 'select' || $field_def['type'] === 'radio' ):
// Only allow value if it's one of the predefined options.
if ( isset( $field_def['options'][ $meta_value ] ) ) {
case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'meta-robots-adv' ):
$clean = self::validate_meta_robots_adv( $meta_value );
case ( $field_def['type'] === 'url' || $meta_key === self::$meta_prefix . 'canonical' ):
// Validate as url(-part).
$url = WPSEO_Utils::sanitize_url( $meta_value );
case ( $field_def['type'] === 'upload' && in_array( $meta_key, [ self::$meta_prefix . 'opengraph-image', self::$meta_prefix . 'twitter-image' ], true ) ):
$url = WPSEO_Utils::sanitize_url( $meta_value, [ 'http', 'https', 'ftp', 'ftps' ] );
case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'is_cornerstone' ):
* This used to be a checkbox, then became a hidden input.
* To make sure the value remains consistent, we cast 'true' to '1'.
if ( $meta_value === 'true' ) {
case ( $field_def['type'] === 'hidden' && isset( $field_def['options'] ) ):
// Only allow value if it's one of the predefined options.
if ( isset( $field_def['options'][ $meta_value ] ) ) {