diff --git a/dokan-migrator.php b/dokan-migrator.php index 5740835..dca7139 100644 --- a/dokan-migrator.php +++ b/dokan-migrator.php @@ -45,6 +45,8 @@ * * @class Dokan_Migrator The class that holds the entire Dokan_Migrator plugin * + * @property WeDevs\DokanMigrator\Migrator\Manager $migrator Instance of migrator class. + * * @since 1.0.0 */ final class Dokan_Migrator { @@ -85,6 +87,9 @@ private function __construct() { register_activation_hook( __FILE__, [ $this, 'activate' ] ); + // Add woocommerce HPOS support. + add_action( 'before_woocommerce_init', [ $this, 'add_plugin_hpos_support' ] ); + // load the addon add_action( 'dokan_loaded', array( $this, 'plugin_init' ) ); @@ -163,7 +168,7 @@ private function init_classes() { } $this->container['migrator'] = new \WeDevs\DokanMigrator\Migrator\Manager(); - $this->container = apply_filters( 'dokan_migrator_get_class_container', $this->container ); + $this->container = apply_filters( 'dokan_migrator_get_class_container', $this->container ); } /** @@ -206,6 +211,19 @@ function() { $insights->init(); } + + /** + * Make dokan migrator plugin HPOS supported. + * + * @since DOKAN_MIG_SINCE + * + * @return void + */ + public function add_plugin_hpos_support() { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + } + } } /** diff --git a/includes/Abstracts/OrderMigration.php b/includes/Abstracts/OrderMigration.php index cf94e3a..d842d84 100644 --- a/includes/Abstracts/OrderMigration.php +++ b/includes/Abstracts/OrderMigration.php @@ -1,7 +1,13 @@ $this->order_id, - 'post_type' => 'shop_order', - 'numberposts' => -1, - 'post_status' => 'any', - ); - - return get_children( $args ); + return dokan()->order->get_child_orders( $this->order_id ); } /** @@ -109,10 +108,10 @@ public function get_sub_orders() { public function reset_sub_orders() { if ( $this->has_sub_order() ) { foreach ( $this->get_sub_orders() as $child ) { - wp_delete_post( $child->ID, true ); - $this->clear_dokan_vendor_balance_table( $child->ID ); - $this->clear_dokan_order_table( $child->ID, $child->post_author ); - $this->clear_dokan_refund_table( $child->ID ); + $child->delete( true ); + $this->clear_dokan_vendor_balance_table( $child->get_id() ); + $this->clear_dokan_order_table( $child->get_id(), $child->get_user()->ID ); + $this->clear_dokan_refund_table( $child->get_id() ); } } } @@ -398,7 +397,7 @@ public function dokan_sync_refund_table( $data ) { * * @since 1.0.0 * - * @param \WC_Order $order + * @param WC_Order $order * * @return boolean */ diff --git a/includes/Integrations/Wcfm/OrderMigrator.php b/includes/Integrations/Wcfm/OrderMigrator.php index a8b3d55..50ab80f 100644 --- a/includes/Integrations/Wcfm/OrderMigrator.php +++ b/includes/Integrations/Wcfm/OrderMigrator.php @@ -24,11 +24,11 @@ class OrderMigrator extends OrderMigration { * * @since DOKAN_MIG_SINCE * - * @param \WP_Post $order + * @param \WC_Order $order */ - public function __construct( \WP_Post $order ) { - $this->order_id = $order->ID; - $this->order = wc_get_order( $this->order_id ); + public function __construct( \WC_Order $order ) { + $this->order_id = $order->get_id(); + $this->order = $order; add_filter( 'dokan_shipping_method', [ $this, 'split_parent_order_shipping' ], 10, 3 ); } @@ -52,19 +52,20 @@ public function create_sub_order_if_needed( $seller_id, $seller_products, $paren $this->map_shipping_method_item_meta(); dokan()->order->create_sub_order( $this->order, $seller_id, $seller_products ); - $res = get_posts( - array( - 'numberposts' => 1, - 'post_status' => 'any', - 'post_type' => 'shop_order', - 'post_parent' => $this->order->get_id(), - 'meta_key' => '_dokan_vendor_id', // phpcs:ignore WordPress.DB.SlowDBQuery - 'meta_value' => $seller_id, // phpcs:ignore WordPress.DB.SlowDBQuery - ) + $res = dokan()->order->all( + [ + 'limit' => 1, + 'parent' => $this->order->get_id(), + 'seller_id' => $seller_id, + ] ); + + /** + * @var $created_suborder WC_Order|\WC_Order_Refund + */ $created_suborder = reset( $res ); - return wc_get_order( $created_suborder->ID ); + return $created_suborder; } /** diff --git a/includes/Integrations/Wcfm/VendorMigrator.php b/includes/Integrations/Wcfm/VendorMigrator.php index 5996372..315805e 100644 --- a/includes/Integrations/Wcfm/VendorMigrator.php +++ b/includes/Integrations/Wcfm/VendorMigrator.php @@ -313,13 +313,13 @@ public function get_store_seo( $default ) { */ public function get_commission() { $accepted_commission_types = array( 'percent', 'fixed' ); - $dokan_commission = [ - 'dokan_admin_percentage' => '', // WCFM fixed value, commission_fixed + $dokan_commission = [ + 'dokan_admin_percentage' => '', // WCFM fixed value, commission_fixed 'dokan_admin_percentage_type' => '', // WCFM commission type, commission_mode - 'dokan_admin_additional_fee' => '', // WCFM percentage value, commission_percent + 'dokan_admin_additional_fee' => '', // WCFM percentage value, commission_percent ]; - $commission = $this->get_profile_settings_val( 'commission', [] ); - $commission_type = isset( $commission['commission_mode'] ) ? $commission['commission_mode'] : 'global'; + $commission = $this->get_profile_settings_val( 'commission', [] ); + $commission_type = isset( $commission['commission_mode'] ) ? $commission['commission_mode'] : 'global'; if ( in_array( $commission_type, $accepted_commission_types, true ) ) { $dokan_commission['dokan_admin_percentage'] = isset( $commission['commission_fixed'] ) ? $commission['commission_fixed'] : ''; diff --git a/includes/Migrator/Manager.php b/includes/Migrator/Manager.php index ea71884..3496c65 100644 --- a/includes/Migrator/Manager.php +++ b/includes/Migrator/Manager.php @@ -4,6 +4,9 @@ defined( 'ABSPATH' ) || exit; +use WeDevs\DokanMigrator\Integrations\Wcfm\OrderMigrator as WcfmOrderMigrator; +use WeDevs\DokanMigrator\Integrations\Wcfm\VendorMigrator as WcfmVendorMigrator; +use WeDevs\DokanMigrator\Integrations\Wcfm\WithdrawMigrator as WcfmWithdrawMigrator; use WeDevs\DokanMigrator\Migrator\Ajax; use WeDevs\DokanMigrator\Migrator\Assets; @@ -161,7 +164,7 @@ public function get_total( $import_type, $migratable ) { * @param string $plugin Handle of the plugin which is being migrated * @param array{number:int,offset:int,total_count:int,total_migrated:int} $data * - * @return void + * @return array * @throws \Exception */ public function migrate( $import_type, $plugin, $data ) { @@ -171,17 +174,20 @@ public function migrate( $import_type, $plugin, $data ) { $processor = $this->processor_class( $import_type ); - $data = 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 ); - foreach ( $data as $value ) { + foreach ( $data_to_migrate as $value ) { + /** + * @var $migrator WcfmOrderMigrator|WcfmVendorMigrator|WcfmWithdrawMigrator + */ $migrator = call_user_func( [ $processor, 'get_migration_class' ], $plugin, $value ); $migrator->process_migration(); } $args = [ - 'migrated' => count( $data ), - 'next' => count( $data ) + $this->offset, - 'total_migrated' => count( $data ) + $this->total_migrated, + 'migrated' => count( $data_to_migrate ), + 'next' => count( $data_to_migrate ) + $this->offset, + 'total_migrated' => count( $data_to_migrate ) + $this->total_migrated, ]; $progress = ( $args['total_migrated'] * 100 ) / $this->total_count; diff --git a/includes/Processors/Order.php b/includes/Processors/Order.php index 9b35080..21fe20b 100644 --- a/includes/Processors/Order.php +++ b/includes/Processors/Order.php @@ -25,9 +25,7 @@ class Order extends Processor { * @return integer */ public static function get_total( $plugin ) { - global $wpdb; - - return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_type='shop_order' AND post_parent=0" ); + return (int) dokan()->order->all( [ 'return' => 'count' ] ); } /** @@ -35,20 +33,19 @@ public static function get_total( $plugin ) { * * @since 1.0.0 * - * @return array + * @return \WC_Order[] + * * @throws \Exception */ public static function get_items( $plugin, $number, $offset ) { $args = array( - 'post_type' => 'shop_order', - 'orderby' => 'ASC', - 'post_status' => 'any', - 'offset' => $offset, - 'posts_per_page' => $number, - 'post_parent' => 0, + 'order' => 'ASC', + 'paged' => $offset + 1, + 'limit' => $number, + 'parent' => 0, ); - $orders = get_posts( $args ); + $orders = dokan()->order->all( $args ); if ( empty( $orders ) ) { self::throw_error(); @@ -65,7 +62,7 @@ public static function get_items( $plugin, $number, $offset ) { * @param string $plugin * @param object $payload * - * @return object + * @return object|WcfmOrderMigrator * @throws \Exception */ public static function get_migration_class( $plugin, $payload ) {