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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 / OrderCouponDataMigrator.php
woocommerce / src / Internal Last commit date
Abilities 4 weeks ago AbilitiesApi 7 months ago AddressProvider 7 months ago Admin 2 weeks ago Agentic 7 months ago Api 4 weeks ago BatchProcessing 4 weeks ago CLI 5 months ago Caches 4 months ago ComingSoon 9 months ago CostOfGoodsSold 9 months ago Customers 9 months ago DataStores 4 weeks ago DependencyManagement 5 months ago Email 4 weeks ago EmailEditor 4 weeks ago Features 4 weeks ago Integrations 7 months ago Jetpack 5 months ago Logging 2 months ago MCP 4 weeks ago OrderReviews 4 weeks ago Orders 4 weeks ago ProductAttributes 4 weeks ago ProductAttributesLookup 4 weeks ago ProductDownloads 5 months ago ProductFeed 1 week ago ProductFilters 4 weeks ago ProductImage 1 year ago PushNotifications 4 weeks ago ReceiptRendering 2 months ago RestApi 4 weeks ago Settings 1 year ago ShopperLists 2 weeks ago StockNotifications 4 weeks ago Traits 4 months ago TransientFiles 4 weeks ago Utilities 4 weeks ago VariationGallery 4 weeks ago WCCom 2 years ago AssignDefaultCategory.php 1 year ago Brands.php 11 months ago DownloadPermissionsAdjuster.php 2 months ago McStats.php 1 year ago OrderCouponDataMigrator.php 1 year ago RegisterHooksInterface.php 1 year ago RestApiControllerBase.php 1 year ago RestApiParameterUtil.php 2 years ago RestockRefundedItemsAdjuster.php 2 months ago
OrderCouponDataMigrator.php
232 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal;
4
5 use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController;
6 use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessorInterface;
7 use Automattic\WooCommerce\Utilities\StringUtil;
8 use \Exception;
9
10 /**
11 * This class is intended to be used with BatchProcessingController and converts verbose
12 * 'coupon_data' metadata entries in coupon line items (corresponding to coupons applied to orders)
13 * into simplified 'coupon_info' entries. See WC_Coupon::get_short_info.
14 *
15 * Additionally, this class manages the "Convert order coupon data" tool.
16 */
17 class OrderCouponDataMigrator implements BatchProcessorInterface, RegisterHooksInterface {
18
19 /**
20 * Register hooks for the class.
21 */
22 public function register() {
23 add_filter( 'woocommerce_debug_tools', array( $this, 'handle_woocommerce_debug_tools' ), 999, 1 );
24 }
25
26 /**
27 * Get a user-friendly name for this processor.
28 *
29 * @return string Name of the processor.
30 */
31 public function get_name(): string {
32 return "Coupon line item 'coupon_data' to 'coupon_info' metadata migrator";
33 }
34
35 /**
36 * Get a user-friendly description for this processor.
37 *
38 * @return string Description of what this processor does.
39 */
40 public function get_description(): string {
41 return "Migrates verbose metadata about coupons applied to an order ('coupon_data' metadata key in coupon line items) to simplified metadata ('coupon_info' keys)";
42 }
43
44 /**
45 * Get the total number of pending items that require processing.
46 *
47 * @return int Number of items pending processing.
48 */
49 public function get_total_pending_count(): int {
50 global $wpdb;
51
52 return $wpdb->get_var(
53 $wpdb->prepare(
54 "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s",
55 'coupon_data'
56 )
57 );
58 }
59
60 /**
61 * Returns the next batch of items that need to be processed.
62 * A batch in this context is a list of 'meta_id' values from the wp_woocommerce_order_itemmeta table.
63 *
64 * @param int $size Maximum size of the batch to be returned.
65 *
66 * @return array Batch of items to process, containing $size or less items.
67 */
68 public function get_next_batch_to_process( int $size ): array {
69 global $wpdb;
70
71 $meta_ids = $wpdb->get_col(
72 $wpdb->prepare(
73 "SELECT meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s ORDER BY meta_id ASC LIMIT %d",
74 'coupon_data',
75 $size
76 )
77 );
78
79 return array_map( 'absint', $meta_ids );
80 }
81
82 /**
83 * Process data for the supplied batch. See the convert_item method.
84 *
85 * @throw \Exception Something went wrong while processing the batch.
86 *
87 * @param array $batch Batch to process, as returned by 'get_next_batch_to_process'.
88 */
89 public function process_batch( array $batch ): void {
90 global $wpdb;
91
92 if ( empty( $batch ) ) {
93 return;
94 }
95
96 $meta_ids = StringUtil::to_sql_list( $batch );
97
98 $meta_ids_and_values = $wpdb->get_results(
99 //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
100 "SELECT meta_id,meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_id IN $meta_ids",
101 ARRAY_N
102 );
103
104 foreach ( $meta_ids_and_values as $meta_id_and_value ) {
105 try {
106 $this->convert_item( (int) $meta_id_and_value[0], $meta_id_and_value[1] );
107 } catch ( Exception $ex ) {
108 wc_get_logger()->error( StringUtil::class_name_without_namespace( self::class ) . ": when converting meta row with id {$meta_id_and_value[0]}: {$ex->getMessage()}" );
109 }
110 }
111 }
112
113 /**
114 * Convert one verbose 'coupon_data' entry into a simplified 'coupon_info' entry.
115 *
116 * The existing database row is updated in place, both the 'meta_key' and the 'meta_value' columns.
117 *
118 * @param int $meta_id Value of 'meta_id' of the row being converted.
119 * @param string $meta_value Value of 'meta_value' of the row being converted.
120 * @throws Exception Database error.
121 */
122 private function convert_item( int $meta_id, string $meta_value ) {
123 global $wpdb;
124
125 //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
126 $coupon_data = unserialize( $meta_value );
127
128 $temp_coupon = new \WC_Coupon();
129 $temp_coupon->set_props( $coupon_data );
130
131 //phpcs:disable WordPress.DB.SlowDBQuery
132 $wpdb->update(
133 "{$wpdb->prefix}woocommerce_order_itemmeta",
134 array(
135 'meta_key' => 'coupon_info',
136 'meta_value' => $temp_coupon->get_short_info(),
137 ),
138 array( 'meta_id' => $meta_id )
139 );
140 //phpcs:enable WordPress.DB.SlowDBQuery
141
142 if ( $wpdb->last_error ) {
143 throw new Exception( $wpdb->last_error );
144 }
145 }
146
147 /**
148 * Default (preferred) batch size to pass to 'get_next_batch_to_process'.
149 *
150 * @return int Default batch size.
151 */
152 public function get_default_batch_size(): int {
153 return 1000;
154 }
155
156 /**
157 * Add the tool to start or stop the background process that converts order coupon metadata entries.
158 *
159 * @param array $tools Old tools array.
160 * @return array Updated tools array.
161 *
162 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
163 */
164 public function handle_woocommerce_debug_tools( array $tools ): array {
165 $batch_processor = wc_get_container()->get( BatchProcessingController::class );
166 $pending_count = $this->get_total_pending_count();
167
168 if ( 0 === $pending_count ) {
169 $tools['start_convert_order_coupon_data'] = array(
170 'name' => __( 'Start converting order coupon data to the simplified format', 'woocommerce' ),
171 'button' => __( 'Start converting', 'woocommerce' ),
172 'disabled' => true,
173 'desc' => __( 'This will convert <code>coupon_data</code> order item meta entries to simplified <code>coupon_info</code> entries. The conversion will happen overtime in the background (via Action Scheduler). There are currently no entries to convert.', 'woocommerce' ),
174 );
175 } elseif ( $batch_processor->is_enqueued( self::class ) ) {
176 $tools['stop_convert_order_coupon_data'] = array(
177 'name' => __( 'Stop converting order coupon data to the simplified format', 'woocommerce' ),
178 'button' => __( 'Stop converting', 'woocommerce' ),
179 'desc' =>
180 /* translators: %d=count of entries pending conversion */
181 sprintf( __( 'This will stop the background process that converts <code>coupon_data</code> order item meta entries to simplified <code>coupon_info</code> entries. There are currently %d entries that can be converted.', 'woocommerce' ), $pending_count ),
182 'callback' => array( $this, 'dequeue' ),
183 );
184 } else {
185 $tools['start_converting_order_coupon_data'] = array(
186 'name' => __( 'Convert order coupon data to the simplified format', 'woocommerce' ),
187 'button' => __( 'Start converting', 'woocommerce' ),
188 'desc' =>
189 /* translators: %d=count of entries pending conversion */
190 sprintf( __( 'This will convert <code>coupon_data</code> order item meta entries to simplified <code>coupon_info</code> entries. The conversion will happen overtime in the background (via Action Scheduler). There are currently %d entries that can be converted.', 'woocommerce' ), $pending_count ),
191 'callback' => array( $this, 'enqueue' ),
192 );
193 }
194
195 return $tools;
196 }
197
198 /**
199 * Start the background process for coupon data conversion.
200 *
201 * @return string Informative string to show after the tool is triggered in UI.
202 *
203 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
204 */
205 public function enqueue(): string {
206 $batch_processor = wc_get_container()->get( BatchProcessingController::class );
207 if ( $batch_processor->is_enqueued( self::class ) ) {
208 return __( 'Background process for coupon meta conversion already started, nothing done.', 'woocommerce' );
209 }
210
211 $batch_processor->enqueue_processor( self::class );
212 return __( 'Background process for coupon meta conversion started', 'woocommerce' );
213 }
214
215 /**
216 * Stop the background process for coupon data conversion.
217 *
218 * @return string Informative string to show after the tool is triggered in UI.
219 *
220 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
221 */
222 public function dequeue(): string {
223 $batch_processor = wc_get_container()->get( BatchProcessingController::class );
224 if ( ! $batch_processor->is_enqueued( self::class ) ) {
225 return __( 'Background process for coupon meta conversion not started, nothing done.', 'woocommerce' );
226 }
227
228 $batch_processor->remove_processor( self::class );
229 return __( 'Background process for coupon meta conversion stopped', 'woocommerce' );
230 }
231 }
232