wp_reschedule_event()重新安排某个已发生事件

目录

描述

译文

WordPress内部使用该函数来重新安排某个已发生事件。用户可能永远都不需要手动使用这个函数。将函数列在这里是为了保证手册的完整性。

原文

This function is used internally by WordPress to reschedule a recurring event. You'll likely never need to use this function manually, it is documented here for completeness.

wp_reschedule_event() 描述

用法

 <?php wp_reschedule_event$timestamp$recurrence$hook$args); ?>

wp_reschedule_event() 用法

参数

$timestamp

(integer) (必填) The time the scheduled event will occur (unix timestamp)

默认值: None

$recurrence

(string) (必填) How often the event recurs, either 'hourly' or 'daily'

默认值: None

$hook

(string) (必填) Name of action hook to fire (string)

默认值: None

$args

(array) (可选) Arguments to pass into the hook function(s)

默认值: array()

wp_reschedule_event() 参数

历史

添加于 版本: 2.1.0

wp_reschedule_event() 历史

源文件

wp_reschedule_event() 函数的代码位于 wp-includes/cron.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Reschedule a recurring event.
 *
 * @since 2.1.0
 *
 * @param int $timestamp Timestamp for when to run the event.
 * @param string $recurrence How often the event should recur.
 * @param string $hook Action hook to execute when cron is run.
 * @param array $args Optional. Arguments to pass to the hook's callback function.
 * @return false|void False when does not schedule event.
 */
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
	$crons = _get_cron_array();
	$schedules = wp_get_schedules();
	$key = md5( serialize( $args ) );
	$interval = 0;

	// First we try to get it from the schedule
	if ( isset( $schedules[ $recurrence ] ) ) {
		$interval = $schedules[ $recurrence ]['interval'];
	}
	// Now we try to get it from the saved interval in case the schedule disappears
	if ( 0 == $interval ) {
		$interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
	}
	// Now we assume something is wrong and fail to schedule
	if ( 0 == $interval ) {
		return false;
	}

	$now = time();

	if ( $timestamp >= $now ) {
		$timestamp = $now + $interval;
	} else {
		$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
	}

	wp_schedule_event( $timestamp, $recurrence, $hook, $args );
}

wp_reschedule_event() 源文件

相关