函数文档

wp_ajax_health_check_dotorg_communication()

💡 云策文档标注

概述

该函数用于通过 AJAX 处理站点健康检查中的服务器通信测试,但自 WordPress 5.6.0 起已被弃用,推荐使用 WP_REST_Site_Health_Controller::test_dotorg_communication() 替代。

关键要点

  • 函数 wp_ajax_health_check_dotorg_communication() 是一个 AJAX 处理函数,用于执行站点健康检查中的服务器通信测试。
  • 自 WordPress 5.6.0 版本起,该函数已被弃用,开发者应改用 WP_REST_Site_Health_Controller::test_dotorg_communication() 方法。
  • 函数内部包含权限检查(current_user_can('view_site_health_checks'))和 AJAX 请求验证(check_ajax_referer),确保安全执行。
  • 如果 WP_Site_Health 类未加载,函数会自动包含相关文件并获取实例,然后通过 wp_send_json_success 返回测试结果。

注意事项

使用此函数时需注意其已弃用状态,避免在新代码中直接调用,而应迁移到 REST API 替代方案。


📄 原文内容

Handles site health checks on server communication via AJAX.

Description

See also

Source

function wp_ajax_health_check_dotorg_communication() {
	_doing_it_wrong(
		'wp_ajax_health_check_dotorg_communication',
		sprintf(
			/* translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it. */
			__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
			'wp_ajax_health_check_dotorg_communication',
			'WP_REST_Site_Health_Controller::test_dotorg_communication'
		),
		'5.6.0'
	);

	check_ajax_referer( 'health-check-site-status' );

	if ( ! current_user_can( 'view_site_health_checks' ) ) {
		wp_send_json_error();
	}

	if ( ! class_exists( 'WP_Site_Health' ) ) {
		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
	}

	$site_health = WP_Site_Health::get_instance();
	wp_send_json_success( $site_health->get_test_dotorg_communication() );
}

Changelog

Version Description
5.6.0 Deprecated. Use WP_REST_Site_Health_Controller::test_dotorg_communication()
5.2.0 Introduced.