| @@ -1,490 +1,503 @@ | ||
| 1 | -<?php | |
| 2 | -defined( 'ABSPATH' ) || exit(); | |
| 3 | - | |
| 4 | -/** | |
| 5 | - * Class LP_Abstract_Submenu | |
| 6 | - * | |
| 7 | - * @since 3.0.0 | |
| 8 | - */ | |
| 9 | -abstract class LP_Abstract_Submenu { | |
| 10 | - /** | |
| 11 | - * Menu slug | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - protected $id = ''; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Menu title | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $menu_title = ''; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Page title | |
| 26 | - * | |
| 27 | - * @var string | |
| 28 | - */ | |
| 29 | - protected $page_title = ''; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Priority | |
| 33 | - * | |
| 34 | - * @var int | |
| 35 | - */ | |
| 36 | - protected $priority = 10; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * Capability | |
| 40 | - * | |
| 41 | - * @var string | |
| 42 | - */ | |
| 43 | - protected $capability = 'manage_options'; | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Menu icon | |
| 47 | - * | |
| 48 | - * @var string | |
| 49 | - */ | |
| 50 | - protected $icon = ''; | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * Callback (may not be used) | |
| 54 | - * | |
| 55 | - * @var bool | |
| 56 | - */ | |
| 57 | - protected $callback = false; | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Heading tabs | |
| 61 | - * | |
| 62 | - * @var array | |
| 63 | - */ | |
| 64 | - protected $tabs = array(); | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Tab's sections | |
| 68 | - * | |
| 69 | - * @var array | |
| 70 | - */ | |
| 71 | - protected $sections = array(); | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Current page | |
| 75 | - * | |
| 76 | - * @var bool | |
| 77 | - */ | |
| 78 | - protected $page = false; | |
| 79 | - | |
| 80 | - /** | |
| 81 | - * LP_Abstract_Submenu constructor. | |
| 82 | - */ | |
| 83 | - public function __construct() { | |
| 84 | - add_action( 'learn-press/admin/page-content-sections', array( $this, 'output_section_nav' ) ); | |
| 85 | - add_filter( 'admin_body_class', array( $this, 'body_class' ) ); | |
| 86 | - | |
| 87 | - if ( $this->is_displaying() ) { | |
| 88 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); | |
| 89 | - } | |
| 90 | - } | |
| 91 | - | |
| 92 | - public function enqueue_assets() { | |
| 93 | - } | |
| 94 | - | |
| 95 | - public function is_displaying() { | |
| 96 | - return $this->get_id() === LP_Request::get_string( 'page' ); | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * @return string | |
| 101 | - */ | |
| 102 | - public function get_id() { | |
| 103 | - return $this->id; | |
| 104 | - } | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * @param string $id | |
| 108 | - */ | |
| 109 | - public function set_id( $id ) { | |
| 110 | - $this->id = $id; | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * @return string | |
| 115 | - */ | |
| 116 | - public function get_menu_title() { | |
| 117 | - return $this->menu_title; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * @param string $menu_title | |
| 122 | - */ | |
| 123 | - public function set_menu_title( $menu_title ) { | |
| 124 | - $this->menu_title = $menu_title; | |
| 125 | - } | |
| 126 | - | |
| 127 | - /** | |
| 128 | - * @return string | |
| 129 | - */ | |
| 130 | - public function get_page_title() { | |
| 131 | - return $this->page_title; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * @param string $page_title | |
| 136 | - */ | |
| 137 | - public function set_page_title( $page_title ) { | |
| 138 | - $this->page_title = $page_title; | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | - * @return int|string | |
| 143 | - */ | |
| 144 | - public function get_priority() { | |
| 145 | - return $this->priority; | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | - * @param string|int $priority | |
| 150 | - */ | |
| 151 | - public function set_priority( $priority ) { | |
| 152 | - $this->priority = $priority; | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * @return string | |
| 157 | - */ | |
| 158 | - public function get_capability() { | |
| 159 | - return $this->capability; | |
| 160 | - } | |
| 161 | - | |
| 162 | - /** | |
| 163 | - * @param string $capability | |
| 164 | - */ | |
| 165 | - public function set_capability( $capability ) { | |
| 166 | - $this->capability = $capability; | |
| 167 | - } | |
| 168 | - | |
| 169 | - /** | |
| 170 | - * @return string | |
| 171 | - */ | |
| 172 | - public function get_icon() { | |
| 173 | - return $this->icon; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * @param string $icon | |
| 178 | - */ | |
| 179 | - public function set_icon( $icon ) { | |
| 180 | - $this->icon = $icon; | |
| 181 | - } | |
| 182 | - | |
| 183 | - /** | |
| 184 | - * Get heading tabs. | |
| 185 | - * | |
| 186 | - * @return mixed|array | |
| 187 | - */ | |
| 188 | - public function get_tabs() { | |
| 189 | - return apply_filters( 'learn-press/submenu-' . $this->get_id() . '-heading-tabs', $this->sanitize_tabs( $this->tabs ) ); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Sanitize the tabs. | |
| 194 | - * | |
| 195 | - * @param $tabs | |
| 196 | - * | |
| 197 | - * @return array | |
| 198 | - */ | |
| 199 | - public function sanitize_tabs( $tabs ) { | |
| 200 | - $sanitized_tabs = array(); | |
| 201 | - if ( $tabs ) { | |
| 202 | - foreach ( $tabs as $tab => $name ) { | |
| 203 | - // Maybe a tab is name of a class? Try to locate it. | |
| 204 | - if ( is_string( $name ) && class_exists( $name ) ) { | |
| 205 | - $objSettings = new $name(); | |
| 206 | - $sanitized_tabs[ $objSettings->id ] = $objSettings; | |
| 207 | - } else { | |
| 208 | - $sanitized_tabs[ $tab ] = $name; | |
| 209 | - } | |
| 210 | - } | |
| 211 | - } | |
| 212 | - | |
| 213 | - return $sanitized_tabs; | |
| 214 | - } | |
| 215 | - | |
| 216 | - /** | |
| 217 | - * Get active tab by checking ?tab=tab-name | |
| 218 | - * | |
| 219 | - * @return bool|mixed | |
| 220 | - */ | |
| 221 | - public function get_active_tab() { | |
| 222 | - $tabs = $this->get_tabs(); | |
| 223 | - if ( ! $tabs ) { | |
| 224 | - return false; | |
| 225 | - } | |
| 226 | - $tab = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] ?? '' ); | |
| 227 | - if ( empty( $tab ) || empty( $tabs[ $tab ] ) ) { | |
| 228 | - $tab_keys = array_keys( $tabs ); | |
| 229 | - $tab = reset( $tab_keys ); | |
| 230 | - } | |
| 231 | - | |
| 232 | - return $tab; | |
| 233 | - } | |
| 234 | - | |
| 235 | - /** | |
| 236 | - * @return array|mixed | |
| 237 | - */ | |
| 238 | - public function has_tabs() { | |
| 239 | - return $this->get_tabs(); | |
| 240 | - } | |
| 241 | - | |
| 242 | - /** | |
| 243 | - * @return mixed | |
| 244 | - */ | |
| 245 | - public function get_sections() { | |
| 246 | - $active_tab = $this->get_active_tab(); | |
| 247 | - | |
| 248 | - if ( ! empty( $this->tabs[ $active_tab ] ) ) { | |
| 249 | - if ( is_callable( array( $this->tabs[ $active_tab ], 'get_sections' ) ) ) { | |
| 250 | - $this->sections = call_user_func( array( $this->tabs[ $active_tab ], 'get_sections' ) ); | |
| 251 | - } | |
| 252 | - } | |
| 253 | - | |
| 254 | - return apply_filters( 'learn-press/submenu-sections', $this->sections ); | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Get current page is displaying. | |
| 259 | - * | |
| 260 | - * @param bool $prefix | |
| 261 | - * | |
| 262 | - * @return bool|mixed|null | |
| 263 | - */ | |
| 264 | - public function get_active_page( $prefix = true ) { | |
| 265 | - if ( false === $this->page ) { | |
| 266 | - $this->page = ! empty( $_REQUEST['page'] ) ? sanitize_text_field( $_REQUEST['page'] ) : null; | |
| 267 | - } | |
| 268 | - | |
| 269 | - return $prefix ? $this->page : str_replace( 'learn-press-', '', $this->page ); | |
| 270 | - } | |
| 271 | - | |
| 272 | - /** | |
| 273 | - * Get active section by checking ?section=tab-name | |
| 274 | - * | |
| 275 | - * @return bool|mixed | |
| 276 | - */ | |
| 277 | - public function get_active_section() { | |
| 278 | - $sections = $this->get_sections(); | |
| 279 | - | |
| 280 | - if ( ! $sections ) { | |
| 281 | - return false; | |
| 282 | - } | |
| 283 | - | |
| 284 | - $section = LP_Helper::sanitize_params_submitted( $_REQUEST['section'] ?? '' ); | |
| 285 | - if ( empty( $section ) || empty( $sections[ $section ] ) ) { | |
| 286 | - $section_keys = array_keys( $sections ); | |
| 287 | - $section = reset( $section_keys ); | |
| 288 | - } | |
| 289 | - | |
| 290 | - return $section; | |
| 291 | - } | |
| 292 | - | |
| 293 | - /** | |
| 294 | - * Callback return template. | |
| 295 | - * | |
| 296 | - * @return bool|array|mixed | |
| 297 | - */ | |
| 298 | - public function get_callback() { | |
| 299 | - return $this->callback; | |
| 300 | - } | |
| 301 | - | |
| 302 | - /** | |
| 303 | - * Display menu content | |
| 304 | - */ | |
| 305 | - public function display() { | |
| 306 | - $tabs = $this->get_tabs(); | |
| 307 | - $active_tab = $this->get_active_tab(); | |
| 308 | - $classes = array( 'wrap', 'lp-submenu-page', $this->get_id() ); | |
| 309 | - ?> | |
| 310 | - | |
| 311 | - <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> | |
| 312 | - <?php do_action( 'learn-press/admin/heading-icon', $active_tab ); ?> | |
| 313 | - | |
| 314 | - <h1 class="wp-heading-inline"> | |
| 315 | - <?php echo wp_kses_post( $this->get_menu_title() ); ?> | |
| 316 | - <?php do_action( 'learn-press/admin/heading-title', $active_tab ); ?> | |
| 317 | - </h1> | |
| 318 | - | |
| 319 | - <?php if ( $tabs ) { ?> | |
| 320 | - <h2 class="nav-tab-wrapper"> | |
| 321 | - <?php foreach ( $tabs as $tab => $name ) { ?> | |
| 322 | - <?php | |
| 323 | - $obj_tab = false; | |
| 324 | - | |
| 325 | - if ( is_object( $name ) ) { | |
| 326 | - $obj_tab = $name; | |
| 327 | - $name = $obj_tab->text; | |
| 328 | - $tab = $obj_tab->id; | |
| 329 | - } | |
| 330 | - | |
| 331 | - $active_class = ( $tab == $active_tab ) ? ' nav-tab-active' : ''; | |
| 332 | - $tab_title = apply_filters( 'learn-press/admin/submenu-heading-tab-title', $name, $tab ); | |
| 333 | - ?> | |
| 334 | - | |
| 335 | - <?php if ( $active_class ) { ?> | |
| 336 | - <span class="nav-tab<?php echo esc_attr( $active_class ); ?>"><?php echo esc_html( $tab_title ); ?></span> | |
| 337 | - <?php } else { ?> | |
| 338 | - <a class="nav-tab" href="?page=<?php echo esc_attr( $this->id ); ?>&tab=<?php echo esc_attr( $tab ); ?>"><?php echo esc_html( $tab_title ); ?></a> | |
| 339 | - <?php } ?> | |
| 340 | - <?php } ?> | |
| 341 | - </h2> | |
| 342 | - <?php } ?> | |
| 343 | - | |
| 344 | - <?php | |
| 345 | - $classes = array( 'lp-admin-tabs' ); | |
| 346 | - $sections = $this->get_sections(); | |
| 347 | - | |
| 348 | - if ( $sections && sizeof( $sections ) > 1 ) { | |
| 349 | - $classes[] = 'has-sections'; | |
| 350 | - } | |
| 351 | - ?> | |
| 352 | - | |
| 353 | - <form class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" method="post" enctype="multipart/form-data"> | |
| 354 | - <?php $this->page_content(); ?> | |
| 355 | - </form> | |
| 356 | - </div> | |
| 357 | - <?php | |
| 358 | - } | |
| 359 | - | |
| 360 | - /** | |
| 361 | - * This function for displaying content of active tab only. | |
| 362 | - * For displaying content of main page without tab, | |
| 363 | - * overwrite this function in sub class. | |
| 364 | - */ | |
| 365 | - public function page_content() { | |
| 366 | - do_action( 'learn-press/admin/page-content-sections', $this ); | |
| 367 | - | |
| 368 | - echo '<div class="lp-admin-tab-content">'; | |
| 369 | - | |
| 370 | - if ( $this->has_tabs() ) { | |
| 371 | - $page = $this->_get_page(); | |
| 372 | - $tab = $this->get_active_tab(); | |
| 373 | - | |
| 374 | - do_action( 'learn-press/admin/before-page-content-sections', $page, $tab, $this ); | |
| 375 | - | |
| 376 | - // If I have a function named 'page_content_TAB_SLUG' then call it. | |
| 377 | - $callback = array( $this, sprintf( 'page_content_%s', $tab ) ); | |
| 378 | - | |
| 379 | - if ( is_callable( $callback ) ) { | |
| 380 | - call_user_func_array( $callback, array() ); | |
| 381 | - } else { | |
| 382 | - do_action( 'learn-press/admin/page-content-' . $page, $tab ); | |
| 383 | - do_action( 'learn-press/admin/page-content-' . $page . '/' . $tab ); | |
| 384 | - } | |
| 385 | - | |
| 386 | - do_action( 'learn-press/admin/after-page-content-sections', $page, $tab, $this ); | |
| 387 | - } | |
| 388 | - | |
| 389 | - echo '</div>'; | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * Output section navigation. | |
| 394 | - */ | |
| 395 | - public function output_section_nav() { | |
| 396 | - if ( $this->id !== $this->get_active_page() ) { | |
| 397 | - return; | |
| 398 | - } | |
| 399 | - | |
| 400 | - $active_section = $this->get_active_section(); | |
| 401 | - $sections = $this->get_sections(); | |
| 402 | - | |
| 403 | - if ( ! $sections ) { | |
| 404 | - return; | |
| 405 | - } | |
| 406 | - ?> | |
| 407 | - | |
| 408 | - <ul class="lp-admin-tab-navs"> | |
| 409 | - <?php foreach ( $sections as $slug => $section ) { ?> | |
| 410 | - <?php | |
| 411 | - $active_class = ( $slug == $active_section ) ? ' nav-section-active' : ''; | |
| 412 | - $section_title = apply_filters( 'learn-press/admin/submenu-section-title', $section, $slug ); | |
| 413 | - ?> | |
| 414 | - | |
| 415 | - <li class="nav-section<?php echo esc_attr( $active_class ); ?>"> | |
| 416 | - <?php if ( $active_class ) { ?> | |
| 417 | - <span><?php echo wp_kses_post( $section_title ); ?></span> | |
| 418 | - <?php } else { ?> | |
| 419 | - <a href="<?php echo esc_url_raw( remove_query_arg( 'sub-section', add_query_arg( 'section', $slug ) ) ); ?>"><?php echo wp_kses_post( $section_title ); ?></a> | |
| 420 | - <?php } ?> | |
| 421 | - </li> | |
| 422 | - <?php } ?> | |
| 423 | - </ul> | |
| 424 | - | |
| 425 | - <?php | |
| 426 | - } | |
| 427 | - | |
| 428 | - /** | |
| 429 | - * Display section content | |
| 430 | - */ | |
| 431 | - public function display_section() { | |
| 432 | - $section = $this->get_active_section(); | |
| 433 | - | |
| 434 | - if ( ! $section ) { | |
| 435 | - return false; | |
| 436 | - } | |
| 437 | - | |
| 438 | - $section_class = $this->sections[ $section ]; | |
| 439 | - | |
| 440 | - if ( ! $section_class ) { | |
| 441 | - return false; | |
| 442 | - } | |
| 443 | - | |
| 444 | - do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content', $section ); | |
| 445 | - | |
| 446 | - if ( is_callable( array( $this, 'section_content_' . $section ) ) ) { | |
| 447 | - call_user_func( array( $this, 'section_content_' . $section ) ); | |
| 448 | - } else { | |
| 449 | - do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content-' . $section ); | |
| 450 | - } | |
| 451 | - | |
| 452 | - return true; | |
| 453 | - } | |
| 454 | - | |
| 455 | - /** | |
| 456 | - * Get this page id without prefix. | |
| 457 | - * | |
| 458 | - * @return mixed | |
| 459 | - */ | |
| 460 | - protected function _get_page() { | |
| 461 | - return str_replace( 'learn-press-', '', $this->get_id() ); | |
| 462 | - } | |
| 463 | - | |
| 464 | - /** | |
| 465 | - * Append new class to body tag to control our page. | |
| 466 | - * | |
| 467 | - * @param $classes | |
| 468 | - * | |
| 469 | - * @return array|string | |
| 470 | - */ | |
| 471 | - public function body_class( $classes ) { | |
| 472 | - $page = $this->get_active_page( false ); | |
| 473 | - | |
| 474 | - if ( $page ) { | |
| 475 | - if ( $classes ) { | |
| 476 | - $classes = explode( ' ', $classes ); | |
| 477 | - } else { | |
| 478 | - $classes = array(); | |
| 479 | - } | |
| 480 | - | |
| 481 | - $classes[] = 'learnpress'; | |
| 482 | - $classes[] = 'lp-submenu-' . $page; | |
| 483 | - $classes = array_filter( $classes ); | |
| 484 | - $classes = array_unique( $classes ); | |
| 485 | - $classes = join( ' ', $classes ); | |
| 486 | - } | |
| 487 | - | |
| 488 | - return $classes; | |
| 489 | - } | |
| 490 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +use LearnPress\Helpers\Template; | |
| 4 | + | |
| 5 | +defined( 'ABSPATH' ) || exit(); | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Class LP_Abstract_Submenu | |
| 9 | + * | |
| 10 | + * @since 3.0.0 | |
| 11 | + */ | |
| 12 | +abstract class LP_Abstract_Submenu { | |
| 13 | + /** | |
| 14 | + * Menu slug | |
| 15 | + * | |
| 16 | + * @var string | |
| 17 | + */ | |
| 18 | + protected $id = ''; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Menu title | |
| 22 | + * | |
| 23 | + * @var string | |
| 24 | + */ | |
| 25 | + protected $menu_title = ''; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Page title | |
| 29 | + * | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + protected $page_title = ''; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Priority | |
| 36 | + * | |
| 37 | + * @var int | |
| 38 | + */ | |
| 39 | + protected $priority = 10; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Capability | |
| 43 | + * | |
| 44 | + * @var string | |
| 45 | + */ | |
| 46 | + protected $capability = 'manage_options'; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Menu icon | |
| 50 | + * | |
| 51 | + * @var string | |
| 52 | + */ | |
| 53 | + protected $icon = ''; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Callback (may not be used) | |
| 57 | + * | |
| 58 | + * @var bool | |
| 59 | + */ | |
| 60 | + protected $callback = false; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Heading tabs | |
| 64 | + * | |
| 65 | + * @var array | |
| 66 | + */ | |
| 67 | + protected $tabs = array(); | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Tab's sections | |
| 71 | + * | |
| 72 | + * @var array | |
| 73 | + */ | |
| 74 | + protected $sections = array(); | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Current page | |
| 78 | + * | |
| 79 | + * @var bool | |
| 80 | + */ | |
| 81 | + protected $page = false; | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * LP_Abstract_Submenu constructor. | |
| 85 | + */ | |
| 86 | + public function __construct() { | |
| 87 | + add_action( 'learn-press/admin/page-content-sections', array( $this, 'output_section_nav' ) ); | |
| 88 | + add_filter( 'admin_body_class', array( $this, 'body_class' ) ); | |
| 89 | + | |
| 90 | + if ( $this->is_displaying() ) { | |
| 91 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + public function enqueue_assets() { | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function is_displaying() { | |
| 99 | + return $this->get_id() === LP_Request::get_param( 'page' ); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * @return string | |
| 104 | + */ | |
| 105 | + public function get_id() { | |
| 106 | + return $this->id; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * @param string $id | |
| 111 | + */ | |
| 112 | + public function set_id( $id ) { | |
| 113 | + $this->id = $id; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * @return string | |
| 118 | + */ | |
| 119 | + public function get_menu_title() { | |
| 120 | + return $this->menu_title; | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * @param string $menu_title | |
| 125 | + */ | |
| 126 | + public function set_menu_title( $menu_title ) { | |
| 127 | + $this->menu_title = $menu_title; | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * @return string | |
| 132 | + */ | |
| 133 | + public function get_page_title() { | |
| 134 | + return $this->page_title; | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * @param string $page_title | |
| 139 | + */ | |
| 140 | + public function set_page_title( $page_title ) { | |
| 141 | + $this->page_title = $page_title; | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * @return int|string | |
| 146 | + */ | |
| 147 | + public function get_priority() { | |
| 148 | + return $this->priority; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * @param string|int $priority | |
| 153 | + */ | |
| 154 | + public function set_priority( $priority ) { | |
| 155 | + $this->priority = $priority; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * @return string | |
| 160 | + */ | |
| 161 | + public function get_capability() { | |
| 162 | + return $this->capability; | |
| 163 | + } | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * @param string $capability | |
| 167 | + */ | |
| 168 | + public function set_capability( $capability ) { | |
| 169 | + $this->capability = $capability; | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * @return string | |
| 174 | + */ | |
| 175 | + public function get_icon() { | |
| 176 | + return $this->icon; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * @param string $icon | |
| 181 | + */ | |
| 182 | + public function set_icon( $icon ) { | |
| 183 | + $this->icon = $icon; | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Get heading tabs. | |
| 188 | + * | |
| 189 | + * @return mixed|array | |
| 190 | + */ | |
| 191 | + public function get_tabs() { | |
| 192 | + return apply_filters( 'learn-press/submenu-' . $this->get_id() . '-heading-tabs', $this->sanitize_tabs( $this->tabs ) ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Sanitize the tabs. | |
| 197 | + * | |
| 198 | + * @param $tabs | |
| 199 | + * | |
| 200 | + * @return array | |
| 201 | + */ | |
| 202 | + public function sanitize_tabs( $tabs ) { | |
| 203 | + $sanitized_tabs = array(); | |
| 204 | + if ( $tabs ) { | |
| 205 | + foreach ( $tabs as $tab => $name ) { | |
| 206 | + // Maybe a tab is name of a class? Try to locate it. | |
| 207 | + if ( is_string( $name ) && class_exists( $name ) ) { | |
| 208 | + $objSettings = new $name(); | |
| 209 | + $sanitized_tabs[ $objSettings->id ] = $objSettings; | |
| 210 | + } else { | |
| 211 | + $sanitized_tabs[ $tab ] = $name; | |
| 212 | + } | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + return $sanitized_tabs; | |
| 217 | + } | |
| 218 | + | |
| 219 | + /** | |
| 220 | + * Get active tab by checking ?tab=tab-name | |
| 221 | + * | |
| 222 | + * @return bool|mixed | |
| 223 | + */ | |
| 224 | + public function get_active_tab() { | |
| 225 | + $tabs = $this->get_tabs(); | |
| 226 | + if ( ! $tabs ) { | |
| 227 | + return false; | |
| 228 | + } | |
| 229 | + $tab = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] ?? '' ); | |
| 230 | + if ( empty( $tab ) || empty( $tabs[ $tab ] ) ) { | |
| 231 | + $tab_keys = array_keys( $tabs ); | |
| 232 | + $tab = reset( $tab_keys ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + return $tab; | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * @return array|mixed | |
| 240 | + */ | |
| 241 | + public function has_tabs() { | |
| 242 | + return $this->get_tabs(); | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * @return mixed | |
| 247 | + */ | |
| 248 | + public function get_sections() { | |
| 249 | + $active_tab = $this->get_active_tab(); | |
| 250 | + | |
| 251 | + if ( ! empty( $this->tabs[ $active_tab ] ) ) { | |
| 252 | + if ( is_callable( array( $this->tabs[ $active_tab ], 'get_sections' ) ) ) { | |
| 253 | + $this->sections = call_user_func( array( $this->tabs[ $active_tab ], 'get_sections' ) ); | |
| 254 | + } | |
| 255 | + } | |
| 256 | + | |
| 257 | + return apply_filters( 'learn-press/submenu-sections', $this->sections ); | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 261 | + * Get current page is displaying. | |
| 262 | + * | |
| 263 | + * @param bool $prefix | |
| 264 | + * | |
| 265 | + * @return bool|mixed|null | |
| 266 | + */ | |
| 267 | + public function get_active_page( $prefix = true ) { | |
| 268 | + if ( false === $this->page ) { | |
| 269 | + $this->page = LP_Request::get_param( 'page' ); | |
| 270 | + } | |
| 271 | + | |
| 272 | + return $prefix ? $this->page : str_replace( 'learn-press-', '', $this->page ); | |
| 273 | + } | |
| 274 | + | |
| 275 | + /** | |
| 276 | + * Get active section by checking ?section=tab-name | |
| 277 | + * | |
| 278 | + * @return bool|mixed | |
| 279 | + */ | |
| 280 | + public function get_active_section() { | |
| 281 | + $sections = $this->get_sections(); | |
| 282 | + | |
| 283 | + if ( ! $sections ) { | |
| 284 | + return false; | |
| 285 | + } | |
| 286 | + | |
| 287 | + $section = LP_Helper::sanitize_params_submitted( $_REQUEST['section'] ?? '' ); | |
| 288 | + if ( empty( $section ) || empty( $sections[ $section ] ) ) { | |
| 289 | + $section_keys = array_keys( $sections ); | |
| 290 | + $section = reset( $section_keys ); | |
| 291 | + } | |
| 292 | + | |
| 293 | + return $section; | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * Callback return template. | |
| 298 | + * | |
| 299 | + * @return bool|array|mixed | |
| 300 | + */ | |
| 301 | + public function get_callback() { | |
| 302 | + return $this->callback; | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * Display menu content | |
| 307 | + */ | |
| 308 | + public function display() { | |
| 309 | + $tabs = $this->get_tabs(); | |
| 310 | + $active_tab = $this->get_active_tab(); | |
| 311 | + $classes = array( 'wrap', 'lp-submenu-page', $this->get_id() ); | |
| 312 | + ?> | |
| 313 | + | |
| 314 | + <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> | |
| 315 | + <?php do_action( 'learn-press/admin/heading-icon', $active_tab ); ?> | |
| 316 | + | |
| 317 | + <h1 class="wp-heading-inline"> | |
| 318 | + <?php echo wp_kses_post( $this->get_menu_title() ); ?> | |
| 319 | + <?php do_action( 'learn-press/admin/heading-title', $active_tab ); ?> | |
| 320 | + </h1> | |
| 321 | + | |
| 322 | + <?php if ( $tabs ) { ?> | |
| 323 | + <h2 class="nav-tab-wrapper"> | |
| 324 | + <?php foreach ( $tabs as $tab => $name ) { ?> | |
| 325 | + <?php | |
| 326 | + $obj_tab = false; | |
| 327 | + | |
| 328 | + if ( is_object( $name ) ) { | |
| 329 | + $obj_tab = $name; | |
| 330 | + $name = $obj_tab->text; | |
| 331 | + $tab = $obj_tab->id; | |
| 332 | + } | |
| 333 | + | |
| 334 | + $active_class = ( $tab == $active_tab ) ? ' nav-tab-active' : ''; | |
| 335 | + $tab_title = apply_filters( 'learn-press/admin/submenu-heading-tab-title', $name, $tab ); | |
| 336 | + ?> | |
| 337 | + | |
| 338 | + <?php if ( $active_class ) { ?> | |
| 339 | + <span class="nav-tab<?php echo esc_attr( $active_class ); ?>"><?php echo esc_html( $tab_title ); ?></span> | |
| 340 | + <?php } else { ?> | |
| 341 | + <a class="nav-tab" href="?page=<?php echo esc_attr( $this->id ); ?>&tab=<?php echo esc_attr( $tab ); ?>"><?php echo esc_html( $tab_title ); ?></a> | |
| 342 | + <?php } ?> | |
| 343 | + <?php } ?> | |
| 344 | + </h2> | |
| 345 | + <?php } ?> | |
| 346 | + | |
| 347 | + <?php | |
| 348 | + $classes = array( 'lp-admin-tabs' ); | |
| 349 | + $sections = $this->get_sections(); | |
| 350 | + | |
| 351 | + $has_sections = $sections && sizeof( $sections ) > 1; | |
| 352 | + $has_sections = apply_filters( 'learn-press/admin/submenu-has-sections', $has_sections, $sections, $active_tab, $this ); | |
| 353 | + | |
| 354 | + if ( $has_sections ) { | |
| 355 | + $classes[] = 'has-sections'; | |
| 356 | + } | |
| 357 | + | |
| 358 | + ob_start(); | |
| 359 | + $this->page_content(); | |
| 360 | + $content = ob_get_clean(); | |
| 361 | + if ( 'learn-press-settings' === LP_Request::get_param( 'page' ) ) { | |
| 362 | + $wrapper = [ sprintf( '<form class="%s" method="post" enctype="multipart/form-data">', esc_attr( implode( ' ', $classes ) ) ) => '</form>' ]; | |
| 363 | + } else { | |
| 364 | + $wrapper = [ sprintf( '<div class="%s">', esc_attr( implode( ' ', $classes ) ) ) => '</div>' ]; | |
| 365 | + } | |
| 366 | + | |
| 367 | + echo Template::instance()->nest_elements( $wrapper, $content ); | |
| 368 | + ?> | |
| 369 | + </div> | |
| 370 | + <?php | |
| 371 | + } | |
| 372 | + | |
| 373 | + /** | |
| 374 | + * This function for displaying content of active tab only. | |
| 375 | + * For displaying content of main page without tab, | |
| 376 | + * overwrite this function in subclass. | |
| 377 | + */ | |
| 378 | + public function page_content() { | |
| 379 | + do_action( 'learn-press/admin/page-content-sections', $this ); | |
| 380 | + | |
| 381 | + echo '<div class="lp-admin-tab-content">'; | |
| 382 | + | |
| 383 | + if ( $this->has_tabs() ) { | |
| 384 | + $page = $this->_get_page(); | |
| 385 | + $tab = $this->get_active_tab(); | |
| 386 | + | |
| 387 | + do_action( 'learn-press/admin/before-page-content-sections', $page, $tab, $this ); | |
| 388 | + | |
| 389 | + // If I have a function named 'page_content_TAB_SLUG' then call it. | |
| 390 | + $callback = array( $this, sprintf( 'page_content_%s', $tab ) ); | |
| 391 | + | |
| 392 | + if ( is_callable( $callback ) ) { | |
| 393 | + call_user_func_array( $callback, array() ); | |
| 394 | + } else { | |
| 395 | + do_action( 'learn-press/admin/page-content-' . $page, $tab ); | |
| 396 | + do_action( 'learn-press/admin/page-content-' . $page . '/' . $tab ); | |
| 397 | + } | |
| 398 | + | |
| 399 | + do_action( 'learn-press/admin/after-page-content-sections', $page, $tab, $this ); | |
| 400 | + } | |
| 401 | + | |
| 402 | + echo '</div>'; | |
| 403 | + } | |
| 404 | + | |
| 405 | + /** | |
| 406 | + * Output section navigation. | |
| 407 | + */ | |
| 408 | + public function output_section_nav() { | |
| 409 | + if ( $this->id !== $this->get_active_page() ) { | |
| 410 | + return; | |
| 411 | + } | |
| 412 | + | |
| 413 | + $active_section = $this->get_active_section(); | |
| 414 | + $sections = $this->get_sections(); | |
| 415 | + | |
| 416 | + if ( ! $sections ) { | |
| 417 | + return; | |
| 418 | + } | |
| 419 | + ?> | |
| 420 | + | |
| 421 | + <ul class="lp-admin-tab-navs"> | |
| 422 | + <?php foreach ( $sections as $slug => $section ) { ?> | |
| 423 | + <?php | |
| 424 | + $active_class = ( $slug == $active_section ) ? ' nav-section-active' : ''; | |
| 425 | + $section_title = apply_filters( 'learn-press/admin/submenu-section-title', $section, $slug ); | |
| 426 | + ?> | |
| 427 | + | |
| 428 | + <li class="nav-section<?php echo esc_attr( $active_class ); ?>"> | |
| 429 | + <?php if ( $active_class ) { ?> | |
| 430 | + <span><?php echo wp_kses_post( $section_title ); ?></span> | |
| 431 | + <?php } else { ?> | |
| 432 | + <a href="<?php echo esc_url_raw( remove_query_arg( 'sub-section', add_query_arg( 'section', $slug ) ) ); ?>"><?php echo wp_kses_post( $section_title ); ?></a> | |
| 433 | + <?php } ?> | |
| 434 | + </li> | |
| 435 | + <?php } ?> | |
| 436 | + </ul> | |
| 437 | + | |
| 438 | + <?php | |
| 439 | + } | |
| 440 | + | |
| 441 | + /** | |
| 442 | + * Display section content | |
| 443 | + */ | |
| 444 | + public function display_section() { | |
| 445 | + $section = $this->get_active_section(); | |
| 446 | + | |
| 447 | + if ( ! $section ) { | |
| 448 | + return false; | |
| 449 | + } | |
| 450 | + | |
| 451 | + $section_class = $this->sections[ $section ]; | |
| 452 | + | |
| 453 | + if ( ! $section_class ) { | |
| 454 | + return false; | |
| 455 | + } | |
| 456 | + | |
| 457 | + do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content', $section ); | |
| 458 | + | |
| 459 | + if ( is_callable( array( $this, 'section_content_' . $section ) ) ) { | |
| 460 | + call_user_func( array( $this, 'section_content_' . $section ) ); | |
| 461 | + } else { | |
| 462 | + do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content-' . $section ); | |
| 463 | + } | |
| 464 | + | |
| 465 | + return true; | |
| 466 | + } | |
| 467 | + | |
| 468 | + /** | |
| 469 | + * Get this page id without prefix. | |
| 470 | + * | |
| 471 | + * @return mixed | |
| 472 | + */ | |
| 473 | + protected function _get_page() { | |
| 474 | + return str_replace( 'learn-press-', '', $this->get_id() ); | |
| 475 | + } | |
| 476 | + | |
| 477 | + /** | |
| 478 | + * Append new class to body tag to control our page. | |
| 479 | + * | |
| 480 | + * @param $classes | |
| 481 | + * | |
| 482 | + * @return array|string | |
| 483 | + */ | |
| 484 | + public function body_class( $classes ) { | |
| 485 | + $page = $this->get_active_page( false ); | |
| 486 | + | |
| 487 | + if ( $page ) { | |
| 488 | + if ( $classes ) { | |
| 489 | + $classes = explode( ' ', $classes ); | |
| 490 | + } else { | |
| 491 | + $classes = array(); | |
| 492 | + } | |
| 493 | + | |
| 494 | + $classes[] = 'learnpress'; | |
| 495 | + $classes[] = 'lp-submenu-' . $page; | |
| 496 | + $classes = array_filter( $classes ); | |
| 497 | + $classes = array_unique( $classes ); | |
| 498 | + $classes = join( ' ', $classes ); | |
| 499 | + } | |
| 500 | + | |
| 501 | + return $classes; | |
| 502 | + } | |
| 503 | +} | |