This commit is contained in:
Javier Casares 2026-02-18 16:04:13 +00:00
commit ed2cc2f70a
5 changed files with 96 additions and 10 deletions

View file

@ -126,6 +126,9 @@ class Plugin extends \NewsletterMailerAddon {
// Load text domain.
add_action( 'init', array( $this, 'load_textdomain' ) );
// Force batch size in Newsletter cron.
add_filter( 'newsletter_max_emails', array( $this, 'filter_max_emails' ), 999 );
// Register admin pages.
if ( is_admin() ) {
require_once ROBOTSTXT_SMTP_NEWSLETTER_PATH . 'admin/class-settings-page.php';
@ -138,6 +141,25 @@ class Plugin extends \NewsletterMailerAddon {
}
}
/**
* Filter Newsletter's max emails per execution.
*
* Forces Newsletter to use our configured batch_size.
*
* @param int $max_emails Newsletter's calculated max emails.
*
* @return int Our configured batch_size.
*/
public function filter_max_emails( $max_emails ) {
// Only apply if our addon is enabled.
$settings = $this->get_settings();
if ( empty( $settings['enabled'] ) ) {
return $max_emails;
}
return absint( $settings['batch_size'] ?? 10 );
}
/**
* Load plugin text domain for translations.
*
@ -172,10 +194,19 @@ class Plugin extends \NewsletterMailerAddon {
// Calculate optimal speed (emails per hour) based on rate limits.
$speed = $this->calculate_optimal_speed( $smtp_settings );
// CRITICAL: For queue-based architecture, speed must be high enough
// that Newsletter loads all emails at once. Newsletter calculates
// batch_size = speed / (60 / cron_interval). With default 5-minute cron:
// batch_size = speed / 12. To get batch_size = configured value,
// we need: speed = batch_size * 12.
$batch_size = $settings['batch_size'] ?? 10;
$speed_multiplier = 12; // Assumes 5-minute cron (12 executions per hour).
$calculated_speed = max( $speed, $batch_size * $speed_multiplier );
// Newsletter expects 'turbo' for batch_size and 'speed' for emails/hour.
$mailer_options = array(
'turbo' => $settings['batch_size'] ?? 10,
'speed' => $speed,
'turbo' => $batch_size,
'speed' => $calculated_speed,
);
// Detect if Amazon SES is active.
@ -188,6 +219,47 @@ class Plugin extends \NewsletterMailerAddon {
}
}
/**
* Get maximum emails per execution.
*
* Newsletter checks this method to determine batch size.
*
* @return int
*/
public function get_max_emails() {
$settings = $this->get_settings();
return absint( $settings['batch_size'] ?? 10 );
}
/**
* Get batch size.
*
* Alternative method that Newsletter may check.
*
* @return int
*/
public function get_batch() {
return $this->get_max_emails();
}
/**
* Override Newsletter's calculation of emails per run.
*
* Newsletter normally calculates: batch = speed / runs_per_hour
* For queue-based architecture, we want to enqueue all emails at once.
*
* @return int Configured batch_size.
*/
public function get_emails_per_run() {
$settings = $this->get_settings();
if ( empty( $settings['enabled'] ) ) {
// If addon is disabled, use parent's calculation.
return parent::get_emails_per_run();
}
return absint( $settings['batch_size'] ?? 10 );
}
/**
* Calculate optimal speed (emails per hour) based on rate limits.
*