diff --git a/bin/zip.js b/bin/zip.js index f4c2518..5916bc1 100644 --- a/bin/zip.js +++ b/bin/zip.js @@ -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 ); - } ); + ); } -); \ No newline at end of file +); diff --git a/includes/Abstracts/OrderMigration.php b/includes/Abstracts/OrderMigration.php index 4c27de8..8f26658 100644 --- a/includes/Abstracts/OrderMigration.php +++ b/includes/Abstracts/OrderMigration.php @@ -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. * @@ -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(); diff --git a/includes/Abstracts/Processor.php b/includes/Abstracts/Processor.php index 5ab7f9e..4c2de27 100644 --- a/includes/Abstracts/Processor.php +++ b/includes/Abstracts/Processor.php @@ -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. diff --git a/includes/Integrations/Wcfm/OrderMigrator.php b/includes/Integrations/Wcfm/OrderMigrator.php index 02b5b44..538ece9 100644 --- a/includes/Integrations/Wcfm/OrderMigrator.php +++ b/includes/Integrations/Wcfm/OrderMigrator.php @@ -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 = [ @@ -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 ); + } } diff --git a/includes/Migrator/Ajax.php b/includes/Migrator/Ajax.php index 6f1f528..386f09e 100644 --- a/includes/Migrator/Ajax.php +++ b/includes/Migrator/Ajax.php @@ -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 ]; diff --git a/includes/Migrator/Manager.php b/includes/Migrator/Manager.php index 4e48169..fdddb8b 100644 --- a/includes/Migrator/Manager.php +++ b/includes/Migrator/Manager.php @@ -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. * @@ -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; } @@ -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 ) { /** @@ -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; diff --git a/includes/Processors/Vendor.php b/includes/Processors/Vendor.php index be43b55..1161d28 100644 --- a/includes/Processors/Vendor.php +++ b/includes/Processors/Vendor.php @@ -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, diff --git a/includes/Processors/Withdraw.php b/includes/Processors/Withdraw.php index a5ffd7e..2530da6 100644 --- a/includes/Processors/Withdraw.php +++ b/includes/Processors/Withdraw.php @@ -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 ) { diff --git a/src/components/DokanMigrator.js b/src/components/DokanMigrator.js index 19dd5df..941b245 100644 --- a/src/components/DokanMigrator.js +++ b/src/components/DokanMigrator.js @@ -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( { @@ -47,6 +47,7 @@ const DokanMigrator = (props) => { import: importType, number: number, offset: from, + paged: pageNum, total_count: totalItem, total_migrated: getMigrated } @@ -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); @@ -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);