public function schedule() { if ($this->is_scheduled()) { return new WP_Error('hm_backdrop_scheduled', __('Task is already scheduled to run', 'hm_backdrop')); } $data = array('callback' => $this->callback, 'params' => $this->params); WP_Temporary::set('hm_backdrop-' . $this->key, $data, 5 * MINUTE_IN_SECONDS); add_action('shutdown', array($this, 'spawn_server')); return true; }
/** * Process items from queue. * * By default, every six minutes send ten emails. * * @since 1.0 * @access public */ public function process_queue() { // Look for existing addresses $existing = $this->get_queue(); if (!is_array($existing) && $existing) { return false; } // Check how much emails are already sent in this interval $sent = WP_Temporary::get('simple_email_queue_sent'); if (!$sent) { $sent = 0; } /* * Maximum number of allowed email to send * is difference between maximum allowed and * number of sent emails in this interval. */ $max = $this->max() - $sent; $num_sent = 0; foreach ($existing as $key => $value) { if ($num_sent >= $max) { break; } $email_to = key($value); $email_key = $value[$email_to]; $this->send_email($email_to, $email_key); // Remove item from array unset($existing[$key]); // Increase number of sent emails $num_sent++; } // Save temporary that stores existing of temporary based on existence mail in queue if ($existing) { WP_Temporary::set('simple_email_queue_exist', 1, WEEK_IN_SECONDS); } else { WP_Temporary::delete('simple_email_queue_exist'); } // Save new queue $this->set_queue($existing); // Save new number of sent emails in this interval $new_sent = $sent + $num_sent; WP_Temporary::update('simple_email_queue_sent', $new_sent, $this->interval()); }
/** * Update the value of a temporary with existing timeout. * * You do not need to serialize values. If the value needs to be serialized, then * it will be serialized before it is updated. * * @since 1.0.0 * @access public * * @param string $temporary Temporary name. Expected to not be SQL-escaped. Must be * 45 characters or fewer in length. * @param mixed $value Temporary value. Must be serializable if non-scalar. * Expected to not be SQL-escaped. * @param int $expiration Optional. Time until expiration in seconds. Default 0. * @return bool False if value was not updated and true if value was updated. */ public static function update($temporary, $value, $expiration = 0) { /** * Filter a specific temporary before its value is updated. * * The dynamic portion of the hook name, `$temporary`, refers to the temporary name. * * @since 1.0.0 * * @param mixed $value New value of temporary. * @param string $temporary Temporary name. */ $value = apply_filters('pre_update_temporary_' . $temporary, $value, $temporary); $temporary_option = '_temporary_' . $temporary; // If temporary doesn't exist, create new one, // otherwise update it with new value if (false === WP_Temporary::get($temporary)) { $result = WP_Temporary::set($temporary, $value, $expiration); } else { $temporary_option = '_temporary_' . $temporary; $result = update_option($temporary_option, $value); } if ($result) { /** * Fires after the value for a specific temporary has been updated. * * The dynamic portion of the hook name, `$temporary`, refers to the temporary name. * * @since 1.0.0 * * @param mixed $value Temporary value. * @param int $expiration Time until expiration in seconds. Default 0. * @param string $temporary Temporary name. */ do_action('update_temporary_' . $temporary, $value, $expiration, $temporary); /** * Fires after the value for a temporary has been updated. * * @since 1.0.0 * * @param string $temporary The name of the temporary. * @param mixed $value Temporary value. * @param int $expiration Time until expiration in seconds. Default 0. */ do_action('updated_temporary', $temporary, $value, $expiration); } return $result; }