Skip to content

Commit

Permalink
feat: clearer error messages
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jeffersonrabb committed Apr 24, 2020
1 parent 32fe5d7 commit b165075
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
66 changes: 52 additions & 14 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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();

0 comments on commit b165075

Please sign in to comment.