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 |