t 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 ) ); } /** * 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 $this->batch_processing->is_enqueued( self::class ); } /** * 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. * @return boolean Whether the legacy data cleanup was initiated or not. */ public function toggle_flag( bool $enabled ): bool { if ( $enabled && $this->can_run() ) { $this->batch_processing->enqueue_processor( self::class ); return true; } else { $this->batch_processing->remove_processor( self::class ); return $enabled ? false : true; } } /** * 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 ); } /** * 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 ) ); } }