OrderLogsCleanupHelper.php
2 months ago
OrderLogsDeletionProcessor.php
2 months ago
RemoteLogger.php
9 months ago
SafeGlobalFunctionProxy.php
1 year ago
OrderLogsCleanupHelper.php
249 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Logging; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 8 | use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer; |
| 9 | use WC_Logger; |
| 10 | |
| 11 | /** |
| 12 | * Handles cleanup of place-order debug log files and associated order meta. |
| 13 | * |
| 14 | * @since 10.7.0 |
| 15 | */ |
| 16 | class OrderLogsCleanupHelper { |
| 17 | |
| 18 | /** |
| 19 | * Maximum number of log files to delete per run. |
| 20 | */ |
| 21 | public const MAX_FILES_PER_RUN = 100; |
| 22 | |
| 23 | /** |
| 24 | * Maximum number of orders to clean up per run. |
| 25 | */ |
| 26 | public const MAX_ORDERS_PER_RUN = 100; |
| 27 | |
| 28 | /** |
| 29 | * True if HPOS is enabled. |
| 30 | * |
| 31 | * @var bool |
| 32 | */ |
| 33 | private bool $hpos_in_use = false; |
| 34 | |
| 35 | /** |
| 36 | * True if HPOS is disabled and the orders data store in use is the old CPT one. |
| 37 | * |
| 38 | * @var bool |
| 39 | */ |
| 40 | private bool $cpt_in_use = false; |
| 41 | |
| 42 | /** |
| 43 | * The instance of DataSynchronizer to use. |
| 44 | * |
| 45 | * @var DataSynchronizer |
| 46 | */ |
| 47 | private DataSynchronizer $data_synchronizer; |
| 48 | |
| 49 | /** |
| 50 | * Initialize the instance. |
| 51 | * This is invoked by the dependency injection container. |
| 52 | * |
| 53 | * @internal |
| 54 | * |
| 55 | * @param CustomOrdersTableController $hpos_controller The instance of CustomOrdersTableController to use. |
| 56 | * @param DataSynchronizer $data_synchronizer The instance of DataSynchronizer to use. |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | final public function init( CustomOrdersTableController $hpos_controller, DataSynchronizer $data_synchronizer ): void { |
| 61 | $this->hpos_in_use = $hpos_controller->custom_orders_table_usage_is_enabled(); |
| 62 | if ( ! $this->hpos_in_use ) { |
| 63 | $this->cpt_in_use = \WC_Order_Data_Store_CPT::class === \WC_Data_Store::load( 'order' )->get_current_class_name(); |
| 64 | } |
| 65 | |
| 66 | $this->data_synchronizer = $data_synchronizer; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the maximum age for debug logs before cleanup, in seconds. |
| 71 | * Returns 0 if cleanup is disabled via filter. |
| 72 | * |
| 73 | * @return int |
| 74 | */ |
| 75 | private function get_max_age_in_seconds(): int { |
| 76 | /** |
| 77 | * Filter the retention period for place-order debug logs cleanup. |
| 78 | * Return 0 to disable cleanup entirely. |
| 79 | * |
| 80 | * @param int $max_age_in_seconds The maximum age in seconds before cleanup. Default 3 days. |
| 81 | * |
| 82 | * @since 10.7.0 |
| 83 | */ |
| 84 | return absint( apply_filters( 'woocommerce_cleanup_order_debug_logs_max_age', 3 * DAY_IN_SECONDS ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Run all cleanup tasks: dangling order meta and old log files. |
| 89 | * |
| 90 | * @since 10.7.0 |
| 91 | */ |
| 92 | public function cleanup(): void { |
| 93 | $max_age = $this->get_max_age_in_seconds(); |
| 94 | |
| 95 | if ( 0 === $max_age ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Dangling orders have `_debug_log_source` meta but no `_debug_log_source_pending_deletion`. |
| 100 | $dangling_orders = $this->get_dangling_orders( $max_age ); |
| 101 | $this->clear_logs_and_delete_meta( $dangling_orders ); |
| 102 | |
| 103 | // Old log files are those that are older than the given max age. |
| 104 | $this->cleanup_old_log_files( $max_age ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Delete place-order-debug-* log files from the filesystem. |
| 109 | * |
| 110 | * @param int $max_age Maximum age in seconds before a file is eligible for deletion. |
| 111 | */ |
| 112 | private function cleanup_old_log_files( int $max_age ): void { |
| 113 | if ( \Automattic\WooCommerce\Utilities\LoggingUtil::get_default_handler() !== \Automattic\WooCommerce\Internal\Admin\Logging\LogHandlerFileV2::class ) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | $file_controller = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\Logging\FileV2\FileController::class ); |
| 118 | $files = $file_controller->get_files( |
| 119 | array( |
| 120 | 'source' => 'place-order-debug', |
| 121 | 'date_filter' => 'modified', |
| 122 | 'date_start' => 1, |
| 123 | 'date_end' => time() - $max_age, |
| 124 | 'per_page' => self::MAX_FILES_PER_RUN, |
| 125 | ) |
| 126 | ); |
| 127 | |
| 128 | if ( ! is_array( $files ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | foreach ( $files as $file ) { |
| 133 | $file->delete(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Clear debug log files and delete associated order meta for the given items. |
| 139 | * Deletes both `_debug_log_source` and `_debug_log_source_pending_deletion` meta. |
| 140 | * |
| 141 | * @since 10.7.0 |
| 142 | * |
| 143 | * @param array $items Associative array of order ID => log source name. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | public function clear_logs_and_delete_meta( array $items ): void { |
| 148 | if ( empty( $items ) ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $logger = wc_get_logger(); |
| 153 | if ( $logger instanceof WC_Logger ) { |
| 154 | foreach ( $items as $source ) { |
| 155 | $logger->clear( $source ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $order_ids = array_keys( $items ); |
| 160 | $this->delete_debug_log_meta_entries( $order_ids ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get orders with `_debug_log_source` meta older than the given max age. |
| 165 | * |
| 166 | * Orders that also have `_debug_log_source_pending_deletion` will be handled |
| 167 | * by the batch processor, but cleaning them up here too is harmless. |
| 168 | * |
| 169 | * @param int $max_age Maximum age in seconds. |
| 170 | * |
| 171 | * @return array Associative array of order ID => log source name. |
| 172 | */ |
| 173 | private function get_dangling_orders( int $max_age ): array { |
| 174 | if ( ! $this->hpos_in_use && ! $this->cpt_in_use ) { |
| 175 | return array(); |
| 176 | } |
| 177 | |
| 178 | global $wpdb; |
| 179 | |
| 180 | $cutoff_date = gmdate( 'Y-m-d H:i:s', time() - $max_age ); |
| 181 | |
| 182 | $meta_table = $this->hpos_in_use ? "{$wpdb->prefix}wc_orders_meta" : $wpdb->postmeta; |
| 183 | $order_table = $this->hpos_in_use ? "{$wpdb->prefix}wc_orders" : $wpdb->posts; |
| 184 | $id_column = $this->hpos_in_use ? 'order_id' : 'post_id'; |
| 185 | $type_column = $this->hpos_in_use ? 'type' : 'post_type'; |
| 186 | $date_column = $this->hpos_in_use ? 'date_created_gmt' : 'post_date_gmt'; |
| 187 | |
| 188 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 189 | $rows = $wpdb->get_results( |
| 190 | $wpdb->prepare( |
| 191 | "SELECT m.{$id_column} as order_id, m.meta_value |
| 192 | FROM {$meta_table} m |
| 193 | INNER JOIN {$order_table} o ON m.{$id_column} = o.id |
| 194 | WHERE m.meta_key = %s |
| 195 | AND o.{$type_column} = %s |
| 196 | AND o.{$date_column} < %s |
| 197 | LIMIT %d", |
| 198 | '_debug_log_source', |
| 199 | 'shop_order', |
| 200 | $cutoff_date, |
| 201 | self::MAX_ORDERS_PER_RUN |
| 202 | ), |
| 203 | ARRAY_A |
| 204 | ); |
| 205 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 206 | |
| 207 | return array_column( $rows, 'meta_value', 'order_id' ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Delete `_debug_log_source` and `_debug_log_source_pending_deletion` meta entries for the given order IDs |
| 212 | * from the authoritative table and the backup table (when data sync is enabled). |
| 213 | * |
| 214 | * @param array $order_ids Array of order IDs to delete meta for. |
| 215 | */ |
| 216 | private function delete_debug_log_meta_entries( array $order_ids ): void { |
| 217 | global $wpdb; |
| 218 | |
| 219 | $tables = array( |
| 220 | array( |
| 221 | 'table' => $this->hpos_in_use ? "{$wpdb->prefix}wc_orders_meta" : $wpdb->postmeta, |
| 222 | 'id_column' => $this->hpos_in_use ? 'order_id' : 'post_id', |
| 223 | ), |
| 224 | ); |
| 225 | |
| 226 | if ( $this->data_synchronizer->data_sync_is_enabled() ) { |
| 227 | $tables[] = array( |
| 228 | 'table' => $this->hpos_in_use ? $wpdb->postmeta : "{$wpdb->prefix}wc_orders_meta", |
| 229 | 'id_column' => $this->hpos_in_use ? 'post_id' : 'order_id', |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | $id_placeholders = implode( ',', array_fill( 0, count( $order_ids ), '%d' ) ); |
| 234 | |
| 235 | foreach ( $tables as $table_config ) { |
| 236 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 237 | $wpdb->query( |
| 238 | $wpdb->prepare( |
| 239 | "DELETE FROM {$table_config['table']} |
| 240 | WHERE {$table_config['id_column']} IN ({$id_placeholders}) |
| 241 | AND meta_key IN (%s, %s)", |
| 242 | array_merge( $order_ids, array( '_debug_log_source', '_debug_log_source_pending_deletion' ) ) |
| 243 | ) |
| 244 | ); |
| 245 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 |