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 |