Merged in feature/MAW-855-import-code-into-aws (pull request #2)

code import from pantheon

* code import from pantheon
This commit is contained in:
Tony Volpe
2023-12-04 23:08:14 +00:00
parent 8c9b1312bc
commit 8f4b5efda6
4766 changed files with 185592 additions and 239967 deletions

View File

@@ -79,12 +79,7 @@ class Auth extends AuthAbstract {
)
);
$state = [
wp_create_nonce( 'wp_mail_smtp_provider_client_state' ),
$connection->get_id(),
];
return add_query_arg( 'state', implode( '-', $state ), $auth_url );
return add_query_arg( 'state', self::get_state_param( $connection ), $auth_url );
}
/**
@@ -122,7 +117,12 @@ class Auth extends AuthAbstract {
// We request only the sending capability, as it's what we only need to do.
$client->setScopes( array( Gmail::MAIL_GOOGLE_COM ) );
$client->setRedirectUri( self::get_oauth_redirect_url() );
$client->setState( self::get_plugin_auth_url( $this->connection ) );
if ( self::use_self_oauth_redirect_url() ) {
$client->setState( self::get_state_param( $this->connection ) );
} else {
$client->setState( self::get_plugin_auth_url( $this->connection ) );
}
// Apply custom options to the client.
$client = apply_filters( 'wp_mail_smtp_providers_gmail_auth_get_client_custom_options', $client );
@@ -297,15 +297,21 @@ class Auth extends AuthAbstract {
exit;
}
if ( isset( $_GET['code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['code'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$code = urldecode( $_GET['code'] );
}
if ( isset( $_GET['scope'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$scope = urldecode( base64_decode( $_GET['scope'] ) );
if ( isset( $_GET['scope'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$scope = $_GET['scope'];
if ( self::use_self_oauth_redirect_url() ) {
$scope = urldecode( $scope );
} else {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$scope = urldecode( base64_decode( $scope ) );
}
}
// Let's try to get the access token.
@@ -459,6 +465,48 @@ class Auth extends AuthAbstract {
*/
public static function get_oauth_redirect_url() {
return 'https://connect.wpmailsmtp.com/google/';
if ( self::use_self_oauth_redirect_url() ) {
return remove_query_arg( 'state', self::get_plugin_auth_url() );
} else {
return 'https://connect.wpmailsmtp.com/google/';
}
}
/**
* Get the state parameter for the Google oAuth redirect URL.
*
* @since 3.10.0
*
* @param ConnectionInterface $connection The Connection object.
*
* @return string
*/
private static function get_state_param( $connection ) {
$state = [
wp_create_nonce( 'wp_mail_smtp_provider_client_state' ),
$connection->get_id(),
];
return implode( '-', $state );
}
/**
* Whether to use self website redirect URL for the Google oAuth.
*
* @since 3.10.0
*
* @return bool
*/
private static function use_self_oauth_redirect_url() {
/**
* Filter whether to use self website redirect URL for the Google oAuth.
*
* @since 3.10.0
*
* @param bool $use Whether to use self website redirect URL for the Google oAuth.
*/
return apply_filters( 'wp_mail_smtp_providers_gmail_auth_use_self_oauth_redirect_url', false );
}
}

View File

