| @@ -6,8 +6,10 @@ | ||
| 6 | 6 | * @package LearnPress |
| 7 | 7 | * @version 1.0 |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | +use LearnPress\Helpers\Config; | |
| 11 | + | |
| 10 | 12 | defined( 'ABSPATH' ) || exit; |
| 11 | 13 | |
| 12 | 14 | /** |
| 13 | 15 | * Class LP_Admin_Menu |
| @@ -21,8 +23,15 @@ | ||
| 21 | 23 | */ |
| 22 | 24 | protected $menu_items = array(); |
| 23 | 25 | |
| 24 | 26 | /** |
| 27 | + * Array of menu groups. | |
| 28 | + * | |
| 29 | + * @var array | |
| 30 | + */ | |
| 31 | + protected $group_menus = array(); | |
| 32 | + | |
| 33 | + /** | |
| 25 | 34 | * Main menu capability. |
| 26 | 35 | * |
| 27 | 36 | * @var string |
| 28 | 37 | */ |
| @@ -32,9 +41,9 @@ | ||
| 32 | 41 | * LP_Admin_Menu Construct |
| 33 | 42 | */ |
| 34 | 43 | public function __construct() { |
| 35 | 44 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 36 | - //add_action( 'admin_menu', array( $this, 'notify_new_course' ) ); | |
| 45 | + add_action( 'admin_menu', array( $this, 'lp_group_menus' ), 9999 ); | |
| 37 | 46 | |
| 38 | 47 | if ( apply_filters( 'learn_press_show_admin_bar_courses_page', true ) ) { |
| 39 | 48 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 50 ); |
| 40 | 49 | } |
| @@ -45,29 +54,48 @@ | ||
| 45 | 54 | $this->capability = 'edit_' . LP_COURSE_CPT . 's'; |
| 46 | 55 | include_once 'sub-menus/abstract-submenu.php'; |
| 47 | 56 | } |
| 48 | 57 | |
| 58 | + /** | |
| 59 | + * Added url Pages of LP. | |
| 60 | + * | |
| 61 | + * @param WP_Admin_Bar $wp_admin_bar | |
| 62 | + * | |
| 63 | + * @return void | |
| 64 | + */ | |
| 49 | 65 | public function admin_bar_menus( $wp_admin_bar ) { |
| 50 | - if ( ! is_admin() || ! is_user_logged_in() ) { | |
| 66 | + if ( ! current_user_can( 'administrator' ) ) { | |
| 51 | 67 | return; |
| 52 | 68 | } |
| 53 | 69 | |
| 54 | - if ( ! is_user_member_of_blog() && ! is_super_admin() ) { | |
| 55 | - return; | |
| 56 | - } | |
| 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 | + ); | |
| 57 | 87 | |
| 58 | - if ( get_option( 'page_on_front' ) == learn_press_get_page_id( 'courses' ) ) { | |
| 59 | - return; | |
| 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 | + ); | |
| 60 | 97 | } |
| 61 | - | |
| 62 | - $wp_admin_bar->add_node( | |
| 63 | - array( | |
| 64 | - 'parent' => 'site-name', | |
| 65 | - 'id' => 'courses-page', | |
| 66 | - 'title' => esc_html__( 'View Courses', 'learnpress' ), | |
| 67 | - 'href' => learn_press_get_page_link( 'courses' ), | |
| 68 | - ) | |
| 69 | - ); | |
| 70 | 98 | } |
| 71 | 99 | |
| 72 | 100 | /** |
| 73 | 101 | * Get main menu capability. |
| @@ -88,38 +116,60 @@ | ||
| 88 | 116 | 'LearnPress', |
| 89 | 117 | $this->get_capability(), |
| 90 | 118 | 'learn_press', |
| 91 | 119 | '', |
| 92 | - 'dashicons-welcome-learn-more', | |
| 120 | + LP_PLUGIN_URL . '/assets/images/logo-menu.png', | |
| 93 | 121 | '3.14' |
| 94 | 122 | ); |
| 95 | 123 | |
| 96 | - // Default submenu items | |
| 97 | - $menu_items = array(); | |
| 98 | - $menu_items['statistic'] = include_once 'sub-menus/class-lp-submenu-statistics.php'; | |
| 99 | - $menu_items['addons'] = include_once 'sub-menus/class-lp-submenu-addons.php'; | |
| 100 | - $menu_items['settings'] = include_once 'sub-menus/class-lp-submenu-settings.php'; | |
| 101 | - $menu_items['tools'] = include_once 'sub-menus/class-lp-submenu-tools.php'; | |
| 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(); | |
| 102 | 128 | |
| 103 | - // Deprecated hooks | |
| 104 | - $menu_items = apply_filters( 'learn_press_menu_items', $menu_items ); | |
| 129 | + // Old hook declare type Class Submenu. | |
| 130 | + $menu_items = apply_filters( 'learn-press/admin/menu-items', $menu_items ); | |
| 105 | 131 | |
| 106 | - $menu_items = apply_filters( 'learn-press/admin/menu-items', $menu_items ); | |
| 132 | + add_action( | |
| 133 | + 'parent_file', | |
| 134 | + function ( $parent_file ) { | |
| 135 | + global $current_screen; | |
| 107 | 136 | |
| 108 | - // Sort menu items by it's priority | |
| 109 | - // uasort( $menu_items, array( $this, 'sort_menu_items' ) ); | |
| 110 | - uasort( $menu_items, 'learn_press_sort_list_by_priority_callback' ); | |
| 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 | + } | |
| 111 | 143 | |
| 144 | + return $parent_file; | |
| 145 | + } | |
| 146 | + ); | |
| 147 | + | |
| 112 | 148 | if ( $menu_items ) { |
| 113 | - foreach ( $menu_items as $item ) { | |
| 149 | + foreach ( $menu_items as $k => $item ) { | |
| 150 | + // Third-party classes passed as string. | |
| 114 | 151 | if ( is_string( $item ) && class_exists( $item ) ) { |
| 115 | 152 | $item = new $item(); |
| 116 | 153 | } |
| 117 | 154 | |
| 118 | - if ( ! $item instanceof LP_Abstract_Submenu ) { | |
| 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 | + | |
| 119 | 168 | continue; |
| 120 | 169 | } |
| 121 | 170 | |
| 171 | + // For declare type old menu item from class. | |
| 122 | 172 | add_submenu_page( |
| 123 | 173 | 'learn_press', |
| 124 | 174 | $item->get_page_title(), |
| 125 | 175 | $item->get_menu_title(), |
| @@ -124,16 +174,101 @@ | ||
| 124 | 174 | $item->get_page_title(), |
| 125 | 175 | $item->get_menu_title(), |
| 126 | 176 | $item->get_capability(), |
| 127 | 177 | $item->get_id(), |
| 128 | - array( $item, 'display' ) | |
| 178 | + $item->get_callback(), | |
| 179 | + //$item->get_priority() | |
| 129 | 180 | ); |
| 181 | + } | |
| 182 | + $this->menu_items = $menu_items; | |
| 183 | + } | |
| 130 | 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 | + } | |
| 131 | 219 | } |
| 132 | - $this->menu_items = $menu_items; | |
| 220 | + | |
| 221 | + // If not in any group, add to operations | |
| 222 | + if ( ! $in_group ) { | |
| 223 | + $group_menus['operations'][] = $item; | |
| 224 | + } | |
| 133 | 225 | } |
| 134 | 226 | |
| 135 | - $addons = LP_Admin::instance()->get_addons(); | |
| 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; | |
| 136 | 271 | } |
| 137 | 272 | |
| 138 | 273 | /** |
| 139 | 274 | * Callback function using for "usort". |
| @@ -150,28 +285,11 @@ | ||
| 150 | 285 | public function get_menu_items() { |
| 151 | 286 | return $this->menu_items; |
| 152 | 287 | } |
| 153 | 288 | |
| 154 | - /* | |
| 155 | - * Notify an administrator with pending courses | |
| 156 | - */ | |
| 157 | - public function notify_new_course() { | |
| 158 | - global $menu; | |
| 159 | - | |
| 160 | - $current_user = wp_get_current_user(); | |
| 161 | - if ( ! in_array( 'administrator', $current_user->roles ) ) { | |
| 162 | - return; | |
| 163 | - } | |
| 164 | - | |
| 165 | - $count_courses = wp_count_posts( LP_COURSE_CPT ); | |
| 166 | - $awaiting_mod = $count_courses->pending; | |
| 167 | - $menu['3.14'][0] .= " <span class='awaiting-mod count-$awaiting_mod'><span class='pending-count'>" . number_format_i18n( $awaiting_mod ) . '</span></span>'; | |
| 168 | - } | |
| 169 | - | |
| 170 | 289 | public static function instance() { |
| 171 | - static $instance = false; | |
| 172 | - | |
| 173 | - if ( ! $instance ) { | |
| 290 | + static $instance; | |
| 291 | + if ( is_null( $instance ) ) { | |
| 174 | 292 | $instance = new self(); |
| 175 | 293 | } |
| 176 | 294 | |
| 177 | 295 | return $instance; |