Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: let the exceptions be thrown #1698

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}