| 1 |
<?php |
| 2 |
|
| 3 |
/* * class |
| 4 |
* Description of AbstractPage |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @position: 2 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
// phpcs:ignoreFile WordPress.Security.EscapeOutput.OutputNotEscaped |
| 13 |
abstract class AbstractAdminPage extends AbstractController |
| 14 |
{ |
| 15 |
|
| 16 |
private $page_title; |
| 17 |
private $menu_title; |
| 18 |
private $capability; |
| 19 |
private $menu_slug; |
| 20 |
private $menu_type; |
| 21 |
private $style_assets = array(); |
| 22 |
private $script_assets = array(); |
| 23 |
private $script_data_assets = array(); |
| 24 |
|
| 25 |
private GlobalSystemMessageService $GlobalSystemMessageService; |
| 26 |
|
| 27 |
|
| 28 |
// $menu_type: 0 - standart menu, 1 - menu as link, 2 - skip menu |
| 29 |
public function __construct($page_title, $menu_title, $capability, $menu_slug, $priority = 10, $menu_type=0) { |
| 30 |
parent::__construct(A2WL()->plugin_path() . '/view/'); |
| 31 |
|
| 32 |
$this->GlobalSystemMessageService = A2WL()->getDI()->get('AliNext_Lite\GlobalSystemMessageService'); |
| 33 |
|
| 34 |
if (is_admin()) { |
| 35 |
$this->init($page_title, $menu_title, $capability, $menu_slug, $priority, $menu_type); |
| 36 |
|
| 37 |
add_action('a2wl_admin_assets', array($this, 'admin_register_assets'), 1); |
| 38 |
|
| 39 |
add_action('a2wl_admin_assets', array($this, 'admin_enqueue_assets'), 2); |
| 40 |
|
| 41 |
add_action('wp_loaded', array($this, 'before_render_action')); |
| 42 |
|
| 43 |
if ($this->is_current_page() && !Woocommerce::is_woocommerce_installed() && !has_action('admin_notices', array($this, 'woocomerce_check_error'))) { |
| 44 |
add_action('admin_notices', array($this, 'woocomerce_check_error')); |
| 45 |
} |
| 46 |
|
| 47 |
if ($this->is_current_page() && !has_action('admin_notices', [$this, 'global_system_message'])) { |
| 48 |
add_action('admin_notices', [$this, 'global_system_message']); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
function woocomerce_check_error(): void |
| 54 |
{ |
| 55 |
echo '<div id="message2222" class="notice error is-dismissible"><p>' . |
| 56 |
esc_html__('AliNext (Lite version) notice! Please install the <a href="https://woocommerce.com/" target="_blank">WooCommerce</a> plugin first.', 'ali2woo') . |
| 57 |
'</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>'; |
| 58 |
} |
| 59 |
|
| 60 |
function global_system_message(): void |
| 61 |
{ |
| 62 |
$messages = $this->GlobalSystemMessageService->getAllMessages(); |
| 63 |
echo implode('', $messages); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
protected function init($page_title, $menu_title, $capability, $menu_slug, $priority, $menu_type): void |
| 68 |
{ |
| 69 |
$this->page_title = $page_title; |
| 70 |
$this->menu_title = $menu_title; |
| 71 |
$this->capability = $capability; |
| 72 |
$this->menu_slug = $menu_slug; |
| 73 |
$this->menu_type = $menu_type; |
| 74 |
|
| 75 |
add_action('a2wl_init_admin_menu', array($this, 'add_submenu_page'), $priority); |
| 76 |
|
| 77 |
if ($this->menu_type == 2) { |
| 78 |
add_action('admin_menu', function () { |
| 79 |
remove_menu_page($this->menu_slug); |
| 80 |
}); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function add_submenu_page($parent_slug): void |
| 85 |
{ |
| 86 |
if ($this->menu_type == 1) { |
| 87 |
$page_id = add_submenu_page( |
| 88 |
$parent_slug, $this->page_title, $this->menu_title, $this->capability, $this->menu_slug |
| 89 |
); |
| 90 |
} else if ($this->menu_type == 2) { |
| 91 |
$page_id = $this->addHiddenAdminPage(); |
| 92 |
} else { |
| 93 |
$page_id = add_submenu_page( |
| 94 |
$parent_slug, $this->page_title, $this->menu_title, |
| 95 |
$this->capability, $this->menu_slug, [$this, 'render'] |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
add_action("load-{$page_id}", [$this, 'configure_screen_options']); |
| 100 |
} |
| 101 |
|
| 102 |
public function before_render_action(): void |
| 103 |
{ |
| 104 |
if ($this->is_current_page()) { |
| 105 |
$this->before_admin_render(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function before_admin_render() { |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public function configure_screen_options() { |
| 114 |
} |
| 115 |
|
| 116 |
abstract public function render($params = array()); |
| 117 |
|
| 118 |
public function add_style($handle, $src, $deps = array(), $ver = false, $media = 'all'){ |
| 119 |
$this->style_assets[] = array('handle'=>$handle, 'src'=>$src, 'deps'=>$deps, 'ver'=>$ver, 'media'=>$media); |
| 120 |
} |
| 121 |
|
| 122 |
public function add_script($handle, $src, $deps = array(), $ver = false, $in_footer = false){ |
| 123 |
$this->script_assets[] = array('handle'=>$handle, 'src'=>$src, 'deps'=>$deps, 'ver'=>$ver, 'in_footer'=>$in_footer); |
| 124 |
} |
| 125 |
|
| 126 |
public function add_data_script($handle, $name, $data){ |
| 127 |
$this->script_data_assets[] = array('handle'=>$handle, 'name'=>$name, 'data'=>$data); |
| 128 |
} |
| 129 |
|
| 130 |
public function admin_register_assets() { |
| 131 |
if ($this->is_current_page()) { |
| 132 |
if (!wp_style_is('a2wl-admin-style', 'registered')) { |
| 133 |
wp_register_style('a2wl-admin-style', A2WL()->plugin_url() . '/assets/css/admin_style.css', array(), A2WL()->version); |
| 134 |
} |
| 135 |
|
| 136 |
if (!wp_script_is('a2wl-sprintf-script', 'registered')) { |
| 137 |
wp_register_script('a2wl-sprintf-script', A2WL()->plugin_url() . '/assets/js/sprintf.js', array(), A2WL()->version); |
| 138 |
} |
| 139 |
|
| 140 |
if (!wp_script_is('a2wl-admin-script', 'registered')) { |
| 141 |
wp_register_script('a2wl-admin-script', A2WL()->plugin_url() . '/assets/js/admin_script.js', array('jquery'), A2WL()->version); |
| 142 |
} |
| 143 |
|
| 144 |
if (!wp_script_is('a2wl-admin-svg', 'registered')) { |
| 145 |
wp_register_script('a2wl-admin-svg', A2WL()->plugin_url() . '/assets/js/svg.min.js', array('jquery','a2wl-admin-script'), A2WL()->version); |
| 146 |
} |
| 147 |
|
| 148 |
/* select2 */ |
| 149 |
if (!wp_style_is('a2wl-select2-style', 'registered')) { |
| 150 |
wp_register_style('a2wl-select2-style', A2WL()->plugin_url() . '/assets/js/select2/css/select2.min.css', array(), A2WL()->version); |
| 151 |
} |
| 152 |
if (!wp_script_is('a2wl-select2-js', 'registered')) { |
| 153 |
// wp_register_script('a2wl-select2-js', A2WL()->plugin_url() . '/assets/js/select2/js/select2.min.js', array('jquery'), A2WL()->version); |
| 154 |
wp_register_script('a2wl-select2-js', A2WL()->plugin_url() . '/assets/js/select2/js/select2.js', array('jquery'), A2WL()->version); |
| 155 |
} |
| 156 |
|
| 157 |
/* jsrender */ |
| 158 |
if (!wp_script_is('a2wl-jsrender-js', 'registered')) { |
| 159 |
wp_register_script('a2wl-jsrender-js', A2WL()->plugin_url() . '/assets/js/jsrender.min.js', ['jquery'], A2WL()->version); |
| 160 |
} |
| 161 |
|
| 162 |
/*jquery.lazyload*/ |
| 163 |
if (!wp_script_is('a2wl-lazyload-js', 'registered')) { |
| 164 |
wp_register_script('a2wl-lazyload-js', A2WL()->plugin_url() . '/assets/js/jquery/jquery.lazyload.js', array('jquery'), A2WL()->version); |
| 165 |
} |
| 166 |
|
| 167 |
/* custom styles */ |
| 168 |
if (!wp_style_is('a2wl-custom-style', 'registered')) { |
| 169 |
wp_register_style('a2wl-custom-style', A2WL()->plugin_url() . '/assets/css/custom.css', array(), A2WL()->version); |
| 170 |
} |
| 171 |
|
| 172 |
foreach($this->style_assets as $s){ |
| 173 |
if (!wp_style_is($s['handle'], 'registered')) { |
| 174 |
wp_register_style($s['handle'], A2WL()->plugin_url() . $s['src'], $s['deps'], !empty($s['ver'])?$s['ver']:A2WL()->version, $s['media']); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
foreach($this->script_assets as $s){ |
| 179 |
if (!wp_script_is($s['handle'], 'registered')) { |
| 180 |
wp_register_script($s['handle'], A2WL()->plugin_url() . $s['src'], $s['deps'], !empty($s['ver'])?$s['ver']:A2WL()->version, $s['in_footer']); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$lang_data = []; |
| 185 |
wp_localize_script( |
| 186 |
'a2wl-admin-script', |
| 187 |
'a2wl_common_data', |
| 188 |
[ |
| 189 |
'baseurl' => A2WL()->plugin_url().'/', |
| 190 |
'lang' => apply_filters('a2wl_configure_lang_data', $lang_data), |
| 191 |
'lang_cookies' => AliexpressLocalizator::getInstance()->getLocaleCookies(false) |
| 192 |
] |
| 193 |
); |
| 194 |
|
| 195 |
foreach ($this->script_data_assets as $d) { |
| 196 |
wp_localize_script($d['handle'], $d['name'], $d['data']); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public function admin_enqueue_assets($page) { |
| 202 |
if ($this->is_current_page()) { |
| 203 |
wp_enqueue_script('jquery-ui-sortable'); |
| 204 |
|
| 205 |
wp_enqueue_script('jquery-effects-core'); |
| 206 |
|
| 207 |
if (!wp_style_is('a2wl-admin-style', 'enqueued')) { |
| 208 |
wp_enqueue_style('a2wl-admin-style'); |
| 209 |
wp_style_add_data( 'a2wl-admin-style', 'rtl', 'replace' ); |
| 210 |
} |
| 211 |
if (!wp_style_is('a2wl-admin-style-new', 'enqueued')) { |
| 212 |
wp_enqueue_style('a2wl-admin-style-new'); |
| 213 |
} |
| 214 |
|
| 215 |
if (!wp_script_is('a2wl-sprintf-script', 'enqueued')) { |
| 216 |
wp_enqueue_script('a2wl-sprintf-script'); |
| 217 |
} |
| 218 |
if (!wp_script_is('a2wl-admin-script', 'enqueued')) { |
| 219 |
wp_enqueue_script('a2wl-admin-script'); |
| 220 |
} |
| 221 |
if (!wp_script_is('a2wl-admin-svg', 'enqueued')) { |
| 222 |
wp_enqueue_script('a2wl-admin-svg'); |
| 223 |
} |
| 224 |
|
| 225 |
/* select2 */ |
| 226 |
if (!wp_style_is('a2wl-select2-style', 'enqueued')) { |
| 227 |
wp_enqueue_style('a2wl-select2-style'); |
| 228 |
} |
| 229 |
if (!wp_script_is('a2wl-select2-js', 'enqueued')) { |
| 230 |
wp_enqueue_script('a2wl-select2-js'); |
| 231 |
} |
| 232 |
|
| 233 |
/* jsrender */ |
| 234 |
if (!wp_script_is('a2wl-jsrender-js', 'enqueued')) { |
| 235 |
wp_enqueue_script('a2wl-jsrender-js'); |
| 236 |
} |
| 237 |
|
| 238 |
/*jquery.lazyload*/ |
| 239 |
if (!wp_script_is('a2wl-lazyload-js', 'enqueued')) { |
| 240 |
wp_enqueue_script('a2wl-lazyload-js'); |
| 241 |
} |
| 242 |
|
| 243 |
/* custom */ |
| 244 |
if (!wp_style_is('a2wl-custom-style', 'enqueued')) { |
| 245 |
wp_enqueue_style('a2wl-custom-style'); |
| 246 |
} |
| 247 |
|
| 248 |
foreach($this->style_assets as $style){ |
| 249 |
if (!wp_style_is($style['handle'], 'enqueued')) { |
| 250 |
wp_enqueue_style($style['handle']); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
foreach($this->script_assets as $script){ |
| 255 |
if (!wp_script_is($script['handle'], 'enqueued')) { |
| 256 |
wp_enqueue_script($script['handle']); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
$this->localizeAdminScript(); |
| 261 |
|
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
static public function localizeAdminScript(): void |
| 266 |
{ |
| 267 |
$data = [ |
| 268 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 269 |
'nonce_action' => wp_create_nonce(self::AJAX_NONCE_ACTION), |
| 270 |
]; |
| 271 |
wp_localize_script('a2wl-admin-script', 'a2wl_admin_script', $data); |
| 272 |
} |
| 273 |
|
| 274 |
protected function is_current_page(): bool |
| 275 |
{ |
| 276 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 277 |
return is_admin() && isset($_REQUEST['page']) && $_REQUEST['page'] && $this->menu_slug == $_REQUEST['page']; |
| 278 |
} |
| 279 |
|
| 280 |
protected function modelPutArray(array $data): void |
| 281 |
{ |
| 282 |
foreach ($data as $key => $value) { |
| 283 |
$this->model_put($key, $value); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
private function addHiddenAdminPage(): string|false |
| 288 |
{ |
| 289 |
// Добавляем страницу напрямую, но скрываем её из меню |
| 290 |
$pageId = add_menu_page( |
| 291 |
$this->page_title, |
| 292 |
'', |
| 293 |
$this->capability, |
| 294 |
$this->menu_slug, |
| 295 |
[$this, 'render'], |
| 296 |
'', |
| 297 |
99 |
| 298 |
); |
| 299 |
|
| 300 |
return $pageId; |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|