Skip to content

Commit

Permalink
fix: handle missing Mailchimp API key
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Sep 3, 2024
1 parent 28f5b50 commit 83a0d6f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ public static function add_cron_interval( $schedules ) {
return $schedules;
}

/**
* Retrieves an instance of the Mailchimp api
*
* @return DrewM\MailChimp\MailChimp|WP_Error
*/
private static function get_mc_api() {
$api_key = self::get_mc_instance()->api_key();
if ( empty( $api_key ) ) {
return new WP_Error(
'newspack_newsletters_mailchimp_error',
__( 'Missing Mailchimp API key.', 'newspack-newsletters' )
);
}
try {
return new Mailchimp( $api_key );
} catch ( Exception $e ) {
return new WP_Error(
'newspack_newsletters_mailchimp_error',
$e->getMessage()
);
}
}

/**
* Get segments of a given audience (list)
*
Expand Down Expand Up @@ -461,7 +484,10 @@ private static function refresh_cached_data( $list_id ) {
*/
public static function handle_cron() {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Handling cron request to refresh cache' );
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return;
}
$lists_response = ( self::get_mc_instance() )->validate(
$mc->get(
'lists',
Expand Down Expand Up @@ -490,9 +516,13 @@ public static function handle_cron() {
* @return array The audience segments
*/
public static function fetch_segments( $list_id ) {
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$segments = [];

$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return $segments;
}

$saved_segments_response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segments",
Expand All @@ -517,7 +547,10 @@ public static function fetch_segments( $list_id ) {
* @return array The audience interest_categories
*/
private static function fetch_interest_categories( $list_id ) {
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$interest_categories = $list_id ? ( self::get_mc_instance() )->validate(
$mc->get( "lists/$list_id/interest-categories", [ 'count' => 1000 ], 60 ),
__( 'Error retrieving Mailchimp groups.', 'newspack_newsletters' )
Expand All @@ -544,7 +577,10 @@ private static function fetch_interest_categories( $list_id ) {
* @return array The audience tags
*/
public static function fetch_tags( $list_id ) {
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$tags = $list_id ? ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segments",
Expand All @@ -571,7 +607,10 @@ public static function fetch_tags( $list_id ) {
* @return array The list folders
*/
private static function fetch_folders() {
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$response = ( self::get_mc_instance() )->validate(
$mc->get( 'campaign-folders', [ 'count' => 1000 ], 60 ),
__( 'Error retrieving Mailchimp folders.', 'newspack_newsletters' )
Expand All @@ -587,7 +626,10 @@ private static function fetch_folders() {
* @return array The list interest_categories
*/
private static function fetch_merge_fields( $list_id ) {
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/merge-fields",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,14 @@
class Newspack_Newsletters_Mailchimp_Usage_Reports {
const REPORTS_OPTION_NAME = 'newspack_newsletters_mailchimp_usage_reports';

/**
* Retrieves the main Mailchimp instance
*
* @return Newspack_Newsletters_Mailchimp
*/
private static function get_mc_instance() {
return Newspack_Newsletters_Mailchimp::instance();
}

/**
* Retrieves an instance of the Mailchimp api
*
* @return DrewM\MailChimp\MailChimp|WP_Error
*/
private static function get_mc_api() {
try {
return new Mailchimp( self::get_mc_instance()->api_key() );
return new Mailchimp( Newspack_Newsletters_Mailchimp::instance()->api_key() );
} catch ( Exception $e ) {
return new WP_Error(
'newspack_newsletters_mailchimp_error',
Expand Down
28 changes: 28 additions & 0 deletions tests/test-mailchimp-cached-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Class Newsletters Test Mailchimp Cached Data
*
* @package Newspack_Newsletters
*/

/**
* Tests the Mailchimp Cached Data Class.
*/
class Newsletters_Mailchimp_Cached_Data_Test extends WP_UnitTestCase {
/**
* Setup.
*/
public function set_up() {
// Reset the API key.
delete_option( 'newspack_mailchimp_api_key' );
}

/**
* Test the API setup.
*/
public function test_mailchimp_cached_data_api_setup() {
// This tests if an empty API key won't cause the code to error out.
$segments = Newspack_Newsletters_Mailchimp_Cached_Data::fetch_segments( 'list1' );
$this->assertEquals( [], $segments );
}
}

0 comments on commit 83a0d6f

Please sign in to comment.