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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Caches / OrderCountCacheService.php
woocommerce / src / Caches Last commit date
OrderCache.php 1 year ago OrderCacheController.php 1 year ago OrderCountCache.php 2 months ago OrderCountCacheService.php 2 months ago
OrderCountCacheService.php
236 lines
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Automattic\WooCommerce\Caches;
6
7 use WC_Order;
8 use Automattic\WooCommerce\Enums\OrderStatus;
9 use Automattic\WooCommerce\Utilities\OrderUtil;
10
11 /**
12 * A service class to help with updates to the aggregate orders cache.
13 *
14 * @internal
15 */
16 class OrderCountCacheService {
17
18 const BACKGROUND_EVENT_HOOK = 'woocommerce_refresh_order_count_cache';
19
20 /**
21 * OrderCountCache instance.
22 *
23 * @var OrderCountCache
24 */
25 private $order_count_cache;
26
27 /**
28 * Array of order ids with their last transitioned status as key value pairs.
29 *
30 * @var array
31 */
32 private $order_statuses = array();
33
34 /**
35 * Array of order ids with their initial status as key value pairs.
36 *
37 * @var array
38 */
39 private $initial_order_statuses = array();
40
41 /**
42 * Class initialization, invoked by the DI container.
43 *
44 * @internal
45 */
46 final public function init() {
47 $this->order_count_cache = new OrderCountCache();
48 add_action( 'woocommerce_new_order', array( $this, 'update_on_new_order' ), 10, 2 );
49 add_action( 'woocommerce_order_status_changed', array( $this, 'update_on_order_status_changed' ), 10, 4 );
50 add_action( 'woocommerce_before_trash_order', array( $this, 'update_on_order_trashed' ), 10, 2 );
51 add_action( 'woocommerce_before_delete_order', array( $this, 'update_on_order_deleted' ), 10, 2 );
52 add_action( self::BACKGROUND_EVENT_HOOK, array( $this, 'prime_cache_if_cold' ) );
53 add_action( 'action_scheduler_ensure_recurring_actions', array( $this, 'schedule_background_actions' ) );
54
55 if ( defined( 'WC_PLUGIN_BASENAME' ) ) {
56 add_action( 'deactivate_' . WC_PLUGIN_BASENAME, array( $this, 'unschedule_background_actions' ) );
57 }
58 }
59
60 /**
61 * Refresh the cache for a given order type.
62 *
63 * @internal
64 * @deprecated 10.7.0 Was used for handling `woocommerce_refresh_order_count_cache` actions.
65 *
66 * @param string $order_type The order type.
67 * @return void
68 */
69 public function refresh_cache( $order_type ) {
70 $this->order_count_cache->flush( $order_type );
71 OrderUtil::get_count_for_type( $order_type );
72 }
73
74 /**
75 * Keeps the cache warm for a specific order type to maintain admin performance, especially after extended
76 * periods of inactivity or when the cache has been cleared.
77 *
78 * @internal
79 * @since 10.7.0
80 *
81 * @param string $order_type The order type.
82 * @return void
83 */
84 public function prime_cache_if_cold( $order_type ) {
85 // Cache warm-up is only effective when an object cache plugin is active, and the cache entry is missing.
86 if ( wp_using_ext_object_cache() && null === $this->order_count_cache->get( $order_type ) ) {
87 $this->order_count_cache->flush( $order_type );
88 OrderUtil::get_count_for_type( $order_type );
89 }
90 }
91
92 /**
93 * Register background caching for each order type.
94 *
95 * @return void
96 */
97 public function schedule_background_actions() {
98 $order_types = wc_get_order_types( 'order-count' );
99 $frequency = HOUR_IN_SECONDS * 12;
100 $timestamp = time() + $frequency;
101 foreach ( $order_types as $order_type ) {
102 as_schedule_recurring_action( $timestamp, $frequency, self::BACKGROUND_EVENT_HOOK, array( $order_type ), 'count', true );
103 }
104 }
105
106 /**
107 * Unschedules background actions.
108 *
109 * @since 10.0.0
110 * @internal
111 */
112 public function unschedule_background_actions() {
113 WC()->queue()->cancel_all( self::BACKGROUND_EVENT_HOOK );
114 }
115
116 /**
117 * Update the cache when a new order is made.
118 *
119 * @param int $order_id Order id.
120 * @param WC_Order $order The order.
121 */
122 public function update_on_new_order( $order_id, $order ) {
123 $order_type = $order->get_type();
124 $order_status = $order->get_status();
125
126 if ( ! $this->order_count_cache->is_cached( $order_type, $this->get_prefixed_status( $order_status ) ) ) {
127 return;
128 }
129
130 // If the order status was updated, we need to increment the order count cache for the
131 // initial status that was errantly decremented on order status change.
132 if ( isset( $this->initial_order_statuses[ $order_id ] ) ) {
133 $this->order_count_cache->increment( $order_type, $this->get_prefixed_status( $this->initial_order_statuses[ $order_id ] ) );
134 }
135
136 // If the order status count has already been incremented, we can skip incrementing it again.
137 if ( isset( $this->order_statuses[ $order_id ] ) && $this->order_statuses[ $order_id ] === $order_status ) {
138 return;
139 }
140
141 $this->order_statuses[ $order_id ] = $order_status;
142 $this->order_count_cache->increment( $order_type, $this->get_prefixed_status( $order_status ) );
143 }
144
145 /**
146 * Update the cache when an order is trashed.
147 *
148 * @param int $order_id Order id.
149 * @param WC_Order $order The order.
150 */
151 public function update_on_order_trashed( $order_id, $order ) {
152 $order_type = $order->get_type();
153 $order_status = $order->get_status();
154
155 if (
156 ! $this->order_count_cache->is_cached( $order_type, $this->get_prefixed_status( $order_status ) ) ||
157 ! $this->order_count_cache->is_cached( $order_type, OrderStatus::TRASH ) ) {
158 return;
159 }
160
161 $this->order_count_cache->decrement( $order_type, $this->get_prefixed_status( $order_status ) );
162 $this->order_count_cache->increment( $order_type, OrderStatus::TRASH );
163 }
164
165 /**
166 * Update the cache when an order is deleted.
167 *
168 * @param int $order_id Order id.
169 * @param WC_Order $order The order.
170 */
171 public function update_on_order_deleted( $order_id, $order ) {
172 $order_type = $order->get_type();
173 $order_status = $order->get_status();
174
175 if ( ! $this->order_count_cache->is_cached( $order_type, $this->get_prefixed_status( $order_status ) ) ) {
176 return;
177 }
178
179 $this->order_count_cache->decrement( $order_type, $this->get_prefixed_status( $order_status ) );
180 }
181
182 /**
183 * Update the cache whenver an order status changes.
184 *
185 * @param int $order_id Order id.
186 * @param string $previous_status the old WooCommerce order status.
187 * @param string $next_status the new WooCommerce order status.
188 * @param WC_Order $order The order.
189 */
190 public function update_on_order_status_changed( $order_id, $previous_status, $next_status, $order ) {
191 $order_type = $order->get_type();
192
193 if (
194 ! $this->order_count_cache->is_cached( $order_type, $this->get_prefixed_status( $next_status ) ) ||
195 ! $this->order_count_cache->is_cached( $order_type, $this->get_prefixed_status( $previous_status ) )
196 ) {
197 return;
198 }
199
200 // If the order status count has already been incremented, we can skip incrementing it again.
201 if ( isset( $this->order_statuses[ $order_id ] ) && $this->order_statuses[ $order_id ] === $next_status ) {
202 return;
203 }
204
205 $this->order_statuses[ $order_id ] = $next_status;
206 $was_decremented = $this->order_count_cache->decrement( $order_type, $this->get_prefixed_status( $previous_status ) );
207 $this->order_count_cache->increment( $order_type, $this->get_prefixed_status( $next_status ) );
208
209 // Set the initial order status in case this is a new order and the previous status should not be decremented.
210 if ( ! isset( $this->initial_order_statuses[ $order_id ] ) && $was_decremented ) {
211 $this->initial_order_statuses[ $order_id ] = $previous_status;
212 }
213 }
214
215 /**
216 * Get the prefixed status.
217 *
218 * @param string $status The status.
219 * @return string
220 */
221 private function get_prefixed_status( $status ) {
222 $status = 'wc-' . $status;
223
224 $special_statuses = array(
225 'wc-' . OrderStatus::AUTO_DRAFT => OrderStatus::AUTO_DRAFT,
226 'wc-' . OrderStatus::TRASH => OrderStatus::TRASH,
227 );
228
229 if ( isset( $special_statuses[ $status ] ) ) {
230 return $special_statuses[ $status ];
231 }
232
233 return $status;
234 }
235 }
236