// translators: %1$d is an order ID, %2$s is an error message. __( 'Order %1$d legacy data could not be cleaned up during batch process. Error: %2$s', 'woocommerce' ), $order_id, $e->getMessage() ) ); } } if ( $batch_failed ) { $this->error_logger->error( __( 'Order legacy cleanup failed for an entire batch of orders. Aborting cleanup.', 'woocommerce' ) ); } if ( ! $this->orders_pending() || $batch_failed ) { $this->toggle_flag( false ); } } /** * Default batch size to use. * * @return int Default batch size. */ public function get_default_batch_size(): int { return self::BATCH_SIZE; } /** * Determine whether the cleanup process can be initiated. Legacy data cleanup requires HPOS to be authoritative and * compatibility mode to be disabled. * * @return boolean TRUE if the cleanup process can be enabled, FALSE otherwise. */ public function can_run() { 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 ) ); } /** * Checks whether the cleanup process should run. That is, it must be activated and {@see can_run()} must return TRUE. * * @return boolean TRUE if the cleanup process should be run, FALSE otherwise. */ public function should_run() { return $this->can_run() && $this->is_flag_set(); } /** * Whether the user has initiated the cleanup process. * * @return boolean TRUE if the user has initiated the cleanup process, FALSE otherwise. */ public function is_flag_set() { return 'yes' === get_option( self::OPTION_NAME, 'no' ); } /** * Sets the flag that indicates that the cleanup process should be initiated. * * @param boolean $enabled TRUE if the process should be initiated, FALSE if it should be canceled. */ public function toggle_flag( bool $enabled ) { if ( $enabled ) { update_option( self::OPTION_NAME, wc_bool_to_string( $enabled ) ); } else { delete_option( self::OPTION_NAME ); } } /** * Returns an array in format required by 'woocommerce_debug_tools' to register the cleanup tool in WC. * * @return array Tools entries to register with WC. */ public function get_tools_entries() { $orders_for_cleanup_exist = ! empty( $this->legacy_handler->get_orders_for_cleanup( array(), 1 ) ); $entry_id = $this->is_flag_set() ? 'hpos_legacy_cleanup_cancel' : 'hpos_legacy_cleanup'; $entry = array( 'name' => __( 'Clean up order data from legacy tables', 'woocommerce' ), 'desc' => __( 'This tool will clear the data from legacy order tables in WooCommerce.', 'woocommerce' ), 'requires_refresh' => true, 'button' => __( 'Clear data', 'woocommerce' ), 'disabled' => ! ( $this->can_run() && ( $orders_for_cleanup_exist || $this->is_flag_set() ) ), ); if ( ! $this->can_run() ) { $entry['desc'] .= '
'; $entry['desc'] .= sprintf( '%1$s %2$s', __( 'Note:', 'woocommerce' ), __( 'Only available when HPOS is authoritative and compatibility mode is disabled.', 'woocommerce' ) ); } else { if ( $this->is_flag_set() ) { $entry['status_text'] = sprintf( '%1$s %2$s', '', __( 'Clearing data...', 'woocommerce' ) ); $entry['button'] = __( 'Cancel', 'woocommerce' ); $entry['callback'] = function() { $this->toggle_flag( false ); return __( 'Order legacy data cleanup has been canceled.', 'woocommerce' ); }; } elseif ( ! $orders_for_cleanup_exist ) { $entry['button'] = __( 'No orders in need of cleanup', 'woocommerce' ); } else { $entry['callback'] = function() { $this->toggle_flag( true ); return __( 'Order legacy data cleanup process has been started.', 'woocommerce' ); }; } } return array( $entry_id => $entry ); } /** * Hooked onto 'add_option' to enqueue the batch processor (if needed). * * @param string $option Name of the option to add. * @param mixed $value Value of the option. */ private function process_added_option( string $option, $value ) { $this->process_updated_option( false, $value ); } /** * Hooked onto 'delete_option' to remove the batch processor. */ private function process_deleted_option() { $this->process_updated_option( false, false ); } /** * Hooked onto 'update_option' to enqueue the batch processor as needed. * * @param mixed $old_value Previous option value. * @param mixed $new_value New option value. */ private function process_updated_option( $old_value, $new_value ) { $enable = wc_string_to_bool( $new_value ); if ( $enable ) { $this->batch_processing->enqueue_processor( self::class ); } else { $this->batch_processing->remove_processor( self::class ); } } /** * Hooked onto 'pre_update_option' to prevent enabling of the cleanup process when conditions aren't met. * * @param mixed $new_value New option value. * @param mixed $old_value Previous option value. */ private function pre_update_option( $new_value, $old_value ) { return $this->can_run() ? $new_value : 'no'; } /** * Checks whether there are any orders in need of cleanup and cleanup can run. * * @return bool TRUE if there are orders in need of cleanup, FALSE otherwise. */ private function orders_pending() { return ! empty( $this->get_next_batch_to_process( 1 ) ); } /** * Hooked onto 'shutdown' to clean up or set things straight in case of failures (timeouts, etc). */ private function maybe_reset_state() { $is_enqueued = $this->batch_processing->is_enqueued( self::class ); $is_flag_set = $this->is_flag_set(); if ( $is_enqueued xor $is_flag_set ) { $this->toggle_flag( false ); $this->batch_processing->remove_processor( self::class ); } } }