WebOrbiton
v1.0.0.0

StocketBase

51 lines · 1.5 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/database.php';
  6. require_once __DIR__ . '/activity-log.php';
  7. require_once __DIR__ . '/indexnow.php';
  8. ​
  9. final class ProductScheduler
  10. {
  11. private static bool $running = false;
  12. ​
  13. public static function publishDue(): void
  14. {
  15. if (self::$running) {
  16. return;
  17. }
  18. self::$running = true;
  19. ​
  20. try {
  21. $db = Database::site();
  22. $due = $db->query(
  23. "SELECT id FROM products WHERE status = 'scheduled' AND scheduled_at IS NOT NULL AND scheduled_at <= NOW() ORDER BY scheduled_at ASC LIMIT 20"
  24. )->fetchAll(PDO::FETCH_COLUMN);
  25. ​
  26. if (empty($due)) {
  27. return;
  28. }
  29. ​
  30. $publish = $db->prepare(
  31. "UPDATE products SET status = 'published', published_at = scheduled_at, scheduled_at = NULL, updated_at = NOW()
  32. WHERE id = :id AND status = 'scheduled'"
  33. );
  34. ​
  35. foreach ($due as $productId) {
  36. $publish->execute(['id' => (int) $productId]);
  37. if ($publish->rowCount() !== 1) {
  38. continue;
  39. }
  40. ​
  41. ActivityLog::record('product.auto_publish', 'product', (int) $productId, 'scheduled publication', true);
  42. IndexNow::notifyProduct((int) $productId);
  43. }
  44. } catch (PDOException $e) {
  45. error_log('StocketBase: scheduled publishing failed: ' . $e->getMessage());
  46. } finally {
  47. self::$running = false;
  48. }
  49. }
  50. }
  51. ​