Skip to content

Commit

Permalink
fix: improve error handling for get_send_list methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Sep 6, 2024
1 parent b28ca0c commit e300157
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand All @@ -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'];
Expand Down Expand Up @@ -338,22 +346,27 @@ 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() ) {
return [];
}
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand All @@ -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'];
Expand Down

0 comments on commit e300157

Please sign in to comment.