-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathClearProductStatsCache.php
64 lines (57 loc) · 1.42 KB
/
ClearProductStatsCache.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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Event;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantStatuses;
use Exception;
defined( 'ABSPATH' ) || exit;
/**
* Class ClearProductStatsCache
*
* @since 1.1.0
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product
*/
class ClearProductStatsCache implements Registerable, Service {
/**
* @var MerchantStatuses
*/
protected $merchant_statuses;
/**
* ClearProductStatsCache constructor.
*
* @param MerchantStatuses $merchant_statuses
*/
public function __construct( MerchantStatuses $merchant_statuses ) {
$this->merchant_statuses = $merchant_statuses;
}
/**
* Register a service.
*/
public function register(): void {
add_action(
'woocommerce_gla_batch_updated_products',
function() {
$this->clear_stats_cache();
}
);
add_action(
'woocommerce_gla_batch_deleted_products',
function() {
$this->clear_stats_cache();
}
);
}
/**
* Clears the product statistics cache
*/
protected function clear_stats_cache() {
try {
$this->merchant_statuses->clear_cache();
} catch ( Exception $exception ) {
// log and fail silently
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}
}
}