Skip to content

Commit

Permalink
feat: enable disabling the newsletter send failure email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Jun 5, 2024
1 parent 37967f1 commit b40c6b2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public static function get_conditional_tag_support() {
* Manage singleton instances of all descendant service provider classes.
*/
public static function instance() {
if ( empty( self::$instances[ static::class ] ) ) {
// Escape hatch from the OOP logic for tests. When running in PHPUnit, some tests only pass if
// the class is always instantiated and not returned from self::$instances.
$is_test = defined( 'IS_TEST_ENV' ) && IS_TEST_ENV;
if ( $is_test || empty( self::$instances[ static::class ] ) ) {
self::$instances[ static::class ] = new static();
}
return self::$instances[ static::class ];
Expand Down Expand Up @@ -373,29 +376,32 @@ public function send_newsletter( $post ) {
$errors = array_slice( $errors, -10, 10, true );
update_post_meta( $post_id, 'newsletter_send_errors', $errors );

$message = sprintf(
/* translators: %1$s is the campaign title, %2$s is the edit link, %3$s is the error message. */
__(
'Hi,
$email_sending_disabled = defined( 'NEWSPACK_NEWSLETTERS_DISABLE_SEND_FAILURE_EMAIL' ) && NEWSPACK_NEWSLETTERS_DISABLE_SEND_FAILURE_EMAIL;
if ( ! $email_sending_disabled ) {
$message = sprintf(
/* translators: %1$s is the campaign title, %2$s is the edit link, %3$s is the error message. */
__(
'Hi,
A newsletter campaign called "%1$s" failed to send on your site.
You can edit the campaign here: %2$s.
Details of the error message: "%3$s"
',
'newspack-newsletters'
),
$post->post_title,
get_edit_post_link( $post_id ),
$error_message
);
',
'newspack-newsletters'
),
$post->post_title,
get_edit_post_link( $post_id ),
$error_message
);

\wp_mail( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
get_option( 'admin_email' ),
__( 'Sending a newsletter failed', 'newspack-newsletters' ),
$message
);
\wp_mail( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
get_option( 'admin_email' ),
__( 'Sending a newsletter failed', 'newspack-newsletters' ),
$message
);
}
}

return $result;
Expand Down
13 changes: 13 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ function _manually_load_plugin() {
require_once 'mocks/wc-memberships.php';

ini_set( 'error_log', 'php://stdout' ); // phpcs:ignore WordPress.PHP.IniSet.Risky


/**
* Exception to be thrown when wp_die is called.
*
* @param string $message The error message.
* @throws WPDieException The exception.
*/
function handle_wpdie_in_tests( $message ) {
throw new WPDieException( $message ); // phpcs:ignore
}

define( 'IS_TEST_ENV', 1 );
2 changes: 1 addition & 1 deletion tests/test-newsletter-ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

/**
* Newsletters Tracking Test.
* Newsletters Ads Test.
*/
class Newsletters_Newsletter_Ads_Test extends WP_UnitTestCase {
/**
Expand Down
50 changes: 50 additions & 0 deletions tests/test-service-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Test Newsletter Service Provider class.
*
* @package Newspack_Newsletters
*/

/**
* Newsletters Service Provider class Test.
*/
class Newsletters_Newsletter_Service_Provider_Test extends WP_UnitTestCase {
/**
* Test set up.
*/
public function set_up() {
// Set an ESP.
\Newspack_Newsletters::set_service_provider( 'mailchimp' );
// Ensure the API key is not set (might be set by a different test).
delete_option( 'newspack_mailchimp_api_key' );

add_filter(
'wp_die_handler',
function() {
return 'handle_wpdie_in_tests';}
);
}

/**
* Test sending a newsletter.
*/
public function test_service_provider_send_newsletter_unconfigured() {
// Create a draft and then publish, to trigger a sending.
$post_id = self::factory()->post->create(
[
'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT,
'post_status' => 'draft',
]
);

$this->expectException( WPDieException::class );
$this->expectExceptionMessage( 'No Mailchimp API key available.' );

wp_update_post(
[
'ID' => $post_id,
'post_status' => 'publish',
]
);
}
}

0 comments on commit b40c6b2

Please sign in to comment.