Skip to content

Commit

Permalink
feat(core): Add an updater to update vendor and product geolocation d…
Browse files Browse the repository at this point in the history
…ata (#354)
  • Loading branch information
ediamin authored and sabbir1991 committed Aug 30, 2018
1 parent eb53101 commit b1c468b
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 0 deletions.
41 changes: 41 additions & 0 deletions classes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}
}

}
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();
}
}
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,
);
}
}
23 changes: 23 additions & 0 deletions includes/upgrades/dokan-upgrade-2.9.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit b1c468b

Please sign in to comment.