[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> cron.php (source)

   1  <?php
   2  /**
   3   * WordPress Cron API
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Schedules an event to run only once.
  10   *
  11   * Schedules a hook which will be triggered by WordPress at the specified UTC time.
  12   * The action will trigger when someone visits your WordPress site if the scheduled
  13   * time has passed.
  14   *
  15   * Note that scheduling an event to occur within 10 minutes of an existing event
  16   * with the same action hook will be ignored unless you pass unique `$args` values
  17   * for each scheduled event.
  18   *
  19   * Use wp_next_scheduled() to prevent duplicate events.
  20   *
  21   * Use wp_schedule_event() to schedule a recurring event.
  22   *
  23   * @since 2.1.0
  24   * @since 5.1.0 Return value modified to boolean indicating success or failure,
  25   *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
  26   * @since 5.7.0 The `$wp_error` parameter was added.
  27   *
  28   * @link https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
  29   *
  30   * @param int    $timestamp  Unix timestamp (UTC) for when to next run the event.
  31   * @param string $hook       Action hook to execute when the event is run.
  32   * @param array  $args       Optional. Array containing arguments to pass to the
  33   *                           hook's callback function. Each value in the array
  34   *                           is passed to the callback as an individual parameter.
  35   *                           The array keys are ignored. Default empty array.
  36   *
  37   *                           These arguments are used to uniquely identify the
  38   *                           scheduled event and must match those used when the
  39   *                           event was originally scheduled. If the arguments
  40   *                           do not match exactly, WordPress will treat the
  41   *                           event as different, which can lead to duplicate
  42   *                           cron events being scheduled unintentionally,
  43   *                           excessive growth of the 'cron' option, and
  44   *                           database performance issues.
  45   * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
  46   * @return bool|WP_Error True if event successfully scheduled. False or WP_Error on failure.
  47   *
  48   * @phpstan-return ( $wp_error is false ? bool : true|WP_Error )
  49   */
  50  function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
  51      // Make sure timestamp is a positive integer.
  52      if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
  53          if ( $wp_error ) {
  54              return new WP_Error(
  55                  'invalid_timestamp',
  56                  __( 'Event timestamp must be a valid Unix timestamp.' )
  57              );
  58          }
  59  
  60          return false;
  61      }
  62  
  63      $event = (object) array(
  64          'hook'      => $hook,
  65          'timestamp' => $timestamp,
  66          'schedule'  => false,
  67          'args'      => $args,
  68      );
  69  
  70      /**
  71       * Filter to override scheduling an event.
  72       *
  73       * Returning a non-null value will short-circuit adding the event to the
  74       * cron array, causing the function to return the filtered value instead.
  75       *
  76       * Both single events and recurring events are passed through this filter;
  77       * single events have `$event->schedule` as false, whereas recurring events
  78       * have this set to a recurrence from wp_get_schedules(). Recurring
  79       * events also have the integer recurrence interval set as `$event->interval`.
  80       *
  81       * For plugins replacing wp-cron, it is recommended you check for an
  82       * identical event within ten minutes and apply the {@see 'schedule_event'}
  83       * filter to check if another plugin has disallowed the event before scheduling.
  84       *
  85       * Return true if the event was scheduled, false or a WP_Error if not.
  86       *
  87       * @since 5.1.0
  88       * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
  89       *
  90       * @param null|bool|WP_Error $result   The value to return instead. Default null to continue adding the event.
  91       * @param object             $event    {
  92       *     An object containing an event's data.
  93       *
  94       *     @type string       $hook      Action hook to execute when the event is run.
  95       *     @type int          $timestamp Unix timestamp (UTC) for when to next run the event.
  96       *     @type string|false $schedule  How often the event should subsequently recur.
  97       *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
  98       *     @type int          $interval  Optional. The interval time in seconds for the schedule. Only present for recurring events.
  99       * }
 100       * @param bool               $wp_error Whether to return a WP_Error on failure.
 101       */
 102      $pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
 103  
 104      if ( null !== $pre ) {
 105          if ( $wp_error && false === $pre ) {
 106              return new WP_Error(
 107                  'pre_schedule_event_false',
 108                  __( 'A plugin prevented the event from being scheduled.' )
 109              );
 110          }
 111  
 112          if ( ! $wp_error && is_wp_error( $pre ) ) {
 113              return false;
 114          }
 115  
 116          return $pre;
 117      }
 118  
 119      /*
 120       * Check for a duplicated event.
 121       *
 122       * Don't schedule an event if there's already an identical event
 123       * within 10 minutes.
 124       *
 125       * When scheduling events within ten minutes of the current time,
 126       * all past identical events are considered duplicates.
 127       *
 128       * When scheduling an event with a past timestamp (ie, before the
 129       * current time) all events scheduled within the next ten minutes
 130       * are considered duplicates.
 131       */
 132      $crons = _get_cron_array();
 133  
 134      $key       = md5( serialize( $event->args ) );
 135      $duplicate = false;
 136  
 137      if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) {
 138          $min_timestamp = 0;
 139      } else {
 140          $min_timestamp = $event->timestamp - 10 * MINUTE_IN_SECONDS;
 141      }
 142  
 143      if ( $event->timestamp < time() ) {
 144          $max_timestamp = time() + 10 * MINUTE_IN_SECONDS;
 145      } else {
 146          $max_timestamp = $event->timestamp + 10 * MINUTE_IN_SECONDS;
 147      }
 148  
 149      foreach ( $crons as $event_timestamp => $cron ) {
 150          if ( $event_timestamp < $min_timestamp ) {
 151              continue;
 152          }
 153  
 154          if ( $event_timestamp > $max_timestamp ) {
 155              break;
 156          }
 157  
 158          if ( isset( $cron[ $event->hook ][ $key ] ) ) {
 159              $duplicate = true;
 160              break;
 161          }
 162      }
 163  
 164      if ( $duplicate ) {
 165          if ( $wp_error ) {
 166              return new WP_Error(
 167                  'duplicate_event',
 168                  __( 'A duplicate event already exists.' )
 169              );
 170          }
 171  
 172          return false;
 173      }
 174  
 175      /**
 176       * Modify an event before it is scheduled.
 177       *
 178       * @since 3.1.0
 179       *
 180       * @param object|false $event {
 181       *     An object containing an event's data, or boolean false to prevent the event from being scheduled.
 182       *
 183       *     @type string       $hook      Action hook to execute when the event is run.
 184       *     @type int          $timestamp Unix timestamp (UTC) for when to next run the event.
 185       *     @type string|false $schedule  How often the event should subsequently recur.
 186       *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
 187       *     @type int          $interval  Optional. The interval time in seconds for the schedule. Only present for recurring events.
 188       * }
 189       */
 190      $event = apply_filters( 'schedule_event', $event );
 191  
 192      // A plugin disallowed this event.
 193      if ( ! $event ) {
 194          if ( $wp_error ) {
 195              return new WP_Error(
 196                  'schedule_event_false',
 197                  __( 'A plugin disallowed this event.' )
 198              );
 199          }
 200  
 201          return false;
 202      }
 203  
 204      $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
 205          'schedule' => $event->schedule,
 206          'args'     => $event->args,
 207      );
 208      uksort( $crons, 'strnatcasecmp' );
 209  
 210      return _set_cron_array( $crons, $wp_error );
 211  }
 212  
 213  /**
 214   * Schedules a recurring event.
 215   *
 216   * Schedules a hook which will be triggered by WordPress at the specified interval.
 217   * The action will trigger when someone visits your WordPress site if the scheduled
 218   * time has passed.
 219   *
 220   * Valid values for the recurrence are 'hourly', 'twicedaily', 'daily', and 'weekly'.
 221   * These can be extended using the {@see 'cron_schedules'} filter in wp_get_schedules().
 222   *
 223   * Use wp_next_scheduled() to prevent duplicate events.
 224   *
 225   * Use wp_schedule_single_event() to schedule a non-recurring event.
 226   *
 227   * @since 2.1.0
 228   * @since 5.1.0 Return value modified to boolean indicating success or failure,
 229   *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
 230   * @since 5.7.0 The `$wp_error` parameter was added.
 231   *
 232   * @link https://developer.wordpress.org/reference/functions/wp_schedule_event/
 233   *
 234   * @param int    $timestamp  Unix timestamp (UTC) for when to next run the event.
 235   * @param string $recurrence How often the event should subsequently recur.
 236   *                           See wp_get_schedules() for accepted values.
 237   * @param string $hook       Action hook to execute when the event is run.
 238   * @param array  $args       Optional. Array containing arguments to pass to the
 239   *                           hook's callback function. Each value in the array
 240   *                           is passed to the callback as an individual parameter.
 241   *                           The array keys are ignored. Default empty array.
 242   *
 243   *                           These arguments are used to uniquely identify the
 244   *                           scheduled event and must match those used when the
 245   *                           event was originally scheduled. If the arguments
 246   *                           do not match exactly, WordPress will treat the
 247   *                           event as different, which can lead to duplicate
 248   *                           cron events being scheduled unintentionally,
 249   *                           excessive growth of the 'cron' option, and
 250   *                           database performance issues.
 251   * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
 252   * @return bool|WP_Error True if event successfully scheduled. False or WP_Error on failure.
 253   *
 254   * @phpstan-return ( $wp_error is false ? bool : true|WP_Error )
 255   */
 256  function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
 257      // Make sure timestamp is a positive integer.
 258      if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
 259          if ( $wp_error ) {
 260              return new WP_Error(
 261                  'invalid_timestamp',
 262                  __( 'Event timestamp must be a valid Unix timestamp.' )
 263              );
 264          }
 265  
 266          return false;
 267      }
 268  
 269      $schedules = wp_get_schedules();
 270  
 271      if ( ! isset( $schedules[ $recurrence ] ) ) {
 272          if ( $wp_error ) {
 273              return new WP_Error(
 274                  'invalid_schedule',
 275                  __( 'Event schedule does not exist.' )
 276              );
 277          }
 278  
 279          return false;
 280      }
 281  
 282      $event = (object) array(
 283          'hook'      => $hook,
 284          'timestamp' => $timestamp,
 285          'schedule'  => $recurrence,
 286          'args'      => $args,
 287          'interval'  => $schedules[ $recurrence ]['interval'],
 288      );
 289  
 290      /** This filter is documented in wp-includes/cron.php */
 291      $pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
 292  
 293      if ( null !== $pre ) {
 294          if ( $wp_error && false === $pre ) {
 295              return new WP_Error(
 296                  'pre_schedule_event_false',
 297                  __( 'A plugin prevented the event from being scheduled.' )
 298              );
 299          }
 300  
 301          if ( ! $wp_error && is_wp_error( $pre ) ) {
 302              return false;
 303          }
 304  
 305          return $pre;
 306      }
 307  
 308      /** This filter is documented in wp-includes/cron.php */
 309      $event = apply_filters( 'schedule_event', $event );
 310  
 311      // A plugin disallowed this event.
 312      if ( ! $event ) {
 313          if ( $wp_error ) {
 314              return new WP_Error(
 315                  'schedule_event_false',
 316                  __( 'A plugin disallowed this event.' )
 317              );
 318          }
 319  
 320          return false;
 321      }
 322  
 323      $key = md5( serialize( $event->args ) );
 324  
 325      $crons = _get_cron_array();
 326  
 327      $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
 328          'schedule' => $event->schedule,
 329          'args'     => $event->args,
 330          'interval' => $event->interval,
 331      );
 332      uksort( $crons, 'strnatcasecmp' );
 333  
 334      return _set_cron_array( $crons, $wp_error );
 335  }
 336  
 337  /**
 338   * Reschedules a recurring event.
 339   *
 340   * Mainly for internal use, this takes the Unix timestamp (UTC) of a previously run
 341   * recurring event and reschedules it for its next run.
 342   *
 343   * To change upcoming scheduled events, use wp_schedule_event() to
 344   * change the recurrence frequency.
 345   *
 346   * @since 2.1.0
 347   * @since 5.1.0 Return value modified to boolean indicating success or failure,
 348   *              {@see 'pre_reschedule_event'} filter added to short-circuit the function.
 349   * @since 5.7.0 The `$wp_error` parameter was added.
 350   *
 351   * @param int    $timestamp  Unix timestamp (UTC) for when the event was scheduled.
 352   * @param string $recurrence How often the event should subsequently recur.
 353   *                           See wp_get_schedules() for accepted values.
 354   * @param string $hook       Action hook to execute when the event is run.
 355   * @param array  $args       Optional. Array containing arguments to pass to the
 356   *                           hook's callback function. Each value in the array
 357   *                           is passed to the callback as an individual parameter.
 358   *                           The array keys are ignored. Default empty array.
 359   *
 360   *                           These arguments are used to uniquely identify the
 361   *                           scheduled event and must match those used when the
 362   *                           event was originally scheduled. If the arguments
 363   *                           do not match exactly, WordPress will treat the
 364   *                           event as different, which can lead to duplicate
 365   *                           cron events being scheduled unintentionally,
 366   *                           excessive growth of the 'cron' option, and
 367   *                           database performance issues.
 368   * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
 369   * @return bool|WP_Error True if event successfully rescheduled. False or WP_Error on failure.
 370   *
 371   * @phpstan-return ( $wp_error is false ? bool : true|WP_Error )
 372   */
 373  function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
 374      // Make sure timestamp is a positive integer.
 375      if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
 376          if ( $wp_error ) {
 377              return new WP_Error(
 378                  'invalid_timestamp',
 379                  __( 'Event timestamp must be a valid Unix timestamp.' )
 380              );
 381          }
 382  
 383          return false;
 384      }
 385  
 386      $schedules = wp_get_schedules();
 387      $interval  = 0;
 388  
 389      // First we try to get the interval from the schedule.
 390      if ( isset( $schedules[ $recurrence ] ) ) {
 391          $interval = $schedules[ $recurrence ]['interval'];
 392      }
 393  
 394      // Now we try to get it from the saved interval in case the schedule disappears.
 395      if ( 0 === $interval ) {
 396          $scheduled_event = wp_get_scheduled_event( $hook, $args, $timestamp );
 397  
 398          if ( $scheduled_event && isset( $scheduled_event->interval ) ) {
 399              $interval = $scheduled_event->interval;
 400          }
 401      }
 402  
 403      $event = (object) array(
 404          'hook'      => $hook,
 405          'timestamp' => $timestamp,
 406          'schedule'  => $recurrence,
 407          'args'      => $args,
 408          'interval'  => $interval,
 409      );
 410  
 411      /**
 412       * Filter to override rescheduling of a recurring event.
 413       *
 414       * Returning a non-null value will short-circuit the normal rescheduling
 415       * process, causing the function to return the filtered value instead.
 416       *
 417       * For plugins replacing wp-cron, return true if the event was successfully
 418       * rescheduled, false or a WP_Error if not.
 419       *
 420       * @since 5.1.0
 421       * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
 422       *
 423       * @param null|bool|WP_Error $pre      Value to return instead. Default null to continue adding the event.
 424       * @param object             $event    {
 425       *     An object containing an event's data.
 426       *
 427       *     @type string $hook      Action hook to execute when the event is run.
 428       *     @type int    $timestamp Unix timestamp (UTC) for when to next run the event.
 429       *     @type string $schedule  How often the event should subsequently recur.
 430       *     @type array  $args      Array containing each separate argument to pass to the hook's callback function.
 431       *     @type int    $interval  The interval time in seconds for the schedule.
 432       * }
 433       * @param bool               $wp_error Whether to return a WP_Error on failure.
 434       */
 435      $pre = apply_filters( 'pre_reschedule_event', null, $event, $wp_error );
 436  
 437      if ( null !== $pre ) {
 438          if ( $wp_error && false === $pre ) {
 439              return new WP_Error(
 440                  'pre_reschedule_event_false',
 441                  __( 'A plugin prevented the event from being rescheduled.' )
 442              );
 443          }
 444  
 445          if ( ! $wp_error && is_wp_error( $pre ) ) {
 446              return false;
 447          }
 448  
 449          return $pre;
 450      }
 451  
 452      // Now we assume something is wrong and fail to schedule.
 453      if ( 0 === $interval ) {
 454          if ( $wp_error ) {
 455              return new WP_Error(
 456                  'invalid_schedule',
 457                  __( 'Event schedule does not exist.' )
 458              );
 459          }
 460  
 461          return false;
 462      }
 463  
 464      $now = time();
 465  
 466      if ( $timestamp >= $now ) {
 467          $timestamp = $now + $interval;
 468      } else {
 469          $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
 470      }
 471  
 472      return wp_schedule_event( $timestamp, $recurrence, $hook, $args, $wp_error );
 473  }
 474  
 475  /**
 476   * Unschedules a previously scheduled event.
 477   *
 478   * The `$timestamp` and `$hook` parameters are required so that the event can be
 479   * identified.
 480   *
 481   * @since 2.1.0
 482   * @since 5.1.0 Return value modified to boolean indicating success or failure,
 483   *              {@see 'pre_unschedule_event'} filter added to short-circuit the function.
 484   * @since 5.7.0 The `$wp_error` parameter was added.
 485   *
 486   * @param int    $timestamp Unix timestamp (UTC) of the event.
 487   * @param string $hook      Action hook of the event.
 488   * @param array  $args      Optional. Array containing each separate argument to pass to the hook's callback function.
 489   *                          Although not passed to a callback, these arguments are used to uniquely identify the
 490   *                          event, so they must match those used when originally scheduling the event. If the
 491   *                          arguments do not match exactly, the event will not be found. Default empty array.
 492   * @param bool   $wp_error  Optional. Whether to return a WP_Error on failure. Default false.
 493   * @return bool|WP_Error True if event successfully unscheduled. False or WP_Error on failure.
 494   *
 495   * @phpstan-return ( $wp_error is false ? bool : true|WP_Error )
 496   */
 497  function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
 498      // Make sure timestamp is a positive integer.
 499      if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
 500          if ( $wp_error ) {
 501              return new WP_Error(
 502                  'invalid_timestamp',
 503                  __( 'Event timestamp must be a valid Unix timestamp.' )
 504              );
 505          }
 506  
 507          return false;
 508      }
 509  
 510      /**
 511       * Filter to override unscheduling of events.
 512       *
 513       * Returning a non-null value will short-circuit the normal unscheduling
 514       * process, causing the function to return the filtered value instead.
 515       *
 516       * For plugins replacing wp-cron, return true if the event was successfully
 517       * unscheduled, false or a WP_Error if not.
 518       *
 519       * @since 5.1.0
 520       * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
 521       *
 522       * @param null|bool|WP_Error $pre       Value to return instead. Default null to continue unscheduling the event.
 523       * @param int                $timestamp Unix timestamp (UTC) for when to run the event.
 524       * @param string             $hook      Action hook, the execution of which will be unscheduled.
 525       * @param array              $args      Arguments to pass to the hook's callback function.
 526       * @param bool               $wp_error  Whether to return a WP_Error on failure.
 527       */
 528      $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );
 529  
 530      if ( null !== $pre ) {
 531          if ( $wp_error && false === $pre ) {
 532              return new WP_Error(
 533                  'pre_unschedule_event_false',
 534                  __( 'A plugin prevented the event from being unscheduled.' )
 535              );
 536          }
 537  
 538          if ( ! $wp_error && is_wp_error( $pre ) ) {
 539              return false;
 540          }
 541  
 542          return $pre;
 543      }
 544  
 545      $crons = _get_cron_array();
 546      $key   = md5( serialize( $args ) );
 547  
 548      unset( $crons[ $timestamp ][ $hook ][ $key ] );
 549  
 550      if ( empty( $crons[ $timestamp ][ $hook ] ) ) {
 551          unset( $crons[ $timestamp ][ $hook ] );
 552      }
 553  
 554      if ( empty( $crons[ $timestamp ] ) ) {
 555          unset( $crons[ $timestamp ] );
 556      }
 557  
 558      return _set_cron_array( $crons, $wp_error );
 559  }
 560  
 561  /**
 562   * Unschedules all events attached to the hook with the specified arguments.
 563   *
 564   * Warning: This function may return boolean false, but may also return a non-boolean
 565   * value which evaluates to false. For information about casting to booleans see the
 566   * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
 567   * the `===` operator for testing the return value of this function.
 568   *
 569   * @since 2.1.0
 570   * @since 5.1.0 Return value modified to indicate success or failure,
 571   *              {@see 'pre_clear_scheduled_hook'} filter added to short-circuit the function.
 572   * @since 5.7.0 The `$wp_error` parameter was added.
 573   *
 574   * @param string $hook     Action hook, the execution of which will be unscheduled.
 575   * @param array  $args     Optional. Array containing each separate argument to pass to the hook's callback function.
 576   *                         Although not passed to a callback, these arguments are used to uniquely identify the
 577   *                         event, so they must match those used when originally scheduling the event. If the
 578   *                         arguments do not match exactly, the event will not be found. Default empty array.
 579   * @param bool   $wp_error Optional. Whether to return a WP_Error on failure. Default false.
 580   * @return int|false|WP_Error On success an integer indicating number of events unscheduled (0 indicates no
 581   *                            events were registered with the hook and arguments combination), false or WP_Error
 582   *                            if unscheduling one or more events fail.
 583   *
 584   * @phpstan-return ( int|( $wp_error is false ? false : WP_Error ) )
 585   */
 586  function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) {
 587      /*
 588       * Backward compatibility.
 589       * Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.
 590       */
 591      if ( ! is_array( $args ) ) {
 592          _deprecated_argument(
 593              __FUNCTION__,
 594              '3.0.0',
 595              __( 'This argument has changed to an array to match the behavior of the other cron functions.' )
 596          );
 597  
 598          $args     = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
 599          $wp_error = false;
 600      }
 601  
 602      /**
 603       * Filter to override clearing a scheduled hook.
 604       *
 605       * Returning a non-null value will short-circuit the normal unscheduling
 606       * process, causing the function to return the filtered value instead.
 607       *
 608       * For plugins replacing wp-cron, return the number of events successfully
 609       * unscheduled (zero if no events were registered with the hook) or false
 610       * or a WP_Error if unscheduling one or more events fails.
 611       *
 612       * @since 5.1.0
 613       * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
 614       *
 615       * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the event.
 616       * @param string                  $hook     Action hook, the execution of which will be unscheduled.
 617       * @param array                   $args     Arguments to pass to the hook's callback function.
 618       * @param bool                    $wp_error Whether to return a WP_Error on failure.
 619       */
 620      $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args, $wp_error );
 621  
 622      if ( null !== $pre ) {
 623          if ( $wp_error && false === $pre ) {
 624              return new WP_Error(
 625                  'pre_clear_scheduled_hook_false',
 626                  __( 'A plugin prevented the hook from being cleared.' )
 627              );
 628          }
 629  
 630          if ( ! $wp_error && is_wp_error( $pre ) ) {
 631              return false;
 632          }
 633  
 634          return $pre;
 635      }
 636  
 637      /*
 638       * This logic duplicates wp_next_scheduled().
 639       * It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
 640       * and, wp_next_scheduled() returns the same schedule in an infinite loop.
 641       */
 642      $crons = _get_cron_array();
 643      if ( empty( $crons ) ) {
 644          return 0;
 645      }
 646  
 647      $results = array();
 648      $key     = md5( serialize( $args ) );
 649  
 650      foreach ( $crons as $timestamp => $cron ) {
 651          if ( isset( $cron[ $hook ][ $key ] ) ) {
 652              $results[] = wp_unschedule_event( $timestamp, $hook, $args, true );
 653          }
 654      }
 655  
 656      $errors = array_filter( $results, 'is_wp_error' );
 657      $error  = new WP_Error();
 658  
 659      if ( $errors ) {
 660          if ( $wp_error ) {
 661              array_walk( $errors, array( $error, 'merge_from' ) );
 662  
 663              return $error;
 664          }
 665  
 666          return false;
 667      }
 668  
 669      return count( $results );
 670  }
 671  
 672  /**
 673   * Unschedules all events attached to the hook.
 674   *
 675   * Can be useful for plugins when deactivating to clean up the cron queue.
 676   *
 677   * Warning: This function may return boolean false, but may also return a non-boolean
 678   * value which evaluates to false. For information about casting to booleans see the
 679   * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
 680   * the `===` operator for testing the return value of this function.
 681   *
 682   * @since 4.9.0
 683   * @since 5.1.0 Return value added to indicate success or failure.
 684   * @since 5.7.0 The `$wp_error` parameter was added.
 685   *
 686   * @param string $hook     Action hook, the execution of which will be unscheduled.
 687   * @param bool   $wp_error Optional. Whether to return a WP_Error on failure. Default false.
 688   * @return int|false|WP_Error On success an integer indicating number of events unscheduled (0 indicates no
 689   *                            events were registered on the hook), false or WP_Error if unscheduling fails.
 690   *
 691   * @phpstan-return ( $wp_error is false ? int|false : int|WP_Error )
 692   */
 693  function wp_unschedule_hook( $hook, $wp_error = false ) {
 694      /**
 695       * Filter to override clearing all events attached to the hook.
 696       *
 697       * Returning a non-null value will short-circuit the normal unscheduling
 698       * process, causing the function to return the filtered value instead.
 699       *
 700       * For plugins replacing wp-cron, return the number of events successfully
 701       * unscheduled (zero if no events were registered with the hook). If unscheduling
 702       * one or more events fails then return either a WP_Error object or false depending
 703       * on the value of the `$wp_error` parameter.
 704       *
 705       * @since 5.1.0
 706       * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
 707       *
 708       * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the hook.
 709       * @param string                  $hook     Action hook, the execution of which will be unscheduled.
 710       * @param bool                    $wp_error Whether to return a WP_Error on failure.
 711       */
 712      $pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );
 713  
 714      if ( null !== $pre ) {
 715          if ( $wp_error && false === $pre ) {
 716              return new WP_Error(
 717                  'pre_unschedule_hook_false',
 718                  __( 'A plugin prevented the hook from being cleared.' )
 719              );
 720          }
 721  
 722          if ( ! $wp_error && is_wp_error( $pre ) ) {
 723              return false;
 724          }
 725  
 726          return $pre;
 727      }
 728  
 729      $crons = _get_cron_array();
 730      if ( empty( $crons ) ) {
 731          return 0;
 732      }
 733  
 734      $results = array();
 735  
 736      foreach ( $crons as $timestamp => $args ) {
 737          if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
 738              $results[] = count( $crons[ $timestamp ][ $hook ] );
 739          }
 740  
 741          unset( $crons[ $timestamp ][ $hook ] );
 742  
 743          if ( empty( $crons[ $timestamp ] ) ) {
 744              unset( $crons[ $timestamp ] );
 745          }
 746      }
 747  
 748      /*
 749       * If the results are empty (zero events to unschedule), no attempt
 750       * to update the cron array is required.
 751       */
 752      if ( empty( $results ) ) {
 753          return 0;
 754      }
 755  
 756      $set = _set_cron_array( $crons, $wp_error );
 757  
 758      if ( true === $set ) {
 759          return array_sum( $results );
 760      }
 761  
 762      return $set;
 763  }
 764  
 765  /**
 766   * Retrieves a scheduled event.
 767   *
 768   * Retrieves the full event object for a given event, if no timestamp is specified the next
 769   * scheduled event is returned.
 770   *
 771   * @since 5.1.0
 772   *
 773   * @param string   $hook      Action hook of the event.
 774   * @param array    $args      Optional. Array containing each separate argument to pass to the hook's callback function.
 775   *                            Although not passed to a callback, these arguments are used to uniquely identify the
 776   *                            event, so they should be the same as those used when originally scheduling the event.
 777   *                            Default empty array.
 778   * @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event
 779   *                            is returned. Default null.
 780   * @return stdClass|false {
 781   *     The event object. False if the event does not exist.
 782   *
 783   *     @type string       $hook      Action hook to execute when the event is run.
 784   *     @type int          $timestamp Unix timestamp (UTC) for when to next run the event.
 785   *     @type string|false $schedule  How often the event should subsequently recur.
 786   *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
 787   *     @type int          $interval  Optional. The interval time in seconds for the schedule. Only present for recurring events.
 788   * }
 789   */
 790  function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
 791      /**
 792       * Filter to override retrieving a scheduled event.
 793       *
 794       * Returning a non-null value will short-circuit the normal process,
 795       * returning the filtered value instead.
 796       *
 797       * Return false if the event does not exist, otherwise an event object
 798       * should be returned.
 799       *
 800       * @since 5.1.0
 801       *
 802       * @param null|false|object $pre       Value to return instead. Default null to continue retrieving the event.
 803       * @param string            $hook      Action hook of the event.
 804       * @param array             $args      Array containing each separate argument to pass to the hook's callback function.
 805       *                                     Although not passed to a callback, these arguments are used to uniquely identify
 806       *                                     the event.
 807       * @param int|null          $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event.
 808       */
 809      $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp );
 810  
 811      if ( null !== $pre ) {
 812          return $pre;
 813      }
 814  
 815      if ( null !== $timestamp && ! is_numeric( $timestamp ) ) {
 816          return false;
 817      }
 818  
 819      $crons = _get_cron_array();
 820      if ( empty( $crons ) ) {
 821          return false;
 822      }
 823  
 824      $key = md5( serialize( $args ) );
 825  
 826      if ( ! $timestamp ) {
 827          // Get next event.
 828          $next = false;
 829          foreach ( $crons as $timestamp => $cron ) {
 830              if ( isset( $cron[ $hook ][ $key ] ) ) {
 831                  $next = $timestamp;
 832                  break;
 833              }
 834          }
 835  
 836          if ( ! $next ) {
 837              return false;
 838          }
 839  
 840          $timestamp = $next;
 841      } elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {
 842          return false;
 843      }
 844  
 845      $event = (object) array(
 846          'hook'      => $hook,
 847          'timestamp' => $timestamp,
 848          'schedule'  => $crons[ $timestamp ][ $hook ][ $key ]['schedule'],
 849          'args'      => $args,
 850      );
 851  
 852      if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) {
 853          $event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
 854      }
 855  
 856      return $event;
 857  }
 858  
 859  /**
 860   * Retrieves the timestamp of the next scheduled event for the given hook.
 861   *
 862   * @since 2.1.0
 863   *
 864   * @param string $hook Action hook of the event.
 865   * @param array  $args Optional. Array containing each separate argument to pass to the hook's callback function.
 866   *                     Although not passed to a callback, these arguments are used to uniquely identify the
 867   *                     event, so they must match those used when originally scheduling the event. If the
 868   *                     arguments do not match exactly, the event will not be found. Default empty array.
 869   * @return int|false The Unix timestamp (UTC) of the next time the event will occur. False if the event doesn't exist.
 870   */
 871  function wp_next_scheduled( $hook, $args = array() ) {
 872      $next_event = wp_get_scheduled_event( $hook, $args );
 873  
 874      if ( ! $next_event ) {
 875          return false;
 876      }
 877  
 878      /**
 879       * Filters the timestamp of the next scheduled event for the given hook.
 880       *
 881       * @since 6.8.0
 882       *
 883       * @param int    $timestamp  Unix timestamp (UTC) for when to next run the event.
 884       * @param object $next_event {
 885       *     An object containing an event's data.
 886       *
 887       *     @type string $hook      Action hook of the event.
 888       *     @type int    $timestamp Unix timestamp (UTC) for when to next run the event.
 889       *     @type string $schedule  How often the event should subsequently recur.
 890       *     @type array  $args      Array containing each separate argument to pass to the hook
 891       *                             callback function.
 892       *     @type int    $interval  Optional. The interval time in seconds for the schedule. Only
 893       *                             present for recurring events.
 894       * }
 895       * @param string $hook       Action hook of the event.
 896       * @param array  $args       Array containing each separate argument to pass to the hook
 897       *                           callback function.
 898       */
 899      return apply_filters( 'wp_next_scheduled', $next_event->timestamp, $next_event, $hook, $args );
 900  }
 901  
 902  /**
 903   * Sends a request to run cron through HTTP request that doesn't halt page loading.
 904   *
 905   * @since 2.1.0
 906   * @since 5.1.0 Return values added.
 907   *
 908   * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
 909   * @return bool True if spawned, false if no events spawned.
 910   */
 911  function spawn_cron( $gmt_time = 0 ) {
 912      if ( ! $gmt_time ) {
 913          $gmt_time = microtime( true );
 914      }
 915  
 916      if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
 917          return false;
 918      }
 919  
 920      /*
 921       * Get the cron lock, which is a Unix timestamp of when the last cron was spawned
 922       * and has not finished running.
 923       *
 924       * Multiple processes on multiple web servers can run this code concurrently,
 925       * this lock attempts to make spawning as atomic as possible.
 926       */
 927      $lock = (float) get_transient( 'doing_cron' );
 928  
 929      if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
 930          $lock = 0;
 931      }
 932  
 933      // Don't run if another process is currently running it or more than once every 60 sec.
 934      if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
 935          return false;
 936      }
 937  
 938      // Confidence check.
 939      $crons = wp_get_ready_cron_jobs();
 940      if ( empty( $crons ) ) {
 941          return false;
 942      }
 943  
 944      if ( array_key_first( $crons ) > $gmt_time ) {
 945          return false;
 946      }
 947  
 948      if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
 949          if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
 950              return false;
 951          }
 952  
 953          $doing_wp_cron = sprintf( '%.22F', $gmt_time );
 954          set_transient( 'doing_cron', $doing_wp_cron );
 955  
 956          ob_start();
 957          wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 958          echo ' ';
 959  
 960          // Flush any buffers and send the headers.
 961          wp_ob_end_flush_all();
 962          flush();
 963  
 964          require_once  ABSPATH . 'wp-cron.php';
 965          return true;
 966      }
 967  
 968      // Set the cron lock with the current unix timestamp, when the cron is being spawned.
 969      $doing_wp_cron = sprintf( '%.22F', $gmt_time );
 970      set_transient( 'doing_cron', $doing_wp_cron );
 971  
 972      $cron_url = add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) );
 973  
 974      /**
 975       * Filters the cron request arguments.
 976       *
 977       * @since 3.5.0
 978       * @since 4.5.0 The `$doing_wp_cron` parameter was added.
 979       *
 980       * @param array  $cron_request_array {
 981       *     An array of cron request URL arguments.
 982       *
 983       *     @type string $url  The cron request URL.
 984       *     @type string $key  The Unix timestamp (UTC) of the cron lock with microseconds.
 985       *     @type array  $args {
 986       *         An array of cron request arguments.
 987       *
 988       *         @type int  $timeout   The request timeout in seconds. Default .01 seconds.
 989       *         @type bool $blocking  Whether to set blocking for the request. Default false.
 990       *         @type bool $sslverify Whether SSL should be verified for the request. Default false.
 991       *     }
 992       * }
 993       * @param string $doing_wp_cron      The Unix timestamp (UTC) of the cron lock with microseconds.
 994       */
 995      $cron_request = apply_filters(
 996          'cron_request',
 997          array(
 998              'url'  => $cron_url,
 999              'key'  => $doing_wp_cron,
1000              'args' => array(
1001                  'timeout'   => 0.01,
1002                  'blocking'  => false,
1003                  /** This filter is documented in wp-includes/class-wp-http-streams.php */
1004                  'sslverify' => apply_filters( 'https_local_ssl_verify', false, $cron_url ),
1005              ),
1006          ),
1007          $doing_wp_cron
1008      );
1009  
1010      $result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
1011  
1012      return ! is_wp_error( $result );
1013  }
1014  
1015  /**
1016   * Registers _wp_cron() to run on the {@see 'shutdown'} action.
1017   *
1018   * The spawn_cron() function attempts to make a non-blocking loopback request to `wp-cron.php` (when alternative
1019   * cron is not being used). However, the wp_remote_post() function does not always respect the `timeout` and
1020   * `blocking` parameters. A timeout of `0.01` may end up taking 1 second. When this runs at the {@see 'wp_loaded'}
1021   * action, it increases the Time To First Byte (TTFB) since the HTML cannot be sent while waiting for the cron request
1022   * to initiate. Moving the spawning of cron to the {@see 'shutdown'} hook allows for the server to flush the HTML document to
1023   * the browser while waiting for the request.
1024   *
1025   * @since 2.1.0
1026   * @since 5.1.0 Return value added to indicate success or failure.
1027   * @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper.
1028   * @since 6.9.0 The _wp_cron() callback is moved from {@see 'wp_loaded'} to the {@see 'shutdown'} action,
1029   *              unless `ALTERNATE_WP_CRON` is enabled; the function now always returns void.
1030   */
1031  function wp_cron(): void {
1032      if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
1033          if ( did_action( 'wp_loaded' ) ) {
1034              _wp_cron();
1035          } else {
1036              add_action( 'wp_loaded', '_wp_cron', 20 );
1037          }
1038      } elseif ( doing_action( 'shutdown' ) ) {
1039          _wp_cron();
1040      } else {
1041          add_action( 'shutdown', '_wp_cron' );
1042      }
1043  }
1044  
1045  /**
1046   * Runs scheduled callbacks or spawns cron for all scheduled events.
1047   *
1048   * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
1049   * value which evaluates to FALSE. For information about casting to booleans see the
1050   * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
1051   * the `===` operator for testing the return value of this function.
1052   *
1053   * @since 5.7.0
1054   * @access private
1055   *
1056   * @return int|false On success an integer indicating number of events spawned (0 indicates no
1057   *                   events needed to be spawned), false if spawning fails for one or more events.
1058   */
1059  function _wp_cron() {
1060      // Prevent infinite loops caused by lack of wp-cron.php.
1061      if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' )
1062          || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON )
1063      ) {
1064          return 0;
1065      }
1066  
1067      $crons = wp_get_ready_cron_jobs();
1068      if ( empty( $crons ) ) {
1069          return 0;
1070      }
1071  
1072      $gmt_time = microtime( true );
1073      if ( array_key_first( $crons ) > $gmt_time ) {
1074          return 0;
1075      }
1076  
1077      $schedules = wp_get_schedules();
1078      $results   = array();
1079  
1080      foreach ( $crons as $timestamp => $cronhooks ) {
1081          if ( $timestamp > $gmt_time ) {
1082              break;
1083          }
1084  
1085          foreach ( (array) $cronhooks as $hook => $args ) {
1086              if ( isset( $schedules[ $hook ]['callback'] )
1087                  && ! call_user_func( $schedules[ $hook ]['callback'] )
1088              ) {
1089                  continue;
1090              }
1091  
1092              $results[] = spawn_cron( $gmt_time );
1093              break 2;
1094          }
1095      }
1096  
1097      if ( in_array( false, $results, true ) ) {
1098          return false;
1099      }
1100  
1101      return count( $results );
1102  }
1103  
1104  /**
1105   * Retrieves supported event recurrence schedules.
1106   *
1107   * The default supported recurrences are 'hourly', 'twicedaily', 'daily', and 'weekly'.
1108   * A plugin may add more by hooking into the {@see 'cron_schedules'} filter.
1109   * The filter accepts an array of arrays. The outer array has a key that is the name
1110   * of the schedule, for example 'monthly'. The value is an array with two keys,
1111   * one is 'interval' and the other is 'display'.
1112   *
1113   * The 'interval' is a number in seconds of when the cron job should run.
1114   * So for 'hourly' the time is `HOUR_IN_SECONDS` (`60 * 60` or `3600`). For 'monthly',
1115   * the value would be `MONTH_IN_SECONDS` (`30 * 24 * 60 * 60` or `2592000`).
1116   *
1117   * The 'display' is the description. For the 'monthly' key, the 'display'
1118   * would be `__( 'Once Monthly' )`.
1119   *
1120   * For your plugin, you will be passed an array. You can add your
1121   * schedule by doing the following:
1122   *
1123   *     // Filter parameter variable name is 'array'.
1124   *     $array['monthly'] = array(
1125   *         'interval' => MONTH_IN_SECONDS,
1126   *         'display'  => __( 'Once Monthly' )
1127   *     );
1128   *
1129   * @since 2.1.0
1130   * @since 5.4.0 The 'weekly' schedule was added.
1131   *
1132   * @return array {
1133   *     The array of cron schedules keyed by the schedule name.
1134   *
1135   *     @type array ...$0 {
1136   *         Cron schedule information.
1137   *
1138   *         @type int    $interval The schedule interval in seconds.
1139   *         @type string $display  The schedule display name.
1140   *     }
1141   * }
1142   */
1143  function wp_get_schedules() {
1144      $schedules = array(
1145          'hourly'     => array(
1146              'interval' => HOUR_IN_SECONDS,
1147              'display'  => __( 'Once Hourly' ),
1148          ),
1149          'twicedaily' => array(
1150              'interval' => 12 * HOUR_IN_SECONDS,
1151              'display'  => __( 'Twice Daily' ),
1152          ),
1153          'daily'      => array(
1154              'interval' => DAY_IN_SECONDS,
1155              'display'  => __( 'Once Daily' ),
1156          ),
1157          'weekly'     => array(
1158              'interval' => WEEK_IN_SECONDS,
1159              'display'  => __( 'Once Weekly' ),
1160          ),
1161      );
1162  
1163      /**
1164       * Filters the non-default cron schedules.
1165       *
1166       * @since 2.1.0
1167       *
1168       * @param array $new_schedules {
1169       *     An array of non-default cron schedules keyed by the schedule name. Default empty array.
1170       *
1171       *     @type array ...$0 {
1172       *         Cron schedule information.
1173       *
1174       *         @type int    $interval The schedule interval in seconds.
1175       *         @type string $display  The schedule display name.
1176       *     }
1177       * }
1178       */
1179      return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
1180  }
1181  
1182  /**
1183   * Retrieves the name of the recurrence schedule for an event.
1184   *
1185   * @see wp_get_schedules() for available schedules.
1186   *
1187   * @since 2.1.0
1188   * @since 5.1.0 {@see 'get_schedule'} filter added.
1189   *
1190   * @param string $hook Action hook to identify the event.
1191   * @param array  $args Optional. Arguments passed to the event's callback function.
1192   *                     Default empty array.
1193   * @return string|false Schedule name on success, false if no schedule.
1194   */
1195  function wp_get_schedule( $hook, $args = array() ) {
1196      $schedule = false;
1197      $event    = wp_get_scheduled_event( $hook, $args );
1198  
1199      if ( $event ) {
1200          $schedule = $event->schedule;
1201      }
1202  
1203      /**
1204       * Filters the schedule name for a hook.
1205       *
1206       * @since 5.1.0
1207       *
1208       * @param string|false $schedule Schedule for the hook. False if not found.
1209       * @param string       $hook     Action hook to execute when cron is run.
1210       * @param array        $args     Arguments to pass to the hook's callback function.
1211       */
1212      return apply_filters( 'get_schedule', $schedule, $hook, $args );
1213  }
1214  
1215  /**
1216   * Retrieves cron jobs ready to be run.
1217   *
1218   * Returns the results of _get_cron_array() limited to events ready to be run,
1219   * ie, with a timestamp in the past.
1220   *
1221   * @since 5.1.0
1222   *
1223   * @return array[] Array of cron job arrays ready to be run.
1224   */
1225  function wp_get_ready_cron_jobs() {
1226      /**
1227       * Filter to override retrieving ready cron jobs.
1228       *
1229       * Returning an array will short-circuit the normal retrieval of ready
1230       * cron jobs, causing the function to return the filtered value instead.
1231       *
1232       * @since 5.1.0
1233       *
1234       * @param null|array[] $pre Array of ready cron tasks to return instead. Default null
1235       *                          to continue using results from _get_cron_array().
1236       */
1237      $pre = apply_filters( 'pre_get_ready_cron_jobs', null );
1238  
1239      if ( null !== $pre ) {
1240          return $pre;
1241      }
1242  
1243      $crons    = _get_cron_array();
1244      $gmt_time = microtime( true );
1245      $results  = array();
1246  
1247      foreach ( $crons as $timestamp => $cronhooks ) {
1248          if ( $timestamp > $gmt_time ) {
1249              break;
1250          }
1251  
1252          $results[ $timestamp ] = $cronhooks;
1253      }
1254  
1255      return $results;
1256  }
1257  
1258  //
1259  // Private functions.
1260  //
1261  
1262  /**
1263   * Retrieves cron info array option.
1264   *
1265   * @since 2.1.0
1266   * @since 6.1.0 Return type modified to consistently return an array.
1267   * @access private
1268   *
1269   * @return array<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>>|array{} Array of cron events.
1270   */
1271  function _get_cron_array() {
1272      $cron = get_option( 'cron' );
1273      if ( ! is_array( $cron ) ) {
1274          return array();
1275      }
1276  
1277      /**
1278       * @var array{ version: int, ...<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>> }
1279       *     |array<int, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>> $cron
1280       */
1281      if ( ! isset( $cron['version'] ) ) {
1282          $cron = _upgrade_cron_array( $cron );
1283      }
1284  
1285      unset( $cron['version'] );
1286  
1287      /** @var array<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>> $cron */
1288      return $cron;
1289  }
1290  
1291  /**
1292   * Updates the cron option with the new cron array.
1293   *
1294   * @since 2.1.0
1295   * @since 5.1.0 Return value modified to outcome of update_option().
1296   * @since 5.7.0 The `$wp_error` parameter was added.
1297   *
1298   * @access private
1299   *
1300   * @param array[] $cron     Array of cron info arrays from _get_cron_array().
1301   * @param bool    $wp_error Optional. Whether to return a WP_Error on failure. Default false.
1302   * @return bool|WP_Error True if cron array updated. False or WP_Error on failure.
1303   *
1304   * @phpstan-param array<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>> $cron
1305   * @phpstan-return ( $wp_error is true ? true|WP_Error : bool )
1306   */
1307  function _set_cron_array( $cron, $wp_error = false ) {
1308      if ( ! is_array( $cron ) ) {
1309          $cron = array();
1310      }
1311  
1312      $cron['version'] = 2;
1313  
1314      $result = update_option( 'cron', $cron, true );
1315  
1316      if ( $wp_error && ! $result ) {
1317          return new WP_Error(
1318              'could_not_set',
1319              __( 'The cron event list could not be saved.' )
1320          );
1321      }
1322  
1323      return $result;
1324  }
1325  
1326  /**
1327   * Upgrades a cron info array.
1328   *
1329   * This function upgrades the cron info array to version 2.
1330   *
1331   * @since 2.1.0
1332   * @access private
1333   *
1334   * @param array $cron Cron info array from _get_cron_array().
1335   * @return array An upgraded cron info array.
1336   *
1337   * @phpstan-param array{ version: int, ...<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>> }
1338   *               |array<int, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>> $cron
1339   * @phpstan-return array{ version: 2, ...<int, array<string, array<string, array{ schedule: string|false, args: array<mixed>, interval?: non-negative-int }>>> }
1340   */
1341  function _upgrade_cron_array( $cron ) {
1342      if ( isset( $cron['version'] ) && 2 === $cron['version'] ) {
1343          return $cron;
1344      }
1345  
1346      $new_cron = array();
1347  
1348      foreach ( (array) $cron as $timestamp => $hooks ) {
1349          foreach ( (array) $hooks as $hook => $args ) {
1350              $key = md5( serialize( $args['args'] ?? array() ) );
1351  
1352              $new_cron[ $timestamp ][ $hook ][ $key ] = $args;
1353          }
1354      }
1355  
1356      $new_cron['version'] = 2;
1357  
1358      update_option( 'cron', $new_cron, true );
1359  
1360      return $new_cron;
1361  }


Generated : Wed Sep 30 08:20:29 2026 Cross-referenced by PHPXref