Skip to content

Commit

Permalink
Add paged key support in api
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Sep 21, 2023
1 parent c402689 commit 2c6e726
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
7 changes: 5 additions & 2 deletions includes/Abstracts/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ abstract public static function get_total( $plugin );
*
* @since 1.0.0
*
* @param string $plugin
* @param string $plugin
* @param integer $number
* @param integer $offset
* @param integer $paged
*
* @return array
*/
abstract public static function get_items( $plugin, $number, $offset );
abstract public static function get_items( $plugin, $number, $offset, $paged );

/**
* Return class to handle migration.
Expand Down
1 change: 1 addition & 0 deletions includes/Migrator/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function import() {
$args = [
'number' => ! empty( $_REQUEST['number'] ) ? absint( $_REQUEST['number'] ) : 10, // phpcs:ignore WordPress.Security.NonceVerification
'offset' => ! empty( $_REQUEST['offset'] ) ? absint( $_REQUEST['offset'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification
'paged' => ! empty( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification
'total_count' => ! empty( $_REQUEST['total_count'] ) ? absint( $_REQUEST['total_count'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification
'total_migrated' => ! empty( $_REQUEST['total_migrated'] ) ? absint( $_REQUEST['total_migrated'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification
];
Expand Down
13 changes: 12 additions & 1 deletion includes/Migrator/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class Manager {
*/
private $offset = 0;

/**
* Get data page id
*
* @since DOKAN_MIG_SINCE
*
* @var integer
*/
private $paged = 1;

/**
* Number of vendors to be migrated.
*
Expand Down Expand Up @@ -133,6 +142,7 @@ protected function set_import_type( $import_type ) {
protected function set_data( $data ) {
$this->number = ! empty( $data['number'] ) ? intval( $data['number'] ) : $this->number;
$this->offset = ! empty( $data['offset'] ) ? intval( $data['offset'] ) : $this->offset;
$this->paged = ! empty( $data['paged'] ) ? intval( $data['paged'] ) : $this->paged;
$this->total_count = ! empty( $data['total_count'] ) ? intval( $data['total_count'] ) : $this->total_count;
$this->total_migrated = ! empty( $data['total_migrated'] ) ? intval( $data['total_migrated'] ) : $this->total_migrated;
}
Expand Down Expand Up @@ -177,7 +187,7 @@ public function migrate( $import_type, $plugin, $data ) {

$processor = $this->processor_class( $import_type );

$data_to_migrate = call_user_func( [ $processor, 'get_items' ], $plugin, $this->number, $this->offset );
$data_to_migrate = call_user_func( [ $processor, 'get_items' ], $plugin, $this->number, $this->offset, $this->paged );

foreach ( $data_to_migrate as $value ) {
/**
Expand All @@ -191,6 +201,7 @@ public function migrate( $import_type, $plugin, $data ) {
'migrated' => count( $data_to_migrate ),
'next' => count( $data_to_migrate ) + $this->offset,
'total_migrated' => count( $data_to_migrate ) + $this->total_migrated,
'paged' => $this->paged,
];

$progress = ( $args['total_migrated'] * 100 ) / $this->total_count;
Expand Down
5 changes: 3 additions & 2 deletions includes/Processors/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public static function get_total( $plugin ) {
*
* @throws \Exception
*/
public static function get_items( $plugin, $number, $offset ) {
public static function get_items( $plugin, $number, $offset, $paged ) {
global $wpdb;
$args = array(
'order' => 'ASC',
'paged' => $offset + 1,
'paged' => $paged,
'limit' => $number,
'parent' => 0,
);
Expand Down
2 changes: 1 addition & 1 deletion includes/Processors/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function get_total( $plugin ) {
* @return array
* @throws \Exception
*/
public static function get_items( $plugin, $number, $offset ) {
public static function get_items( $plugin, $number, $offset, $paged ) {
$args = [
'number' => $number,
'offset' => $offset,
Expand Down
4 changes: 2 additions & 2 deletions includes/Processors/Withdraw.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public static function get_total( $plugin ) {
* @return array
* @throws \Exception
*/
public static function get_items( $plugin, $number, $offset ) {
global $wpdb;
public static function get_items( $plugin, $number, $offset, $paged ) {
global $wpdb;
$withdraws = [];

if ( 0 === (int) $offset ) {
Expand Down
8 changes: 5 additions & 3 deletions src/components/DokanMigrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const DokanMigrator = (props) => {
},[isLoading]);

// Functions.
const migrateVendorsHandler = ( from = offset, totalItem = totalCount, getMigrated = totalMigrated ) => {
const migrationHandler = ( from = offset, totalItem = totalCount, getMigrated = totalMigrated, pageNum = 1 ) => {
setIsLoading(true);
sendRequest(
{
Expand All @@ -47,6 +47,7 @@ const DokanMigrator = (props) => {
import: importType,
number: number,
offset: from,
paged: pageNum,
total_count: totalItem,
total_migrated: getMigrated
}
Expand All @@ -56,7 +57,8 @@ const DokanMigrator = (props) => {
setTotalMigrated(resp.data.process.total_migrated);
if ( resp.data.process && resp.data.process.migrated != 0 ) {
setOffset(resp.data.process.next);
migrateVendorsHandler( resp.data.process.next, totalItem, resp.data.process.total_migrated );

migrationHandler( resp.data.process.next, totalItem, resp.data.process.total_migrated, pageNum + 1 );
}
} else {
setIsLoading(false);
Expand Down Expand Up @@ -96,7 +98,7 @@ const DokanMigrator = (props) => {
props.lastCompleted ? setTotalMigrated( resp.data.migrate.total_count ) : setTotalMigrated(respTotalMigrated);

! startMigration ? setIsLoading(false) : '';
startMigration ? migrateVendorsHandler( respNext, resp.data.migrate.total_count, respTotalMigrated ) : '';
startMigration ? migrationHandler( respNext, resp.data.migrate.total_count, respTotalMigrated, 1 ) : '';

} else {
console.error(resp.data.message);
Expand Down

0 comments on commit 2c6e726

Please sign in to comment.