WPSEO_Option_Titles::validate_option()protectedYoast 1.0

Validate the option.

Метод класса: WPSEO_Option_Titles{}

Хуки из метода

Возвращает

Строку[]. Validated clean value for the option to be saved to the database.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_option( $dirty, $clean, $old );
$dirty(string[]) (обязательный)
New value for the option.
$clean(string[]) (обязательный)
Clean value for the option, normally the defaults.
$old(string[]) (обязательный)
Old value of the option.

Код WPSEO_Option_Titles::validate_option() Yoast 22.4

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;
}