wp/wp-includes/cron.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   116 	 *
   116 	 *
   117 	 * When scheduling an event with a past timestamp (ie, before the
   117 	 * When scheduling an event with a past timestamp (ie, before the
   118 	 * current time) all events scheduled within the next ten minutes
   118 	 * current time) all events scheduled within the next ten minutes
   119 	 * are considered duplicates.
   119 	 * are considered duplicates.
   120 	 */
   120 	 */
   121 	$crons     = (array) _get_cron_array();
   121 	$crons = _get_cron_array();
       
   122 	if ( ! is_array( $crons ) ) {
       
   123 		$crons = array();
       
   124 	}
       
   125 
   122 	$key       = md5( serialize( $event->args ) );
   126 	$key       = md5( serialize( $event->args ) );
   123 	$duplicate = false;
   127 	$duplicate = false;
   124 
   128 
   125 	if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) {
   129 	if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) {
   126 		$min_timestamp = 0;
   130 		$min_timestamp = 0;
   300 	}
   304 	}
   301 
   305 
   302 	$key = md5( serialize( $event->args ) );
   306 	$key = md5( serialize( $event->args ) );
   303 
   307 
   304 	$crons = _get_cron_array();
   308 	$crons = _get_cron_array();
       
   309 	if ( ! is_array( $crons ) ) {
       
   310 		$crons = array();
       
   311 	}
       
   312 
   305 	$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
   313 	$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
   306 		'schedule' => $event->schedule,
   314 		'schedule' => $event->schedule,
   307 		'args'     => $event->args,
   315 		'args'     => $event->args,
   308 		'interval' => $event->interval,
   316 		'interval' => $event->interval,
   309 	);
   317 	);
  1030  *     );
  1038  *     );
  1031  *
  1039  *
  1032  * @since 2.1.0
  1040  * @since 2.1.0
  1033  * @since 5.4.0 The 'weekly' schedule was added.
  1041  * @since 5.4.0 The 'weekly' schedule was added.
  1034  *
  1042  *
  1035  * @return array
  1043  * @return array[]
  1036  */
  1044  */
  1037 function wp_get_schedules() {
  1045 function wp_get_schedules() {
  1038 	$schedules = array(
  1046 	$schedules = array(
  1039 		'hourly'     => array(
  1047 		'hourly'     => array(
  1040 			'interval' => HOUR_IN_SECONDS,
  1048 			'interval' => HOUR_IN_SECONDS,
  1057 	/**
  1065 	/**
  1058 	 * Filters the non-default cron schedules.
  1066 	 * Filters the non-default cron schedules.
  1059 	 *
  1067 	 *
  1060 	 * @since 2.1.0
  1068 	 * @since 2.1.0
  1061 	 *
  1069 	 *
  1062 	 * @param array $new_schedules An array of non-default cron schedules. Default empty.
  1070 	 * @param array[] $new_schedules An array of non-default cron schedule arrays. Default empty.
  1063 	 */
  1071 	 */
  1064 	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
  1072 	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
  1065 }
  1073 }
  1066 
  1074 
  1067 /**
  1075 /**
  1103  * Returns the results of _get_cron_array() limited to events ready to be run,
  1111  * Returns the results of _get_cron_array() limited to events ready to be run,
  1104  * ie, with a timestamp in the past.
  1112  * ie, with a timestamp in the past.
  1105  *
  1113  *
  1106  * @since 5.1.0
  1114  * @since 5.1.0
  1107  *
  1115  *
  1108  * @return array Cron jobs ready to be run.
  1116  * @return array[] Array of cron job arrays ready to be run.
  1109  */
  1117  */
  1110 function wp_get_ready_cron_jobs() {
  1118 function wp_get_ready_cron_jobs() {
  1111 	/**
  1119 	/**
  1112 	 * Filter to preflight or hijack retrieving ready cron jobs.
  1120 	 * Filter to preflight or hijack retrieving ready cron jobs.
  1113 	 *
  1121 	 *
  1114 	 * Returning an array will short-circuit the normal retrieval of ready
  1122 	 * Returning an array will short-circuit the normal retrieval of ready
  1115 	 * cron jobs, causing the function to return the filtered value instead.
  1123 	 * cron jobs, causing the function to return the filtered value instead.
  1116 	 *
  1124 	 *
  1117 	 * @since 5.1.0
  1125 	 * @since 5.1.0
  1118 	 *
  1126 	 *
  1119 	 * @param null|array $pre Array of ready cron tasks to return instead. Default null
  1127 	 * @param null|array[] $pre Array of ready cron tasks to return instead. Default null
  1120 	 *                        to continue using results from _get_cron_array().
  1128 	 *                          to continue using results from _get_cron_array().
  1121 	 */
  1129 	 */
  1122 	$pre = apply_filters( 'pre_get_ready_cron_jobs', null );
  1130 	$pre = apply_filters( 'pre_get_ready_cron_jobs', null );
  1123 	if ( null !== $pre ) {
  1131 	if ( null !== $pre ) {
  1124 		return $pre;
  1132 		return $pre;
  1125 	}
  1133 	}
  1126 
  1134 
  1127 	$crons = _get_cron_array();
  1135 	$crons = _get_cron_array();
  1128 
  1136 	if ( ! is_array( $crons ) ) {
  1129 	if ( false === $crons ) {
       
  1130 		return array();
  1137 		return array();
  1131 	}
  1138 	}
  1132 
  1139 
  1133 	$gmt_time = microtime( true );
  1140 	$gmt_time = microtime( true );
  1134 	$keys     = array_keys( $crons );
  1141 	$keys     = array_keys( $crons );
  1155  * Retrieve cron info array option.
  1162  * Retrieve cron info array option.
  1156  *
  1163  *
  1157  * @since 2.1.0
  1164  * @since 2.1.0
  1158  * @access private
  1165  * @access private
  1159  *
  1166  *
  1160  * @return array|false Cron info array on success, false on failure.
  1167  * @return array[]|false Array of cron info arrays on success, false on failure.
  1161  */
  1168  */
  1162 function _get_cron_array() {
  1169 function _get_cron_array() {
  1163 	$cron = get_option( 'cron' );
  1170 	$cron = get_option( 'cron' );
  1164 	if ( ! is_array( $cron ) ) {
  1171 	if ( ! is_array( $cron ) ) {
  1165 		return false;
  1172 		return false;
  1181  * @since 5.1.0 Return value modified to outcome of update_option().
  1188  * @since 5.1.0 Return value modified to outcome of update_option().
  1182  * @since 5.7.0 The `$wp_error` parameter was added.
  1189  * @since 5.7.0 The `$wp_error` parameter was added.
  1183  *
  1190  *
  1184  * @access private
  1191  * @access private
  1185  *
  1192  *
  1186  * @param array $cron     Cron info array from _get_cron_array().
  1193  * @param array[] $cron     Array of cron info arrays from _get_cron_array().
  1187  * @param bool  $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  1194  * @param bool    $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  1188  * @return bool|WP_Error True if cron array updated. False or WP_Error on failure.
  1195  * @return bool|WP_Error True if cron array updated. False or WP_Error on failure.
  1189  */
  1196  */
  1190 function _set_cron_array( $cron, $wp_error = false ) {
  1197 function _set_cron_array( $cron, $wp_error = false ) {
       
  1198 	if ( ! is_array( $cron ) ) {
       
  1199 		$cron = array();
       
  1200 	}
       
  1201 
  1191 	$cron['version'] = 2;
  1202 	$cron['version'] = 2;
  1192 	$result          = update_option( 'cron', $cron );
  1203 	$result          = update_option( 'cron', $cron );
  1193 
  1204 
  1194 	if ( $wp_error && ! $result ) {
  1205 	if ( $wp_error && ! $result ) {
  1195 		return new WP_Error(
  1206 		return new WP_Error(