rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

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,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,9 @@ class Mailer extends MailerAbstract {
* Get the email body.
*
* @since 1.6.0
* @since 3.9.0 Returns email body array instead of `SendSmtpEmail` object.
*
* @return SendSmtpEmail
* @return array
*/
public function get_body() {
@@ -275,82 +333,110 @@ class Mailer extends MailerAbstract {
*
* @param array $body Email body.
*/
$body = apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
return new SendSmtpEmail( $body );
return apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
}
/**
* Use a library to send emails.
* Send email.
*
* @since 1.6.0
* @since 3.9.0 Use API instead of SDK to send email.
*/
public function send() {
try {
$api = new Api( $this->connection );
$response = wp_safe_remote_post(
$this->url,
[
'headers' => $this->get_headers(),
'body' => wp_json_encode( $this->get_body() ),
]
);
$response = $api->get_smtp_client()->sendTransacEmail( $this->get_body() );
$this->process_response( $response );
}
DebugEvents::add_debug(
esc_html__( 'An email request was sent to the Sendinblue API.', 'wp-mail-smtp' )
);
/**
* 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
* @since 3.9.0 Expect a generic class object instead of `CreateSmtpEmail`.
*
* @param mixed $response Response from the API.
*/
protected function process_response( $response ) {
$this->process_response( $response );
} catch ( ApiException $e ) {
$error = json_decode( $e->getResponseBody() );
parent::process_response( $response );
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();
if ( $this->has_message_id() ) {
$this->phpmailer->MessageID = $this->response['body']->messageId;
$this->verify_sent_status = true;
}
}
/**
* 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.
* Get a Sendinblue-specific response with a helpful error.
*
* @since 1.6.0
* @since 3.9.0
*
* @param CreateSmtpEmail $response The Sendinblue Email object.
* @return string
*/
protected function process_response( $response ) {
public function get_response_error() {
$this->response = $response;
$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 (
is_a( $response, 'WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail' ) &&
method_exists( $response, 'getMessageId' )
! in_array(
wp_remote_retrieve_response_code( $this->response ),
[ $this->email_sent_code, $this->email_scheduled_code ],
true
) ||
empty( $this->response['body']->messageId )
) {
$this->phpmailer->MessageID = $response->getMessageId();
$this->verify_sent_status = true;
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
}
/**

View File

@@ -34,11 +34,11 @@ class Options extends OptionsAbstract {
}
$description = sprintf(
wp_kses( /* translators: %1$s - URL to sendinblue.com site. */
__( '<strong><a href="%1$s" target="_blank" rel="noopener noreferrer">Sendinblue</a> is one of our recommended mailers.</strong> It\'s a transactional email provider with scalable price plans, so it\'s suitable for any size of business.<br><br>If you\'re just starting out, you can use Sendinblue\'s free plan to send up to 300 emails a day. You don\'t need to use a credit card to try it out. When you\'re ready, you can upgrade to a higher plan to increase your sending limits.', 'wp-mail-smtp' ) .
wp_kses( /* translators: %1$s - URL to brevo.com site. */
__( '<strong><a href="%1$s" target="_blank" rel="noopener noreferrer">Brevo</a> (formerly Sendinblue) is one of our recommended mailers.</strong> It\'s a transactional email provider with scalable price plans, so it\'s suitable for any size of business.<br><br>If you\'re just starting out, you can use Brevo\'s free plan to send up to 300 emails a day. You don\'t need to use a credit card to try it out. When you\'re ready, you can upgrade to a higher plan to increase your sending limits.', 'wp-mail-smtp' ) .
'<br><br>' .
/* translators: %2$s - URL to wpmailsmtp.com doc. */
__( 'To get started, read our <a href="%2$s" target="_blank" rel="noopener noreferrer">Sendinblue documentation</a>.', 'wp-mail-smtp' ),
__( 'To get started, read our <a href="%2$s" target="_blank" rel="noopener noreferrer">Brevo documentation</a>.', 'wp-mail-smtp' ),
[
'strong' => true,
'br' => true,
@@ -50,7 +50,7 @@ class Options extends OptionsAbstract {
]
),
'https://wpmailsmtp.com/go/sendinblue/',
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/', 'Sendinblue documentation' ) )
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/', 'Brevo documentation' ) )
);
$api_key = $connection->get_options()->get( self::SLUG, 'api_key' );
@@ -59,21 +59,21 @@ class Options extends OptionsAbstract {
$description .= sprintf(
'</p><p class="buttonned"><a href="%1$s" target="_blank" rel="noopener noreferrer" class="wp-mail-smtp-btn wp-mail-smtp-btn-md wp-mail-smtp-btn-blueish">%2$s</a></p>',
'https://wpmailsmtp.com/go/sendinblue/',
esc_html__( 'Get Sendinblue Now (Free)', 'wp-mail-smtp' )
esc_html__( 'Get Brevo Now (Free)', 'wp-mail-smtp' )
);
}
$description .= '<p class="wp-mail-smtp-tooltip">' .
esc_html__( 'Transparency and Disclosure', 'wp-mail-smtp' ) .
'<span class="wp-mail-smtp-tooltip-text">' .
esc_html__( 'We believe in full transparency. The Sendinblue links above are tracking links as part of our partnership with Sendinblue. We can recommend just about any SMTP service, but we only recommend products that we believe will add value to our users.', 'wp-mail-smtp' ) .
esc_html__( 'We believe in full transparency. The Brevo (formerly Sendinblue) links above are tracking links as part of our partnership with Brevo. We can recommend just about any SMTP service, but we only recommend products that we believe will add value to our users.', 'wp-mail-smtp' ) .
'</span></p>';
parent::__construct(
[
'logo_url' => wp_mail_smtp()->assets_url . '/images/providers/sendinblue.svg',
'logo_url' => wp_mail_smtp()->assets_url . '/images/providers/brevo.svg',
'slug' => self::SLUG,
'title' => esc_html__( 'Sendinblue', 'wp-mail-smtp' ),
'title' => esc_html__( 'Brevo', 'wp-mail-smtp' ),
'php' => '5.6',
'description' => $description,
'supports' => [
@@ -128,7 +128,7 @@ class Options extends OptionsAbstract {
<?php
printf( /* translators: %s - link to get an API Key. */
esc_html__( 'Follow this link to get an API Key: %s.', 'wp-mail-smtp' ),
'<a href="https://account.sendinblue.com/advanced/api" target="_blank" rel="noopener noreferrer">' .
'<a href="https://app.brevo.com/settings/keys/api" target="_blank" rel="noopener noreferrer">' .
esc_html__( 'Get v3 API Key', 'wp-mail-smtp' ) .
'</a>'
);
@@ -153,7 +153,7 @@ class Options extends OptionsAbstract {
printf(
wp_kses(
/* translators: %s - URL to Sendinblue documentation on wpmailsmtp.com */
__( 'Please input the sending domain/subdomain you configured in your Sendinblue dashboard. More information can be found in our <a href="%s" target="_blank" rel="noopener noreferrer">Sendinblue documentation</a>.', 'wp-mail-smtp' ),
__( 'Please input the sending domain/subdomain you configured in your Brevo (formerly Sendinblue) dashboard. More information can be found in our <a href="%s" target="_blank" rel="noopener noreferrer">Brevo documentation</a>.', 'wp-mail-smtp' ),
[
'br' => [],
'a' => [
@@ -163,7 +163,7 @@ class Options extends OptionsAbstract {
],
]
),
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/#setup-smtp', 'Sendinblue documentation' ) )
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/#setup-smtp', 'Brevo documentation' ) )
);
?>
</p>