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

482 lines 10.0 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 * Display menu content
295 */
296 public function display() {
297 $tabs = $this->get_tabs();
298 $active_tab = $this->get_active_tab();
299 $classes = array( 'wrap', 'lp-submenu-page', $this->get_id() );
300 ?>
301
302 <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
303 <?php do_action( 'learn-press/admin/heading-icon', $active_tab ); ?>
304
305 <h1 class="wp-heading-inline">
306 <?php echo wp_kses_post( $this->get_menu_title() ); ?>
307 <?php do_action( 'learn-press/admin/heading-title', $active_tab ); ?>
308 </h1>
309
310 <?php if ( $tabs ) { ?>
311 <h2 class="nav-tab-wrapper">
312 <?php foreach ( $tabs as $tab => $name ) { ?>
313 <?php
314 $obj_tab = false;
315
316 if ( is_object( $name ) ) {
317 $obj_tab = $name;
318 $name = $obj_tab->text;
319 $tab = $obj_tab->id;
320 }
321
322 $active_class = ( $tab == $active_tab ) ? ' nav-tab-active' : '';
323 $tab_title = apply_filters( 'learn-press/admin/submenu-heading-tab-title', $name, $tab );
324 ?>
325
326 <?php if ( $active_class ) { ?>
327 <span class="nav-tab<?php echo esc_attr( $active_class ); ?>"><?php echo esc_html( $tab_title ); ?></span>
328 <?php } else { ?>
329 <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>
330 <?php } ?>
331 <?php } ?>
332 </h2>
333 <?php } ?>
334
335 <?php
336 $classes = array( 'lp-admin-tabs' );
337 $sections = $this->get_sections();
338
339 if ( $sections && sizeof( $sections ) > 1 ) {
340 $classes[] = 'has-sections';
341 }
342 ?>
343
344 <form class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" method="post" enctype="multipart/form-data">
345 <?php $this->page_content(); ?>
346 </form>
347 </div>
348 <?php
349 }
350
351 /**
352 * This function for displaying content of active tab only.
353 * For displaying content of main page without tab,
354 * overwrite this function in sub class.
355 */
356 public function page_content() {
357 do_action( 'learn-press/admin/page-content-sections', $this );
358
359 echo '<div class="lp-admin-tab-content">';
360
361 if ( $this->has_tabs() ) {
362 $page = $this->_get_page();
363 $tab = $this->get_active_tab();
364
365 do_action( 'learn-press/admin/before-page-content-sections', $page, $tab, $this );
366
367 // If I have a function named 'page_content_TAB_SLUG' then call it.
368 $callback = array( $this, sprintf( 'page_content_%s', $tab ) );
369
370 if ( is_callable( $callback ) ) {
371 call_user_func_array( $callback, array() );
372 } else {
373 do_action( 'learn-press/admin/page-content-' . $page, $tab );
374 do_action( 'learn-press/admin/page-content-' . $page . '/' . $tab );
375 }
376
377 do_action( 'learn-press/admin/after-page-content-sections', $page, $tab, $this );
378 }
379
380 echo '</div>';
381 }
382
383 /**
384 * Output section navigation.
385 */
386 public function output_section_nav() {
387 if ( $this->id !== $this->get_active_page() ) {
388 return;
389 }
390
391 $active_section = $this->get_active_section();
392 $sections = $this->get_sections();
393
394 if ( ! $sections ) {
395 return;
396 }
397 ?>
398
399 <ul class="lp-admin-tab-navs">
400 <?php foreach ( $sections as $slug => $section ) { ?>
401 <?php
402 $active_class = ( $slug == $active_section ) ? ' nav-section-active' : '';
403 $section_title = apply_filters( 'learn-press/admin/submenu-section-title', $section, $slug );
404 ?>
405
406 <li class="nav-section<?php echo esc_attr( $active_class ); ?>">
407 <?php if ( $active_class ) { ?>
408 <span><?php echo wp_kses_post( $section_title ); ?></span>
409 <?php } else { ?>
410 <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>
411 <?php } ?>
412 </li>
413 <?php } ?>
414 </ul>
415
416 <?php
417 }
418
419 /**
420 * Display section content
421 */
422 public function display_section() {
423 $section = $this->get_active_section();
424
425 if ( ! $section ) {
426 return false;
427 }
428
429 $section_class = $this->sections[ $section ];
430
431 if ( ! $section_class ) {
432 return false;
433 }
434
435 do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content', $section );
436
437 if ( is_callable( array( $this, 'section_content_' . $section ) ) ) {
438 call_user_func( array( $this, 'section_content_' . $section ) );
439 } else {
440 do_action( 'learn-press/admin/page-' . $this->_get_page() . '/section-content-' . $section );
441 }
442
443 return true;
444 }
445
446 /**
447 * Get this page id without prefix.
448 *
449 * @return mixed
450 */
451 protected function _get_page() {
452 return str_replace( 'learn-press-', '', $this->get_id() );
453 }
454
455 /**
456 * Append new class to body tag to control our page.
457 *
458 * @param $classes
459 *
460 * @return array|string
461 */
462 public function body_class( $classes ) {
463 $page = $this->get_active_page( false );
464
465 if ( $page ) {
466 if ( $classes ) {
467 $classes = explode( ' ', $classes );
468 } else {
469 $classes = array();
470 }
471
472 $classes[] = 'learnpress';
473 $classes[] = 'lp-submenu-' . $page;
474 $classes = array_filter( $classes );
475 $classes = array_unique( $classes );
476 $classes = join( ' ', $classes );
477 }
478
479 return $classes;
480 }
481 }
482