函数文档

wp_get_auto_update_message()

💡 云策文档标注

概述

wp_get_auto_update_message() 函数用于确定并返回自动更新的状态消息,基于 WP-Cron 的下一次计划时间。它检查更新是否已计划、是否逾期,并生成相应的用户友好消息。

关键要点

  • 函数返回一个字符串,表示自动更新的状态消息。
  • 通过 wp_next_scheduled('wp_version_check') 获取下一次计划更新时间。
  • 如果事件不存在,返回 WP-Cron 可能有问题。
  • 如果事件存在,计算时间差,判断是否逾期,并生成相应消息。
  • 使用 human_time_diff() 格式化时间差,__() 进行翻译。

代码示例

function wp_get_auto_update_message() {
    $next_update_time = wp_next_scheduled( 'wp_version_check' );

    // Check if the event exists.
    if ( false === $next_update_time ) {
        $message = __( 'Automatic update not scheduled. There may be a problem with WP-Cron.' );
    } else {
        $time_to_next_update = human_time_diff( (int) $next_update_time );

        // See if cron is overdue.
        $overdue = ( time() - $next_update_time ) > 0;

        if ( $overdue ) {
            $message = sprintf(
                /* translators: %s: Duration that WP-Cron has been overdue. */
                __( 'Automatic update overdue by %s. There may be a problem with WP-Cron.' ),
                $time_to_next_update
            );
        } else {
            $message = sprintf(
                /* translators: %s: Time until the next update. */
                __( 'Automatic update scheduled in %s.' ),
                $time_to_next_update
            );
        }
    }

    return $message;
}

注意事项

  • 此函数依赖于 WP-Cron 系统,如果 WP-Cron 未正确配置,可能返回错误消息。
  • 消息已国际化,使用 __() 函数支持翻译。
  • 自 WordPress 5.5.0 版本引入。

📄 原文内容

Determines the appropriate auto-update message to be displayed.

Return

string The update message to be shown.

Source

function wp_get_auto_update_message() {
	$next_update_time = wp_next_scheduled( 'wp_version_check' );

	// Check if the event exists.
	if ( false === $next_update_time ) {
		$message = __( 'Automatic update not scheduled. There may be a problem with WP-Cron.' );
	} else {
		$time_to_next_update = human_time_diff( (int) $next_update_time );

		// See if cron is overdue.
		$overdue = ( time() - $next_update_time ) > 0;

		if ( $overdue ) {
			$message = sprintf(
				/* translators: %s: Duration that WP-Cron has been overdue. */
				__( 'Automatic update overdue by %s. There may be a problem with WP-Cron.' ),
				$time_to_next_update
			);
		} else {
			$message = sprintf(
				/* translators: %s: Time until the next update. */
				__( 'Automatic update scheduled in %s.' ),
				$time_to_next_update
			);
		}
	}

	return $message;
}

Changelog

Version Description
5.5.0 Introduced.