Skip to content

enhance: Add WordPress Cron for Per-Post Email Batch Processing#27

Open
sapayth wants to merge 1 commit intoweMail:developfrom
sapayth:enhance/add_wp_cron_for_sending_per_post_email
Open

enhance: Add WordPress Cron for Per-Post Email Batch Processing#27
sapayth wants to merge 1 commit intoweMail:developfrom
sapayth:enhance/add_wp_cron_for_sending_per_post_email

Conversation

@sapayth
Copy link
Collaborator

@sapayth sapayth commented Feb 20, 2026

Summary

Fixes timeout issues when sending per-post notification emails to large subscriber lists by implementing WordPress Cron-based batch processing.

Previously, when "Send Emails" was set to "For each Post" (per-post mode), all emails were sent synchronously in a single request. This caused PHP timeouts and memory issues for sites with large subscriber lists (1000+ subscribers).

Changes

Modified publish() method

  • Now collects recipients by format (plain_excerpt, plain_full, html_excerpt, html_full, public)
  • Stores prepared email data (subject, bodies, sender info) in post_meta
  • Schedules batch processing via cron instead of sending directly
  • Preview mode unchanged - still sends immediately for testing

New methods added

Method Purpose
schedule_per_post_email() Stores email data in post_meta and schedules first cron batch
process_per_post_email_batch() Cron callback that processes emails in batches
flatten_recipients() Helper to convert grouped recipients to flat list
cleanup_email_queue() Removes post_meta data when sending complete

Cron hook registered

  • s2_process_per_post_email - triggers every 60 seconds until all emails sent

Configuration

Batch size is filterable:

add_filter( 's2_batch_size', function( $size ) {
    return 100; // Process 100 emails per batch
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant