函数文档

wp_ajax_health_check_get_sizes()

💡 云策文档标注

概述

wp_ajax_health_check_get_sizes() 是一个用于通过 AJAX 处理站点健康检查以获取目录和数据库大小的 WordPress 函数。该函数自 WordPress 5.6.0 起已被弃用,建议使用 WP_REST_Site_Health_Controller::get_directory_sizes() 替代。

关键要点

  • 函数 wp_ajax_health_check_get_sizes() 通过 AJAX 请求获取站点目录和数据库的大小数据。
  • 自 WordPress 5.6.0 起,此函数已被弃用,应改用 WP_REST_Site_Health_Controller::get_directory_sizes()。
  • 函数执行前会检查 AJAX 引用、用户权限(需 view_site_health_checks 能力)和非多站点环境。
  • 使用 WP_Debug_Data::get_sizes() 获取大小数据,并进行数据清理和格式化。
  • 根据数据可用性,函数通过 wp_send_json_success() 或 wp_send_json_error() 返回 JSON 响应。

代码示例

function wp_ajax_health_check_get_sizes() {
    _doing_it_wrong(
        'wp_ajax_health_check_get_sizes',
        sprintf(
            __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
            'wp_ajax_health_check_get_sizes',
            'WP_REST_Site_Health_Controller::get_directory_sizes'
        ),
        '5.6.0'
    );

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

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

    if ( ! class_exists( 'WP_Debug_Data' ) ) {
        require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
    }

    $sizes_data = WP_Debug_Data::get_sizes();
    $all_sizes  = array( 'raw' => 0 );

    foreach ( $sizes_data as $name => $value ) {
        $name = sanitize_text_field( $name );
        $data = array();

        if ( isset( $value['size'] ) ) {
            if ( is_string( $value['size'] ) ) {
                $data['size'] = sanitize_text_field( $value['size'] );
            } else {
                $data['size'] = (int) $value['size'];
            }
        }

        if ( isset( $value['debug'] ) ) {
            if ( is_string( $value['debug'] ) ) {
                $data['debug'] = sanitize_text_field( $value['debug'] );
            } else {
                $data['debug'] = (int) $value['debug'];
            }
        }

        if ( ! empty( $value['raw'] ) ) {
            $data['raw'] = (int) $value['raw'];
        }

        $all_sizes[ $name ] = $data;
    }

    if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
        wp_send_json_error( $all_sizes );
    }

    wp_send_json_success( $all_sizes );
}

注意事项

  • 此函数已弃用,新开发中应避免使用,转而使用 REST API 替代方案。
  • 函数依赖于 WP_Debug_Data 类,需确保该类已加载。
  • 权限检查严格,仅允许具有 view_site_health_checks 能力的用户访问,且不支持多站点环境。
  • 数据返回格式为 JSON,包含清理后的目录大小信息,便于前端处理。

📄 原文内容

Handles site health check to get directories and database sizes via AJAX.

Description

See also

Source

function wp_ajax_health_check_get_sizes() {
	_doing_it_wrong(
		'wp_ajax_health_check_get_sizes',
		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_get_sizes',
			'WP_REST_Site_Health_Controller::get_directory_sizes'
		),
		'5.6.0'
	);

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

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

	if ( ! class_exists( 'WP_Debug_Data' ) ) {
		require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
	}

	$sizes_data = WP_Debug_Data::get_sizes();
	$all_sizes  = array( 'raw' => 0 );

	foreach ( $sizes_data as $name => $value ) {
		$name = sanitize_text_field( $name );
		$data = array();

		if ( isset( $value['size'] ) ) {
			if ( is_string( $value['size'] ) ) {
				$data['size'] = sanitize_text_field( $value['size'] );
			} else {
				$data['size'] = (int) $value['size'];
			}
		}

		if ( isset( $value['debug'] ) ) {
			if ( is_string( $value['debug'] ) ) {
				$data['debug'] = sanitize_text_field( $value['debug'] );
			} else {
				$data['debug'] = (int) $value['debug'];
			}
		}

		if ( ! empty( $value['raw'] ) ) {
			$data['raw'] = (int) $value['raw'];
		}

		$all_sizes[ $name ] = $data;
	}

	if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
		wp_send_json_error( $all_sizes );
	}

	wp_send_json_success( $all_sizes );
}

Changelog

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