Skip to content

Commit

Permalink
feat: display list remote name on settings page (#1672)
Browse files Browse the repository at this point in the history
* feat: display list remote name on settings page

* feat: update list title if there is no local customization

* fix: fix the update_lists behavior and add tests
  • Loading branch information
leogermani authored Oct 23, 2024
1 parent 57d83fc commit 562d396
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion includes/class-newspack-newsletters-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private static function render_lists_table() {
/>
</td>
<td class="name">
<label for="<?php echo esc_attr( $checkbox_id ); ?>"><?php echo esc_html( $list['name'] ); ?></strong>
<label for="<?php echo esc_attr( $checkbox_id ); ?>"><?php echo esc_html( $list['remote_name'] ); ?></strong>
<br/>
<small>
<?php echo esc_html( $list['type_label'] ); ?>
Expand Down
1 change: 1 addition & 0 deletions includes/class-subscription-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public function to_array() {
'type_label' => $this->get_type_label(),
'edit_link' => $this->get_edit_link(),
'active' => $this->is_active(),
'remote_name' => $this->get_remote_name(),
];
}
}
25 changes: 17 additions & 8 deletions includes/class-subscription-lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,18 @@ public static function get_or_create_remote_list( $list ) {
$subscriber_count = ! empty( $list['member_count'] ) ? (int) $list['member_count'] : 0; // Tags have member_count instead of subscriber_count.
$saved_list = Subscription_List::from_public_id( $list['id'] );
if ( $saved_list ) {
// Update remote name, in case it's changed in the ESP.
$saved_list->set_remote_name( $list['title'] );

$has_customized_title = $saved_list->get_title() !== $saved_list->get_remote_name();

if ( $list['title'] !== $saved_list->get_remote_name() ) {
// The remote name has changed, let's update it locally.
$saved_list->set_remote_name( $list['title'] );

// Only update the title if it was not customized by the user.
if ( ! $has_customized_title ) {
$saved_list->update( [ 'title' => $list['title'] ] );
}
}

// Update subscriber count, if available.
if ( 0 > $subscriber_count ) {
Expand Down Expand Up @@ -551,11 +561,11 @@ public static function update_lists( $lists ) {
$existing_ids = [];

foreach ( $lists as $list ) {
if ( Subscription_List::is_local_public_id( $list['id'] ) ) {
// Local lists will be fetched here.
$stored_list = Subscription_List::from_public_id( $list['id'] );
} else {
// Remote lists will be either fetched or created here.

$stored_list = Subscription_List::from_public_id( $list['id'] );

// If a remote list was not found, create one.
if ( ! $stored_list instanceof Subscription_List && ! Subscription_List::is_local_public_id( $list['id'] ) ) {
$stored_list = self::get_or_create_remote_list( $list );
}

Expand All @@ -564,7 +574,6 @@ public static function update_lists( $lists ) {
}

$existing_ids[] = $stored_list->get_id();

$stored_list->update( $list );

}
Expand Down
32 changes: 31 additions & 1 deletion tests/test-subscription-lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function test_get_or_create_remote_list() {
// existing.
$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp new title',
'title' => 'Test List 5',
];
$list = Subscription_Lists::get_or_create_remote_list( $existing );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
Expand All @@ -152,6 +152,36 @@ public function test_get_or_create_remote_list() {
$this->assertSame( $count + 1, $count_after_new );
}

/**
* Test update title on fetch from ESP
*/
public function test_update_title_when_fetching_from_esp() {

$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp new title',
];
$list = Subscription_Lists::get_or_create_remote_list( $existing );

$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Remote mailchimp new title', $list->get_title() );
$this->assertSame( 'Remote mailchimp new title', $list->get_remote_name() );

// Now we edit the local title.
$list->update( [ 'title' => 'Customized title' ] );

$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp super new title',
];

$list = Subscription_Lists::get_or_create_remote_list( $existing );

$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Customized title', $list->get_title(), 'The local name should not be updated if it was customized' );
$this->assertSame( 'Remote mailchimp super new title', $list->get_remote_name(), 'The remote name should always be updated' );
}

/**
* Test update_lists method
*
Expand Down

0 comments on commit 562d396

Please sign in to comment.