PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / admin / sub-menus / abstract-submenu.php

abstract-submenu.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/admin/sub-menus/abstract-submenu.php

504 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
504