函数文档

_wp_cron()

💡 云策文档标注

概述

_wp_cron() 是 WordPress 内部函数,用于执行已到期的定时任务回调或触发 cron 事件。它检查并处理准备就绪的 cron 作业,通过 HTTP 请求异步运行,避免阻塞页面加载。

关键要点

  • 函数返回类型为 int|false:成功时返回触发的 cron 事件数量(0 表示无事件需要触发),失败时返回 false。
  • 使用注意事项:返回值可能为布尔 false 或非布尔值但评估为 false,建议使用 === 运算符进行严格比较。
  • 函数逻辑:首先检查是否在 wp-cron.php 请求中或 DISABLE_WP_CRON 已定义,以避免无限循环;然后获取准备就绪的 cron 作业,基于当前时间戳筛选并触发 spawn_cron()。
  • 相关函数:依赖 wp_get_ready_cron_jobs()、wp_get_schedules() 和 spawn_cron(),由 wp_cron() 在 'shutdown' 动作中注册调用。
  • 版本历史:自 WordPress 5.7.0 版本引入。

代码示例

function _wp_cron() {
    // Prevent infinite loops caused by lack of wp-cron.php.
    if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' )
        || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON )
    ) {
        return 0;
    }

    $crons = wp_get_ready_cron_jobs();
    if ( empty( $crons ) ) {
        return 0;
    }

    $gmt_time = microtime( true );
    $keys     = array_keys( $crons );
    if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
        return 0;
    }

    $schedules = wp_get_schedules();
    $results   = array();

    foreach ( $crons as $timestamp => $cronhooks ) {
        if ( $timestamp > $gmt_time ) {
            break;
        }

        foreach ( (array) $cronhooks as $hook => $args ) {
            if ( isset( $schedules[ $hook ]['callback'] )
                && ! call_user_func( $schedules[ $hook ]['callback'] )
            ) {
                continue;
            }

            $results[] = spawn_cron( $gmt_time );
            break 2;
        }
    }

    if ( in_array( false, $results, true ) ) {
        return false;
    }

    return count( $results );
}

注意事项

此函数可能返回布尔 FALSE 或评估为 FALSE 的非布尔值,建议使用 === 运算符测试返回值以避免类型混淆错误。


📄 原文内容

Runs scheduled callbacks or spawns cron for all scheduled events.

Description

Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. For information about casting to booleans see the PHP documentation. Use the === operator for testing the return value of this function.

Return

int|false On success an integer indicating number of events spawned (0 indicates no events needed to be spawned), false if spawning fails for one or more events.

Source

function _wp_cron() {
	// Prevent infinite loops caused by lack of wp-cron.php.
	if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' )
		|| ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON )
	) {
		return 0;
	}

	$crons = wp_get_ready_cron_jobs();
	if ( empty( $crons ) ) {
		return 0;
	}

	$gmt_time = microtime( true );
	$keys     = array_keys( $crons );
	if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
		return 0;
	}

	$schedules = wp_get_schedules();
	$results   = array();

	foreach ( $crons as $timestamp => $cronhooks ) {
		if ( $timestamp > $gmt_time ) {
			break;
		}

		foreach ( (array) $cronhooks as $hook => $args ) {
			if ( isset( $schedules[ $hook ]['callback'] )
				&& ! call_user_func( $schedules[ $hook ]['callback'] )
			) {
				continue;
			}

			$results[] = spawn_cron( $gmt_time );
			break 2;
		}
	}

	if ( in_array( false, $results, true ) ) {
		return false;
	}

	return count( $results );
}

Changelog

Version Description
5.7.0 Introduced.