wp_is_authorize_application_redirect_url_valid

函数
wp_is_authorize_application_redirect_url_valid ( $url )
参数
  • (string) $url - The redirect URL to be validated.
    Required:
返回值
  • (true|WP_Error) True if the redirect URL is valid, a WP_Error object otherwise.
定义位置
相关方法
wp_is_authorize_application_password_request_validwp_authenticate_application_passwordwp_is_application_passwords_availablerest_authorization_required_codewp_sanitize_redirect
引入
6.3.2
弃用
-

验证重定向URL协议方案。协议可以是除http和javascript之外的任何内容。

function wp_is_authorize_application_redirect_url_valid( $url ) {
	$bad_protocols = array( 'javascript', 'data' );
	if ( empty( $url ) ) {
		return true;
	}

	// Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1
	$valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/';
	if ( ! preg_match( $valid_scheme_regex, $url ) ) {
		return new WP_Error(
			'invalid_redirect_url_format',
			__( 'Invalid URL format.' )
		);
	}

	/**
	 * Filters the list of invalid protocols used in applications redirect URLs.
	 *
	 * @since 6.3.2
	 *
	 * @param string[]  $bad_protocols Array of invalid protocols.
	 * @param string    $url The redirect URL to be validated.
	 */
	$invalid_protocols = array_map( 'strtolower', apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url ) );

	$scheme   = wp_parse_url( $url, PHP_URL_SCHEME );
	$host     = wp_parse_url( $url, PHP_URL_HOST );
	$is_local = 'local' === wp_get_environment_type();

	// validates if the proper URI format is applied to the $url
	if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) {
		return new WP_Error(
			'invalid_redirect_url_format',
			__( 'Invalid URL format.' )
		);
	}

	if ( 'http' === $scheme && ! $is_local ) {
		return new WP_Error(
			'invalid_redirect_scheme',
			__( 'The URL must be served over a secure connection.' )
		);
	}

	return true;
}

常见问题

FAQs
查看更多 >