/** * 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()); }
/** * Make CloudFlare API request. * * @since 1.0 * @access public * * @param string $endpoint Endpoint of CloudFlare API URL. * @param array $args Request arguments. * @return WP_Error|array $response The response or WP_Error on failure. */ public function request($endpoint, $args) { // Have we passed limit $requests = $this->can_fetch(); if (!$requests) { return new WP_Error('purge-cache-for-cloudflare-requests-limit', __('Requests limit passed.', 'purge-cache-for-cloudflare'), 429); } // Save new number of requests in this interval WP_Temporary::update('purge_cache_for_cloudflare_requests_limit', $requests + 1, 15 * MINUTE_IN_SECONDS); $defaults = array('headers' => array('X-Auth-Email' => $this->email, 'X-Auth-Key' => $this->api_key, 'Content-Type' => 'application/json')); $r = wp_parse_args($args, $defaults); $response = wp_remote_request($this->base_endpoint . $endpoint, $r); return $response; }
/** * Set cache content for name. * * @access public * * @param string $string Name of cache that is set. * @param mixed $content Value of cache that should be set. * @return bool False if value was not set and true if value was set. */ public static function set($name, $content) { return \WP_Temporary::update(self::key($name), $content, 5 * MINUTE_IN_SECONDS); }