Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add max limit of 5 consecutive scheduled syncs #3639

Draft
wants to merge 1 commit into
base: release
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions includes/reader-activation/sync/class-esp-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public static function sync( $contact, $context = '' ) {

$result = \Newspack_Newsletters_Contacts::upsert( $contact, $master_list_id, $context );

if ( ! \is_wp_error( $result ) ) {
$user = get_user_by( 'email', $contact['email'] );
if ( $user ) {
\delete_user_meta( $user->ID, 'newspack_sync_retries' );
}
}

return \is_wp_error( $result ) ? $result : true;
}

Expand All @@ -136,7 +143,7 @@ public static function schedule_sync( $user_id, $context, $delay ) {
static::log(
sprintf(
// Translators: %s is the email address of the contact to synced.
__( 'Scheduling secondary sync for contact %s.', 'newspack-plugin' ),
__( 'Scheduling sync for contact %s.', 'newspack-plugin' ),
$user->data->user_email
),
[
Expand All @@ -155,11 +162,31 @@ public static function schedule_sync( $user_id, $context, $delay ) {
* @param string $context The context of the sync.
*/
public static function scheduled_sync( $user_id, $context ) {
$retries = (int) get_user_meta( $user_id, 'newspack_sync_retries', true );
$retries++;
if ( 5 < $retries ) {
static::log(
sprintf(
// Translators: %s is the email address of the contact to synced.
__( 'Failed to sync contact %s after 5 retries.', 'newspack-plugin' ),
$user_id
),
[
'user_id' => $user_id,
'context' => $context,
]
);
return;
}

$contact = Sync\WooCommerce::get_contact_from_customer( new \WC_Customer( $user_id ) );
if ( ! $contact ) {
return;
}
self::sync( $contact, $context );
$result = self::sync( $contact, $context );
if ( true !== $result ) {
update_user_meta( $user_id, 'newspack_sync_retries', $retries );
}
}

/**
Expand Down