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