Skip to content

Commit

Permalink
feat: update the cron schedule for product status rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
mralaminahamed committed Dec 11, 2024
1 parent 95d9a6b commit c9c3b6f
Showing 1 changed file with 65 additions and 177 deletions.
242 changes: 65 additions & 177 deletions includes/ProductStatusRollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,25 @@ public function __construct() {
/**
* Set up necessary hooks
*
* @return void
* @since DOKAN_PRO_SINCE
*
* @return void
*/
private function setup_hooks(): void {
add_action( 'dokan_rollback_product_status_draft_to_reject_schedule', [ $this, 'process_batch_draft_operation' ], 10, 2 );
add_action( 'dokan_rollback_product_status_reject_to_draft_schedule', [ $this, 'process_batch_reject_operation' ], 10, 2 );
}

/**
* Process draft to reject batch operation
* Process reject to draft batch operation
*
* @since DOKAN_PRO_SINCE
*
* @param int $batch Current batch number
* @param int $total_batches Total number of batches
*
* @return void
* @since DOKAN_PRO_SINCE
*
*/
public function process_batch_draft_operation( int $batch, int $total_batches ): void {
public function process_batch_reject_operation( int $batch, int $total_batches ): void {
global $wpdb;

try {
Expand All @@ -62,100 +61,84 @@ public function process_batch_draft_operation( int $batch, int $total_batches ):
throw new Exception( 'WooCommerce queue not available' );
}

// Get draft products with previous rejection status
// Get products for this batch
$products = $wpdb->get_col(
$wpdb->prepare(
"SELECT p.ID
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status = %s
AND pm.meta_key = %s
ORDER BY p.ID
LIMIT %d OFFSET %d",
'draft',
'_dokan_previous_status',
"SELECT ID FROM $wpdb->posts
WHERE post_type = 'product'
AND post_status = %s
ORDER BY ID
LIMIT %d OFFSET %d",
'reject',
self::BATCH_SIZE,
( $batch - 1 ) * self::BATCH_SIZE
)
);

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

$processed = 0;
foreach ( $products as $product_id ) {
if ( $this->rollback_product( $product_id, 'reject' ) ) {
++ $processed;
}
}
try {
$product = wc_get_product( $product_id );
if ( ! $product instanceof WC_Product ) {
throw new Exception( 'Invalid product' );
}

/**
* Filter the target status for product rollback
*
* @since DOKAN_PRO_SINCE
*
* @param string $target_status Target status
* @param WC_Product $product Product object
*/
$target_status = apply_filters( 'dokan_product_rollback_status', 'draft', $product );

/**
* Action before product rollback
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product Product object
* @param string $target_status Target status
*/
do_action( 'dokan_before_product_rollback', $product, $target_status );

// Track previous status
$product->add_meta_data( '_dokan_previous_status', $product->get_status(), true );
$product->save_meta_data();

$product->set_status( $target_status );
$product->save();

/**
* Action after product status rollback
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product Product object
* @param string $target_status Target status
*/
do_action( 'dokan_after_product_status_rollback', $product, $target_status );

dokan_log(
sprintf(
'Processed draft->reject batch %d/%d: %d products',
$batch,
$total_batches,
$processed
)
);

// Schedule next batch if needed
if ( $batch < $total_batches ) {
$queue->add(
'dokan_rollback_product_status_draft_to_reject_schedule',
[
'batch' => $batch + 1,
'total_batches' => $total_batches,
],
self::QUEUE_GROUP
);
}
} catch ( Throwable $e ) {
dokan_log(
sprintf(
'Error processing draft->reject batch %d: %s',
$batch,
$e->getMessage()
),
'error'
);
}
}

/**
* Process reject to draft batch operation
*
* @param int $batch
* @param int $total_batches
*
* @return void
* @since DOKAN_PRO_SINCE
*
*/
public function process_batch_reject_operation( int $batch, int $total_batches ): void {
try {
$queue = WC()->queue();
if ( ! $queue instanceof WC_Queue_Interface ) {
throw new Exception( 'WooCommerce queue not available' );
}

// Get products for this batch
$products = $this->get_products_batch( 'reject', $batch );
if ( empty( $products ) ) {
return;
}

$processed = 0;
foreach ( $products as $product_id ) {
if ( $this->rollback_product( $product_id, 'draft' ) ) {
++ $processed;

Check failure on line 126 in includes/ProductStatusRollback.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Expected no spaces between the increment operator and $processed; 1 found
} catch ( Throwable $e ) {
dokan_log(
sprintf(
'[Product Status Rollback] Error rolling back product #%d: %s',
$product_id,
$e->getMessage()
),
'error'
);
}
}

dokan_log(
sprintf(
'Processed reject->draft batch %d/%d: %d products',
'[Product Status Rollback] Processed reject->draft batch %d/%d: %d products',
$batch,
$total_batches,
$processed
Expand All @@ -176,107 +159,12 @@ public function process_batch_reject_operation( int $batch, int $total_batches )
} catch ( Throwable $e ) {
dokan_log(
sprintf(
'Error processing reject->draft batch %d: %s',
'[Product Status Rollback] Error processing reject->draft batch %d: %s',
$batch,
$e->getMessage()
),
'error'
);
}
}

/**
* Get batch of products
*
* @param string $status Current status to query
* @param int $batch Batch number
*
* @return array
*/
private function get_products_batch( string $status, int $batch ): array {
global $wpdb;

$offset = ( $batch - 1 ) * self::BATCH_SIZE;

return $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts
WHERE post_type = 'product'
AND post_status = %s
ORDER BY ID
LIMIT %d OFFSET %d",
$status,
self::BATCH_SIZE,
$offset
)
);
}

/**
* Rollback a single product's status
*
* @param int $product_id
* @param string $target_status
*
* @return bool
*/
private function rollback_product( int $product_id, string $target_status ): bool {
try {
$product = wc_get_product( $product_id );
if ( ! $product instanceof WC_Product ) {
throw new Exception( 'Invalid product' );
}

/**
* Filter the target status for product rollback
*
* @since DOKAN_PRO_SINCE
*
* @param string $target_status Target status
* @param WC_Product $product Product object
*/
$target_status = apply_filters( 'dokan_product_rollback_status', $target_status, $product );

/**
* Action before product rollback
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product Product object
* @param string $target_status Target status
*/
do_action( 'dokan_before_product_rollback', $product, $target_status );

// Track previous status
$product->add_meta_data( '_dokan_previous_status', $product->get_status(), true );
$product->save_meta_data();

$product->set_status( $target_status );
$product->save();

/**
* Action after product status rollback
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product Product object
* @param string $target_status Target status
*/
do_action( 'dokan_after_product_status_rollback', $product, $target_status );

return true;
} catch ( Throwable $e ) {
dokan_log(
sprintf(
'Error rolling back product #%d: %s',
$product_id,
$e->getMessage()
),
'error'
);

return false;
}
}

}

0 comments on commit c9c3b6f

Please sign in to comment.