diff --git a/includes/service-providers/mailchimp/class-newspack-newsletters-mailchimp-cached-data.php b/includes/service-providers/mailchimp/class-newspack-newsletters-mailchimp-cached-data.php index f2ab743a7..065989168 100644 --- a/includes/service-providers/mailchimp/class-newspack-newsletters-mailchimp-cached-data.php +++ b/includes/service-providers/mailchimp/class-newspack-newsletters-mailchimp-cached-data.php @@ -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). * @@ -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', @@ -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']; @@ -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", @@ -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( @@ -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' ) @@ -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", @@ -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' ) @@ -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", diff --git a/tests/test-mailchimp-cached-data.php b/tests/test-mailchimp-cached-data.php index e5354cbe2..dc9731c7f 100644 --- a/tests/test-mailchimp-cached-data.php +++ b/tests/test-mailchimp-cached-data.php @@ -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 ); } }