WPSEO_Option_Social::validate_twitter_id()publicYoast 1.0

Validates a twitter id.

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

Хуков нет.

Возвращает

Строку|false. The validated twitter id or false if it is not valid.

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

$WPSEO_Option_Social = new WPSEO_Option_Social();
$WPSEO_Option_Social->validate_twitter_id( $twitter_id, $strip_at_sign );
$twitter_id(строка) (обязательный)
The twitter id to be validated.
$strip_at_sign(true|false)
Whether or not to strip the @ sign.
По умолчанию: true

Код WPSEO_Option_Social::validate_twitter_id() Yoast 22.4

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