-
Notifications
You must be signed in to change notification settings - Fork 21
/
AccountService.php
677 lines (587 loc) · 22.7 KB
/
AccountService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter;
use Automattic\Jetpack\Connection\Client;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Ads;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Merchant;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Middleware;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\SiteVerification;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\OAuthService;
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Table\MerchantIssueTable;
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Table\ShippingRateTable;
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Table\ShippingTimeTable;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ApiNotReady;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ExceptionWithResponseData;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\CleanupSyncedProducts;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\AdsAccountState;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\MerchantAccountState;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\TransientsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Container\ContainerInterface;
use Exception;
use Jetpack_Options;
defined( 'ABSPATH' ) || exit;
/**
* Class AccountService
*
* Container used to access:
* - Ads
* - AdsAccountState
* - CleanupSyncedProducts
* - Merchant
* - MerchantAccountState
* - MerchantCenterService
* - MerchantIssueTable
* - MerchantStatuses
* - Middleware
* - SiteVerification
* - ShippingRateTable
* - ShippingTimeTable
*
* @since 1.12.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter
*/
class AccountService implements OptionsAwareInterface, Service {
use OptionsAwareTrait;
use PluginHelper;
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var MerchantAccountState
*/
protected $state;
/**
* Perform a website claim with overwrite.
*
* @var bool
*/
protected $overwrite_claim = false;
/**
* Allow switching the existing website URL.
*
* @var bool
*/
protected $allow_switch_url = false;
/**
* AccountService constructor.
*
* @param ContainerInterface $container
*/
public function __construct( ContainerInterface $container ) {
$this->state = $container->get( MerchantAccountState::class );
$this->container = $container;
}
/**
* Get all Merchant Accounts associated with the connected account.
*
* @return array
* @throws Exception When an API error occurs.
*/
public function get_accounts(): array {
return $this->container->get( Middleware::class )->get_merchant_accounts();
}
/**
* Use an existing MC account. Mark the 'set_id' step as done, update the MC account's website URL,
* and sets the Merchant ID.
*
* @param int $account_id The merchant ID to use.
*
* @throws ExceptionWithResponseData If there's a website URL conflict, or account data can't be retrieved.
*/
public function use_existing_account_id( int $account_id ): void {
// Reset the process if the provided ID isn't the same as the one stored in options.
$merchant_id = $this->options->get_merchant_id();
if ( $merchant_id && $merchant_id !== $account_id ) {
$this->reset_account_setup();
}
$state = $this->state->get();
// Don't do anything if this step was already finished.
if ( MerchantAccountState::STEP_DONE === $state['set_id']['status'] ) {
return;
}
try {
// Make sure the existing account has the correct website URL (or fail).
$this->maybe_add_merchant_center_url( $account_id );
// Re-fetch state as it might have changed.
$state = $this->state->get();
$middleware = $this->container->get( Middleware::class );
// Maybe the existing account is a sub-account!
$state['set_id']['data']['from_mca'] = false;
foreach ( $middleware->get_merchant_accounts() as $existing_account ) {
if ( $existing_account['id'] === $account_id ) {
$state['set_id']['data']['from_mca'] = $existing_account['subaccount'];
break;
}
}
$middleware->link_merchant_account( $account_id );
$state['set_id']['status'] = MerchantAccountState::STEP_DONE;
$this->state->update( $state );
} catch ( ExceptionWithResponseData $e ) {
throw $e;
} catch ( Exception $e ) {
throw $this->prepare_exception( $e->getMessage(), [], $e->getCode() );
}
}
/**
* Run the process for setting up a Merchant Center account (sub-account or standalone).
*
* @param int $account_id
*
* @return array The account ID if setup has completed.
* @throws ExceptionWithResponseData When the account is already connected or a setup error occurs.
*/
public function setup_account( int $account_id ) {
// Reset the process if the provided ID isn't the same as the one stored in options.
$merchant_id = $this->options->get_merchant_id();
if ( $merchant_id && $merchant_id !== $account_id ) {
$this->reset_account_setup();
}
try {
return $this->setup_account_steps();
} catch ( ExceptionWithResponseData | ApiNotReady $e ) {
throw $e;
} catch ( Exception $e ) {
throw $this->prepare_exception( $e->getMessage(), [], $e->getCode() );
}
}
/**
* Create or link an account, switching the URL during the set_id step.
*
* @param int $account_id
*
* @return array
* @throws ExceptionWithResponseData When a setup error occurs.
*/
public function switch_url( int $account_id ): array {
$state = $this->state->get();
$switch_necessary = ! empty( $state['set_id']['data']['old_url'] );
$set_id_status = $state['set_id']['status'] ?? MerchantAccountState::STEP_PENDING;
if ( ! $account_id || MerchantAccountState::STEP_DONE === $set_id_status || ! $switch_necessary ) {
throw $this->prepare_exception(
__( 'Attempting invalid URL switch.', 'google-listings-and-ads' )
);
}
$this->allow_switch_url = true;
$this->use_existing_account_id( $account_id );
return $this->setup_account( $account_id );
}
/**
* Create or link an account, overwriting the website claim during the claim step.
*
* @param int $account_id
*
* @return array
* @throws ExceptionWithResponseData When a setup error occurs.
*/
public function overwrite_claim( int $account_id ): array {
$state = $this->state->get( false );
$overwrite_necessary = ! empty( $state['claim']['data']['overwrite_required'] );
$claim_status = $state['claim']['status'] ?? MerchantAccountState::STEP_PENDING;
if ( MerchantAccountState::STEP_DONE === $claim_status || ! $overwrite_necessary ) {
throw $this->prepare_exception(
__( 'Attempting invalid claim overwrite.', 'google-listings-and-ads' )
);
}
$this->overwrite_claim = true;
return $this->setup_account( $account_id );
}
/**
* Get the connected merchant account.
*
* @return array
*/
public function get_connected_status(): array {
/** @var NotificationsService $notifications_service */
$notifications_service = $this->container->get( NotificationsService::class );
$id = $this->options->get_merchant_id();
$wpcom_rest_api_status = $this->options->get( OptionsInterface::WPCOM_REST_API_STATUS );
// If token is revoked outside the extension. Set the status as error to force the merchant to grant access again.
if ( $wpcom_rest_api_status === 'approved' && ! $this->is_wpcom_api_status_healthy() ) {
$wpcom_rest_api_status = OAuthService::STATUS_ERROR;
$this->options->update( OptionsInterface::WPCOM_REST_API_STATUS, $wpcom_rest_api_status );
}
$status = [
'id' => $id,
'status' => $id ? 'connected' : 'disconnected',
'notification_service_enabled' => $notifications_service->is_enabled(),
'wpcom_rest_api_status' => $wpcom_rest_api_status,
];
$incomplete = $this->state->last_incomplete_step();
if ( ! empty( $incomplete ) ) {
$status['status'] = 'incomplete';
$status['step'] = $incomplete;
}
return $status;
}
/**
* Return the setup status to determine what step to continue at.
*
* @return array
*/
public function get_setup_status(): array {
return $this->container->get( MerchantCenterService::class )->get_setup_status();
}
/**
* Disconnect Merchant Center account
*/
public function disconnect() {
$this->options->delete( OptionsInterface::CONTACT_INFO_SETUP );
$this->options->delete( OptionsInterface::MC_SETUP_COMPLETED_AT );
$this->options->delete( OptionsInterface::MERCHANT_ACCOUNT_STATE );
$this->options->delete( OptionsInterface::MERCHANT_CENTER );
$this->options->delete( OptionsInterface::SITE_VERIFICATION );
$this->options->delete( OptionsInterface::TARGET_AUDIENCE );
$this->options->delete( OptionsInterface::MERCHANT_ID );
$this->options->delete( OptionsInterface::CLAIMED_URL_HASH );
$this->container->get( MerchantStatuses::class )->delete();
$this->container->get( MerchantIssueTable::class )->truncate();
$this->container->get( ShippingRateTable::class )->truncate();
$this->container->get( ShippingTimeTable::class )->truncate();
$this->container->get( CleanupSyncedProducts::class )->schedule();
$this->container->get( TransientsInterface::class )->delete( TransientsInterface::MC_ACCOUNT_REVIEW );
$this->container->get( TransientsInterface::class )->delete( TransientsInterface::URL_MATCHES );
$this->container->get( TransientsInterface::class )->delete( TransientsInterface::MC_IS_SUBACCOUNT );
}
/**
* Performs the steps necessary to initialize a Merchant Center account.
* Should always resume up at the last pending or unfinished step. If the Merchant Center account
* has already been created, the ID is simply returned.
*
* @return array The newly created (or pre-existing) Merchant account data.
* @throws ExceptionWithResponseData If an error occurs during any step.
* @throws Exception If the step is unknown.
* @throws ApiNotReady If we should wait to complete the next step.
*/
private function setup_account_steps() {
$state = $this->state->get();
$merchant_id = $this->options->get_merchant_id();
$merchant = $this->container->get( Merchant::class );
$middleware = $this->container->get( Middleware::class );
foreach ( $state as $name => &$step ) {
if ( MerchantAccountState::STEP_DONE === $step['status'] ) {
continue;
}
if ( 'link' === $name ) {
$time_to_wait = $this->state->get_seconds_to_wait_after_created();
if ( $time_to_wait ) {
sleep( $time_to_wait );
}
}
try {
switch ( $name ) {
case 'set_id':
// Just in case, don't create another merchant ID.
if ( ! empty( $merchant_id ) ) {
break;
}
$merchant_id = $middleware->create_merchant_account();
$step['data']['from_mca'] = true;
$step['data']['created_timestamp'] = time();
break;
case 'verify':
// Skip if previously verified.
if ( $this->state->is_site_verified() ) {
break;
}
$site_url = esc_url_raw( $this->get_site_url() );
$this->container->get( SiteVerification::class )->verify_site( $site_url );
break;
case 'link':
$middleware->link_merchant_to_mca();
break;
case 'claim':
// At this step, the website URL is assumed to be correct.
// If the URL is already claimed, no claim should be attempted.
if ( $merchant->get_accountstatus( $merchant_id )->getWebsiteClaimed() ) {
break;
}
if ( $this->overwrite_claim ) {
$middleware->claim_merchant_website( true );
} else {
$merchant->claimwebsite();
}
break;
case 'link_ads':
// Continue to next step if Ads account is not connected yet.
if ( ! $this->options->get_ads_id() ) {
// Save step as pending and continue the foreach loop with `continue 2`.
$state[ $name ]['status'] = MerchantAccountState::STEP_PENDING;
$this->state->update( $state );
continue 2;
}
$this->link_ads_account();
break;
default:
throw new Exception(
sprintf(
/* translators: 1: is a string representing an unknown step name */
__( 'Unknown merchant account creation step %1$s', 'google-listings-and-ads' ),
$name
)
);
}
$step['status'] = MerchantAccountState::STEP_DONE;
$step['message'] = '';
$this->state->update( $state );
} catch ( Exception $e ) {
$step['status'] = MerchantAccountState::STEP_ERROR;
$step['message'] = $e->getMessage();
// URL already claimed.
if ( 'claim' === $name && 403 === $e->getCode() ) {
$data = [
'id' => $merchant_id,
'website_url' => $this->strip_url_protocol(
esc_url_raw( $this->get_site_url() )
),
];
// Sub-account: request overwrite confirmation.
if ( $state['set_id']['data']['from_mca'] ?? true ) {
do_action( 'woocommerce_gla_site_claim_overwrite_required', [] );
$step['data']['overwrite_required'] = true;
$e = $this->prepare_exception( $e->getMessage(), $data, $e->getCode() );
} else {
do_action( 'woocommerce_gla_site_claim_failure', [ 'details' => 'independent_account' ] );
// Independent account: overwrite not possible.
$e = $this->prepare_exception(
__( 'Unable to claim website URL with this Merchant Center Account.', 'google-listings-and-ads' ),
$data,
406
);
}
} elseif ( 'link' === $name && 401 === $e->getCode() ) {
// New sub-account not yet manipulable.
$state['set_id']['data']['created_timestamp'] = time();
$e = ApiNotReady::retry_after( MerchantAccountState::MC_DELAY_AFTER_CREATE );
}
$this->state->update( $state );
throw $e;
}
}
return [ 'id' => $merchant_id ];
}
/**
* Restart the account setup when we are connecting with a different account ID.
* Do not allow reset when the full setup process has completed.
*
* @throws ExceptionWithResponseData When the full setup process is completed.
*/
private function reset_account_setup() {
// Can't reset if the MC connection process has been completed previously.
if ( $this->container->get( MerchantCenterService::class )->is_setup_complete() ) {
throw $this->prepare_exception(
sprintf(
/* translators: 1: is a numeric account ID */
__( 'Merchant Center account already connected: %d', 'google-listings-and-ads' ),
$this->options->get_merchant_id()
)
);
}
$this->disconnect();
}
/**
* Ensure the Merchant Center account's Website URL matches the site URL. Update an empty value or
* a different, unclaimed URL value. Throw a 409 exception if a different, claimed URL is found.
*
* @param int $merchant_id The Merchant Center account to update.
*
* @throws ExceptionWithResponseData If the account URL doesn't match the site URL or the URL is invalid.
*/
private function maybe_add_merchant_center_url( int $merchant_id ) {
$site_url = esc_url_raw( $this->get_site_url() );
if ( ! wc_is_valid_url( $site_url ) ) {
throw $this->prepare_exception( __( 'Invalid site URL.', 'google-listings-and-ads' ) );
}
/** @var Merchant $merchant */
$merchant = $this->container->get( Merchant::class );
/** @var Account $account */
$account = $merchant->get_account( $merchant_id );
$account_url = $account->getWebsiteUrl() ?: '';
if ( untrailingslashit( $site_url ) !== untrailingslashit( $account_url ) ) {
$is_website_claimed = $merchant->get_accountstatus( $merchant_id )->getWebsiteClaimed();
if ( ! empty( $account_url ) && $is_website_claimed && ! $this->allow_switch_url ) {
$state = $this->state->get();
$state['set_id']['data']['old_url'] = $account_url;
$state['set_id']['status'] = MerchantAccountState::STEP_ERROR;
$this->state->update( $state );
$clean_account_url = $this->strip_url_protocol( $account_url );
$clean_site_url = $this->strip_url_protocol( $site_url );
do_action( 'woocommerce_gla_url_switch_required', [] );
throw $this->prepare_exception(
sprintf(
/* translators: 1: is a website URL (without the protocol) */
__( 'This Merchant Center account already has a verified and claimed URL, %1$s', 'google-listings-and-ads' ),
$clean_account_url
),
[
'id' => $merchant_id,
'claimed_url' => $clean_account_url,
'new_url' => $clean_site_url,
],
409
);
}
$account->setWebsiteUrl( $site_url );
$merchant->update_account( $account );
// Clear previous hashed URL.
$this->options->delete( OptionsInterface::CLAIMED_URL_HASH );
do_action( 'woocommerce_gla_url_switch_success', [] );
}
}
/**
* Get the callback function for linking an Ads account.
*
* @throws Exception When the merchant account hasn't been set yet.
*/
private function link_ads_account() {
if ( ! $this->options->get_merchant_id() ) {
throw new Exception( 'A Merchant Center account must be connected' );
}
$ads_state = $this->container->get( AdsAccountState::class );
// Create link for Merchant and accept it in Ads.
$this->container->get( Merchant::class )->link_ads_id( $this->options->get_ads_id() );
$this->container->get( Ads::class )->accept_merchant_link( $this->options->get_merchant_id() );
$ads_state->complete_step( 'link_merchant' );
}
/**
* Prepares an Exception to be thrown with Merchant data:
* - Ensure it has the merchant_id value
* - Default to a 400 error code
*
* @param string $message
* @param array $data
* @param int|null $code
*
* @return ExceptionWithResponseData
*/
private function prepare_exception( string $message, array $data = [], int $code = null ): ExceptionWithResponseData {
$merchant_id = $this->options->get_merchant_id();
if ( $merchant_id && ! isset( $data['id'] ) ) {
$data['id'] = $merchant_id;
}
return new ExceptionWithResponseData( $message, $code ?: 400, null, $data );
}
/**
* Delete the option regarding WPCOM authorization
*
* @return bool
*/
public function reset_wpcom_api_authorization_data(): bool {
$this->delete_wpcom_api_auth_nonce();
$this->delete_wpcom_api_status_transient();
return $this->options->delete( OptionsInterface::WPCOM_REST_API_STATUS );
}
/**
* Update the status of the merchant granting access to Google's WPCOM app in the database.
* Before updating the status in the DB it will compare the nonce stored in the DB with the nonce passed to the API.
*
* @param string $status The status of the merchant granting access to Google's WPCOM app.
* @param string $nonce The nonce provided by Google in the URL query parameter when Google redirects back to merchant's site.
*
* @return bool
* @throws ExceptionWithResponseData If the stored nonce / nonce from query param is not provided, or the nonces mismatch.
*/
public function update_wpcom_api_authorization( string $status, string $nonce ): bool {
try {
$stored_nonce = $this->options->get( OptionsInterface::GOOGLE_WPCOM_AUTH_NONCE );
if ( empty( $stored_nonce ) ) {
throw $this->prepare_exception(
__( 'No stored nonce found in the database, skip updating auth status.', 'google-listings-and-ads' )
);
}
if ( empty( $nonce ) ) {
throw $this->prepare_exception(
__( 'Nonce is not provided, skip updating auth status.', 'google-listings-and-ads' )
);
}
if ( $stored_nonce !== $nonce ) {
$this->delete_wpcom_api_auth_nonce();
throw $this->prepare_exception(
__( 'Nonces mismatch, skip updating auth status.', 'google-listings-and-ads' )
);
}
$this->delete_wpcom_api_auth_nonce();
/**
* When the WPCOM Authorization status has been updated.
*
* @event update_wpcom_api_authorization
* @property string status The status of the request.
* @property int|null blog_id The blog ID.
*/
do_action(
'woocommerce_gla_track_event',
'update_wpcom_api_authorization',
[
'status' => $status,
'blog_id' => Jetpack_Options::get_option( 'id' ),
]
);
$this->delete_wpcom_api_status_transient();
return $this->options->update( OptionsInterface::WPCOM_REST_API_STATUS, $status );
} catch ( ExceptionWithResponseData $e ) {
/**
* When the WPCOM Authorization status has been updated with errors.
*
* @event update_wpcom_api_authorization
* @property string status The status of the request.
* @property int|null blog_id The blog ID.
*/
do_action(
'woocommerce_gla_track_event',
'update_wpcom_api_authorization',
[
'status' => $e->getMessage(),
'blog_id' => Jetpack_Options::get_option( 'id' ),
]
);
throw $e;
}
}
/**
* Delete the nonce of "verifying Google is the one redirect back to merchant site and set the auth status" in the database.
*
* @return bool
*/
public function delete_wpcom_api_auth_nonce(): bool {
return $this->options->delete( OptionsInterface::GOOGLE_WPCOM_AUTH_NONCE );
}
/**
* Deletes the transient storing the WPCOM Status data.
*/
public function delete_wpcom_api_status_transient(): void {
$transients = $this->container->get( TransientsInterface::class );
$transients->delete( TransientsInterface::WPCOM_API_STATUS );
}
/**
* Check if the WPCOM API Status is healthy by doing a request to /wc/partners/google/remote-site-status endpoint in WPCOM.
*
* @return bool True when the status is healthy, false otherwise.
*/
public function is_wpcom_api_status_healthy() {
/** @var TransientsInterface $transients */
$transients = $this->container->get( TransientsInterface::class );
$status = $transients->get( TransientsInterface::WPCOM_API_STATUS );
if ( ! $status ) {
$integration_status_args = [
'method' => 'GET',
'timeout' => 30,
'url' => 'https://public-api.wordpress.com/wpcom/v2/sites/' . Jetpack_Options::get_option( 'id' ) . '/wc/partners/google/remote-site-status',
'user_id' => get_current_user_id(),
];
$integration_remote_request_response = Client::remote_request( $integration_status_args, null );
if ( is_wp_error( $integration_remote_request_response ) ) {
$status = [ 'is_healthy' => false ];
} else {
$status = json_decode( wp_remote_retrieve_body( $integration_remote_request_response ), true ) ?? [ 'is_healthy' => false ];
}
$transients->set( TransientsInterface::WPCOM_API_STATUS, $status, MINUTE_IN_SECONDS * 30 );
}
return isset( $status['is_healthy'] ) && $status['is_healthy'] && $status['is_wc_rest_api_healthy'] && $status['is_partner_token_healthy'];
}
}