-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add an updater to update vendor and product geolocation d…
…ata (#354)
- Loading branch information
1 parent
eb53101
commit b1c468b
Showing
4 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
includes/upgrades/background-processes/class-dokan-background-updater.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
if ( ! class_exists( 'WP_Async_Request', false ) ) { | ||
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php'; | ||
} | ||
|
||
if ( ! class_exists( 'WP_Background_Process', false ) ) { | ||
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php'; | ||
} | ||
|
||
/** | ||
* Abstract Dokan_Background_Updater class | ||
*/ | ||
abstract class Dokan_Background_Updater extends WP_Background_Process { | ||
|
||
/** | ||
* Action | ||
* | ||
* Override this action in your updater class | ||
* | ||
* @since 2.9.0 | ||
* | ||
* @var string | ||
*/ | ||
protected $action = 'dokan_background_process'; | ||
|
||
/** | ||
* Execute after complete a task | ||
* | ||
* @since 2.9.0 | ||
* | ||
* @return void | ||
*/ | ||
public function complete() { | ||
$processes = get_option( 'dokan_background_updater_processes', array() ); | ||
|
||
if ( array_key_exists( $this->action , $processes ) ) { | ||
unset( $processes[ $this->action ] ); | ||
update_option( 'dokan_background_updater_processes', $processes, 'no' ); | ||
} | ||
|
||
parent::complete(); | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
...pgrades/background-processes/class_dokan_update_2_9_0_vendor_and_product_geolocations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
if ( ! class_exists( 'Dokan_Background_Updater', false ) ) { | ||
include_once DOKAN_INC_DIR . '/upgrades/background-processes/class-dokan-background-updater.php'; | ||
} | ||
|
||
/** | ||
* Update vendor and product geolocation data | ||
* | ||
* @since 2.9.0 | ||
*/ | ||
class dokan_update_2_9_0_vendor_and_product_geolocations extends Dokan_Background_Updater { | ||
|
||
/** | ||
* Action | ||
* | ||
* @since 2.9.0 | ||
* | ||
* @var string | ||
*/ | ||
protected $action = 'dokan_update_2_9_0_vendor_and_product_geolocations'; | ||
|
||
/** | ||
* Perform updates | ||
* | ||
* @since 2.9.0 | ||
* | ||
* @param mixed $item | ||
* | ||
* @return mixed | ||
*/ | ||
public function task( $item ) { | ||
if ( empty( $item ) ) { | ||
return false; | ||
} | ||
|
||
if ( 'vendors' === $item['updating'] ) { | ||
return $this->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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters