From b165075401ad82f317dd2d102dde4c547b0811ef Mon Sep 17 00:00:00 2001 From: Jefferson Rabb Date: Thu, 23 Apr 2020 23:45:36 -0400 Subject: [PATCH] feat: clearer error messages - Better error messages for all points of Mailchimp API failure. - Support for NEWSPACK_NEWSLETTERS_DEBUG_MODE wp-config flag. If set to true Mailchimp errors will be shown exactly as received. --- .../class-newspack-newsletters-renderer.php | 2 +- includes/class-newspack-newsletters.php | 66 +++++++++++++++---- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/includes/class-newspack-newsletters-renderer.php b/includes/class-newspack-newsletters-renderer.php index 1ab235682..a2883207b 100644 --- a/includes/class-newspack-newsletters-renderer.php +++ b/includes/class-newspack-newsletters-renderer.php @@ -554,7 +554,7 @@ public static function render_html_email( $post ) { ) ); if ( 401 === intval( $request['response']['code'] ) ) { - throw new Exception( __( 'MJML error.', 'newspack_newsletters' ) ); + throw new Exception( __( 'MJML rendering error.', 'newspack_newsletters' ) ); } return is_wp_error( $request ) ? $request : json_decode( $request['body'] )->html; } diff --git a/includes/class-newspack-newsletters.php b/includes/class-newspack-newsletters.php index e56f59a23..28968317e 100644 --- a/includes/class-newspack-newsletters.php +++ b/includes/class-newspack-newsletters.php @@ -481,7 +481,8 @@ public static function api_set_campaign_settings( $request ) { 'settings' => $settings, ]; $result = self::validate_mailchimp_operation( - $mc->patch( "campaigns/$mc_campaign_id", $payload ) + $mc->patch( "campaigns/$mc_campaign_id", $payload ), + __( 'Error setting sender name and email.', 'newspack_newsletters' ) ); $data = self::retrieve_data( $id ); @@ -529,7 +530,8 @@ public static function api_set_mailchimp_list( $request ) { ], ]; $result = self::validate_mailchimp_operation( - $mc->patch( "campaigns/$mc_campaign_id", $payload ) + $mc->patch( "campaigns/$mc_campaign_id", $payload ), + __( 'Error setting Mailchimp list.', 'newspack_newsletters' ) ); $data = self::retrieve_data( $id ); @@ -579,7 +581,8 @@ public static function api_set_mailchimp_interest( $request ) { try { $mc = new Mailchimp( self::mailchimp_api_key() ); $campaign = self::validate_mailchimp_operation( - $mc->get( "campaigns/$mc_campaign_id" ) + $mc->get( "campaigns/$mc_campaign_id" ), + __( 'Error retrieving Mailchimp campaign.', 'newspack_newsletters' ) ); $list_id = isset( $campaign, $campaign['recipients'], $campaign['recipients']['list_id'] ) ? $campaign['recipients']['list_id'] : null; @@ -614,7 +617,8 @@ public static function api_set_mailchimp_interest( $request ) { ]; $result = self::validate_mailchimp_operation( - $mc->patch( "campaigns/$mc_campaign_id", $payload ) + $mc->patch( "campaigns/$mc_campaign_id", $payload ), + __( 'Error updating Mailchimp groups.', 'newspack_newsletters' ) ); $data = self::retrieve_data( $id ); @@ -674,7 +678,8 @@ public static function api_test_mailchimp_campaign( $request ) { $mc->post( "campaigns/$mc_campaign_id/actions/test", $payload - ) + ), + __( 'Error sending test email.', 'newspack_newsletters' ) ); $data = self::retrieve_data( $id ); @@ -719,18 +724,30 @@ public static function retrieve_data( $id ) { ); } $mc = new Mailchimp( self::mailchimp_api_key() ); - $campaign = self::validate_mailchimp_operation( $mc->get( "campaigns/$mc_campaign_id" ) ); + $campaign = self::validate_mailchimp_operation( + $mc->get( "campaigns/$mc_campaign_id" ), + __( 'Error retrieving Mailchimp campaign.', 'newspack_newsletters' ) + ); $list_id = $campaign && isset( $campaign['recipients']['list_id'] ) ? $campaign['recipients']['list_id'] : null; - $interest_categories = $list_id ? self::validate_mailchimp_operation( $mc->get( "lists/$list_id/interest-categories" ) ) : null; + $interest_categories = $list_id ? self::validate_mailchimp_operation( + $mc->get( "lists/$list_id/interest-categories" ), + __( 'Error retrieving Mailchimp groups.', 'newspack_newsletters' ) + ) : null; if ( $interest_categories && count( $interest_categories['categories'] ) ) { foreach ( $interest_categories['categories'] as &$category ) { $category_id = $category['id']; - $category['interests'] = self::validate_mailchimp_operation( $mc->get( "lists/$list_id/interest-categories/$category_id/interests" ) ); + $category['interests'] = self::validate_mailchimp_operation( + $mc->get( "lists/$list_id/interest-categories/$category_id/interests" ), + __( 'Error retrieving Mailchimp groups.', 'newspack_newsletters' ) + ); } } return [ - 'lists' => self::validate_mailchimp_operation( $mc->get( 'lists' ) ), + 'lists' => self::validate_mailchimp_operation( + $mc->get( 'lists' ), + __( 'Error retrieving Mailchimp lists.', 'newspack_newsletters' ) + ), 'campaign' => $campaign, 'campaign_id' => $mc_campaign_id, 'interest_categories' => $interest_categories, @@ -858,9 +875,15 @@ public static function sync_with_mailchimp( $post ) { $mc_campaign_id = get_post_meta( $post->ID, 'mc_campaign_id', true ); if ( $mc_campaign_id ) { - $campaign_result = self::validate_mailchimp_operation( $mc->patch( "campaigns/$mc_campaign_id", $payload ) ); + $campaign_result = self::validate_mailchimp_operation( + $mc->patch( "campaigns/$mc_campaign_id", $payload ), + __( 'Error updating campaign title.', 'newspack_newsletters' ) + ); } else { - $campaign_result = self::validate_mailchimp_operation( $mc->post( 'campaigns', $payload ) ); + $campaign_result = self::validate_mailchimp_operation( + $mc->post( 'campaigns', $payload ), + __( 'Error setting campaign title.', 'newspack_newsletters' ) + ); $mc_campaign_id = $campaign_result['id']; update_post_meta( $post->ID, 'mc_campaign_id', $mc_campaign_id ); } @@ -870,7 +893,10 @@ public static function sync_with_mailchimp( $post ) { 'html' => $renderer->render_html_email( $post ), ]; - $content_result = self::validate_mailchimp_operation( $mc->put( "campaigns/$mc_campaign_id/content", $content_payload ) ); + $content_result = self::validate_mailchimp_operation( + $mc->put( "campaigns/$mc_campaign_id/content", $content_payload ), + __( 'Error updating campaign content.', 'newspack_newsletters' ) + ); return [ 'campaign_result' => $campaign_result, 'content_result' => $content_result, @@ -937,7 +963,10 @@ public static function send_campaign( $id, $post ) { $payload = [ 'send_type' => 'html', ]; - $result = self::validate_mailchimp_operation( $mc->post( "campaigns/$mc_campaign_id/actions/send", $payload ) ); + $result = self::validate_mailchimp_operation( + $mc->post( "campaigns/$mc_campaign_id/actions/send", $payload ), + __( 'Error sending campaign.', 'newspack_newsletters' ) + ); } catch ( Exception $e ) { $transient = sprintf( 'newspack_newsletters_error_%s_%s', $post->ID, get_current_user_id() ); set_transient( $transient, $e->getMessage(), 45 ); @@ -1078,7 +1107,7 @@ public static function validate_mailchimp_operation( $result, $preferred_error = } } if ( ! empty( $result['status'] ) && in_array( $result['status'], [ 400, 404 ] ) ) { - if ( $preferred_error ) { + if ( $preferred_error && ! self::debug_mode() ) { throw new Exception( $preferred_error ); } $messages = []; @@ -1099,5 +1128,14 @@ public static function validate_mailchimp_operation( $result, $preferred_error = } return $result; } + + /** + * Is wp-config debug flag set. + * + * @return boolean Is debug mode on? + */ + public static function debug_mode() { + return defined( 'NEWSPACK_NEWSLETTERS_DEBUG_MODE' ) ? NEWSPACK_NEWSLETTERS_DEBUG_MODE : false; + } } Newspack_Newsletters::instance();