wp/wp-includes/cron.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress CRON API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * Schedules a hook to run only once.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * Schedules a hook which will be executed once by the WordPress actions core at
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * a time which you specify. The action will fire off when someone visits your
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * WordPress site, if the schedule time has passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    16
 * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * @param int $timestamp Timestamp for when to run the event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @param string $hook Action hook to execute when cron is run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @param array $args Optional. Arguments to pass to the hook's callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
	// don't schedule a duplicate if there's already an identical event due within 10 minutes of it
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	$next = wp_next_scheduled($hook, $args);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
	if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
	 * Filter a single event before it is scheduled.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
	 * @param object $event An object containing an event's data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	$event = apply_filters( 'schedule_event', $event );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	// A plugin disallowed this event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	if ( ! $event )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	$key = md5(serialize($event->args));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	uksort( $crons, "strnatcasecmp" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	_set_cron_array( $crons );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
 * Schedule a periodic event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
 * Schedules a hook which will be executed by the WordPress actions core on a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
 * specific interval, specified by you. The action will trigger when someone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
 * visits your WordPress site, if the scheduled time has passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
 * Valid values for the recurrence are hourly, daily and twicedaily. These can
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
 * be extended using the cron_schedules filter in wp_get_schedules().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
 * Use wp_next_scheduled() to prevent duplicates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
 * @param int $timestamp Timestamp for when to run the event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
 * @param string $recurrence How often the event should recur.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
 * @param string $hook Action hook to execute when cron is run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
 * @param array $args Optional. Arguments to pass to the hook's callback function.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
 * @return false|null False on failure, null when complete with scheduling event.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	$schedules = wp_get_schedules();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	if ( !isset( $schedules[$recurrence] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
	/** This filter is documented in wp-includes/cron.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
	$event = apply_filters( 'schedule_event', $event );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	// A plugin disallowed this event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	if ( ! $event )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
	$key = md5(serialize($event->args));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
	$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	uksort( $crons, "strnatcasecmp" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	_set_cron_array( $crons );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
 * Reschedule a recurring event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 * @param int $timestamp Timestamp for when to run the event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 * @param string $recurrence How often the event should recur.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
 * @param string $hook Action hook to execute when cron is run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 * @param array $args Optional. Arguments to pass to the hook's callback function.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
 * @return false|null False on failure. Null when event is rescheduled.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	$schedules = wp_get_schedules();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
	$key = md5( serialize( $args ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	$interval = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	// First we try to get it from the schedule
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
	if ( isset( $schedules[ $recurrence ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
		$interval = $schedules[ $recurrence ]['interval'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	// Now we try to get it from the saved interval in case the schedule disappears
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
	if ( 0 == $interval ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
		$interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	// Now we assume something is wrong and fail to schedule
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
	if ( 0 == $interval ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	$now = time();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
	if ( $timestamp >= $now ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		$timestamp = $now + $interval;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   127
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
		$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	wp_schedule_event( $timestamp, $recurrence, $hook, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * Unschedule a previously scheduled cron job.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 * The $timestamp and $hook parameters are required, so that the event can be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
 * identified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 * @param int $timestamp Timestamp for when to run the event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
 * @param string $hook Action hook, the execution of which will be unscheduled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
 * @param array $args Arguments to pass to the hook's callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
 * Although not passed to a callback function, these arguments are used
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
 * to uniquely identify the scheduled event, so they should be the same
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
 * as those used when originally scheduling the event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	$key = md5(serialize($args));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	unset( $crons[$timestamp][$hook][$key] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
	if ( empty($crons[$timestamp][$hook]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		unset( $crons[$timestamp][$hook] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
	if ( empty($crons[$timestamp]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		unset( $crons[$timestamp] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	_set_cron_array( $crons );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 * Unschedule all cron jobs attached to a specific hook.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
 * @param string $hook Action hook, the execution of which will be unscheduled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
 * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
function wp_clear_scheduled_hook( $hook, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	// Backward compatibility
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
	// Previously this function took the arguments as discrete vars rather than an array like the rest of the API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	if ( !is_array($args) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
		_deprecated_argument( __FUNCTION__, '3.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		$args = array_slice( func_get_args(), 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
	// This logic duplicates wp_next_scheduled()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
	// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	// and, wp_next_scheduled() returns the same schedule in an infinite loop.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	$crons = _get_cron_array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
	if ( empty( $crons ) )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
		return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
	$key = md5( serialize( $args ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
	foreach ( $crons as $timestamp => $cron ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
		if ( isset( $cron[ $hook ][ $key ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
			wp_unschedule_event( $timestamp, $hook, $args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 * Retrieve the next timestamp for a cron event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 * @param string $hook Action hook to execute when cron is run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 * @param array $args Optional. Arguments to pass to the hook's callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
function wp_next_scheduled( $hook, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
	$key = md5(serialize($args));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	if ( empty($crons) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
	foreach ( $crons as $timestamp => $cron ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		if ( isset( $cron[$hook][$key] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
			return $timestamp;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
 * Send request to run cron through HTTP request that doesn't halt page loading.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 * @return null Cron could not be spawned, because it is not needed to run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
function spawn_cron( $gmt_time = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
	if ( ! $gmt_time )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
		$gmt_time = microtime( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
	if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	* multiple processes on multiple web servers can run this code concurrently
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	* try to make this as atomic as possible by setting doing_cron switch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	*/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	$lock = get_transient('doing_cron');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
	if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		$lock = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
	// don't run if another process is currently running it or more than once every 60 sec.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
	if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	//sanity check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	if ( !is_array($crons) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
	$keys = array_keys( $crons );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	if ( isset($keys[0]) && $keys[0] > $gmt_time )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
	if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
		if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) ||  defined( 'XMLRPC_REQUEST' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		$doing_wp_cron = sprintf( '%.22F', $gmt_time );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		set_transient( 'doing_cron', $doing_wp_cron );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		ob_start();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
		wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		echo ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		// flush any buffers and send the headers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
		while ( @ob_end_flush() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
		flush();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
		WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	$doing_wp_cron = sprintf( '%.22F', $gmt_time );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
	set_transient( 'doing_cron', $doing_wp_cron );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
	 * Filter the cron request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	 * @param array $cron_request_array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	 *     An array of cron request URL arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
	 *     @type string $url  The cron request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
	 *     @type int    $key  The 22 digit GMT microtime.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
	 *     @type array  $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
	 *         An array of cron request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
	 *         @type int  $timeout   The request timeout in seconds. Default .01 seconds.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	 *         @type bool $blocking  Whether to set blocking for the request. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
	 *         @type bool $sslverify Whether SSL should be verified for the request. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	$cron_request = apply_filters( 'cron_request', array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
		'url'  => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
		'key'  => $doing_wp_cron,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
		'args' => array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
			'timeout'   => 0.01,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
			'blocking'  => false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
			/** This filter is documented in wp-includes/class-http.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
			'sslverify' => apply_filters( 'https_local_ssl_verify', false )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
		)
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	wp_remote_post( $cron_request['url'], $cron_request['args'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
 * Run scheduled callbacks or spawn cron for all scheduled events.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
 * @return null When doesn't need to run Cron.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
function wp_cron() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
	// Prevent infinite loops caused by lack of wp-cron.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
	if ( false === $crons = _get_cron_array() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
	$gmt_time = microtime( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
	$keys = array_keys( $crons );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
	if ( isset($keys[0]) && $keys[0] > $gmt_time )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	$schedules = wp_get_schedules();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
	foreach ( $crons as $timestamp => $cronhooks ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		if ( $timestamp > $gmt_time ) break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
		foreach ( (array) $cronhooks as $hook => $args ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
			spawn_cron( $gmt_time );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
			break 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
 * Retrieve supported and filtered Cron recurrences.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
 * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
 * hooking into the 'cron_schedules' filter. The filter accepts an array of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
 * arrays. The outer array has a key that is the name of the schedule or for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
 * example 'weekly'. The value is an array with two keys, one is 'interval' and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
 * the other is 'display'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
 * The 'interval' is a number in seconds of when the cron job should run. So for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
 * The 'display' is the description. For the 'weekly' key, the 'display' would
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
 * be `__( 'Once Weekly' )`.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
 * For your plugin, you will be passed an array. you can easily add your
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
 * schedule by doing the following.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
 *     // Filter parameter variable name is 'array'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
 *     $array['weekly'] = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
 *         'interval' => 604800,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
 *     	   'display'  => __( 'Once Weekly' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
 *     );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
function wp_get_schedules() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	$schedules = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
		'hourly'     => array( 'interval' => HOUR_IN_SECONDS,      'display' => __( 'Once Hourly' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
		'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		'daily'      => array( 'interval' => DAY_IN_SECONDS,       'display' => __( 'Once Daily' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
	 * Filter the non-default cron schedules.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @param array $new_schedules An array of non-default cron schedules. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
 * Retrieve Cron schedule for hook with arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
 * @param string $hook Action hook to execute when cron is run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
 * @param array $args Optional. Arguments to pass to the hook's callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
 * @return string|bool False, if no schedule. Schedule on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
function wp_get_schedule($hook, $args = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	$crons = _get_cron_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	$key = md5(serialize($args));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	if ( empty($crons) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
	foreach ( $crons as $timestamp => $cron ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		if ( isset( $cron[$hook][$key] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			return $cron[$hook][$key]['schedule'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
// Private functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
 * Retrieve cron info array option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
 * @return array CRON info array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
function _get_cron_array()  {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	$cron = get_option('cron');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
	if ( ! is_array($cron) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	if ( !isset($cron['version']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
		$cron = _upgrade_cron_array($cron);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
	unset($cron['version']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
	return $cron;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 * Updates the CRON option with the new CRON array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
 * @param array $cron Cron info array from {@link _get_cron_array()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
function _set_cron_array($cron) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	$cron['version'] = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	update_option( 'cron', $cron );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
 * Upgrade a Cron info array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
 * This function upgrades the Cron info array to version 2.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
 * @param array $cron Cron info array from {@link _get_cron_array()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
 * @return array An upgraded Cron info array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
function _upgrade_cron_array($cron) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
	if ( isset($cron['version']) && 2 == $cron['version'])
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
		return $cron;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
	$new_cron = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	foreach ( (array) $cron as $timestamp => $hooks) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
		foreach ( (array) $hooks as $hook => $args ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
			$key = md5(serialize($args['args']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
			$new_cron[$timestamp][$hook][$key] = $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	$new_cron['version'] = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	update_option( 'cron', $new_cron );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	return $new_cron;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
}