| 1 |
<?php |
| 2 |
/** |
| 3 |
* Description of AddonsController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
use Pages; |
| 14 |
|
| 15 |
class AddonsController extends AbstractAdminPage { |
| 16 |
private $update_period = 3600; //60*60*1; |
| 17 |
private $addons; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
$this->addons = get_option('a2wl_addons_data', array()); |
| 21 |
if(empty($this->addons['addons'])){ |
| 22 |
$this->addons['addons'] = array(); |
| 23 |
} |
| 24 |
$new_addons_cnt = is_admin()?$this->get_new_addons_count():0; |
| 25 |
|
| 26 |
$menuTitle = Pages::getLabel(Pages::ADDONS) . |
| 27 |
($new_addons_cnt ? ' <span class="update-plugins count-' . |
| 28 |
$new_addons_cnt . '"><span class="plugin-count">' . |
| 29 |
$new_addons_cnt . '</span></span>' : ''); |
| 30 |
|
| 31 |
parent::__construct( |
| 32 |
Pages::getLabel(Pages::ADDONS), |
| 33 |
$menuTitle, |
| 34 |
Capability::pluginAccess(), |
| 35 |
Pages::ADDONS, |
| 36 |
100 |
| 37 |
); |
| 38 |
|
| 39 |
if (empty($this->addons['next_update']) || $this->addons['next_update'] < time()) { |
| 40 |
$request = a2wl_remote_get(get_setting('api_endpoint').'addons.php'); |
| 41 |
if (!is_wp_error($request) && intval($request['response']['code']) == 200) { |
| 42 |
$this->addons['addons'] = json_decode($request['body'], true); |
| 43 |
} |
| 44 |
$this->addons['next_update'] = time() + $this->update_period; |
| 45 |
update_option('a2wl_addons_data', $this->addons, 'no'); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function render($params = []): void |
| 50 |
{ |
| 51 |
if (!PageGuardHelper::canAccessPage(Pages::ADDONS)) { |
| 52 |
wp_die($this->getErrorTextNoPermissions()); |
| 53 |
} |
| 54 |
|
| 55 |
$this->set_viewed_addons(); |
| 56 |
$this->model_put('addons', $this->addons); |
| 57 |
$this->include_view('addons.php'); |
| 58 |
} |
| 59 |
|
| 60 |
private function get_new_addons_count() { |
| 61 |
if(empty($this->addons['viewed_addons'])){ |
| 62 |
return empty($this->addons['addons'])?0:count($this->addons['addons']); |
| 63 |
}else{ |
| 64 |
$viewed_cnt = 0; |
| 65 |
$addons = !empty($this->addons['addons']) ? $this->addons['addons'] : array(); |
| 66 |
foreach ($addons as $addon) { |
| 67 |
if (in_array($addon['id'], $this->addons['viewed_addons'])) { |
| 68 |
$viewed_cnt++; |
| 69 |
} |
| 70 |
} |
| 71 |
return count($addons) - $viewed_cnt; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
private function set_viewed_addons() { |
| 76 |
$this->addons['viewed_addons'] = array(); |
| 77 |
foreach ($this->addons['addons'] as $addon) { |
| 78 |
$this->addons['viewed_addons'][]=$addon['id']; |
| 79 |
} |
| 80 |
update_option('a2wl_addons_data', $this->addons, 'no'); |
| 81 |
} |
| 82 |
} |
| 83 |
|