WPSEO_Option_Social::validate_option()protectedYoast 1.0

Validate the option.

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

Хуков нет.

Возвращает

Массив. Validated clean value for the option to be saved to the database.

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

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

Код WPSEO_Option_Social::validate_option() Yoast 22.4

protected function validate_option( $dirty, $clean, $old ) {

	foreach ( $clean as $key => $value ) {
		switch ( $key ) {
			/* Text fields. */
			case 'og_frontpage_desc':
			case 'og_frontpage_title':
				if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
					$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
				}
				break;

			case 'og_default_image_id':
			case 'og_frontpage_image_id':
				if ( isset( $dirty[ $key ] ) ) {
					$clean[ $key ] = (int) $dirty[ $key ];

					if ( $dirty[ $key ] === '' ) {
						$clean[ $key ] = $dirty[ $key ];
					}
				}
				break;

			/* URL text fields - no ftp allowed. */
			case 'facebook_site':
			case 'instagram_url':
			case 'linkedin_url':
			case 'myspace_url':
			case 'pinterest_url':
			case 'og_default_image':
			case 'og_frontpage_image':
			case 'youtube_url':
			case 'wikipedia_url':
			case 'mastodon_url':
				$this->validate_url( $key, $dirty, $old, $clean );
				break;

			case 'pinterestverify':
				$this->validate_verification_string( $key, $dirty, $old, $clean );
				break;

			/* Twitter user name. */
			case 'twitter_site':
				if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
					$twitter_id = $this->validate_twitter_id( $dirty[ $key ] );

					if ( $twitter_id ) {
						$clean[ $key ] = $twitter_id;
					}
					else {
						if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
							$twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) );
							if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
								$clean[ $key ] = $twitter_id;
							}
						}
						if ( function_exists( 'add_settings_error' ) ) {
							add_settings_error(
								$this->group_name, // Slug title of the setting.
								$key, // Suffix-ID for the error message box.
								sprintf(
									/* translators: %s expands to a twitter user name. */
									__( '%s does not seem to be a valid Twitter Username. Please correct.', 'wordpress-seo' ),
									'<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
								), // The error message.
								'error' // Message type.
							);
						}
					}
					unset( $twitter_id );

					Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] );
				}
				break;

			case 'twitter_card_type':
				if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) {
					$clean[ $key ] = $dirty[ $key ];
				}
				break;

			/* Boolean fields. */
			case 'opengraph':
			case 'twitter':
				$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
				break;

			/* Array fields. */
			case 'other_social_urls':
				if ( isset( $dirty[ $key ] ) ) {
					$items = $dirty[ $key ];
					if ( ! is_array( $items ) ) {
						$items = json_decode( $dirty[ $key ], true );
					}

					if ( is_array( $items ) ) {
						foreach ( $items as $item_key => $item ) {
							$validated_url = $this->validate_social_url( $item );

							if ( $validated_url === false ) {
								// Restore the previous URL values, if any.
								$old_urls = ( isset( $old[ $key ] ) ) ? $old[ $key ] : [];
								foreach ( $old_urls as $old_item_key => $old_url ) {
									if ( $old_url !== '' ) {
										$url = WPSEO_Utils::sanitize_url( $old_url );
										if ( $url !== '' ) {
											$clean[ $key ][ $old_item_key ] = $url;
										}
									}
								}
								break;
							}

							// The URL format is valid, let's sanitize it.
							$url = WPSEO_Utils::sanitize_url( $validated_url );
							if ( $url !== '' ) {
								$clean[ $key ][ $item_key ] = $url;
							}
						}
					}
				}

				break;
		}
	}

	return $clean;
}