-
Notifications
You must be signed in to change notification settings - Fork 21
/
CleanupSyncedProducts.php
86 lines (77 loc) · 2.17 KB
/
CleanupSyncedProducts.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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;
defined( 'ABSPATH' ) || exit;
/**
* Class CleanupSyncedProducts
*
* Marks products as unsynced when we disconnect the Merchant Account.
* The Merchant Center must remain disconnected during the job. If it is
* reconnected during the job it will stop processing, since the
* ProductSyncer will take over and update all the products.
*
* @since 1.12.0
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs
*/
class CleanupSyncedProducts extends AbstractProductSyncerBatchedJob {
/**
* Get whether Merchant Center is connected.
*
* @return bool
*/
public function is_mc_connected(): bool {
return $this->merchant_center->is_connected();
}
/**
* Get the name of the job.
*
* @return string
*/
public function get_name(): string {
return 'cleanup_synced_products';
}
/**
* Can the job be scheduled.
* Only cleanup when the Merchant Center is disconnected.
*
* @param array|null $args
*
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return ! $this->is_running( $args ) && ! $this->is_mc_connected();
}
/**
* Get a single batch of items.
*
* If no items are returned the job will stop.
*
* @param int $batch_number The batch number increments for each new batch in the job cycle.
*
* @return int[]
*/
public function get_batch( int $batch_number ): array {
return $this->product_repository->find_synced_product_ids( [], $this->get_batch_size(), $this->get_query_offset( $batch_number ) );
}
/**
* Process batch items.
* Skips processing if the Merchant Center has been connected again.
*
* @param int[] $items A single batch of WooCommerce product IDs from the get_batch() method.
*/
protected function process_items( array $items ) {
if ( $this->is_mc_connected() ) {
do_action(
'woocommerce_gla_debug_message',
sprintf(
'Skipping cleanup of unsynced products because Merchant Center is connected: %s',
implode( ',', $items )
),
__METHOD__
);
return;
}
$this->batch_product_helper->mark_batch_as_unsynced( $items );
}
}