From e300157dda3774e51688e90881a5ee32fe85ad5b Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 6 Sep 2024 12:52:57 -0600 Subject: [PATCH] fix: improve error handling for get_send_list methods --- ...s-newspack-newsletters-active-campaign.php | 60 ++++++++++++------- ...-newspack-newsletters-campaign-monitor.php | 33 ++++++---- ...-newspack-newsletters-constant-contact.php | 23 +++++-- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/includes/service-providers/active_campaign/class-newspack-newsletters-active-campaign.php b/includes/service-providers/active_campaign/class-newspack-newsletters-active-campaign.php index 435098980..c958081ee 100644 --- a/includes/service-providers/active_campaign/class-newspack-newsletters-active-campaign.php +++ b/includes/service-providers/active_campaign/class-newspack-newsletters-active-campaign.php @@ -740,17 +740,49 @@ public function retrieve( $post_id, $skip_sync = false ) { $campaign_id = get_post_meta( $post_id, 'ac_campaign_id', true ); $send_list_id = get_post_meta( $post_id, 'send_list_id', true ); $send_sublist_id = get_post_meta( $post_id, 'send_sublist_id', true ); + + // Handle legacy send-to meta. + if ( ! $send_list_id ) { + $legacy_list_id = get_post_meta( $post_id, 'ac_list_id', true ); + if ( $legacy_list_id ) { + $newsletter_data['list_id'] = $legacy_list_id; + $send_list_id = $legacy_list_id; + } + } + if ( ! $send_sublist_id ) { + $legacy_sublist_id = get_post_meta( $post_id, 'ac_segment_id', true ); + if ( $legacy_sublist_id ) { + $newsletter_data['sublist_id'] = $legacy_sublist_id; + $send_sublist_id = $legacy_sublist_id; + } + } + $send_lists = $this->get_send_lists( // Get first 10 top-level send lists for autocomplete. + [ + 'ids' => $send_list_id ? [ $send_list_id ] : null, // If we have a selected list, make sure to fetch it. + 'type' => 'list', + ] + ); + if ( is_wp_error( $send_lists ) ) { + return $send_lists; + } + $send_sublists = $send_list_id || $send_sublist_id ? + $this->get_send_lists( + [ + 'ids' => [ $send_sublist_id ], // If we have a selected sublist, make sure to fetch it. Otherwise, we'll populate sublists later. + 'parent_id' => $send_list_id, + 'type' => 'sublist', + ] + ) : + []; + if ( is_wp_error( $send_sublists ) ) { + return $send_sublists; + } $newsletter_data = [ 'campaign' => true, // Satisfy the JS API. 'campaign_id' => $campaign_id, 'supports_multiple_test_recipients' => true, - 'lists' => $this->get_send_lists( // Get first 10 top-level send lists for autocomplete. - [ - 'ids' => $send_list_id ? [ $send_list_id ] : null, // If we have a selected list, make sure to fetch it. - 'type' => 'list', - ] - ), - 'sublists' => [], // Will be populated later if needed. + 'lists' => $send_lists, + 'sublists' => $send_sublists, ]; // Handle legacy sender meta. @@ -769,20 +801,6 @@ public function retrieve( $post_id, $skip_sync = false ) { } } - // Handle legacy send-to meta. - if ( ! $send_list_id ) { - $legacy_list_id = get_post_meta( $post_id, 'ac_list_id', true ); - if ( $legacy_list_id ) { - $newsletter_data['list_id'] = $legacy_list_id; - } - } - if ( ! $send_sublist_id ) { - $legacy_sublist_id = get_post_meta( $post_id, 'ac_segment_id', true ); - if ( $legacy_sublist_id ) { - $newsletter_data['sublist_id'] = $legacy_sublist_id; - } - } - if ( ! $campaign_id && true !== $skip_sync ) { $sync_result = $this->sync( get_post( $post_id ) ); if ( ! is_wp_error( $sync_result ) ) { diff --git a/includes/service-providers/campaign_monitor/class-newspack-newsletters-campaign-monitor.php b/includes/service-providers/campaign_monitor/class-newspack-newsletters-campaign-monitor.php index 11d8354fa..ddd901811 100644 --- a/includes/service-providers/campaign_monitor/class-newspack-newsletters-campaign-monitor.php +++ b/includes/service-providers/campaign_monitor/class-newspack-newsletters-campaign-monitor.php @@ -259,6 +259,10 @@ public function get_send_lists( $args = [] ) { ); } + $lists = $this->get_lists(); + if ( is_wp_error( $lists ) ) { + return $lists; + } $send_lists = array_map( function( $list ) use ( $api_key ) { $config = [ @@ -277,9 +281,13 @@ function( $list ) use ( $api_key ) { return new Send_List( $config ); }, - $this->get_lists() + $lists ); - $segments = array_map( + $segments = $this->get_segments(); + if ( is_wp_error( $segments ) ) { + return $segments; + } + $send_segments = array_map( function( $segment ) { $segment_id = (string) $segment['id']; $config = [ @@ -292,9 +300,9 @@ function( $segment ) { return new Send_List( $config ); }, - $this->get_segments() + $segments ); - $send_lists = array_merge( $send_lists, $segments ); + $send_lists = array_merge( $send_lists, $send_segments ); $filtered_lists = $send_lists; if ( ! empty( $args['ids'] ) ) { $ids = ! is_array( $args['ids'] ) ? [ $args['ids'] ] : $args['ids']; @@ -338,6 +346,7 @@ function ( $list ) use ( $search ) { * * @param integer $post_id Numeric ID of the Newsletter post. * @return object|WP_Error API Response or error. + * @throws Exception Error message. */ public function retrieve( $post_id ) { if ( ! $this->has_api_credentials() ) { @@ -345,15 +354,19 @@ public function retrieve( $post_id ) { } try { $send_list_id = get_post_meta( $post_id, 'send_list_id', true ); + $send_lists = $this->get_send_lists( // Get first 10 top-level send lists for autocomplete. + [ + 'ids' => $send_list_id ? [ $send_list_id ] : null, // If we have a selected list, make sure to fetch it. + 'type' => 'list', + ] + ); + if ( is_wp_error( $send_lists ) ) { + throw new Exception( wp_kses_post( $send_lists->get_error_message() ) ); + } $newsletter_data = [ 'campaign' => true, // Satisfy the JS API. 'supports_multiple_test_recipients' => true, - 'lists' => $this->get_send_lists( // Get first 10 top-level send lists for autocomplete. - [ - 'ids' => $send_list_id ? [ $send_list_id ] : null, // If we have a selected list, make sure to fetch it. - 'type' => 'list', - ] - ), + 'lists' => $send_lists, ]; // Handle legacy sender meta. diff --git a/includes/service-providers/constant_contact/class-newspack-newsletters-constant-contact.php b/includes/service-providers/constant_contact/class-newspack-newsletters-constant-contact.php index d3ef38d4d..a7b6e404d 100644 --- a/includes/service-providers/constant_contact/class-newspack-newsletters-constant-contact.php +++ b/includes/service-providers/constant_contact/class-newspack-newsletters-constant-contact.php @@ -472,6 +472,7 @@ public function unset_segment( $post_id ) { * * @param integer $post_id Numeric ID of the Newsletter post. * @return object|WP_Error API Response or error. + * @throws Exception Error message. */ public function retrieve( $post_id ) { if ( ! $this->has_api_credentials() || ! $this->has_valid_connection() ) { @@ -511,12 +512,16 @@ public function retrieve( $post_id ) { } // Prefetch send list info if we have a selected list and/or sublist. - $newsletter_data['lists'] = $this->get_send_lists( + $send_lists = $this->get_send_lists( [ 'ids' => $send_list_id ? [ $send_list_id ] : null, // If we have a selected list, make sure to fetch it. 'type' => 'list', ] ); + if ( is_wp_error( $send_lists ) ) { + throw new Exception( wp_kses_post( $send_lists->get_error_message() ) ); + } + $newsletter_data['lists'] = $send_lists; return $newsletter_data; } catch ( Exception $e ) { @@ -972,6 +977,10 @@ function ( $segment ) { * @return Send_List[]|WP_Error Array of Send_List objects on success, or WP_Error object on failure. */ public function get_send_lists( $args = [] ) { + $lists = $this->get_lists(); + if ( is_wp_error( $lists ) ) { + return $lists; + } $send_lists = array_map( function( $list ) { $config = [ @@ -986,9 +995,13 @@ function( $list ) { return new Send_List( $config ); }, - $this->get_lists() + $lists ); - $segments = array_map( + $segments = $this->get_segments(); + if ( is_wp_error( $segments ) ) { + return $segments; + } + $send_segments = array_map( function( $segment ) { $segment_id = (string) $segment['id']; $config = [ @@ -1002,9 +1015,9 @@ function( $segment ) { return new Send_List( $config ); }, - $this->get_segments() + $segments ); - $send_lists = array_merge( $send_lists, $segments ); + $send_lists = array_merge( $send_lists, $send_segments ); $filtered_lists = $send_lists; if ( ! empty( $args['ids'] ) ) { $ids = ! is_array( $args['ids'] ) ? [ $args['ids'] ] : $args['ids'];