Skip to content

Commit

Permalink
Merge pull request #1079 from woocommerce/add/a-weekly-commerce-integ…
Browse files Browse the repository at this point in the history
…ration-sync

Synchronising Commerce Integration `partner_metadata` weekly.
  • Loading branch information
message-dimke authored Oct 23, 2024
2 parents 342f417 + 2610208 commit 3abdbbe
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions src/CommerceIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public static function init() {
10,
1
);

if ( ! has_action( Heartbeat::WEEKLY, array( self::class, 'handle_sync' ) ) ) {
add_action( Heartbeat::WEEKLY, array( self::class, 'handle_sync' ) );
}
}

/**
Expand All @@ -44,6 +48,7 @@ public static function init() {
*/
public static function maybe_unregister_retries() {
as_unschedule_all_actions( 'pinterest-for-woocommerce-create-commerce-integration-retry' );
as_unschedule_all_actions( 'pinterest-for-woocommerce-sync-commerce-integration' );
}

/**
Expand Down Expand Up @@ -106,7 +111,7 @@ public static function handle_create( $attempt = 0 ): array {
);
$frames = array( MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS );
as_schedule_single_action(
time() + $frames[ $attempt ],
time() + ( $frames[ $attempt ] ?? DAY_IN_SECONDS ),
'pinterest-for-woocommerce-create-commerce-integration-retry',
array(
'attempt' => $attempt + 1,
Expand All @@ -119,17 +124,71 @@ public static function handle_create( $attempt = 0 ): array {
}
}

/**
* Handles Commerce Integration partner_metadata updates.
*
* @since x.x.x
* @return void
*/
public static function handle_sync() {
if ( ! Pinterest_For_Woocommerce::is_connected() ) {
return;
}

/*
* If there is a commerce integration retry action, we know not to run the sync yet,
* since it will also try to create the commerce integration as well.
*/
if ( as_has_scheduled_action( 'pinterest-for-woocommerce-create-commerce-integration-retry' ) ) {
return;
}

$integration_data = Pinterest_For_Woocommerce::get_data( 'integration_data' );
$external_business_id = $integration_data['external_business_id'] ?? '';

if ( ! $external_business_id ) {
self::handle_create();
return;
}

try {
$new_integration_data = self::get_integration_data( $external_business_id );

if ( $integration_data['partner_metadata'] !== $new_integration_data['partner_metadata'] ) {
$response = APIV5::update_commerce_integration( $external_business_id, $new_integration_data );
Pinterest_For_Woocommerce::save_integration_data( $response );
}
} catch ( PinterestApiException $e ) {
Logger::log(
sprintf(
/* translators: 1: Pinterest internal code, 2: Pinterest response message. */
__(
'Commerce Integration Sync has failed with Pinterest code %1$s and the message %2$s. Next attempt in a week.',
'pinterest-for-woocommerce'
),
esc_html( $e->get_pinterest_code() ),
esc_html( $e->getMessage() )
),
'error'
);
}
}

/**
* Prepares Commerce Integration data.
*
* @since x.x.x
* @param string $external_business_id External Business ID.
* @return array
*/
public static function get_integration_data(): array {
public static function get_integration_data( $external_business_id = '' ): array {
global $wp_version;

$external_business_id = self::generate_external_business_id();
$connection_data = Pinterest_For_Woocommerce::get_data( 'connection_info_data', true );
if ( empty( $external_business_id ) ) {
$external_business_id = self::generate_external_business_id();
}

$connection_data = Pinterest_For_Woocommerce::get_data( 'connection_info_data', true );

$integration_data = array(
'external_business_id' => $external_business_id,
Expand Down

0 comments on commit 3abdbbe

Please sign in to comment.