Making WordPress.org


Ignore:
Timestamp:
05/01/2017 11:00:53 AM (9 years ago)
Author:
ocean90
Message:

Plugin Directory: Update a scheduled job if another i18n import is triggered during the 15 minutes timeframe.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-manager.php

    r5451 r5453  
    4343
    4444        /**
    45          * Returns the latest time that the scheduled hook will run
    46          *
    47          * @param string $hook The hook to look for.
    48          * @param string $when 'last' or 'next' for when the hook runs.
    49          * @return bool|int False on failure, The timestamp on success.
    50          */
     45         * Returns the latest time that the scheduled hook will run.
     46         *
     47         * @param string $hook The hook to look for.
     48         * @param string $when 'last' or 'next' for when the hook runs.
     49         * @return bool|int False on failure, The timestamp on success.
     50         */
    5151        public static function get_scheduled_time( $hook, $when = 'last' ) {
    5252
    … …  
    6464                        if ( isset( $cron[ $hook ] ) ) {
    6565                                foreach ( $cron[ $hook ] as $key => $cron_item ) {
     66                                        // Cavalcade should present this field, if not, bail.
     67                                        if ( empty( $cron_item['_job'] ) ) {
     68                                                continue;
     69                                        }
     70
    6671                                        if ( 'waiting' === $cron_item['_job']->status ) {
    6772                                                $timestamps[] = $timestamp;
    … …  
    8489
    8590        /**
    86          * Reschedules a cavalcade job.
    87          * This requires the usage of Cavalcade, and will fail without it.
    88          *
    89          * @param string $hook The Hook to reschedule.
    90          * @param int $new_timestamp The time to reschedule it to.
    91          * @param int $old_timestamp The specific job to schedule. Optional, will affect first job otherwise.
    92          */
     91         * Returns the current scheduled events of a hook.
     92         *
     93         * @param string   $hook           The hook to look for.
     94         * @param int|bool $next_timestamp Optional. Returns events for a specific timestamp.
     95         * @return array Scheduled events.
     96         */
     97        public static function get_scheduled_events( $hook, $next_timestamp = false ) {
     98
     99                // Flush the Cavalcade jobs cache, we need fresh data from the database.
     100                wp_cache_delete( 'jobs', 'cavalcade-jobs' );
     101
     102                $crons = _get_cron_array();
     103                if ( empty( $crons ) ) {
     104                        return [];
     105                }
     106
     107                $events = [];
     108
     109                foreach ( $crons as $timestamp => $cron ) {
     110                        if ( isset( $cron[ $hook ] ) ) {
     111                                foreach ( $cron[ $hook ] as $key => $cron_item ) {
     112                                        // Cavalcade should present this field, if not, bail.
     113                                        if ( empty( $cron_item['_job'] ) ) {
     114                                                continue;
     115                                        }
     116
     117                                        if ( 'waiting' !== $cron_item['_job']->status ) {
     118                                                continue;
     119                                        }
     120
     121                                        if ( ! $next_timestamp || $next_timestamp === $timestamp ) {
     122                                                $events[] = [
     123                                                        'hook'      => $cron_item['_job']->hook,
     124                                                        'args'      => $cron_item['_job']->args,
     125                                                        'nextrun'   => $timestamp,
     126                                                ];
     127                                        }
     128                                }
     129                        }
     130                }
     131
     132                return $events;
     133        }
     134
     135        /**
     136         * Updates a cavalcade job.
     137         *
     138         * This requires the usage of Cavalcade, and will fail without it.
     139         *
     140         * @param string $hook           The hook to update.
     141         * @param int    $next_timestamp The time of the schedule to update.
     142         * @param array  $data           The data to update.
     143         * @return bool True on success, false on error.
     144         */
     145        public static function update_scheduled_event( $hook, $next_timestamp, $data ) {
     146                // Flush the Cavalcade jobs cache, we need fresh data from the database
     147                wp_cache_delete( 'jobs', 'cavalcade-jobs' );
     148
     149                $crons = _get_cron_array();
     150                foreach ( $crons as $timestamp => $cron ) {
     151                        if ( $next_timestamp !== $timestamp ) {
     152                                continue;
     153                        }
     154
     155                        if ( isset( $cron[ $hook ] ) ) {
     156                                foreach ( $cron[ $hook ] as $key => $event ) {
     157                                        // Cavalcade should present this field, if not, bail.
     158                                        if ( empty( $event['_job'] ) ) {
     159                                                return false;
     160                                        }
     161
     162                                        if ( 'waiting' !== $event['_job']->status ) {
     163                                                return false;
     164                                        }
     165
     166                                        $event['_job']->args = $data['args'];
     167                                        $event['_job']->nextrun = $data['nextrun'];
     168                                        $event['_job']->save();
     169
     170                                        return true;
     171                                }
     172                        }
     173                }
     174
     175                return false;
     176        }
     177
     178        /**
     179         * Reschedules a cavalcade job.
     180         * This requires the usage of Cavalcade, and will fail without it.
     181         *
     182         * @param string   $hook          The hook to reschedule.
     183         * @param int|bool $new_timestamp The time to reschedule it to.
     184         * @param int|bool $old_timestamp The specific job to schedule. Optional, will affect first job otherwise.
     185         * @return bool True on success, false on error.
     186         */
    93187        public static function reschedule_event( $hook, $new_timestamp = false, $old_timestamp = false ) {
    94188                $new_timestamp = $new_timestamp ?: time();
    … …  
    105199                        if ( isset( $cron[ $hook ] ) ) {
    106200                                foreach ( $cron[ $hook ] as $key => $event ) {
    107                                         // Cavalcade should present this field,if not, bail.
     201                                        // Cavalcade should present this field, if not, bail.
    108202                                        if ( empty( $event['_job'] ) ) {
    109203                                                return false;
Note: See TracChangeset for help on using the changeset viewer.