@@ -56,7 +56,7 @@ class Options extends OptionsAbstract {
<!-- API Key -->
<div id="wp-mail-smtp-setting-row-<?php echo esc_attr( $this->get_slug() ); ?>-api_key" class="wp-mail-smtp-setting-row wp-mail-smtp-setting-row-text wp-mail-smtp-clear">
<div class="wp-mail-smtp-setting-label">
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"><?php esc_html_e( 'Private API Key', 'wp-mail-smtp' ); ?></label>
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"><?php esc_html_e( 'Mailgun API Key', 'wp-mail-smtp' ); ?></label>
</div>
<div class="wp-mail-smtp-setting-field">
<?php if ( $this->connection_options->is_const_defined( $this->get_slug(), 'api_key' ) ) : ?>
@@ -73,12 +73,18 @@ class Options extends OptionsAbstract {
<?php endif; ?>
<p class="desc">
<?php
printf(
/* translators: %s - API key link. */
esc_html__( 'Follow this link to get a Private API Key from Mailgun: %s.', 'wp-mail-smtp' ),
'<a href="https://app.mailgun.com/app/account/security/api_keys" target="_blank" rel="noopener noreferrer">' .
esc_html__( 'Get a Private API Key', 'wp-mail-smtp' ) .
'</a>'
echo wp_kses(
sprintf( /* translators: %s - API key URL. */
__( 'Follow this link to <a href="%s" target="_blank" rel="noopener noreferrer">get a Mailgun API Key</a>. Generate a key in the "Mailgun API Keys" section.', 'wp-mail-smtp' ),
'https://app.mailgun.com/settings/api_security'
),
[
'a' => [
'href' => [],
'rel' => [],
'target' => [],
],
]
);
?>
</p>
@@ -101,7 +107,7 @@ class Options extends OptionsAbstract {
printf(
/* translators: %s - Domain Name link. */
esc_html__( 'Follow this link to get a Domain Name from Mailgun: %s.', 'wp-mail-smtp' ),
'<a href="https://app.mailgun.com/app/domains" target="_blank" rel="noopener noreferrer">' .
'<a href="https://app.mailgun.com/app/sending/domains" target="_blank" rel="noopener noreferrer">' .
esc_html__( 'Get a Domain Name', 'wp-mail-smtp' ) .
'</a>'
);

View File

@@ -3,6 +3,7 @@
namespace WPMailSMTP\Providers;
use WPMailSMTP\ConnectionInterface;
use WPMailSMTP\Helpers\UI;
use WPMailSMTP\Options;
/**
@@ -294,16 +295,16 @@ abstract class OptionsAbstract implements OptionsInterface {
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-autotls"><?php esc_html_e( 'Auto TLS', 'wp-mail-smtp' ); ?></label>
</div>
<div class="wp-mail-smtp-setting-field">
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-autotls">
<input type="checkbox" id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-autotls"
name="wp-mail-smtp[<?php echo esc_attr( $this->get_slug() ); ?>][autotls]" value="yes"
<?php echo $this->connection_options->is_const_defined( $this->get_slug(), 'autotls' ) ? 'disabled' : ''; ?>
<?php checked( true, (bool) $this->connection_options->get( $this->get_slug(), 'autotls' ) ); ?>
/>
<span class="wp-mail-smtp-setting-toggle-switch"></span>
<span class="wp-mail-smtp-setting-toggle-checked-label"><?php esc_html_e( 'On', 'wp-mail-smtp' ); ?></span>
<span class="wp-mail-smtp-setting-toggle-unchecked-label"><?php esc_html_e( 'Off', 'wp-mail-smtp' ); ?></span>
</label>
<?php
UI::toggle(
[
'name' => 'wp-mail-smtp[' . $this->get_slug() . '][autotls]',
'id' => 'wp-mail-smtp-setting-' . $this->get_slug() . '-autotls',
'checked' => (bool) $this->connection_options->get( $this->get_slug(), 'autotls' ),
'disabled' => $this->connection_options->is_const_defined( $this->get_slug(), 'autotls' ),
]
);
?>
<p class="desc">
<?php esc_html_e( 'By default, TLS encryption is automatically used if the server supports it (recommended). In some cases, due to server misconfigurations, this can cause issues and may need to be disabled.', 'wp-mail-smtp' ); ?>
</p>
@@ -316,16 +317,16 @@ abstract class OptionsAbstract implements OptionsInterface {
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-auth"><?php esc_html_e( 'Authentication', 'wp-mail-smtp' ); ?></label>
</div>
<div class="wp-mail-smtp-setting-field">
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-auth">
<input type="checkbox" id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-auth"
name="wp-mail-smtp[<?php echo esc_attr( $this->get_slug() ); ?>][auth]" value="yes"
<?php echo $this->connection_options->is_const_defined( $this->get_slug(), 'auth' ) ? 'disabled' : ''; ?>
<?php checked( true, (bool) $this->connection_options->get( $this->get_slug(), 'auth' ) ); ?>
/>
<span class="wp-mail-smtp-setting-toggle-switch"></span>
<span class="wp-mail-smtp-setting-toggle-checked-label"><?php esc_html_e( 'On', 'wp-mail-smtp' ); ?></span>
<span class="wp-mail-smtp-setting-toggle-unchecked-label"><?php esc_html_e( 'Off', 'wp-mail-smtp' ); ?></span>
</label>
<?php
UI::toggle(
[
'name' => 'wp-mail-smtp[' . $this->get_slug() . '][auth]',
'id' => 'wp-mail-smtp-setting-' . $this->get_slug() . '-auth',
'checked' => (bool) $this->connection_options->get( $this->get_slug(), 'auth' ),
'disabled' => $this->connection_options->is_const_defined( $this->get_slug(), 'auth' ),
]
);
?>
</div>
</div>

View File

@@ -3,10 +3,6 @@
namespace WPMailSMTP\Providers\Sendinblue;
use WPMailSMTP\ConnectionInterface;
use WPMailSMTP\Vendor\SendinBlue\Client\Api\AccountApi;
use WPMailSMTP\Vendor\SendinBlue\Client\Api\SendersApi;
use WPMailSMTP\Vendor\SendinBlue\Client\Api\TransactionalEmailsApi;
use WPMailSMTP\Vendor\SendinBlue\Client\Configuration;
/**
* Class Api is a wrapper for Sendinblue library with handy methods.
@@ -55,51 +51,54 @@ class Api {
* Configure API key authorization: api-key.
*
* @since 1.6.0
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
*
* @return Configuration
* @return null
*/
protected function get_api_config() {
return Configuration::getDefaultConfiguration()->setApiKey( 'api-key', isset( $this->options['api_key'] ) ? $this->options['api_key'] : '' );
_deprecated_function( __METHOD__, '3.9.0' );
return null;
}
/**
* Get the mailer client instance for Account API.
*
* @since 1.6.0
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
*/
public function get_account_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
_deprecated_function( __METHOD__, '3.9.0' );
return new AccountApi( null, $this->get_api_config() );
return null;
}
/**
* Get the mailer client instance for Sender API.
*
* @since 1.6.0
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
*/
public function get_sender_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
_deprecated_function( __METHOD__, '3.9.0' );
return new SendersApi( null, $this->get_api_config() );
return null;
}
/**
* Get the mailer client instance for SMTP API.
*
* @since 1.6.0
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
*/
public function get_smtp_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
_deprecated_function( __METHOD__, '3.9.0' );
return new TransactionalEmailsApi( null, $this->get_api_config() );
return null;
}
/**

View File

@@ -2,13 +2,10 @@
namespace WPMailSMTP\Providers\Sendinblue;
use WPMailSMTP\Admin\DebugEvents\DebugEvents;
use WPMailSMTP\ConnectionInterface;
use WPMailSMTP\Helpers\Helpers;
use WPMailSMTP\MailCatcherInterface;
use WPMailSMTP\Providers\MailerAbstract;
use WPMailSMTP\Vendor\SendinBlue\Client\ApiException;
use WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail;
use WPMailSMTP\Vendor\SendinBlue\Client\Model\SendSmtpEmail;
use WPMailSMTP\WP;
/**
@@ -27,15 +24,41 @@ class Mailer extends MailerAbstract {
*/
protected $email_sent_code = 201;
/**
* Response code for scheduled email.
*
* @since 3.9.0
*
* @var int
*/
protected $email_scheduled_code = 202;
/**
* URL to make an API request to.
* Not actually used, because we use a lib to make requests.
*
* @since 1.6.0
* @since 3.9.0 Update to use Brevo API.
*
* @var string
*/
protected $url = 'https://api.sendinblue.com/v3';
protected $url = 'https://api.brevo.com/v3/smtp/email';
/**
* Mailer constructor.
*
* @since 3.9.0
*
* @param MailCatcherInterface $phpmailer The MailCatcher object.
* @param ConnectionInterface $connection The Connection object.
*/
public function __construct( $phpmailer, $connection = null ) {
parent::__construct( $phpmailer, $connection );
$this->set_header( 'api-key', $this->connection_options->get( $this->mailer, 'api_key' ) );
$this->set_header( 'Accept', 'application/json' );
$this->set_header( 'content-type', 'application/json' );
}
/**
* The list of allowed attachment files extensions.
@@ -51,15 +74,49 @@ class Mailer extends MailerAbstract {
// @formatter:on
/**
* @inheritDoc
* Redefine the way custom headers are processed for this mailer - they should be in body.
*
* @since 1.6.0
* @since 3.9.0
*
* @param array $headers List of key=>value pairs.
*/
public function set_header( $name, $value ) {
public function set_headers( $headers ) {
foreach ( $headers as $header ) {
$name = isset( $header[0] ) ? $header[0] : false;
$value = isset( $header[1] ) ? $header[1] : false;
$this->set_body_header( $name, $value );
}
// Add custom PHPMailer-specific header.
$this->set_body_header( 'X-Mailer', 'WPMailSMTP/Mailer/' . $this->mailer . ' ' . WPMS_PLUGIN_VER );
}
/**
* This mailer supports email-related custom headers inside a body of the message.
*
* @since 3.9.0
*
* @param string $name Key.
* @param string $value Value.
*/
public function set_body_header( $name, $value ) {
$name = sanitize_text_field( $name );
$this->body['headers'][ $name ] = WP::sanitize_value( $value );
if ( empty( $name ) ) {
return;
}
$headers = isset( $this->body['headers'] ) ? (array) $this->body['headers'] : [];
$headers[ $name ] = WP::sanitize_value( $value );
$this->set_body_param(
[
'headers' => $headers,
]
);
}
/**
@@ -263,8 +320,10 @@ class Mailer extends MailerAbstract {
* Get the email body.
*
* @since 1.6.0
* @since 3.9.0 Returns email body array instead of `SendSmtpEmail` object.
* @since 3.10.0 Returns JSON encoded email body instead of array.
*
* @return SendSmtpEmail
* @return string
*/
public function get_body() {
@@ -277,80 +336,91 @@ class Mailer extends MailerAbstract {
*/
$body = apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
return new SendSmtpEmail( $body );
return wp_json_encode( $body );
}
/**
* Use a library to send emails.
* 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 1.6.0
*/
public function send() {
try {
$api = new Api( $this->connection );
$response = $api->get_smtp_client()->sendTransacEmail( $this->get_body() );
DebugEvents::add_debug(
esc_html__( 'An email request was sent to the Brevo API.', 'wp-mail-smtp' )
);
$this->process_response( $response );
} catch ( ApiException $e ) {
$error = json_decode( $e->getResponseBody() );
if ( json_last_error() === JSON_ERROR_NONE && ! empty( $error ) ) {
$message = Helpers::format_error_message( $error->message, $error->code );
} else {
$message = $e->getMessage();
}
$this->error_message = $message;
} catch ( \Exception $e ) {
$this->error_message = $e->getMessage();
}
}
/**
* Save response from the API to use it later.
* All the actually response processing is done in send() method,
* because SendinBlue throws exception if any error occurs.
* @since 3.9.0 Expect a generic class object instead of `CreateSmtpEmail`.
*
* @since 1.6.0
*
* @param CreateSmtpEmail $response The Sendinblue Email object.
* @param mixed $response Response from the API.
*/
protected function process_response( $response ) {
$this->response = $response;
parent::process_response( $response );
if (
is_a( $response, 'WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail' ) &&
method_exists( $response, 'getMessageId' )
) {
$this->phpmailer->MessageID = $response->getMessageId();
if ( $this->has_message_id() ) {
$this->phpmailer->MessageID = $this->response['body']->messageId;
$this->verify_sent_status = true;
}
}
/**
* Get a Sendinblue-specific response with a helpful error.
*
* @since 3.9.0
*
* @return string
*/
public function get_response_error() {
$error_text = [];
if ( ! empty( $this->error_message ) ) {
$error_text[] = $this->error_message;
}
if ( ! empty( $this->response ) ) {
$body = wp_remote_retrieve_body( $this->response );
if ( ! empty( $body->message ) ) {
$error_text[] = Helpers::format_error_message( $body->message, ! empty( $body->code ) ? $body->code : '' );
} else {
$error_text[] = WP::wp_remote_get_response_error_message( $this->response );
}
}
return implode( WP::EOL, array_map( 'esc_textarea', array_filter( $error_text ) ) );
}
/**
* Check whether the response has `messageId` property.
*
* @since 3.9.0
*
* @return bool
*/
private function has_message_id() {
if (
! in_array(
wp_remote_retrieve_response_code( $this->response ),
[ $this->email_sent_code, $this->email_scheduled_code ],
true
) ||
empty( $this->response['body']->messageId )
) {
return false;
}
return true;
}
/**
* Check whether the email was sent.
*
* @since 1.6.0
* @since 3.9.0 Check if `$this->response` has `messageId` property to check if the email was sent.
*
* @return bool
*/
public function is_email_sent() {
$is_sent = false;
if ( $this->response instanceof CreateSmtpEmail ) {
$is_sent = $this->response->valid();
}
/** This filter is documented in src/Providers/MailerAbstract.php. */
return apply_filters( 'wp_mail_smtp_providers_mailer_is_email_sent', $is_sent, $this->mailer );
return apply_filters( 'wp_mail_smtp_providers_mailer_is_email_sent', $this->has_message_id(), $this->mailer ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
}
/**