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