wp_ajax_health_check_background_updates()
云策文档标注
概述
这是一个用于通过 AJAX 处理站点健康检查中后台更新的 WordPress 函数。该函数在 WordPress 5.6.0 版本中已被弃用,建议使用 WP_REST_Site_Health_Controller::test_background_updates() 替代。
关键要点
- 函数 wp_ajax_health_check_background_updates() 用于 AJAX 请求,执行站点健康检查中的后台更新测试。
- 自 WordPress 5.6.0 起,此函数已被弃用,推荐使用 WP_REST_Site_Health_Controller::test_background_updates() 作为替代。
- 函数内部包含权限检查、AJAX 引用验证和 WP_Site_Health 类的实例化,以确保安全性和正确性。
代码示例
function wp_ajax_health_check_background_updates() {
_doing_it_wrong(
'wp_ajax_health_check_background_updates',
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_background_updates',
'WP_REST_Site_Health_Controller::test_background_updates'
),
'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_background_updates() );
}注意事项
- 此函数已弃用,开发者应避免在新代码中使用,并迁移到 WP_REST_Site_Health_Controller::test_background_updates()。
- 函数执行前会检查用户权限和 AJAX 引用,确保只有授权用户才能访问。
- 如果 WP_Site_Health 类不存在,函数会自动加载相关文件。
原文内容
Handles site health checks on background updates via AJAX.
Description
See also
Source
function wp_ajax_health_check_background_updates() {
_doing_it_wrong(
'wp_ajax_health_check_background_updates',
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_background_updates',
'WP_REST_Site_Health_Controller::test_background_updates'
),
'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_background_updates() );
}
Changelog
| Version | Description |
|---|---|
| 5.6.0 | Deprecated. Use WP_REST_Site_Health_Controller::test_background_updates() |
| 5.2.0 | Introduced. |