Skip to content

Commit

Permalink
fix: let the exceptions be thrown (#1698)
Browse files Browse the repository at this point in the history
* fix: let the exceptions be thrown

* fix: clear errors when successfully fetching lists
  • Loading branch information
leogermani authored Nov 28, 2024
1 parent 656befb commit 85746b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,6 @@ 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 audiences (lists).
*
Expand Down Expand Up @@ -576,10 +553,8 @@ public static function handle_cron() {
* @return array|WP_Error The audiences, or WP_Error if there was an error.
*/
public static function fetch_lists( $limit = null ) {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );

$lists_response = ( self::get_mc_instance() )->validate(
$mc->get(
'lists',
Expand All @@ -595,6 +570,7 @@ public static function fetch_lists( $limit = null ) {
if ( ! $limit ) {
update_option( self::get_lists_cache_key(), $lists_response['lists'], false ); // auto-load false.
update_option( self::get_cache_date_key(), time(), false ); // auto-load false.
self::clear_errors();
}

return $lists_response['lists'];
Expand All @@ -610,10 +586,7 @@ public static function fetch_lists( $limit = null ) {
* @return array The audience segment
*/
public static function fetch_segment( $segment_id, $list_id ) {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return $mc;
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segments/$segment_id",
Expand All @@ -640,10 +613,7 @@ public static function fetch_segment( $segment_id, $list_id ) {
public static function fetch_segments( $list_id, $limit = null ) {
$segments = [];

$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return $segments;
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );

$saved_segments_response = ( self::get_mc_instance() )->validate(
$mc->get(
Expand Down Expand Up @@ -671,10 +641,7 @@ public static function fetch_segments( $list_id, $limit = null ) {
* @return array The audience interest_categories
*/
private static function fetch_interest_categories( $list_id, $limit = null ) {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$interest_categories = $list_id ? ( self::get_mc_instance() )->validate(
$mc->get( "lists/$list_id/interest-categories", [ 'count' => $limit ?? 1000 ], 60 ),
__( 'Error retrieving Mailchimp groups.', 'newspack_newsletters' )
Expand Down Expand Up @@ -703,10 +670,7 @@ private static function fetch_interest_categories( $list_id, $limit = null ) {
* @return array The audience tags
*/
public static function fetch_tags( $list_id, $limit = null ) {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$tags = $list_id ? ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segments",
Expand All @@ -733,10 +697,7 @@ public static function fetch_tags( $list_id, $limit = null ) {
* @return array The list folders
*/
private static function fetch_folders() {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$response = ( self::get_mc_instance() )->validate(
$mc->get( 'campaign-folders', [ 'count' => 1000 ], 60 ),
__( 'Error retrieving Mailchimp folders.', 'newspack_newsletters' )
Expand All @@ -752,10 +713,7 @@ private static function fetch_folders() {
* @return array The list interest_categories
*/
private static function fetch_merge_fields( $list_id ) {
$mc = self::get_mc_api();
if ( \is_wp_error( $mc ) ) {
return [];
}
$mc = new Mailchimp( ( self::get_mc_instance() )->api_key() );
$response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/merge-fields",
Expand Down
4 changes: 2 additions & 2 deletions tests/test-mailchimp-cached-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function set_up() {
* 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.
// Makes sure cached data fetch_methods throw an exception in case of error.
$this->expectException( Exception::class );
$segments = Newspack_Newsletters_Mailchimp_Cached_Data::fetch_segments( 'list1' );
$this->assertEquals( [], $segments );
}
}

0 comments on commit 85746b8

Please sign in to comment.