$details) { $existing_id = $this->existingId($trigger); if ($existing_id) { $this->backfillReminder($trigger, $existing_id, $operations_enabled); continue; } $post_id = wp_insert_post([ 'post_type' => Notification::PT, 'post_title' => $details['label'], 'post_status' => 'publish', ]); if (!$post_id || is_wp_error($post_id)) continue; update_post_meta($post_id, 'daytidy_notification_seed_key', $trigger . ':' . Registry::TYPE_EMAIL); update_post_meta($post_id, 'daytidy_notification_trigger', $trigger); update_post_meta($post_id, 'daytidy_notification_type', Registry::TYPE_EMAIL); update_post_meta($post_id, 'daytidy_notification_enabled', 1); update_post_meta($post_id, 'daytidy_notification_templates', Defaults::templates($trigger)); $this->backfillReminder($trigger, absint($post_id), $operations_enabled); } } private function existingId(string $trigger): int { $posts = get_posts([ 'post_type' => Notification::PT, 'post_status' => ['any', 'trash'], 'posts_per_page' => 1, 'fields' => 'ids', 'meta_query' => [ [ 'key' => 'daytidy_notification_seed_key', 'value' => $trigger . ':' . Registry::TYPE_EMAIL, ], ], ]); return absint($posts[0] ?? 0); } private function backfillReminder(string $trigger, int $post_id, bool $operations_enabled): void { if (!$operations_enabled || $trigger !== Registry::TRIGGER_BOOKING_REMINDER_BEFORE_START) return; (new ReminderScheduler())->backfillNotification($post_id); } }