| 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 |
//add_action( 'admin_menu', array( $this, 'notify_new_course' ) ); |
| 37 |
|
| 38 |
if ( apply_filters( 'learn_press_show_admin_bar_courses_page', true ) ) { |
| 39 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 50 ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @since 3.0.0 |
| 44 |
*/ |
| 45 |
$this->capability = 'edit_' . LP_COURSE_CPT . 's'; |
| 46 |
include_once 'sub-menus/abstract-submenu.php'; |
| 47 |
} |
| 48 |
|
| 49 |
public function admin_bar_menus( $wp_admin_bar ) { |
| 50 |
if ( ! is_admin() || ! is_user_logged_in() ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! is_user_member_of_blog() && ! is_super_admin() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( get_option( 'page_on_front' ) == learn_press_get_page_id( 'courses' ) ) { |
| 59 |
return; |
| 60 |
} |
| 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 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get main menu capability. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function get_capability() { |
| 78 |
return $this->capability; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Register for menu for admin |
| 83 |
*/ |
| 84 |
public function admin_menu() { |
| 85 |
// Not for translate menu_title, because it make compare $current_screen with "learnpress_page_.*" wrong |
| 86 |
add_menu_page( |
| 87 |
__( 'Learning Management System', 'learnpress' ), |
| 88 |
'LearnPress', |
| 89 |
$this->get_capability(), |
| 90 |
'learn_press', |
| 91 |
'', |
| 92 |
'dashicons-welcome-learn-more', |
| 93 |
'3.14' |
| 94 |
); |
| 95 |
|
| 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'; |
| 102 |
|
| 103 |
// Deprecated hooks |
| 104 |
$menu_items = apply_filters( 'learn_press_menu_items', $menu_items ); |
| 105 |
|
| 106 |
$menu_items = apply_filters( 'learn-press/admin/menu-items', $menu_items ); |
| 107 |
|
| 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' ); |
| 111 |
|
| 112 |
if ( $menu_items ) { |
| 113 |
foreach ( $menu_items as $item ) { |
| 114 |
if ( is_string( $item ) && class_exists( $item ) ) { |
| 115 |
$item = new $item(); |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! $item instanceof LP_Abstract_Submenu ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
add_submenu_page( |
| 123 |
'learn_press', |
| 124 |
$item->get_page_title(), |
| 125 |
$item->get_menu_title(), |
| 126 |
$item->get_capability(), |
| 127 |
$item->get_id(), |
| 128 |
array( $item, 'display' ) |
| 129 |
); |
| 130 |
|
| 131 |
} |
| 132 |
$this->menu_items = $menu_items; |
| 133 |
} |
| 134 |
|
| 135 |
$addons = LP_Admin::instance()->get_addons(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Callback function using for "usort". |
| 140 |
* |
| 141 |
* @param LP_Abstract_Submenu $a |
| 142 |
* @param LP_Abstract_Submenu $b |
| 143 |
* |
| 144 |
* @return mixed |
| 145 |
*/ |
| 146 |
public function sort_menu_items( $a, $b ) { |
| 147 |
return $a->get_priority() > $b->get_priority(); |
| 148 |
} |
| 149 |
|
| 150 |
public function get_menu_items() { |
| 151 |
return $this->menu_items; |
| 152 |
} |
| 153 |
|
| 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 |
public static function instance() { |
| 171 |
static $instance = false; |
| 172 |
|
| 173 |
if ( ! $instance ) { |
| 174 |
$instance = new self(); |
| 175 |
} |
| 176 |
|
| 177 |
return $instance; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return LP_Admin_Menu::instance(); |
| 182 |
|