Keine Beschreibung

Beamer.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace MailPoet\Cron\Workers;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Models\ScheduledTask;
  5. use MailPoet\Settings\SettingsController;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. use MailPoetVendor\Carbon\Carbon;
  8. class Beamer extends SimpleWorker {
  9. const TASK_TYPE = 'beamer';
  10. const API_URL = 'https://api.getbeamer.com/v0';
  11. const API_KEY = 'b_neUUX8kIYVEYZqQzSnwhmVggVLA6lT+GzDQOW7hrP38=';
  12. /** @var SettingsController */
  13. private $settings;
  14. public function __construct(
  15. SettingsController $settings,
  16. WPFunctions $wp
  17. ) {
  18. parent::__construct($wp);
  19. $this->settings = $settings;
  20. }
  21. public function processTaskStrategy(ScheduledTask $task, $timer) {
  22. return $this->setLastAnnouncementDate();
  23. }
  24. public function setLastAnnouncementDate() {
  25. $response = $this->wp->wpRemoteGet(self::API_URL . '/posts?published=true&maxResults=1', [
  26. 'headers' => [
  27. 'Beamer-Api-Key' => self::API_KEY,
  28. ],
  29. ]);
  30. $posts = $this->wp->wpRemoteRetrieveBody($response);
  31. if (empty($posts)) return false;
  32. $posts = json_decode($posts);
  33. if (empty($posts) || empty($posts[0]->date)) return false;
  34. $this->settings->set('last_announcement_date', Carbon::createFromTimeString($posts[0]->date)->getTimestamp());
  35. return true;
  36. }
  37. public function getNextRunDate() {
  38. // once a week on a random day of the week, random time of the day
  39. $date = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
  40. return $date
  41. ->next(Carbon::MONDAY)
  42. ->startOfDay()
  43. ->addDays(rand(0, 6))
  44. ->addHours(rand(0, 23))
  45. ->addMinutes(rand(0, 59));
  46. }
  47. }