Skip to content
This repository has been archived by the owner on Nov 28, 2018. It is now read-only.

inject global smtp queue instance through construct function #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions classes/SMTPMailingQueueAdvancedOptions.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class SMTPMailingQueueAdvancedOptions extends SMTPMailingQueueAdmin {
*/
private $tabName = 'advanced';

public function __construct() {
private $smtpMailingQueue;

public function __construct($smtpMailingQueue) {
parent::__construct();
$this->smtpMailingQueue = $smtpMailingQueue;
$this->init();
}

Expand Down Expand Up @@ -114,19 +117,18 @@ public function page_init() {
* @return array
*/
public function sanitize($input) {
global $smtpMailingQueue;
$sanitary_values = array();
if(isset( $input['queue_limit']))
$sanitary_values['queue_limit'] = intval($input['queue_limit']);
if(isset( $input['wpcron_interval'])) {
$sanitary_values['wpcron_interval'] = intval($input['wpcron_interval']);
$smtpMailingQueue->refreshWpCron();
$this->smtpMailingQueue->refreshWpCron();
}
if(isset($input['dont_use_wpcron'])) {
$sanitary_values['dont_use_wpcron'] = 'dont_use_wpcron';
wp_clear_scheduled_hook('smq_start_queue');
} else
$smtpMailingQueue->refreshWpCron();
$this->smtpMailingQueue->refreshWpCron();
if(isset($input['process_key']))
$sanitary_values['process_key'] = sanitize_text_field($input['process_key']);

Expand Down Expand Up @@ -185,13 +187,12 @@ public function wpcron_interval_callback() {
* Prints checkbox field for selecting whether to use wp_cron or not
*/
public function dont_use_wpcron_callback() {
global $smtpMailingQueue;
printf(
'<input type="checkbox" name="%s[dont_use_wpcron]" id="dont_use_wpcron" value="dont_use_wpcron" %s> <label for="dont_use_wpcron">' . __('Use a real cronjob instead of wp_cron.', 'smtp-mailing-queue') . '</label>',
$this->optionName,
(isset($this->options['dont_use_wpcron']) && $this->options['dont_use_wpcron'] === 'dont_use_wpcron') ? 'checked' : ''
);
echo '<p class="description">' . sprintf(__('Call %s in cron to start processing queue.', 'smtp-mailing-queue'), '<strong>' . $smtpMailingQueue->getCronLink() . '</strong>') . '</p>';
echo '<p class="description">' . sprintf(__('Call %s in cron to start processing queue.', 'smtp-mailing-queue'), '<strong>' . $this->smtpMailingQueue->getCronLink() . '</strong>') . '</p>';
}

/**
Expand Down
12 changes: 6 additions & 6 deletions classes/SMTPMailingQueueOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class SMTPMailingQueueOptions extends SMTPMailingQueueAdmin{
*/
private $optionsSanitized = false;

public function __construct() {
private $smtpMailingQueue;

public function __construct($smtpMailingQueue) {
parent::__construct();
$this->smtpMailingQueue = $smtpMailingQueue;
$this->init();
}

Expand Down Expand Up @@ -143,8 +146,6 @@ public function page_init() {
* @return array
*/
public function sanitize($input) {
global $smtpMailingQueue;

// Fix for issue that options are sanitized twice when no db entry exists
// "It seems the data is passed through the sanitize function twice.[...]
// This should only happen when the option is not yet in the wp_options table."
Expand All @@ -171,7 +172,7 @@ public function sanitize($input) {
if(isset($input['auth_username']))
$sanitary_values['auth_username'] = sanitize_text_field($input['auth_username']);
if(isset($input['auth_password']))
$sanitary_values['auth_password'] = $smtpMailingQueue->encrypt($input['auth_password']);
$sanitary_values['auth_password'] = $this->smtpMailingQueue->encrypt($input['auth_password']);

return $sanitary_values;
}
Expand Down Expand Up @@ -270,11 +271,10 @@ public function auth_username_callback() {
* Prints field for SMTP authentication password
*/
public function auth_password_callback() {
global $smtpMailingQueue;
printf(
'<input class="regular-text" type="password" name="%s[auth_password]" id="auth_password" value="%s">',
$this->optionName,
isset($this->options['auth_password']) ? esc_attr($smtpMailingQueue->decrypt($this->options['auth_password'])) : ''
isset($this->options['auth_password']) ? esc_attr($this->smtpMailingQueue->decrypt($this->options['auth_password'])) : ''
);
}
}
112 changes: 59 additions & 53 deletions classes/SMTPMailingQueueOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class SMTPMailingQueueOriginal {
*
* The default content type is 'text/plain' which does not allow using HTML.
* However, you can set the content type of the email by using the
* 'wp_mail_content_type' filter.
* {@see 'wp_mail_content_type'} filter.
*
* The default charset is based on the charset used on the blog. The charset can
* be set using the 'wp_mail_charset' filter.
* be set using the {@see 'wp_mail_charset'} filter.
*
* @since 1.2.1
*
Expand All @@ -36,7 +36,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
// Compact the input, apply the filters, and extract them back out

/**
* Filter the wp_mail() arguments.
* Filters the wp_mail() arguments.
*
* @since 2.2.0
*
Expand Down Expand Up @@ -78,6 +78,8 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
}

// Headers
$cc = $bcc = $reply_to = array();

if ( empty( $headers ) ) {
$headers = array();
} else {
Expand All @@ -89,8 +91,6 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
$tempheaders = $headers;
}
$headers = array();
$cc = array();
$bcc = array();

// If it's actually got contents
if ( !empty( $tempheaders ) ) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
$from_email = str_replace( '>', '', $from_email );
$from_email = trim( $from_email );

// Avoid setting an empty $from_email.
// Avoid setting an empty $from_email.
} elseif ( '' !== trim( $content ) ) {
$from_email = trim( $content );
}
Expand All @@ -142,7 +142,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
$charset = '';
}

// Avoid setting an empty $content_type.
// Avoid setting an empty $content_type.
} elseif ( '' !== trim( $content ) ) {
$content_type = trim( $content );
}
Expand All @@ -153,6 +153,9 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
case 'bcc':
$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
break;
case 'reply-to':
$reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
break;
default:
// Add it to our grand headers array
$headers[trim( $name )] = trim( $content );
Expand Down Expand Up @@ -191,78 +194,67 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
}

/**
* Filter the email address to send from.
* Filters the email address to send from.
*
* @since 2.2.0
*
* @param string $from_email Email address to send from.
*/
$phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
$from_email = apply_filters( 'wp_mail_from', $from_email );

/**
* Filter the name to associate with the "from" email address.
* Filters the name to associate with the "from" email address.
*
* @since 2.3.0
*
* @param string $from_name Name associated with the "from" email address.
*/
$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
$from_name = apply_filters( 'wp_mail_from_name', $from_name );

$phpmailer->setFrom( $from_email, $from_name, false );

// Set destination addresses
if ( !is_array( $to ) )
$to = explode( ',', $to );

foreach ( (array) $to as $recipient ) {
try {
// Break $recipient into name and address parts if in the format "Foo <[email protected]>"
$recipient_name = '';
if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
if ( count( $matches ) == 3 ) {
$recipient_name = $matches[1];
$recipient = $matches[2];
}
}
$phpmailer->AddAddress( $recipient, $recipient_name);
} catch ( phpmailerException $e ) {
continue;
}
}

// Set mail's subject and body
$phpmailer->Subject = $subject;
$phpmailer->Body = $message;

// Add any CC and BCC recipients
if ( !empty( $cc ) ) {
foreach ( (array) $cc as $recipient ) {
try {
// Break $recipient into name and address parts if in the format "Foo <[email protected]>"
$recipient_name = '';
if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
if ( count( $matches ) == 3 ) {
$recipient_name = $matches[1];
$recipient = $matches[2];
}
}
$phpmailer->AddCc( $recipient, $recipient_name );
} catch ( phpmailerException $e ) {
continue;
}
// Use appropriate methods for handling addresses, rather than treating them as generic headers
$address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );

foreach ( $address_headers as $address_header => $addresses ) {
if ( empty( $addresses ) ) {
continue;
}
}

if ( !empty( $bcc ) ) {
foreach ( (array) $bcc as $recipient) {
foreach ( (array) $addresses as $address ) {
try {
// Break $recipient into name and address parts if in the format "Foo <[email protected]>"
$recipient_name = '';
if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {

if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
if ( count( $matches ) == 3 ) {
$recipient_name = $matches[1];
$recipient = $matches[2];
$address = $matches[2];
}
}
$phpmailer->AddBcc( $recipient, $recipient_name );

switch ( $address_header ) {
case 'to':
$phpmailer->addAddress( $address, $recipient_name );
break;
case 'cc':
$phpmailer->addCc( $address, $recipient_name );
break;
case 'bcc':
$phpmailer->addBcc( $address, $recipient_name );
break;
case 'reply_to':
$phpmailer->addReplyTo( $address, $recipient_name );
break;
}
} catch ( phpmailerException $e ) {
continue;
}
Expand All @@ -278,7 +270,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
$content_type = 'text/plain';

/**
* Filter the wp_mail() content type.
* Filters the wp_mail() content type.
*
* @since 2.3.0
*
Expand All @@ -299,7 +291,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
// Set the content-type and charset

/**
* Filter the default wp_mail() charset.
* Filters the default wp_mail() charset.
*
* @since 2.3.0
*
Expand All @@ -309,7 +301,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm

// Set custom headers
if ( !empty( $headers ) ) {
foreach( (array) $headers as $name => $content ) {
foreach ( (array) $headers as $name => $content ) {
$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
}

Expand Down Expand Up @@ -340,7 +332,21 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm
try {
return $phpmailer->Send();
} catch ( phpmailerException $e ) {

$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
$mail_error_data['phpmailer_exception_code'] = $e->getCode();

/**
* Fires after a phpmailerException is caught.
*
* @since 4.4.0
*
* @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
* containing the mail recipient, subject, message, headers, and attachments.
*/
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );

return false;
}
}
}
}
12 changes: 6 additions & 6 deletions classes/SMTPMailingQueueTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class SMTPMailingQueueTools extends SMTPMailingQueueAdmin {
*/
private $prefill = false;

public function __construct() {
private $smtpMailingQueue;

public function __construct($smtpMailingQueue) {
parent::__construct();
$this->smtpMailingQueue = $smtpMailingQueue;
$this->init();
}

Expand Down Expand Up @@ -251,14 +254,12 @@ protected function showNotice($message, $type = 'error') {
* Processes starting queue processing form
*/
public function startProcessQueue() {
global $smtpMailingQueue;

if(!check_admin_referer('smq-process_queue', 'smq-process_queue_nonce')) {
$this->showNotice(__("Looks like you're not allowed to do this", 'smtp-mailing-queue'));
return;
}

$smtpMailingQueue->callProcessQueue();
$this->smtpMailingQueue->callProcessQueue();

$this->showNotice(__('Emails sent', 'smtp-mailing-queue'), 'updated');
}
Expand All @@ -269,8 +270,7 @@ public function startProcessQueue() {
* @param bool $invalid
*/
private function createListQueue($invalid = false) {
global $smtpMailingQueue;
$data = $smtpMailingQueue->loadDataFromFiles(true, $invalid);
$data = $this->smtpMailingQueue->loadDataFromFiles(true, $invalid);
if(!$data) {
echo '<p>' . __('No mails in queue', 'smtp-mailing-queue') . '</p>';
return;
Expand Down
10 changes: 6 additions & 4 deletions classes/SMTPMailingQueueUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ class SMTPMailingQueueUpdate {
*/
protected $smtpMailingQueue;

/**
public function __construct($smtpMailingQueue)
{
$this->smtpMailingQueue = $smtpMailingQueue;
}

/**
* Handles plugin updates if necessary.
*/
public function update() {
require_once ABSPATH . WPINC . '/class-phpmailer.php';

global $smtpMailingQueue;
$this->smtpMailingQueue = $smtpMailingQueue;

$installedVersion = get_option( "smq_version" );

if(version_compare($installedVersion, $this->smtpMailingQueue->pluginVersion, '='))
Expand Down
8 changes: 4 additions & 4 deletions smtp-mailing-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ function smq_min_reqs() {
require_once('classes/SMTPMailingQueueTools.php');

$smtpMailingQueue = new SMTPMailingQueue(__FILE__);
$smtpMailingQueueUpdate = new SMTPMailingQueueUpdate(__FILE__);
new SMTPMailingQueueOptions();
new SMTPMailingQueueAdvancedOptions();
new SMTPMailingQueueTools();
$smtpMailingQueueUpdate = new SMTPMailingQueueUpdate($smtpMailingQueue);
new SMTPMailingQueueOptions($smtpMailingQueue);
new SMTPMailingQueueAdvancedOptions($smtpMailingQueue);
new SMTPMailingQueueTools($smtpMailingQueue);

// overwriting wp_mail() to store mailing data instead of sending immediately
if (!function_exists('wp_mail') && !isset($_GET['smqProcessQueue'])) {
Expand Down