-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUpdateShippingSettings.php
105 lines (92 loc) · 3.24 KB
/
UpdateShippingSettings.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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;
use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Settings as GoogleSettings;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
defined( 'ABSPATH' ) || exit;
/**
* Class UpdateShippingSettings
*
* Submits WooCommerce shipping settings to Google Merchant Center replacing the existing shipping settings.
*
* Note: The job will not start if it is already running or if the Google Merchant Center account is not connected.
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs
*
* @since 2.1.0
*/
class UpdateShippingSettings extends AbstractActionSchedulerJob {
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* @var GoogleSettings
*/
protected $google_settings;
/**
* UpdateShippingSettings constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param MerchantCenterService $merchant_center
* @param GoogleSettings $google_settings
*/
public function __construct( ActionSchedulerInterface $action_scheduler, ActionSchedulerJobMonitor $monitor, MerchantCenterService $merchant_center, GoogleSettings $google_settings ) {
parent::__construct( $action_scheduler, $monitor );
$this->merchant_center = $merchant_center;
$this->google_settings = $google_settings;
}
/**
* Get the name of the job.
*
* @return string
*/
public function get_name(): string {
return 'update_shipping_settings';
}
/**
* Can the job be scheduled.
*
* @param array|null $args
*
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return parent::can_schedule( $args ) && $this->can_sync_shipping();
}
/**
* Process the job.
*
* @param int[] $items An array of job arguments.
*
* @throws JobException If the shipping settings cannot be synced.
*/
public function process_items( array $items ) {
if ( ! $this->can_sync_shipping() ) {
throw new JobException( 'Cannot sync shipping settings. Confirm that the merchant center account is connected and the option to automatically sync the shipping settings is selected.' );
}
$this->google_settings->sync_shipping();
}
/**
* Schedule the job.
*
* @param array $args - arguments.
*/
public function schedule( array $args = [] ) {
if ( $this->can_schedule() ) {
$this->action_scheduler->schedule_immediate( $this->get_process_item_hook() );
}
}
/**
* Can the WooCommerce shipping settings be synced to Google Merchant Center.
*
* @return bool
*/
protected function can_sync_shipping(): bool {
// Confirm that the Merchant Center account is connected, the user has chosen for the shipping rates to be synced from WooCommerce settings and the Push Sync is enabled for Shipping.
return $this->merchant_center->is_connected() && $this->google_settings->should_get_shipping_rates_from_woocommerce() && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING );
}
}