diff --git a/classes/upgrade.php b/classes/upgrade.php index 84ce97aed5..1fa2a6d9b4 100644 --- a/classes/upgrade.php +++ b/classes/upgrade.php @@ -104,6 +104,8 @@ public function show_update_notice() { public function do_updates() { if ( isset( $_GET['dokan_do_update'] ) && $_GET['dokan_do_update'] ) { $this->perform_updates(); + } else if ( $this->running_background_process() ) { + $this->continue_background_processes(); } } @@ -136,4 +138,43 @@ public function perform_updates() { exit(); } + /** + * Is running any background updater + * + * @since 2.9.0 + * + * @return bool + */ + public function running_background_process() { + $processes = get_option( 'dokan_background_updater_processes', array() ); + + if ( ! empty( $processes ) ) { + return true; + } + + return false; + } + + /** + * Continue background updaters + * + * @since 2.9.0 + * + * @return void + */ + public function continue_background_processes() { + $processes = get_option( 'dokan_background_updater_processes', array() ); + + if ( empty( $processes ) ) { + return; + } + + foreach ( $processes as $process => $run_process ) { + if ( $run_process ) { + include_once DOKAN_INC_DIR . '/upgrades/background-processes/class_' . $process . '.php'; + $processor = new $process(); + } + } + } + } diff --git a/includes/upgrades/background-processes/class-dokan-background-updater.php b/includes/upgrades/background-processes/class-dokan-background-updater.php new file mode 100644 index 0000000000..2b0e70c137 --- /dev/null +++ b/includes/upgrades/background-processes/class-dokan-background-updater.php @@ -0,0 +1,46 @@ +action , $processes ) ) { + unset( $processes[ $this->action ] ); + update_option( 'dokan_background_updater_processes', $processes, 'no' ); + } + + parent::complete(); + } +} diff --git a/includes/upgrades/background-processes/class_dokan_update_2_9_0_vendor_and_product_geolocations.php b/includes/upgrades/background-processes/class_dokan_update_2_9_0_vendor_and_product_geolocations.php new file mode 100644 index 0000000000..b36577c2a0 --- /dev/null +++ b/includes/upgrades/background-processes/class_dokan_update_2_9_0_vendor_and_product_geolocations.php @@ -0,0 +1,150 @@ +update_vendors( $item['paged'] ); + } else if ( 'products' === $item['updating'] ) { + return $this->update_products( $item['paged'] ); + } + + return false; + } + + /** + * Update vendors + * + * @since 2.9.0 + * + * @param int $paged + * + * @return array + */ + private function update_vendors( $paged ) { + $args = array( + 'role' => 'seller', + 'number' => 50, + 'paged' => $paged, + ); + + $query = new WP_User_Query( $args ); + + $vendors = $query->get_results(); + + if ( empty( $vendors ) ) { + return array( + 'updating' => 'products', + 'paged' => 1, + ); + } + + foreach ( $vendors as $vendor ) { + $geo_latitude = get_user_meta( $vendor->ID, 'geo_latitude', true ); + + if ( ! empty( $geo_latitude ) ) { + continue; + } + + $profile_settings = get_user_meta( $vendor->ID, 'dokan_profile_settings', true ); + + if ( ! empty( $profile_settings['location'] && ! empty( $profile_settings['find_address'] ) ) ) { + $location = explode( ',', $profile_settings['location'] ); + + if ( 2 !== count( $location ) ) { + continue; + } + + update_usermeta( $vendor->ID, 'geo_latitude', $location[0] ); + update_usermeta( $vendor->ID, 'geo_longitude', $location[1] ); + update_usermeta( $vendor->ID, 'geo_public', 1 ); + update_usermeta( $vendor->ID, 'geo_address', $profile_settings['find_address'] ); + } + } + + return array( + 'updating' => 'vendors', + 'paged' => ++$paged, + ); + } + + /** + * Update products + * + * @since 2.9.0 + * + * @param int $paged + * + * @return array|bool + */ + private function update_products( $paged ) { + $args = array( + 'post_type' => 'product', + 'posts_per_page' => 50, + 'post_status' => 'any', + 'paged' => $paged, + ); + + $query = new WP_Query( $args ); + + if ( empty( $query->posts ) ) { + return false; + + } else { + foreach ( $query->posts as $post ) { + $geo_latitude = get_post_meta( $post->ID, 'geo_latitude', true ); + + if ( empty( $geo_latitude ) ) { + $vendor_geo_latitude = get_user_meta( $post->post_author, 'geo_latitude', true ); + $vendor_geo_longitude = get_user_meta( $post->post_author, 'geo_longitude', true ); + $vendor_geo_address = get_user_meta( $post->post_author, 'geo_address', true ); + + if ( ! empty( $vendor_geo_latitude ) && ! empty( $vendor_geo_longitude ) ) { + update_post_meta( $post->ID, 'geo_latitude', $vendor_geo_latitude ); + update_post_meta( $post->ID, 'geo_longitude', $vendor_geo_longitude ); + update_post_meta( $post->ID, 'geo_public', 1 ); + update_post_meta( $post->ID, 'geo_address', $vendor_geo_address ); + } + } + } + } + + return array( + 'updating' => 'products', + 'paged' => ++$paged, + ); + } +} diff --git a/includes/upgrades/dokan-upgrade-2.9.0.php b/includes/upgrades/dokan-upgrade-2.9.0.php index 6409250c04..09cd6bdaa5 100644 --- a/includes/upgrades/dokan-upgrade-2.9.0.php +++ b/includes/upgrades/dokan-upgrade-2.9.0.php @@ -9,4 +9,27 @@ function dokan_update_fees_recipient() { update_option( 'dokan_general', $options ); } +function dokan_update_vendor_and_product_geolocations() { + $processes = get_option( 'dokan_background_updater_processes', array() ); + + if ( empty( $processes['dokan_update_2_9_0_vendor_and_product_geolocations'] ) ) { + include_once DOKAN_INC_DIR . '/upgrades/background-processes/class_dokan_update_2_9_0_vendor_and_product_geolocations.php'; + + $processor = new dokan_update_2_9_0_vendor_and_product_geolocations(); + + $payload = array( + 'updating' => 'vendors', + 'paged' => 1, + ); + + $processor->push_to_queue( $payload ); + $processor->save()->dispatch(); + + $processes['dokan_update_2_9_0_vendor_and_product_geolocations'] = true; + + update_option( 'dokan_background_updater_processes', $processes, 'no' ); + } +} + dokan_update_fees_recipient(); +dokan_update_vendor_and_product_geolocations();