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