| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of SynchronizePluginDataController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_admin_init |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
class SynchronizePluginDataController extends AbstractController |
| 14 |
{ |
| 15 |
private Synchronize $Synchronize; |
| 16 |
private GlobalSystemMessageService $GlobalSystemMessageService; |
| 17 |
|
| 18 |
public const SYSTEM_MESSAGE_UPDATE_PERIOD = 3600; |
| 19 |
|
| 20 |
public function __construct( |
| 21 |
Synchronize $Synchronize, |
| 22 |
GlobalSystemMessageService $GlobalSystemMessageService |
| 23 |
) { |
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
$this->Synchronize = $Synchronize; |
| 27 |
$this->GlobalSystemMessageService = $GlobalSystemMessageService; |
| 28 |
|
| 29 |
$system_message_last_update = intval(get_setting(Settings::SETTING_PLUGIN_DATA_LAST_UPDATE)); |
| 30 |
if (!$system_message_last_update || $system_message_last_update < time()) { |
| 31 |
set_setting( |
| 32 |
Settings::SETTING_PLUGIN_DATA_LAST_UPDATE, |
| 33 |
time() + self::SYSTEM_MESSAGE_UPDATE_PERIOD |
| 34 |
); |
| 35 |
|
| 36 |
$request_url = RequestHelper::build_request( |
| 37 |
'sync_plugin_data', |
| 38 |
['pc' => $this->Synchronize->get_product_cnt()] |
| 39 |
); |
| 40 |
$request = a2wl_remote_get($request_url); |
| 41 |
if (!is_wp_error($request) && intval($request['response']['code']) == 200) { |
| 42 |
$pluginData = json_decode($request['body'], true); |
| 43 |
$categories = isset($pluginData['categories']) && |
| 44 |
is_array($pluginData['categories']) ? |
| 45 |
$pluginData['categories'] : []; |
| 46 |
|
| 47 |
if (isset($pluginData['messages'])) { |
| 48 |
$this->GlobalSystemMessageService->addMessages($pluginData['messages']); |
| 49 |
} |
| 50 |
|
| 51 |
update_option('a2wl_all_categories', $categories, 'no'); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|