| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of Synchronize |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class Synchronize { |
| 12 |
public function sync_products($ids, $action='add'){ |
| 13 |
if(in_array($action, array('add', 'remove'))){ |
| 14 |
$request_url = RequestHelper::build_request('sync_data'); |
| 15 |
$data = array('type'=>'single', 'pc'=>$this->get_product_cnt(),'action'=>$action, 'ids'=>implode(',', (is_array($ids)?$ids:array($ids)))); |
| 16 |
$request = a2wl_remote_post($request_url, $data); |
| 17 |
if (is_wp_error($request)) { |
| 18 |
$request = a2wl_remote_post($request_url, $data); |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public function gloabal_sync_products(){ |
| 24 |
$request_url = RequestHelper::build_request('sync_data'); |
| 25 |
$data = array('type'=>'gloabal', 'pc'=>$this->get_product_cnt(),'data'=>gzcompress(implode(',', $this->get_product_ids()), 9)); |
| 26 |
$request = a2wl_remote_post($request_url, $data); |
| 27 |
if (is_wp_error($request)) { |
| 28 |
$request = a2wl_remote_post($request_url, $data); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public function get_product_cnt(){ |
| 33 |
global $wpdb; |
| 34 |
$cnt = $wpdb->get_var( "select count(pm.meta_value) from {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm on (p.ID=pm.post_id) WHERE p.post_status<>'trash' and pm.meta_key='_a2w_external_id'"); |
| 35 |
return intval($cnt); |
| 36 |
} |
| 37 |
|
| 38 |
public function get_product_ids(){ |
| 39 |
global $wpdb; |
| 40 |
$results = $wpdb->get_results( "select pm.meta_value as eid from {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm on (p.ID=pm.post_id) WHERE p.post_status<>'trash' and pm.meta_key='_a2w_external_id'", ARRAY_A); |
| 41 |
$ids = array(); |
| 42 |
foreach($results as $r){ |
| 43 |
$ids[] = $r['eid']; |
| 44 |
} |
| 45 |
return $ids; |
| 46 |
} |
| 47 |
} |
| 48 |
|