/**
  * @param string $action_id
  * @param string $message
  * @param DateTime $date
  *
  * @return string The log entry ID
  */
 public function log($action_id, $message, DateTime $date = NULL)
 {
     if (empty($date)) {
         $date = as_get_datetime_object();
     } else {
         $date = clone $date;
     }
     $comment_id = $this->create_wp_comment($action_id, $message, $date);
     return $comment_id;
 }
 /**
  * @param string $hook The hook to trigger when this action runs
  * @param array $args Args to pass when the hook is triggered
  * @param int $first Unix timestamp for the first run
  * @param int $schedule A cron definition string
  * @param string $group A group to put the action in
  *
  * @return string The ID of the stored action
  */
 public function cron($hook, $args = array(), $first = NULL, $schedule = NULL, $group = '')
 {
     if (empty($schedule)) {
         return $this->single($hook, $args, $first, $group);
     }
     $date = as_get_datetime_object($first);
     $cron = CronExpression::factory($schedule);
     $schedule = new ActionScheduler_CronSchedule($date, $cron);
     $action = new ActionScheduler_Action($hook, $args, $schedule, $group);
     return $this->store($action);
 }
 public function mark_failures()
 {
     $timeout = apply_filters('action_scheduler_failure_period', $this->five_minutes);
     if ($timeout < 0) {
         return;
     }
     $cutoff = as_get_datetime_object($timeout . ' seconds ago');
     $actions_to_reset = $this->store->query_actions(array('status' => ActionScheduler_Store::STATUS_RUNNING, 'modified' => $cutoff, 'modified_compare' => '<=', 'per_page' => apply_filters('action_scheduler_cleanup_batch_size', 20)));
     foreach ($actions_to_reset as $action_id) {
         $this->store->mark_failure($action_id);
         do_action('action_scheduler_failed_action', $action_id, $timeout);
     }
 }
 /** Deprecated **/
 public static function get_datetime_object($when = null, $timezone = 'UTC')
 {
     _deprecated_function(__METHOD__, '2.0', 'wcs_add_months()');
     return as_get_datetime_object($when, $timezone);
 }
 public function __wakeup()
 {
     $this->date = as_get_datetime_object($this->timestamp);
 }
 /**
  * @param string $claim_id
  * @param int $limit
  * @param DateTime $before_date Should use UTC timezone.
  * @return int The number of actions that were claimed
  * @throws RuntimeException
  */
 protected function claim_actions($claim_id, $limit, DateTime $before_date = NULL)
 {
     /** @var wpdb $wpdb */
     global $wpdb;
     $date = is_null($before_date) ? as_get_datetime_object() : clone $before_date;
     // can't use $wpdb->update() because of the <= condition, using post_modified to take advantage of indexes
     $sql = "UPDATE {$wpdb->posts} SET post_password = %s, post_modified_gmt = %s, post_modified = %s WHERE post_type = %s AND post_status = %s AND post_password = '' AND post_date_gmt <= %s ORDER BY menu_order ASC, post_date_gmt ASC LIMIT %d";
     $sql = $wpdb->prepare($sql, array($claim_id, current_time('mysql', true), current_time('mysql'), self::POST_TYPE, 'pending', $date->format('Y-m-d H:i:s'), $limit));
     $rows_affected = $wpdb->query($sql);
     if ($rows_affected === false) {
         throw new RuntimeException(__('Unable to claim actions. Database error.', 'action-scheduler'));
     }
     return (int) $rows_affected;
 }
 protected function schedule_next_instance(ActionScheduler_Action $action)
 {
     $next = $action->get_schedule()->next(as_get_datetime_object());
     if ($next) {
         $this->store->save_action($action, $next);
     }
 }
 /**
  * Print the content for our custom columns.
  *
  * @param string $column_name The key for the column for which we should output our content.
  * @param int $post_id The ID of the 'scheduled-action' post for which this row relates.
  * @return void
  */
 public static function list_table_column_content($column_name, $post_id)
 {
     global $post;
     $action = ActionScheduler::store()->fetch_action($post_id);
     $action_title = 'trash' == $post->post_status ? $post->post_title : $action->get_hook();
     $recurrence = 'trash' == $post->post_status ? 0 : $action->get_schedule();
     $next_timestamp = as_get_datetime_object($post->post_date_gmt)->format('U');
     $status = get_post_status($post_id);
     switch ($column_name) {
         case 'hook':
             echo $action_title;
             break;
         case 'status':
             if ('publish' == $status) {
                 _e('Complete', 'action-scheduler');
             } else {
                 echo ucfirst($status);
             }
             break;
         case 'args':
             $action_args = 'trash' == $post->post_status ? $post->post_content : $action->get_args();
             if (is_array($action_args)) {
                 foreach ($action_args as $key => $value) {
                     printf("<code>%s => %s</code><br/>", $key, $value);
                 }
             }
             break;
         case 'recurrence':
             if (method_exists($recurrence, 'interval_in_seconds')) {
                 echo self::human_interval($recurrence->interval_in_seconds());
             } else {
                 _e('Non-repeating', 'action-scheduler');
             }
             break;
         case 'scheduled':
             echo get_date_from_gmt(gmdate('Y-m-d H:i:s', $next_timestamp), 'Y-m-d H:i:s');
             if (gmdate('U') > $next_timestamp) {
                 printf(__(' (%s ago)', 'action-scheduler'), human_time_diff(gmdate('U'), $next_timestamp));
             } else {
                 echo ' (' . human_time_diff(gmdate('U'), $next_timestamp) . ')';
             }
             break;
         case 'modified':
             echo get_post_modified_time('Y-m-d H:i:s');
             $modified_timestamp = get_post_modified_time('U', true);
             if (gmdate('U') > $modified_timestamp) {
                 printf(__(' (%s ago)', 'action-scheduler'), human_time_diff(gmdate('U'), $modified_timestamp));
             } else {
                 echo ' (' . human_time_diff(gmdate('U'), $modified_timestamp) . ')';
             }
             break;
         case 'claim':
             echo $post->post_password;
             break;
     }
 }
Beispiel #9
0
/**
 * Find scheduled actions
 *
 * @param array $args Possible arguments, with their default values:
 *        'hook' => '' - the name of the action that will be triggered
 *        'args' => NULL - the args array that will be passed with the action
 *        'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
 *        'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
 *        'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
 *        'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
 *        'group' => '' - the group the action belongs to
 *        'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
 *        'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
 *        'per_page' => 5 - Number of results to return
 *        'offset' => 0
 *        'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
 *        'order' => 'ASC'
 *
 * @param string $return_format OBJECT, ARRAY_A, or ids
 *
 * @return array
 */
function wc_get_scheduled_actions($args = array(), $return_format = OBJECT)
{
    $store = ActionScheduler::store();
    foreach (array('date', 'modified') as $key) {
        if (isset($args[$key])) {
            $args[$key] = as_get_datetime_object($args[$key]);
        }
    }
    $ids = $store->query_actions($args);
    if ($return_format == 'ids' || $return_format == 'int') {
        return $ids;
    }
    $actions = array();
    foreach ($ids as $action_id) {
        $actions[$action_id] = $store->fetch_action($action_id);
    }
    if ($return_format == ARRAY_A) {
        foreach ($actions as $action_id => $action_object) {
            $actions[$action_id] = get_object_vars($action_object);
        }
    }
    return $actions;
}