v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/database.php';
- require_once __DIR__ . '/activity-log.php';
- require_once __DIR__ . '/indexnow.php';
-
- final class ProductScheduler
- {
- private static bool $running = false;
-
- public static function publishDue(): void
- {
- if (self::$running) {
- return;
- }
- self::$running = true;
-
- try {
- $db = Database::site();
- $due = $db->query(
- "SELECT id FROM products WHERE status = 'scheduled' AND scheduled_at IS NOT NULL AND scheduled_at <= NOW() ORDER BY scheduled_at ASC LIMIT 20"
- )->fetchAll(PDO::FETCH_COLUMN);
-
- if (empty($due)) {
- return;
- }
-
- $publish = $db->prepare(
- "UPDATE products SET status = 'published', published_at = scheduled_at, scheduled_at = NULL, updated_at = NOW()
- WHERE id = :id AND status = 'scheduled'"
- );
-
- foreach ($due as $productId) {
- $publish->execute(['id' => (int) $productId]);
- if ($publish->rowCount() !== 1) {
- continue;
- }
-
- ActivityLog::record('product.auto_publish', 'product', (int) $productId, 'scheduled publication', true);
- IndexNow::notifyProduct((int) $productId);
- }
- } catch (PDOException $e) {
- error_log('StocketBase: scheduled publishing failed: ' . $e->getMessage());
- } finally {
- self::$running = false;
- }
- }
- }
-