Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rollback product statuses on dokan pro deactivation (integration) #2476

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@
$this->getContainer()
->addShared( \WeDevs\Dokan\Privacy::class, \WeDevs\Dokan\Privacy::class )
->addTag( self::TAG );

$this->getContainer()
->addShared( \WeDevs\Dokan\ProductStatusRollback::class, \WeDevs\Dokan\ProductStatusRollback::class )

Check warning on line 70 in includes/DependencyManagement/Providers/CommonServiceProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 1 spaces.
->addTag( self::TAG );

Check warning on line 71 in includes/DependencyManagement/Providers/CommonServiceProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 1 spaces.
}
}
144 changes: 144 additions & 0 deletions includes/ProductStatusRollback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace WeDevs\Dokan;

use RuntimeException;
use Throwable;
use WC_Product;
use WeDevs\Dokan\Contracts\Hookable;

/**
* Class to handle product status rollback operations.
*
* Dokan pro schedule an action during deactivation to change product status from `reject` to `draft`.
*/
class ProductStatusRollback implements Hookable {
/**
* Queue group identifier
*
* @var string
*/
private const QUEUE_GROUP = 'dokan-product-status-rollback';

/**
* Batch size for processing
*
* @var int
*/
private const BATCH_SIZE = 10;

/**
* Constructor.
*
* @since DOKAN_PRO_SINCE
*/
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved
public function __construct() {
$this->register_hooks();
}
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set up necessary hooks
*
* @since DOKAN_PRO_SINCE
*
* @return void
*/
public function register_hooks(): void {
add_action( 'dokan_rollback_product_status_reject_to_draft_schedule', array( $this, 'process_reject_operation' ) );
}

/**
* Process reject to draft batch operation
*
* @since DOKAN_PRO_SINCE
*
* @return void
*/
public function process_reject_operation(): void {
global $wpdb;

try {
// Get products for this batch
$products = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'product' AND post_status = %s ORDER BY ID LIMIT %d",
'reject',
self::BATCH_SIZE,
)
);

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

$processed = 0;
foreach ( $products as $product_id ) {
try {
$product = wc_get_product( $product_id );
if ( ! $product instanceof WC_Product ) {
throw new RuntimeException( 'Invalid product' );
}

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

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

// Track previous status
$product->add_meta_data( '_dokan_previous_status', $product->get_status() );
$product->set_status( $target_status );
$product->save();

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

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

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

// Schedule next batch
WC()->queue()->add( 'dokan_rollback_product_status_reject_to_draft_schedule', array(), self::QUEUE_GROUP );
} catch ( Throwable $e ) {
dokan_log(
sprintf(
'Error processing product status reject->draft: %s',
$e->getMessage()
),
'error'
);
}
}
}
Loading