PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.5
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.5
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 / class-lp-admin-menu.php

class-lp-admin-menu.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.5, at inc/admin/class-lp-admin-menu.php

297 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Setup menus in WP admin.
4 *
5 * @author ThimPress
6 * @package LearnPress
7 * @version 1.0
8 */
9
10 use LearnPress\Helpers\Config;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Class LP_Admin_Menu
16 */
17 class LP_Admin_Menu {
18
19 /**
20 * Array of submenu items.
21 *
22 * @var array
23 */
24 protected $menu_items = array();
25
26 /**
27 * Array of menu groups.
28 *
29 * @var array
30 */
31 protected $group_menus = array();
32
33 /**
34 * Main menu capability.
35 *
36 * @var string
37 */
38 protected $capability = '';
39
40 /**
41 * LP_Admin_Menu Construct
42 */
43 public function __construct() {
44 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
45 add_action( 'admin_menu', array( $this, 'lp_group_menus' ), 9999 );
46
47 if ( apply_filters( 'learn_press_show_admin_bar_courses_page', true ) ) {
48 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 50 );
49 }
50
51 /**
52 * @since 3.0.0
53 */
54 $this->capability = 'edit_' . LP_COURSE_CPT . 's';
55 include_once 'sub-menus/abstract-submenu.php';
56 }
57
58 /**
59 * Added url Pages of LP.
60 *
61 * @param WP_Admin_Bar $wp_admin_bar
62 *
63 * @return void
64 */
65 public function admin_bar_menus( $wp_admin_bar ) {
66 if ( ! current_user_can( 'administrator' ) ) {
67 return;
68 }
69
70 $url_pages = array(
71 'lp-courses' => array(
72 'title' => esc_html__( 'View Page Courses', 'learnpress' ),
73 'href' => learn_press_get_page_link( 'courses' ),
74 'parent' => 'site-name',
75 ),
76 'lp-profile' => array(
77 'title' => esc_html__( 'View Page Profile', 'learnpress' ),
78 'href' => learn_press_get_page_link( 'profile' ),
79 'parent' => 'site-name',
80 ),
81 'lp-instructors' => array(
82 'title' => esc_html__( 'View Page Instructors', 'learnpress' ),
83 'href' => learn_press_get_page_link( 'instructors' ),
84 'parent' => 'site-name',
85 ),
86 );
87
88 foreach ( $url_pages as $id => $url_page ) {
89 $wp_admin_bar->add_node(
90 array(
91 'id' => $id,
92 'parent' => $url_page['parent'] ?? 'appearance',
93 'title' => sprintf( '<span class="ab-label">%s</span>', $url_page['title'] ),
94 'href' => $url_page['href'],
95 )
96 );
97 }
98 }
99
100 /**
101 * Get main menu capability.
102 *
103 * @return string
104 */
105 public function get_capability() {
106 return $this->capability;
107 }
108
109 /**
110 * Register for menu for admin
111 */
112 public function admin_menu() {
113 // Not for translate menu_title, because it make compare $current_screen with "learnpress_page_.*" wrong
114 add_menu_page(
115 __( 'Learning Management System', 'learnpress' ),
116 'LearnPress',
117 $this->get_capability(),
118 'learn_press',
119 '',
120 LP_PLUGIN_URL . '/assets/images/logo-menu.png',
121 '3.14'
122 );
123
124 // Default submenu items and groups from config.
125 $wp_menus_config = Config::instance()->get( 'wp-menus' );
126 $menu_items = $wp_menus_config['menus'] ?? array();
127 $this->group_menus = $wp_menus_config['group_menus'] ?? array();
128
129 add_action(
130 'parent_file',
131 function ( $parent_file ) {
132 global $current_screen;
133
134 $taxonomy = $current_screen->taxonomy;
135 if ( $taxonomy == 'course_tag' ) {
136 $parent_file = 'learn_press';
137 } elseif ( $taxonomy == 'course_category' ) {
138 $parent_file = 'learn_press';
139 }
140
141 return $parent_file;
142 }
143 );
144
145 if ( $menu_items ) {
146 foreach ( $menu_items as $k => $item ) {
147 // Third-party classes passed as string.
148 if ( is_string( $item ) && class_exists( $item ) ) {
149 $item = new $item();
150 }
151
152 // For declare type new menu item from config array.
153 if ( is_array( $item ) ) {
154 add_submenu_page(
155 'learn_press',
156 $item['page_title'] ?? '',
157 $item['menu_title'] ?? '',
158 $item['capability'] ?? '',
159 $item['id'] ?? '',
160 $item['callback'] ?? false,
161 $item['priority'] ?? 10
162 //$item->get_priority()
163 );
164
165 continue;
166 }
167
168 // For declare type old menu item from class.
169 add_submenu_page(
170 'learn_press',
171 $item->get_page_title(),
172 $item->get_menu_title(),
173 $item->get_capability(),
174 $item->get_id(),
175 $item->get_callback(),
176 //$item->get_priority()
177 );
178 }
179 $this->menu_items = $menu_items;
180 }
181
182 // $addons = LP_Admin::instance()->get_addons();
183 }
184
185 /**
186 * Group and reorder the LearnPress admin submenu items.
187 *
188 * Items are grouped by the 'learn-press/wp-admin/menu-group' filter
189 * and ordered by the default group and slug order. Unmatched items
190 * are added to the 'operations' group. A divider is inserted between
191 * each group.
192 *
193 * @return void
194 */
195 public function lp_group_menus() {
196 global $submenu;
197
198 $lp_menus = $submenu['learn_press'] ?? [];
199 if ( empty( $lp_menus ) ) {
200 return;
201 }
202
203 $group_menus_default = $this->group_menus;
204 $group_menus = array();
205
206 // Group menus by default
207 foreach ( $lp_menus as $item ) {
208 $slug = $item[2] ?? '';
209 $in_group = false;
210
211 foreach ( $group_menus_default as $group => $menus ) {
212 if ( in_array( $slug, $menus ) ) {
213 $group_menus[ $group ][] = $item;
214 $in_group = true;
215 }
216 }
217
218 // If not in any group, add to operations
219 if ( ! $in_group ) {
220 $group_menus['operations'][] = $item;
221 }
222 }
223
224 // Reorder menus
225 $menus_new = [];
226 $i = 0;
227 $count = count( $group_menus_default );
228 foreach ( $group_menus_default as $group => $menus ) {
229 if ( ! isset( $group_menus[ $group ] ) ) {
230 continue;
231 }
232
233 // Order items by the default slug order, then append leftovers.
234 $items_by_slug = [];
235 foreach ( $group_menus[ $group ] as $item ) {
236 $items_by_slug[ $item[2] ?? '' ] = $item;
237 }
238
239 foreach ( $menus as $menu_slug ) {
240 if ( ! is_string( $menu_slug ) ) {
241 continue;
242 }
243
244 if ( isset( $items_by_slug[ $menu_slug ] ) ) {
245 $menus_new[] = $items_by_slug[ $menu_slug ];
246 unset( $items_by_slug[ $menu_slug ] );
247 }
248 }
249
250 foreach ( $items_by_slug as $item ) {
251 $menus_new[] = $item;
252 }
253
254 ++$i;
255 if ( $i < $count ) {
256 // Add divider between groups
257 $menus_new[] = array(
258 '',
259 $this->get_capability(),
260 'lp-menu-divider-' . $group,
261 '',
262 'lp-menu-divider',
263 );
264 }
265 }
266
267 $submenu['learn_press'] = $menus_new;
268 }
269
270 /**
271 * Callback function using for "usort".
272 *
273 * @param LP_Abstract_Submenu $a
274 * @param LP_Abstract_Submenu $b
275 *
276 * @return mixed
277 */
278 public function sort_menu_items( $a, $b ) {
279 return $a->get_priority() > $b->get_priority();
280 }
281
282 public function get_menu_items() {
283 return $this->menu_items;
284 }
285
286 public static function instance() {
287 static $instance;
288 if ( is_null( $instance ) ) {
289 $instance = new self();
290 }
291
292 return $instance;
293 }
294 }
295
296 return LP_Admin_Menu::instance();
297