PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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.1.7.3, at inc/admin/sub-menus/abstract-submenu.php

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