This commit is contained in:
Javier Casares 2026-02-18 16:07:17 +00:00
commit 64b13fe7aa
5 changed files with 74 additions and 11 deletions

View file

@ -18,6 +18,9 @@
* @since 2.2.0
*/
// Start output buffering to prevent header warnings from plugins.
ob_start();
// Security: CLI-only execution.
if ( php_sapi_name() !== 'cli' ) {
http_response_code( 403 );
@ -56,12 +59,12 @@ if ( $site_url ) {
$_SERVER['HTTPS'] = 'on';
}
// Set REQUEST_URI to root.
$_SERVER['REQUEST_URI'] = '/';
// Set REQUEST_URI from URL path (critical for MultiSite subdirectory detection).
$_SERVER['REQUEST_URI'] = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '/';
}
}
// Load WordPress.
// Load WordPress with CLI context flags.
define( 'DOING_CRON', true );
// Find wp-load.php (3 levels up from plugin directory).
@ -74,11 +77,37 @@ if ( ! file_exists( $wp_load_path ) ) {
require_once $wp_load_path;
// Discard any output from WordPress initialization (headers, plugin warnings, etc.).
ob_end_clean();
// Load Newsletter base classes if not already loaded
if ( ! class_exists( 'NewsletterAddon' ) ) {
$newsletter_base = WP_PLUGIN_DIR . '/newsletter/classes/NewsletterAddon.php';
if ( file_exists( $newsletter_base ) ) {
require_once $newsletter_base;
} else {
fwrite( STDERR, "Error: Newsletter plugin base addon class not found\n" );
exit( 1 );
}
}
if ( ! class_exists( 'NewsletterMailerAddon' ) ) {
$newsletter_mailer = WP_PLUGIN_DIR . '/newsletter/classes/NewsletterMailerAddon.php';
if ( file_exists( $newsletter_mailer ) ) {
require_once $newsletter_mailer;
} else {
fwrite( STDERR, "Error: Newsletter plugin mailer addon class not found\n" );
exit( 1 );
}
}
// Load plugin if not already loaded (happens in DOING_CRON context)
$plugin_manually_loaded = false;
if ( ! defined( 'ROBOTSTXT_SMTP_NEWSLETTER_VERSION' ) ) {
$plugin_file = __DIR__ . '/robotstxt-smtp-newsletter.php';
if ( file_exists( $plugin_file ) ) {
require_once $plugin_file;
$plugin_manually_loaded = true;
} else {
fwrite( STDERR, "Error: Newsletter SMTP plugin file not found\n" );
exit( 1 );
@ -91,6 +120,12 @@ if ( ! defined( 'ROBOTSTXT_SMTP_NEWSLETTER_VERSION' ) ) {
exit( 1 );
}
// If we manually loaded the plugin, we need to initialize it manually
// (normally this happens via newsletter_loaded hook, but that doesn't fire in DOING_CRON)
if ( $plugin_manually_loaded ) {
\Robotstxt_SMTP_Newsletter\Plugin::instance_init();
}
// Get queue and statistics instances.
$queue = \Robotstxt_SMTP_Newsletter\Queue::get_instance();
$stats = \Robotstxt_SMTP_Newsletter\Statistics::get_instance();