Skip to content

Commit

Permalink
Add paged key support in api (#20)
Browse files Browse the repository at this point in the history
* Add paged key support in api

* Add get seller by order abstract method

* Update zip maker
  • Loading branch information
Aunshon authored Sep 26, 2023
1 parent c402689 commit e8e218c
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 100 deletions.
187 changes: 100 additions & 87 deletions bin/zip.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,147 @@
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
const util = require('util');
const chalk = require('Chalk');
const _ = require('lodash');

const asyncExec = util.promisify( exec );
const fs = require( 'fs-extra' );
const path = require( 'path' );
const { exec } = require( 'child_process' );
const chalk = require( 'Chalk' );
const _ = require( 'lodash' );

const pluginFiles = [
'assets/',
'includes/',
'languages/',
'templates/',
'vendor/',
'changelog.txt',
'readme.txt',
'appsero.json',
'dokan-migrator.php',
];

const removeFiles = [
'src',
'composer.json',
'composer.lock',
];

const allowedVendorFiles = {};
const removeFiles = [ 'assets/src', 'composer.json', 'composer.lock' ];

const { version } = JSON.parse( fs.readFileSync( 'package.json' ) );
const allowedVendorFiles = {
'appsero/client': [ 'src' ],
'jakeasmith/http_build_url': [ 'src' ],
};

// Removing old files.
fs.removeSync( 'build/*.zip' );
const { version } = JSON.parse( fs.readFileSync( 'package.json' ) );

exec(
'rm -rf versions && rm *.zip',
'rm -rf *',
{
cwd: 'build',
},
() => {
const planDir = `build`; // Production build directory.
const dest = `${ planDir }/dokan-migrator`; // Temporary folder name after coping all the files here.
const composerfile = `composer.json`;

// Removing the old build folder.
fs.removeSync( planDir );

console.log( `🗜 Started making the zip...` );

const fileList = [ ...pluginFiles ];
( error ) => {
if ( error ) {
console.log(
chalk.yellow(
`⚠️ Could not find the build directory.`
)
);
console.log(
chalk.green(
`🗂 Creating the build directory ...`
)
);
// Making build folder.
fs.mkdirp( 'build' );
}

// Making build folder.
const dest = 'build/dokan-migrator'; // Temporary folder name after coping all the files here.
fs.mkdirp( dest );

// Coping all the files into build folder.
fileList.forEach( ( file ) => {
fs.copySync( file, `${ dest }/${ file }`);
} );

// copy composer.json file
console.log( `🗜 Started making the zip ...` );
try {
if (fs.pathExistsSync(composerfile)) {
fs.copySync(composerfile, `${dest}/composer.json`);
} else {
fs.copySync(`composer.json`, `${dest}/composer.json`);
}
} catch (err) {
console.error(err);
console.log( `⚙️ Copying plugin files ...` );

// Coping all the files into build folder.
pluginFiles.forEach( ( file ) => {
fs.copySync( file, `${ dest }/${ file }` );
} );
console.log( `📂 Finished copying files.` );
} catch ( err ) {
console.error( chalk.red( '❌ Could not copy plugin files.' ), err );
return;
}

console.log(`📂 Finished copying files.`);

asyncExec(
exec(
'composer install --optimize-autoloader --no-dev',
{
cwd: dest,
cwd: dest
},
() => {
( error ) => {
if ( error ) {
console.log(
chalk.red(
`❌ Could not install composer in ${ dest } directory.`
)
);
console.log( chalk.bgRed.black( error ) );

return;
}

console.log(
`⚡️ Installed composer packages in ${dest} directory.`
`⚡️ Installed composer packages in ${ dest } directory.`
);

// Removing files that is not needed in the production now.
removeFiles.forEach((file) => {
fs.removeSync(`${dest}/${file}`);
});

Object.keys( allowedVendorFiles ).forEach( ( composerPackage ) => {
const packagePath = path.resolve(
`${ dest }/vendor/${ composerPackage }`
);
removeFiles.forEach( ( file ) => {
fs.removeSync( `${ dest }/${ file }` );
} );

if ( !fs.existsSync( packagePath ) ) {
return;
Object.keys( allowedVendorFiles ).forEach(
( composerPackage ) => {
const packagePath = path.resolve(
`${ dest }/vendor/${ composerPackage }`
);

if ( ! fs.existsSync( packagePath ) ) {
return;
}

const list = fs.readdirSync( packagePath );
const deletables = _.difference(
list,
allowedVendorFiles[ composerPackage ]
);

deletables.forEach( ( deletable ) => {
fs.removeSync(
path.resolve( packagePath, deletable )
);
} );
}

const list = fs.readdirSync( packagePath );
const deletables = _.difference(
list,
allowedVendorFiles[ composerPackage ]
);

deletables.forEach( ( deletable ) => {
fs.removeSync( path.resolve( packagePath, deletable ) );
} );
} );
);

// Output zip file name.
const zipFile = `dokan-migrator-${ version }.zip`;
const zipFile = `dokan-migrator-v${ version }.zip`;

console.log(`📦 Making zip file ${ zipFile }...`);
console.log( `📦 Making the zip file ${ zipFile } ...` );

// Making the zip file here.
asyncExec(
exec(
`zip ${ zipFile } dokan-migrator -rq`,
{
cwd: planDir,
cwd: 'build'
},
() => {
( error ) => {
if ( error ) {
console.log(
chalk.red( `❌ Could not make ${ zipFile }.` )
);
console.log( chalk.bgRed.black( error ) );

return;
}

fs.removeSync( dest );
console.log( chalk.green( `✅ ${zipFile} is ready. 🎉` ) );
console.log(
chalk.green( `✅ ${ zipFile } is ready. 🎉` )
);
}
).catch( ( error ) => {
console.log( chalk.red( `Could not make ${ zipFile }.`) );
console.log( error );
} );
);
}
).catch( ( error ) => {
console.log(
chalk.red( `Could not install composer in ${dest} directory.` )
);
console.log( error );
} );
);
}
);
);
11 changes: 10 additions & 1 deletion includes/Abstracts/OrderMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ abstract public function create_sub_order_if_needed( $seller_id, $seller_product
*/
abstract public function reset_sub_orders_if_needed();

/**
* Get seller by order/order id
*
* @since DOKAN_MIG_SINCE
*
* @return void
*/
abstract public function get_seller_by_order( $order_id );

/**
* Returns true if the order has sub orders.
*
Expand Down Expand Up @@ -243,7 +252,7 @@ public function sync_dokan_order_table( $dokan_order_data, $sub_order_id, $selle
* @return void
*/
public function process_migration() {
$vendors = dokan_get_sellers_by( $this->order_id );
$vendors = $this->get_seller_by_order( $this->order_id );

$this->reset_sub_orders_if_needed();

Expand Down
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
13 changes: 12 additions & 1 deletion includes/Integrations/Wcfm/OrderMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public function split_parent_order_shipping( $applied_shipping_method, $order_id
}

$applied_shipping_method = reset( $parent_order->get_shipping_methods() );
$vendors = dokan_get_sellers_by( $parent_order->get_id() );
$vendors = $this->get_seller_by_order( $parent_order->get_id() );

// Here we are dividing the shipping and shipping-tax amount of parent order into the vendors suborders.
$shipping_tax_amount = [
Expand Down Expand Up @@ -482,4 +482,15 @@ public function split_parent_order_shipping( $applied_shipping_method, $order_id

return $item;
}

/**
* Returns all sellers of an order.
*
* @param int $order_id
*
* @return array
*/
public function get_seller_by_order( $order_id ) {
return dokan_get_sellers_by( $order_id );
}
}
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
Loading

0 comments on commit e8e218c

Please sign in to comment.