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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 / Logging / OrderLogsCleanupHelper.php
woocommerce / src / Internal / Logging Last commit date
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