PluginProbe ʕ •ᴥ•ʔ
Disable Admin Notices – Hide Dashboard Notifications / 1.4.6
Disable Admin Notices – Hide Dashboard Notifications v1.4.6
1.4.7 1.4.6 1.4.5 trunk 1.0.0 1.0.2 1.0.3 1.0.5 1.0.6 1.1.1 1.1.3 1.1.4 1.2.0 1.2.2 1.2.3 1.2.4 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4
disable-admin-notices / libs / factory / pages / includes / admin-page.class.php
disable-admin-notices / libs / factory / pages / includes Last commit date
admin-page.class.php 1 month ago index.php 3 years ago page.class.php 1 year ago
admin-page.class.php
584 lines
1 <?php
2 /**
3 * Admin page class
4 *
5 * @author Alex Kovalev <alex.kovalevv@gmail.com>
6 * @since 1.0.0
7 * @package factory-core
8 * @copyright (c) 2018, Webcraftic Ltd
9 *
10 */
11
12 // Exit if accessed directly
13 if( !defined('ABSPATH') ) {
14 exit;
15 }
16
17 if( !class_exists('Wbcr_FactoryPages480_AdminPage') ) {
18
19 class Wbcr_FactoryPages480_AdminPage extends Wbcr_FactoryPages480_Page {
20
21 /**
22 * Visible page title.
23 * For example: 'License Manager'
24 *
25 * @var string
26 */
27 public $page_title;
28
29 /**
30 * Visible title in menu.
31 * For example: 'License Manager'
32 *
33 * @var string
34 */
35 public $menu_title = null;
36
37 /**
38 * If set, an extra sub menu will be created with another title.
39 *
40 * @var string
41 */
42 public $menu_sub_title = null;
43
44 /**
45 * Иконка меню в главном меню админ панели
46 *
47 * Используется только в том случае, если ссылка на страницу отображается
48 * в главном меню админ панели (левый сайдбар) и не является элементом подменю.
49 *
50 * Пример: '~/assets/img/menu-icon.png', ~/ будет заменен ссылкой на корневую
51 * директорию плагина.
52 * Можно использовать dashicons: '\f321'
53 *
54 * @var string
55 */
56 public $menu_icon = null;
57
58 /**
59 * Позиция в главном меню админ панели
60 *
61 * Если эта страница была добавлена в главное меню админ панели (левый сайдбар).
62 * Вы можете установить позицию меню. Подробнее смотрите в Wordpress кодексе.
63 *
64 * @link http://codex.wordpress.org/Function_Reference/add_menu_page
65 *
66 * Позиция может быть установлена от 0 до 100 и чем больше цифра, тем ниже будет
67 * расположен пункт меню.
68 *
69 * Внимание! Если два пункта используют одинаковую цифру-позицию, один из пунктов
70 * меню может быть перезаписан и будет показан только один пункт из дву�
71 .
72 * Чтобы избежать конфликта, можно использовать десятичные значения, вместо целы�
73
74 * чисел: 63.3 вместо 63. Используйте кавычки: "63.3".
75 *
76 * 2 Консоль
77 * 4 Разделитель
78 * 5 Посты
79 * 10 Медиа
80 * 15 Ссылки
81 * 20 Страницы
82 * 25 Комментарии
83 * 59 Разделитель
84 * 60 Внешний вид
85 * 65 Плагины
86 * 70 Пользователи
87 * 75 Инструменты
88 * 80 Настройки
89 * 99 Разделитель
90 *
91 * @var string
92 */
93 public $menu_position = null;
94
95 /**
96 * Тип записи к меню которой, нужно прикрепить ссылку на страницу
97 *
98 * К примеру, если вы установите тип записи "post". В меню "Записи" появится
99 * ссылка на эту страницу, как элемент подменю.
100 *
101 * Пример: 'post'
102 *
103 * @var string
104 */
105 public $menu_post_type = null;
106
107 /**
108 * Название (slug) элемента главного родительского меню в админ панели, в которое будет
109 * добавлен пункт меню этой страницы, как элемент подменю.
110 *
111 * Примеры:
112 * index.php - Консоль (Dashboard). Или спец. функция: add_dashboard_page();
113 * edit.php - Посты (Posts). Или спец. функция: add_posts_page();
114 * upload.php - Медиафайлы (Media). Или спец. функция: add_media_page();
115 * link-manager.php - Ссылки (Links). Или спец. функция: add_links_page();
116 * edit.php?post_type=page - Страницы (Pages). Или спец. функция: add_pages_page();
117 * edit-comments.php - Комментарии (Comments). Или спец. функция: add_comments_page();
118 * edit.php?post_type=your_post_type - Произвольные типы записей.
119 * themes.php - Внешний вид (Appearance). Или спец. функция: add_theme_page();
120 * plugins.php - Плагины (Plugins). Или спец. функция: add_plugins_page();
121 * users.php - Пользователи (Users). Или спец. функция: add_users_page();
122 * tools.php - Инструменты (Tools). Или спец. функция: add_management_page();
123 * options-general.php - Настройки (Settings). Или спец. функция: add_options_page()
124 * settings.php - Настройки (Settings) сети сайтов в MU режиме.
125 *
126 * @var string
127 */
128 public $menu_target = null;
129
130 /**
131 * if true, then admin.php is used as a base url.
132 *
133 * @var bool
134 */
135 public $custom_target = false;
136
137 /**
138 * Разрешения пользователя, чтобы иметь доступ к странице.
139 *
140 * Этот параметр отвечает и за доступ к странице этого пункта меню. Подробнее
141 * смотрите в кодексе Wordpress:
142 *
143 * @link http://codex.wordpress.org/Roles_and_Capabilities
144 *
145 * Указывать массив разрешений, например:
146 * ['manage_options', 'manage_network']
147 *
148 * @var array
149 */
150 public $capabilitiy = null;
151
152 /**
153 * Скрыть страницу из главного меню админ панели?
154 *
155 * Если true, то закладка на эту страницу не будет добавлена в главное меню
156 * админ панели.
157 *
158 * @var bool
159 */
160 public $internal = false;
161
162 /**
163 * If true, the page is for network
164 *
165 * @var bool
166 */
167 public $network = false;
168
169 /**
170 * Предотвратить создание страницы?
171 *
172 * Если true, то страница не будет создана. Может пригодиться в те�
173 случая�
174 ,
175 * когда страница должна быть создана только при выполнении условий.
176 *
177 * @since 3.0.6
178 * @var bool
179 */
180 public $hidden = false;
181
182 /**
183 * Сделать доступной страницу в панели управлениям сайтами (панель суперадминистратора)
184 *
185 * Если установлено true, в панели управления сайтами появится закладка на эту страницу.
186 * Также эта страница получить разрешения на просмотр для группы суперадминистраторов.
187 *
188 * @var bool
189 */
190 public $available_for_multisite = false;
191
192 /**
193 * Задать текст ссылки на странице плагинов (рядом с активировать/деактивировать)
194 *
195 * Будет работать только, свойство $add_link_to_plugin_actions=true.
196 * По умолчанию, если текст ссылки не задан, используется заголовок элемента меню
197 * или заголовок страницы.
198 *
199 * @var string
200 */
201 public $title_plugin_action_link;
202
203 /**
204 * Добавлять ссылку на странице плагинов (рядом с активировать/деактивировать)?
205 *
206 * Если true, будет автоматически добавлена ссылка на эту страницу, внутри страницы
207 * wp-admin/plugins.php (рядом с активировать/деактивировать). Чаще всего требуется
208 * добавить ссылку на страницу настроек. Если эта страница у вас является главной,
209 * то вы можете сделать это свойство активным.
210 *
211 * @link https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
212 * @var bool
213 */
214 public $add_link_to_plugin_actions = false;
215
216 public function __construct(Wbcr_Factory480_Plugin $plugin)
217 {
218 parent::__construct($plugin);
219 $this->configure();
220
221 $this->id = empty($this->id) ? str_replace('adminpage', '', strtolower(get_class($this))) : $this->id;
222
223 if( $this->add_link_to_plugin_actions ) {
224 if( $plugin->isNetworkActive() ) {
225 // plugin settings link
226 add_filter("network_admin_plugin_action_links_" . $this->plugin->get_paths()->basename, [
227 $this,
228 'addLinkToPluginActions'
229 ]);
230 } else {
231 // plugin settings link
232 add_filter("plugin_action_links_" . $this->plugin->get_paths()->basename, [
233 $this,
234 'addLinkToPluginActions'
235 ]);
236 }
237 }
238 }
239
240 /**
241 * May be used to configure the page before uts usage.
242 */
243 public function configure()
244 {
245 }
246
247 /**
248 * Includes the Factory Bootstrap assets for a current page.
249 *
250 * @param string $hook
251 *
252 * @return void
253 */
254 public function actionAdminBootstrapScripts($hook)
255 {
256 $this->scripts->connect('bootstrap');
257 $this->styles->connect('bootstrap');
258 }
259
260 /**
261 * Includes the assets for a current page (all assets except Factory Bootstrap assets).
262 *
263 * @param string $hook
264 *
265 * @return void
266 */
267 public function actionAdminScripts($hook)
268 {
269 $this->scripts->connect();
270 $this->styles->connect();
271 }
272
273 /**
274 * @return string
275 */
276 public function getMenuScope()
277 {
278 return $this->plugin->getPluginName();
279 }
280
281
282 /**
283 * @return string
284 */
285 public function getMenuTitle()
286 {
287 $menu_title = !$this->menu_title ? $this->page_title : $this->menu_title;
288
289 /**
290 * @since 4.0.9 - добавлен
291 */
292 return apply_filters('wbcr/factory/pages/impressive/menu_title', $menu_title, $this->plugin->getPluginName(), $this->id);
293 }
294
295 /**
296 * @return string
297 */
298 public function getPageTitle()
299 {
300
301 $page_title = !$this->page_title ? $this->getMenuTitle() : $this->page_title;
302
303 /**
304 * @since 4.0.9 - добавлен
305 */
306 return apply_filters('wbcr/factory/pages/impressive/page_title', $page_title, $this->plugin->getPluginName(), $this->id);
307 }
308
309 /**
310 * @param null $id
311 *
312 * @return mixed|string
313 */
314 public function getResultId($id = null)
315 {
316 $id = !empty($id) ? $id : $this->id;
317
318 if( $this->plugin ) {
319 return $id . '-' . $this->getMenuScope();
320 }
321
322 return $id;
323 }
324
325 /**
326 * Registers admin page for the admin menu.
327 */
328 public function connect()
329 {
330 $result_id = $this->getResultId();
331
332 $this->hidden = apply_filters('wbcr_factory_480_page_is_hidden_' . $result_id, $this->hidden);
333
334 if( $this->hidden ) {
335 return;
336 }
337
338 $this->internal = apply_filters('wbcr_factory_480_page_is_internal_' . $result_id, $this->internal);
339
340 if( $this->internal ) {
341 $this->menu_target = null;
342 $this->menu_post_type = null;
343 }
344
345 // if this page for a custom menu page
346 if( $this->menu_post_type ) {
347 $this->menu_target = 'edit.php?post_type=' . $this->menu_post_type;
348
349 if( empty($this->capabilitiy) ) {
350 $post_type_object = get_post_type_object( $this->menu_post_type );
351 $this->capabilitiy = $post_type_object ? $post_type_object->cap->edit_posts : 'manage_options';
352 }
353 }
354
355 // sets default capabilities
356 if( empty($this->capabilitiy) ) {
357 $this->capabilitiy = 'manage_options';
358 }
359
360
361 // makes redirect to the page
362 $controller = $this->request->get('fy_page', null, true);
363
364 if( $controller && $controller == $this->id ) {
365 $plugin = $this->request->get('fy_plugin', null, true);
366
367 if( $this->plugin->getPluginName() == $plugin ) {
368
369 $action = $this->request->get('fy_action', 'index', true);
370 $is_ajax = $this->request->get('fy_ajax', false);
371
372 if( $is_ajax && current_user_can( $this->capabilitiy ) ) {
373 $this->executeByName($action);
374 exit;
375 } else {
376
377 $params = (array)$this->request->getAll(true);
378
379 unset($params['fy_page']);
380 unset($params['fy_plugin']);
381 unset($params['fy_action']);
382
383 $this->redirectToAction($action, $params);
384 }
385 }
386 }
387
388 // calls scripts and styles, adds pages to menu
389 if( $this->request->get('page', 'none') == $result_id ) {
390 $this->assets($this->scripts, $this->styles);
391
392 if( !$this->scripts->isEmpty('bootstrap') || !$this->styles->isEmpty('bootstrap') ) {
393 add_action('wbcr_factory_480_bootstrap_enqueue_scripts_' . $this->plugin->getPluginName(), [
394 $this,
395 'actionAdminBootstrapScripts'
396 ]);
397 }
398
399 // includes styles and scripts
400 if( !$this->scripts->isEmpty() || !$this->styles->isEmpty() ) {
401 add_action('admin_enqueue_scripts', [$this, 'actionAdminScripts']);
402 }
403 }
404
405 // submenu
406 if( $this->menu_target ) {
407 add_submenu_page($this->menu_target, $this->getPageTitle(), $this->getMenuTitle(), $this->capabilitiy, $result_id, [
408 $this,
409 'show'
410 ]);
411 // global menu
412 } else {
413 add_menu_page($this->getPageTitle(), $this->getMenuTitle(), $this->capabilitiy, $result_id, [
414 $this,
415 'show'
416 ], null, $this->menu_position);
417
418 if( !empty($this->menu_sub_title) ) {
419
420 add_submenu_page($result_id, $this->menu_sub_title, $this->menu_sub_title, $this->capabilitiy, $result_id, [
421 $this,
422 'show'
423 ]);
424 }
425
426 add_action('admin_head', [$this, 'actionAdminHead']);
427 }
428
429 // executes an action
430 if( $this->current() ) {
431 ob_start();
432 $action = $this->request->get('action', 'index', true);
433 $this->executeByName($action);
434 $this->result = ob_get_contents();
435 ob_end_clean();
436 }
437 }
438
439 protected function current()
440 {
441 $result_id = $this->getResultId();
442
443 if( $result_id == $this->request->get('page', 'none') ) {
444 return true;
445 }
446
447 return false;
448 }
449
450 /**
451 * @param string $action
452 * @param array $query_args
453 */
454 public function redirectToAction($action, $query_args = [])
455 {
456
457 wp_safe_redirect($this->getActionUrl($action, $query_args));
458 exit;
459 }
460
461 /**
462 * @param string $action
463 * @param array $query_args
464 */
465 public function actionUrl($action = null, $query_args = [])
466 {
467 echo $this->getActionUrl($action, $query_args);
468 }
469
470 /**
471 * @param null $action
472 * @param array $query_args
473 *
474 * @return string
475 */
476 public function getActionUrl($action = null, $query_args = [])
477 {
478 $url = $this->getBaseUrl(null, $query_args);
479
480 if( !empty($action) ) {
481 $url = add_query_arg('action', $action, $url);
482 }
483
484 return $url;
485 }
486
487 /**
488 * @return string
489 */
490 public function getBaseUrl($id = null, $query_args = [])
491 {
492 $result_id = $this->getResultId($id);
493
494 if( $this->menu_target && !$this->custom_target ) {
495 $url = $this->network ? network_admin_url($this->menu_target) : admin_url($this->menu_target);
496
497 return add_query_arg(array_merge(['page' => $result_id], $query_args), $url);
498 }
499
500 $url = $this->network ? network_admin_url('admin.php') : admin_url('admin.php');
501
502 return add_query_arg(array_merge(['page' => $result_id, $query_args]), $url);
503 }
504
505 public function actionAdminHead()
506 {
507 $result_id = $this->getResultId();
508
509 if( !empty($this->menu_icon) ) {
510
511 if( preg_match('/\\\f\d{3}/', $this->menu_icon) ) {
512 $icon_code = $this->menu_icon;
513 } else {
514 $icon_url = str_replace('~/', $this->plugin->get_paths()->url . '/', $this->menu_icon);
515 }
516 }
517
518 global $wp_version;
519
520 if( version_compare($wp_version, '3.7.3', '>') ) {
521 ?>
522 <style type="text/css" media="screen">
523 <?php if ( !empty($icon_url) ) { ?>
524
525 a.toplevel_page_<?php echo $result_id ?> .wp-menu-image {
526 background: url('<?php echo $icon_url ?>') no-repeat 10px -30px !important;
527 }
528
529 <?php } ?>
530
531 a.toplevel_page_<?php echo $result_id ?> .wp-menu-image:before {
532 content: "<?php echo !empty($icon_code) ? $icon_code : ''; ?>" !important;
533 }
534
535 a.toplevel_page_<?php echo $result_id ?>:hover .wp-menu-image,
536 a.toplevel_page_<?php echo $result_id ?>.wp-has-current-submenu .wp-menu-image,
537 a.toplevel_page_<?php echo $result_id ?>.current .wp-menu-image {
538 background-position: 10px 2px !important;
539 }
540 </style>
541 <?php } else { ?>
542 <style type="text/css" media="screen">
543 a.toplevel_page_<?php echo $result_id ?> .wp-menu-image {
544 background: url('<?php echo $icon_url ?>') no-repeat 6px -33px !important;
545 }
546
547 a.toplevel_page_<?php echo $result_id ?>:hover .wp-menu-image,
548 a.toplevel_page_<?php echo $result_id ?>.current .wp-menu-image {
549 background-position: 6px -1px !important;
550 }
551 </style>
552 <?php
553 }
554
555 if( $this->internal ) {
556 ?>
557 <style type="text/css" media="screen">
558 li.toplevel_page_<?php echo $result_id ?> {
559 display: none;
560 }
561 </style>
562 <?php
563 }
564 }
565
566
567 /**
568 * Add settings link in plugins list
569 *
570 * @param $links
571 *
572 * @return mixed
573 */
574 function addLinkToPluginActions($links)
575 {
576 $link_title = !empty($this->title_plugin_action_link) ? $this->title_plugin_action_link : $this->getMenuTitle();
577
578 $settings_link = '<a href="' . $this->getBaseUrl() . '">' . $link_title . '</a>';
579 array_unshift($links, $settings_link);
580
581 return $links;
582 }
583 }
584 }