campaignId = $campaignId; $this->limit = $limit ? $limit : 10; } #[\ReturnTypeWillChange] public function current() { return $this->emails; } #[\ReturnTypeWillChange] public function key() { return $this->key++; } #[\ReturnTypeWillChange] public function next() { $this->offset = $this->offset; } #[\ReturnTypeWillChange] public function rewind() { $this->offset = 0; } #[\ReturnTypeWillChange] public function valid() { $currentTime = current_time('mysql'); $emails = CampaignEmail::whereIn('status', ['pending', 'scheduled']) ->when($this->campaignId, function ($query) { return $query->where('campaign_id', $this->campaignId); }) ->where('scheduled_at', '<=', $currentTime) ->whereNotNull('scheduled_at') ->with('campaign', 'subscriber') ->orderBy('scheduled_at', 'ASC') ->offset($this->offset) ->limit($this->limit) ->get(); $ids = $emails->pluck('id')->toArray(); if ($ids) { // Update the status to 'processing' for the selected emails global $wpdb; $placeholders = implode(',', array_fill(0, count($ids), '%d')); $query = "UPDATE {$wpdb->prefix}fc_campaign_emails SET status = %s, updated_at = %s, scheduled_at = %s WHERE id IN ($placeholders)"; $wpdb->query($wpdb->prepare($query, array_merge(['processing', $currentTime, $currentTime], $ids))); } $this->emails = $emails; return !$this->emails->isEmpty(); } }