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rc.2 3.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 / Internal / DataStores / Orders / LegacyDataCleanup.php
woocommerce / src / Internal / DataStores / Orders Last commit date
CustomOrdersTableController.php 3 months ago DataSynchronizer.php 7 months ago LegacyDataCleanup.php 1 year ago LegacyDataHandler.php 5 months ago OrdersTableDataStore.php 1 month ago OrdersTableDataStoreMeta.php 1 year ago OrdersTableFieldQuery.php 2 years ago OrdersTableMetaQuery.php 1 year ago OrdersTableQuery.php 1 month ago OrdersTableRefundDataStore.php 1 month ago OrdersTableSearchQuery.php 11 months ago
LegacyDataCleanup.php
253 lines
1 <?php
2 /**
3 * LegacyDataCleanup class file.
4 */
5
6 namespace Automattic\WooCommerce\Internal\DataStores\Orders;
7
8 use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController;
9 use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessorInterface;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * This class handles the background process in charge of cleaning up legacy data for orders when HPOS is authoritative.
15 */
16 class LegacyDataCleanup implements BatchProcessorInterface {
17
18 /**
19 * Option name for this feature.
20 *
21 * @deprecated 9.1.0
22 */
23 public const OPTION_NAME = 'woocommerce_hpos_legacy_data_cleanup_in_progress';
24
25 /**
26 * The default number of orders to process per batch.
27 */
28 private const BATCH_SIZE = 25;
29
30 /**
31 * The batch processing controller to use.
32 *
33 * @var BatchProcessingController
34 */
35 private $batch_processing;
36
37 /**
38 * The legacy handler to use for the actual cleanup.
39 *
40 * @var LegacyHandler
41 */
42 private $legacy_handler;
43
44 /**
45 * The data synchronizer object to use.
46 *
47 * @var DataSynchronizer
48 */
49 private $data_synchronizer;
50
51 /**
52 * Logger object to be used to log events.
53 *
54 * @var \WC_Logger
55 */
56 private $error_logger;
57
58 /**
59 * Class initialization, invoked by the DI container.
60 *
61 * @param BatchProcessingController $batch_processing The batch processing controller to use.
62 * @param LegacyDataHandler $legacy_handler Legacy order data handler instance.
63 * @param DataSynchronizer $data_synchronizer Data synchronizer instance.
64 * @internal
65 */
66 final public function init( BatchProcessingController $batch_processing, LegacyDataHandler $legacy_handler, DataSynchronizer $data_synchronizer ) {
67 $this->legacy_handler = $legacy_handler;
68 $this->data_synchronizer = $data_synchronizer;
69 $this->batch_processing = $batch_processing;
70 $this->error_logger = wc_get_logger();
71 }
72
73 /**
74 * A user friendly name for this process.
75 *
76 * @return string Name of the process.
77 */
78 public function get_name(): string {
79 return 'Order legacy data cleanup';
80 }
81
82 /**
83 * A user friendly description for this process.
84 *
85 * @return string Description.
86 */
87 public function get_description(): string {
88 return 'Cleans up order data from legacy tables.';
89 }
90
91 /**
92 * Get total number of pending records that require update.
93 *
94 * @return int Number of pending records.
95 */
96 public function get_total_pending_count(): int {
97 return $this->can_run() ? $this->legacy_handler->count_orders_for_cleanup() : 0;
98 }
99
100 /**
101 * Returns the batch with records that needs to be processed for a given size.
102 *
103 * @param int $size Size of the batch.
104 * @return array Batch of records.
105 */
106 public function get_next_batch_to_process( int $size ): array {
107 return $this->can_run()
108 ? array_map( 'absint', $this->legacy_handler->get_orders_for_cleanup( array(), $size ) )
109 : array();
110 }
111
112 /**
113 * Process data for current batch.
114 *
115 * @param array $batch Batch details.
116 */
117 public function process_batch( array $batch ): void {
118 // This is a destructive operation, so check if we need to bail out just in case.
119 if ( ! $this->can_run() ) {
120 $this->toggle_flag( false );
121 return;
122 }
123
124 $batch_failed = true;
125
126 foreach ( $batch as $order_id ) {
127 try {
128 $this->legacy_handler->cleanup_post_data( absint( $order_id ) );
129 $batch_failed = false;
130 } catch ( \Exception $e ) {
131 $this->error_logger->error(
132 sprintf(
133 // translators: %1$d is an order ID, %2$s is an error message.
134 __( 'Order %1$d legacy data could not be cleaned up during batch process. Error: %2$s', 'woocommerce' ),
135 $order_id,
136 $e->getMessage()
137 )
138 );
139 }
140 }
141
142 if ( $batch_failed ) {
143 $this->error_logger->error( __( 'Order legacy cleanup failed for an entire batch of orders. Aborting cleanup.', 'woocommerce' ) );
144 }
145
146 if ( ! $this->orders_pending() || $batch_failed ) {
147 $this->toggle_flag( false );
148 }
149 }
150
151 /**
152 * Default batch size to use.
153 *
154 * @return int Default batch size.
155 */
156 public function get_default_batch_size(): int {
157 return self::BATCH_SIZE;
158 }
159
160 /**
161 * Determine whether the cleanup process can be initiated. Legacy data cleanup requires HPOS to be authoritative and
162 * compatibility mode to be disabled.
163 *
164 * @return boolean TRUE if the cleanup process can be enabled, FALSE otherwise.
165 */
166 public function can_run() {
167 return $this->data_synchronizer->custom_orders_table_is_authoritative() && ! $this->data_synchronizer->data_sync_is_enabled() && ! $this->batch_processing->is_enqueued( get_class( $this->data_synchronizer ) );
168 }
169
170 /**
171 * Whether the user has initiated the cleanup process.
172 *
173 * @return boolean TRUE if the user has initiated the cleanup process, FALSE otherwise.
174 */
175 public function is_flag_set() {
176 return $this->batch_processing->is_enqueued( self::class );
177 }
178
179 /**
180 * Sets the flag that indicates that the cleanup process should be initiated.
181 *
182 * @param boolean $enabled TRUE if the process should be initiated, FALSE if it should be canceled.
183 * @return boolean Whether the legacy data cleanup was initiated or not.
184 */
185 public function toggle_flag( bool $enabled ): bool {
186 if ( $enabled && $this->can_run() ) {
187 $this->batch_processing->enqueue_processor( self::class );
188 return true;
189 } else {
190 $this->batch_processing->remove_processor( self::class );
191 return $enabled ? false : true;
192 }
193 }
194
195 /**
196 * Returns an array in format required by 'woocommerce_debug_tools' to register the cleanup tool in WC.
197 *
198 * @return array Tools entries to register with WC.
199 */
200 public function get_tools_entries() {
201 $orders_for_cleanup_exist = ! empty( $this->legacy_handler->get_orders_for_cleanup( array(), 1 ) );
202 $entry_id = $this->is_flag_set() ? 'hpos_legacy_cleanup_cancel' : 'hpos_legacy_cleanup';
203 $entry = array(
204 'name' => __( 'Clean up order data from legacy tables', 'woocommerce' ),
205 'desc' => __( 'This tool will clear the data from legacy order tables in WooCommerce.', 'woocommerce' ),
206 'requires_refresh' => true,
207 'button' => __( 'Clear data', 'woocommerce' ),
208 'disabled' => ! ( $this->can_run() && ( $orders_for_cleanup_exist || $this->is_flag_set() ) ),
209 );
210
211 if ( ! $this->can_run() ) {
212 $entry['desc'] .= '<br />';
213 $entry['desc'] .= sprintf(
214 '<strong class="red">%1$s</strong> %2$s',
215 __( 'Note:', 'woocommerce' ),
216 __( 'Only available when HPOS is authoritative and compatibility mode is disabled.', 'woocommerce' )
217 );
218 } else {
219 if ( $this->is_flag_set() ) {
220 $entry['status_text'] = sprintf(
221 '%1$s %2$s',
222 '<span class="dashicons dashicons-update spin"></span>',
223 __( 'Clearing data...', 'woocommerce' )
224 );
225 $entry['button'] = __( 'Cancel', 'woocommerce' );
226 $entry['callback'] = function() {
227 $this->toggle_flag( false );
228 return __( 'Order legacy data cleanup has been canceled.', 'woocommerce' );
229 };
230 } elseif ( ! $orders_for_cleanup_exist ) {
231 $entry['button'] = __( 'No orders in need of cleanup', 'woocommerce' );
232 } else {
233 $entry['callback'] = function() {
234 $this->toggle_flag( true );
235 return __( 'Order legacy data cleanup process has been started.', 'woocommerce' );
236 };
237 }
238 }
239
240 return array( $entry_id => $entry );
241 }
242
243 /**
244 * Checks whether there are any orders in need of cleanup and cleanup can run.
245 *
246 * @return bool TRUE if there are orders in need of cleanup, FALSE otherwise.
247 */
248 private function orders_pending() {
249 return ! empty( $this->get_next_batch_to_process( 1 ) );
250 }
251
252 }
253