函数文档

wp_get_https_detection_errors()

💡 云策文档标注

概述

wp_get_https_detection_errors() 函数通过远程 HTTPS 请求检测服务器是否支持 HTTPS,并返回相关错误数组。该函数资源消耗较大,需谨慎使用,特别是在性能敏感环境中。

关键要点

  • 函数执行远程 HTTPS 请求以验证 HTTPS 支持,返回错误数组或空数组
  • 使用 apply_filters('pre_wp_get_https_detection_errors', null) 钩子可短路检测过程
  • 内部逻辑包括 SSL 验证失败、请求失败、响应码非 200 或响应来源非本地的错误处理
  • 自 WordPress 6.4.0 版本引入

代码示例

function wp_get_https_detection_errors() {
    $support_errors = apply_filters( 'pre_wp_get_https_detection_errors', null );
    if ( is_wp_error( $support_errors ) ) {
        return $support_errors->errors;
    }

    $support_errors = new WP_Error();
    $response = wp_remote_request(
        home_url( '/', 'https' ),
        array(
            'headers'   => array(
                'Cache-Control' => 'no-cache',
            ),
            'sslverify' => true,
        )
    );

    if ( is_wp_error( $response ) ) {
        $unverified_response = wp_remote_request(
            home_url( '/', 'https' ),
            array(
                'headers'   => array(
                    'Cache-Control' => 'no-cache',
                ),
                'sslverify' => false,
            )
        );

        if ( is_wp_error( $unverified_response ) ) {
            $support_errors->add(
                'https_request_failed',
                __( 'HTTPS request failed.' )
            );
        } else {
            $support_errors->add(
                'ssl_verification_failed',
                __( 'SSL verification failed.' )
            );
        }

        $response = $unverified_response;
    }

    if ( ! is_wp_error( $response ) ) {
        if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
            $support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) );
        } elseif ( false === wp_is_local_html_output( wp_remote_retrieve_body( $response ) ) ) {
            $support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) );
        }
    }

    return $support_errors->errors;
}

注意事项

  • 该函数可能影响性能,建议在非关键路径或异步任务中使用
  • 错误类型包括 'https_request_failed'、'ssl_verification_failed'、'bad_response_code' 和 'bad_response_source'
  • 相关函数如 wp_is_https_supported() 和 WP_Site_Health::get_test_https_status() 依赖此函数

📄 原文内容

Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors.

Description

This function checks for HTTPS support by making an HTTP request. As this process can be resource-intensive, it should be used cautiously, especially in performance-sensitive environments.
It is called when HTTPS support needs to be validated.

Return

array An array containing potential detection errors related to HTTPS, or an empty array if no errors are found.

Source

function wp_get_https_detection_errors() {
	/**
	 * Short-circuits the process of detecting errors related to HTTPS support.
	 *
	 * Returning a `WP_Error` from the filter will effectively short-circuit the default logic of trying a remote
	 * request to the site over HTTPS, storing the errors array from the returned `WP_Error` instead.
	 *
	 * @since 6.4.0
	 *
	 * @param null|WP_Error $pre Error object to short-circuit detection,
	 *                           or null to continue with the default behavior.
	 */
	$support_errors = apply_filters( 'pre_wp_get_https_detection_errors', null );
	if ( is_wp_error( $support_errors ) ) {
		return $support_errors->errors;
	}

	$support_errors = new WP_Error();

	$response = wp_remote_request(
		home_url( '/', 'https' ),
		array(
			'headers'   => array(
				'Cache-Control' => 'no-cache',
			),
			'sslverify' => true,
		)
	);

	if ( is_wp_error( $response ) ) {
		$unverified_response = wp_remote_request(
			home_url( '/', 'https' ),
			array(
				'headers'   => array(
					'Cache-Control' => 'no-cache',
				),
				'sslverify' => false,
			)
		);

		if ( is_wp_error( $unverified_response ) ) {
			$support_errors->add(
				'https_request_failed',
				__( 'HTTPS request failed.' )
			);
		} else {
			$support_errors->add(
				'ssl_verification_failed',
				__( 'SSL verification failed.' )
			);
		}

		$response = $unverified_response;
	}

	if ( ! is_wp_error( $response ) ) {
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
			$support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) );
		} elseif ( false === wp_is_local_html_output( wp_remote_retrieve_body( $response ) ) ) {
			$support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) );
		}
	}

	return $support_errors->errors;
}

Hooks

apply_filters( ‘pre_wp_get_https_detection_errors’, null|WP_Error $pre )

Short-circuits the process of detecting errors related to HTTPS support.

Changelog

Version Description
6.4.0 Introduced.