updated plugin WP Mail SMTP version 2.5.0

This commit is contained in:
2020-10-23 01:19:42 +00:00
committed by Gitium
parent 1047e0b29f
commit 51360a4729
205 changed files with 36345 additions and 921 deletions

View File

@ -77,15 +77,18 @@ abstract class AuthAbstract implements AuthInterface {
protected function update_auth_code( $code ) {
$options = new PluginOptions();
$all = $options->get_all();
// To save in DB.
$all[ $this->mailer_slug ]['auth_code'] = $code;
$updated_settings = [
$this->mailer_slug => [
'auth_code' => $code,
],
];
// To save in currently retrieved options array.
$this->options['auth_code'] = $code;
$options->set( $all );
$options->set( $updated_settings, false, false );
}
/**
@ -98,15 +101,18 @@ abstract class AuthAbstract implements AuthInterface {
protected function update_access_token( $token ) {
$options = new PluginOptions();
$all = $options->get_all();
// To save in DB.
$all[ $this->mailer_slug ]['access_token'] = $token;
$updated_settings = [
$this->mailer_slug => [
'access_token' => $token,
],
];
// To save in currently retrieved options array.
$this->options['access_token'] = $token;
$options->set( $all );
$options->set( $updated_settings, false, false );
}
/**
@ -119,15 +125,18 @@ abstract class AuthAbstract implements AuthInterface {
protected function update_refresh_token( $token ) {
$options = new PluginOptions();
$all = $options->get_all();
// To save in DB.
$all[ $this->mailer_slug ]['refresh_token'] = $token;
$updated_settings = [
$this->mailer_slug => [
'refresh_token' => $token,
],
];
// To save in currently retrieved options array.
$this->options['refresh_token'] = $token;
$options->set( $all );
$options->set( $updated_settings, false, false );
}
/**

View File

@ -92,7 +92,7 @@ class Auth extends AuthAbstract {
'client_id' => $this->options['client_id'],
'client_secret' => $this->options['client_secret'],
'redirect_uris' => array(
self::get_plugin_auth_url(),
self::get_oauth_redirect_url(),
),
)
);
@ -102,7 +102,8 @@ class Auth extends AuthAbstract {
$client->setIncludeGrantedScopes( true );
// We request only the sending capability, as it's what we only need to do.
$client->setScopes( array( Google_Service_Gmail::MAIL_GOOGLE_COM ) );
$client->setRedirectUri( self::get_plugin_auth_url() );
$client->setRedirectUri( self::get_oauth_redirect_url() );
$client->setState( self::get_plugin_auth_url() );
// Apply custom options to the client.
$client = apply_filters( 'wp_mail_smtp_providers_gmail_auth_get_client_custom_options', $client );
@ -115,15 +116,22 @@ class Auth extends AuthAbstract {
$creds = $client->fetchAccessTokenWithAuthCode( $this->options['auth_code'] );
} catch ( \Exception $e ) {
$creds['error'] = $e->getMessage();
Debug::set(
'Mailer: Gmail' . "\r\n" .
$creds['error']
);
}
// Bail if we have an error.
if ( ! empty( $creds['error'] ) ) {
if ( $creds['error'] === 'invalid_client' ) {
$creds['error'] .= PHP_EOL . esc_html__( 'Please make sure your Google Client ID and Secret in the plugin settings are valid. Save the settings and try the Authorization again.' , 'wp-mail-smtp' );
}
Debug::set(
'Mailer: Gmail' . "\r\n" .
$creds['error']
);
return $client;
} else {
Debug::clear();
}
$this->update_access_token( $client->getAccessToken() );
@ -173,7 +181,7 @@ class Auth extends AuthAbstract {
*/
public function process() {
if ( ! ( isset( $_GET['tab'] ) && $_GET['tab'] === 'auth' ) ) {
if ( ! ( isset( $_GET['tab'] ) && $_GET['tab'] === 'auth' ) ) { // phpcs:ignore
wp_safe_redirect( wp_mail_smtp()->get_admin()->get_admin_page_url() );
exit;
}
@ -199,8 +207,8 @@ class Auth extends AuthAbstract {
$scope = '';
$error = '';
if ( isset( $_GET['error'] ) ) {
$error = sanitize_key( $_GET['error'] );
if ( isset( $_GET['error'] ) ) { // phpcs:ignore
$error = sanitize_key( $_GET['error'] ); // phpcs:ignore
}
// In case of any error: display a message to a user.
@ -215,11 +223,11 @@ class Auth extends AuthAbstract {
exit;
}
if ( isset( $_GET['code'] ) ) {
$code = $_GET['code'];
if ( isset( $_GET['code'] ) ) { // phpcs:ignore
$code = urldecode( $_GET['code'] ); // phpcs:ignore
}
if ( isset( $_GET['scope'] ) ) {
$scope = urldecode( $_GET['scope'] );
if ( isset( $_GET['scope'] ) ) { // phpcs:ignore
$scope = urldecode( base64_decode( $_GET['scope'] ) ); // phpcs:ignore
}
// Let's try to get the access token.
@ -328,4 +336,19 @@ class Auth extends AuthAbstract {
return $this->aliases;
}
/**
* Get the Google oAuth 2.0 redirect URL.
*
* This is the URL that Google will redirect after the access to the Gmail account is granted or rejected.
* The below endpoint will then redirect back to the user's WP site (to self::get_plugin_auth_url() URL).
*
* @since 2.5.0
*
* @return string
*/
public static function get_oauth_redirect_url() {
return 'https://connect.wpmailsmtp.com/google/';
}
}

View File

@ -110,6 +110,8 @@ class Mailer extends MailerAbstract {
$this->process_response( $response );
} catch ( \Exception $e ) {
$this->error_message = $e->getMessage();
Debug::set(
'Mailer: Gmail' . "\r\n" .
$this->process_exception_message( $e->getMessage() )
@ -131,6 +133,16 @@ class Mailer extends MailerAbstract {
$this->response = $response;
if ( ! method_exists( $this->response, 'getId' ) ) {
$this->error_message = esc_html__( 'The response object is invalid (missing getId method).', 'wp-mail-smtp' );
} else {
$message_id = $this->response->getId();
if ( empty( $message_id ) ) {
$this->error_message = esc_html__( 'The email message ID is missing.', 'wp-mail-smtp' );
}
}
do_action( 'wp_mail_smtp_providers_gmail_mailer_process_response', $this->response, $this->phpmailer );
}

View File

@ -118,7 +118,7 @@ class Options extends OptionsAbstract {
</div>
<div class="wp-mail-smtp-setting-field">
<input type="text" readonly="readonly" onfocus="this.select();"
value="<?php echo esc_attr( Auth::get_plugin_auth_url() ); ?>"
value="<?php echo esc_attr( Auth::get_oauth_redirect_url() ); ?>"
id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-client_redirect"
/>
<button type="button" class="wp-mail-smtp-btn wp-mail-smtp-btn-md wp-mail-smtp-btn-light-grey wp-mail-smtp-setting-copy"
@ -245,7 +245,7 @@ class Options extends OptionsAbstract {
return;
}
$old_opt = $options->get_all();
$old_opt = $options->get_all_raw();
foreach ( $old_opt[ $this->get_slug() ] as $key => $value ) {
// Unset everything except Client ID and Secret.

View File

@ -23,18 +23,21 @@ abstract class MailerAbstract implements MailerInterface {
* @var int
*/
protected $email_sent_code = 200;
/**
* @since 1.0.0
*
* @var Options
*/
protected $options;
/**
* @since 1.0.0
*
* @var MailCatcherInterface
*/
protected $phpmailer;
/**
* @since 1.0.0
*
@ -50,18 +53,21 @@ abstract class MailerAbstract implements MailerInterface {
* @var string
*/
protected $url = '';
/**
* @since 1.0.0
*
* @var array
*/
protected $headers = array();
/**
* @since 1.0.0
*
* @var array
*/
protected $body = array();
/**
* @since 1.0.0
*
@ -69,6 +75,24 @@ abstract class MailerAbstract implements MailerInterface {
*/
protected $response = array();
/**
* The error message recorded when email sending failed and the error can't be processed from the API response.
*
* @since 2.5.0
*
* @var string
*/
protected $error_message = '';
/**
* Should the email sent by this mailer have its "sent status" verified via its API?
*
* @since 2.5.0
*
* @var bool
*/
protected $verify_sent_status = false;
/**
* Mailer constructor.
*
@ -269,7 +293,7 @@ abstract class MailerAbstract implements MailerInterface {
// Save the error text.
$errors = $response->get_error_messages();
foreach ( $errors as $error ) {
Debug::set( $error );
$this->error_message .= $error . PHP_EOL;
}
return;
@ -342,15 +366,17 @@ abstract class MailerAbstract implements MailerInterface {
}
/**
* The error message when email sending failed.
* Should be overwritten when appropriate.
*
* @since 1.2.0
* @since 2.5.0 Return a non-empty error_message attribute.
*
* @return string
*/
protected function get_response_error() {
public function get_response_error() {
return '';
return ! empty( $this->error_message ) ? $this->error_message : '';
}
/**
@ -461,4 +487,45 @@ abstract class MailerAbstract implements MailerInterface {
],
];
}
/**
* Should the email sent by this mailer have its "sent status" verified via its API?
*
* @since 2.5.0
*
* @return bool
*/
public function should_verify_sent_status() {
return $this->verify_sent_status;
}
/**
* Verify the "sent status" of the provided email log ID.
* The actual verification background task is triggered in the below action hook.
*
* @since 2.5.0
*
* @param int $email_log_id The ID of the email log.
*/
public function verify_sent_status( $email_log_id ) {
if ( ! $this->should_verify_sent_status() ) {
return;
}
do_action( 'wp_mail_smtp_providers_mailer_verify_sent_status', $email_log_id, $this );
}
/**
* Get the name/slug of the current mailer.
*
* @since 2.5.0
*
* @return string
*/
public function get_mailer_name() {
return $this->mailer;
}
}

View File

@ -366,6 +366,27 @@ class Mailer extends MailerAbstract {
);
}
/**
* We might need to do something after the email was sent to the API.
* In this method we preprocess the response from the API.
*
* @since 2.5.0
*
* @param mixed $response Response data.
*/
protected function process_response( $response ) {
parent::process_response( $response );
if (
! is_wp_error( $response ) &&
! empty( $this->response['body']->id )
) {
$this->phpmailer->MessageID = $this->response['body']->id;
$this->verify_sent_status = true;
}
}
/**
* Whether the email is sent or not.
* We basically check the response code from a request to provider.
@ -392,6 +413,8 @@ class Mailer extends MailerAbstract {
esc_html__( 'This could point to an incorrect Domain Name in the plugin settings.', 'wp-mail-smtp' ) . PHP_EOL .
esc_html__( 'Please check the WP Mail SMTP plugin settings and make sure the Mailgun Domain Name setting is correct.', 'wp-mail-smtp' );
$this->error_message = $message;
Debug::set( $message );
return false;
@ -407,7 +430,7 @@ class Mailer extends MailerAbstract {
*
* @return string
*/
protected function get_response_error() {
public function get_response_error() {
$body = (array) wp_remote_retrieve_body( $this->response );
@ -419,6 +442,8 @@ class Mailer extends MailerAbstract {
} else {
$error_text[] = \json_encode( $body['message'] );
}
} elseif ( ! empty( $this->error_message ) ) {
$error_text[] = $this->error_message;
} elseif ( ! empty( $body[0] ) ) {
if ( is_string( $body[0] ) ) {
$error_text[] = $body[0];

View File

@ -356,7 +356,7 @@ abstract class OptionsAbstract implements OptionsInterface {
id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-pass" spellcheck="false" autocomplete="new-password"
/>
<p class="desc">
<?php esc_html_e( 'The password will be stored in plain text. For improved security, we highly recommend using your site\'s WordPress configuration file to set your password.', 'wp-mail-smtp' ); ?>
<?php esc_html_e( 'The password is encrypted in the database, but for improved security we recommend using your site\'s WordPress configuration file to set your password.', 'wp-mail-smtp' ); ?>
<br>
<a href="https://wpmailsmtp.com/docs/how-to-secure-smtp-settings-by-using-constants/" target="_blank" rel="noopener noreferrer">
<strong><?php esc_html_e( 'Learn More', 'wp-mail-smtp' ); ?></strong>

View File

@ -320,7 +320,7 @@ class Mailer extends MailerAbstract {
*
* @return string
*/
protected function get_response_error() {
public function get_response_error() {
$body = (array) wp_remote_retrieve_body( $this->response );
@ -328,7 +328,9 @@ class Mailer extends MailerAbstract {
$info = ! empty( $body['info'] ) ? $body['info'] : '';
$message = '';
if ( is_string( $error ) ) {
if ( ! empty( $this->error_message ) ) {
$message = $this->error_message;
} elseif ( is_string( $error ) ) {
$message = $error . ( ( ! empty( $info ) ) ? ' - ' . $info : '' );
} elseif ( is_array( $error ) ) {
$message = '';

View File

@ -307,7 +307,7 @@ class Mailer extends MailerAbstract {
$filetype = str_replace( ';', '', trim( $attachment[4] ) );
$data[] = array(
'content' => base64_encode( $file ),
'content' => chunk_split( base64_encode( $file ) ), // phpcs:ignore
'type' => $filetype,
'encoding' => 'base64',
'filename' => empty( $attachment[2] ) ? 'file-' . wp_hash( microtime() ) . '.' . $filetype : trim( $attachment[2] ),
@ -388,6 +388,31 @@ class Mailer extends MailerAbstract {
*/
public function set_return_path( $from_email ) {}
/**
* We might need to do something after the email was sent to the API.
* In this method we preprocess the response from the API.
*
* @since 2.5.0
*
* @param mixed $response Response data.
*/
protected function process_response( $response ) {
parent::process_response( $response );
if (
! is_wp_error( $response ) &&
! empty( $this->response['body']->data->message )
) {
preg_match( '/msg_id: (.*)/', $this->response['body']->data->message, $output );
if ( ! empty( $output[1] ) ) {
$this->phpmailer->addCustomHeader( 'X-Msg-ID', $output[1] );
$this->verify_sent_status = true;
}
}
}
/**
* Get a SMTP.com-specific response with a helpful error.
*
@ -405,7 +430,7 @@ class Mailer extends MailerAbstract {
*
* @return string
*/
protected function get_response_error() {
public function get_response_error() {
$body = (array) wp_remote_retrieve_body( $this->response );
@ -415,6 +440,8 @@ class Mailer extends MailerAbstract {
foreach ( (array) $body['data'] as $error_key => $error_message ) {
$error_text[] = $error_key . ' - ' . $error_message;
}
} elseif ( ! empty( $this->error_message ) ) {
$error_text[] = $this->error_message;
}
return implode( PHP_EOL, array_map( 'esc_textarea', $error_text ) );

View File

@ -352,7 +352,7 @@ class Mailer extends MailerAbstract {
*
* @return string
*/
protected function get_response_error() {
public function get_response_error() { // phpcs:ignore
$body = (array) wp_remote_retrieve_body( $this->response );
@ -374,6 +374,8 @@ class Mailer extends MailerAbstract {
$error_text[] = $error->message . ( ! empty( $extra ) ? ' - ' . $extra : '' );
}
}
} elseif ( ! empty( $this->error_message ) ) {
$error_text[] = $this->error_message;
}
return implode( '<br>', array_map( 'esc_textarea', $error_text ) );

View File

@ -318,8 +318,12 @@ class Mailer extends MailerAbstract {
$message = $e->getMessage();
}
$this->error_message = $message;
Debug::set( 'Mailer: Sendinblue' . PHP_EOL . $message );
} catch ( \Exception $e ) {
$this->error_message = $e->getMessage();
Debug::set( 'Mailer: Sendinblue' . PHP_EOL . $e->getMessage() );
return;
@ -338,6 +342,14 @@ class Mailer extends MailerAbstract {
protected function process_response( $response ) {
$this->response = $response;
if (
is_a( $response, 'WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail' ) &&
method_exists( $response, 'getMessageId' )
) {
$this->phpmailer->MessageID = $response->getMessageId();
$this->verify_sent_status = true;
}
}
/**