| 1 |
<?php /** |
| 2 |
* @version 1.1 |
| 3 |
* @package Any |
| 4 |
* @category Menu in Admin Panel |
| 5 |
* @author wpdevelop |
| 6 |
* |
| 7 |
* @web-site http://oplugins.com/ |
| 8 |
* @email info@oplugins.com |
| 9 |
* |
| 10 |
* @modified 2015-11-02 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
class WPBM_Admin_Menus { |
| 16 |
|
| 17 |
/* Static Variables */ |
| 18 |
static $capability = array( 'administrator' => 'activate_plugins', |
| 19 |
'editor' => 'publish_pages', |
| 20 |
'author' => 'publish_posts', |
| 21 |
'contributor' => 'edit_posts', |
| 22 |
'subscriber' => 'read' |
| 23 |
); |
| 24 |
protected $menu_tag; |
| 25 |
protected $menu_title; |
| 26 |
protected $menu_title_second; // For root menu chnage second title in submenu - little hack |
| 27 |
protected $page_header; // Header - H2, in page content |
| 28 |
protected $browser_header; // Browser Header Title |
| 29 |
protected $in_menu; |
| 30 |
protected $user_role; // Acceess Role for current menu item |
| 31 |
protected $mune_icon_url; // Icon - if used 'none', then you can define it in the CSS backgound: .toplevel_page_wpbm .wp-menu-image { background-image: url("../img/icon-16x16.png"); background-position: 7px 7px; } But its mean that you are need to load this CSS in every admin page. |
| 32 |
|
| 33 |
public $root_position; /* Positions for Core Menu Items |
| 34 |
2 Dashboard |
| 35 |
4 Separator |
| 36 |
5 Posts |
| 37 |
10 Media |
| 38 |
15 Links |
| 39 |
20 Pages |
| 40 |
25 Comments |
| 41 |
59 Separator |
| 42 |
60 Appearance |
| 43 |
65 Plugins |
| 44 |
70 Users |
| 45 |
75 Tools |
| 46 |
80 Settings |
| 47 |
99 Separator |
| 48 |
*/ |
| 49 |
|
| 50 |
function __construct( $slug, $param = array() ) { |
| 51 |
|
| 52 |
$this->in_menu = false; |
| 53 |
$this->menu_tag = $slug; // For exmaple: 'wpbm-settings' - its 'page' parameter in URL |
| 54 |
|
| 55 |
if ( isset( $param['menu_title'] ) ) $this->menu_title = $param['menu_title']; |
| 56 |
if ( isset( $param['menu_title_second'] ) ) $this->menu_title_second = $param['menu_title_second']; |
| 57 |
if ( isset( $param['page_header'] ) ) $this->page_header = $param['page_header']; |
| 58 |
if ( isset( $param['browser_header'] ) ) $this->browser_header = $param['browser_header']; |
| 59 |
if ( isset( $param['in_menu'] ) ) $this->in_menu = $param['in_menu']; |
| 60 |
if ( isset( $param['position'] ) ) $this->root_position = $param['position']; |
| 61 |
else $this->root_position = null; |
| 62 |
|
| 63 |
if ( isset( $param['mune_icon_url'] ) ) $this->mune_icon_url = $param['mune_icon_url']; |
| 64 |
else $this->mune_icon_url = 'none'; |
| 65 |
|
| 66 |
if ( isset( $param['user_role'] ) ) $this->user_role = $param['user_role']; |
| 67 |
else $this->user_role = 'subscriber'; |
| 68 |
|
| 69 |
add_action( 'admin_menu', array($this, 'new_admin_page'), 10 ); |
| 70 |
|
| 71 |
add_action( 'admin_menu', array($this, 'change_second_root_menu_title'), 11 ); // Change Title of Submenu title for root menu item |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public function load_js() { |
| 76 |
do_action( 'wpbm_load_js_on_admin_page' ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
public function load_css() { |
| 81 |
do_action( 'wpbm_load_css_on_admin_page' ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** Define Plugin Menu Page |
| 86 |
* |
| 87 |
*/ |
| 88 |
public function new_admin_page(){ |
| 89 |
|
| 90 |
if ( $this->in_menu == 'root' ) { // Main Menu |
| 91 |
|
| 92 |
$page = $this->create_plugin_menu( |
| 93 |
$this->browser_header // Browser Page Header Title |
| 94 |
, $this->menu_title // Menu Title |
| 95 |
, ( ( isset( self::$capability[ $this->user_role ] ) ) ? self::$capability[ $this->user_role ] : self::$capability[ 'subscriber' ] ) // Capabilities |
| 96 |
, $this->menu_tag // Slug // I was used early -> plugin_basename(WPBM_FILE).'wpbm' |
| 97 |
, $this->mune_icon_url // Icon // - if used 'none', then you can define it in the CSS backgound: .toplevel_page_wpbm .wp-menu-image { background-image: url("../img/icon-16x16.png"); background-position: 7px 7px; } But its mean that you are need to load this CSS in every admin page. |
| 98 |
, array( $this, 'content' ) // Function for output content of page |
| 99 |
); |
| 100 |
|
| 101 |
} else { // Sub Menu |
| 102 |
|
| 103 |
$page = $this->create_plugin_submenu( |
| 104 |
$this->in_menu // The slug name for parent menu (or file name of standard WordPress admin page) |
| 105 |
, $this->browser_header // Page Title |
| 106 |
, $this->menu_title // Menu Title |
| 107 |
, ( ( isset( self::$capability[ $this->user_role ] ) ) ? self::$capability[ $this->user_role ] : self::$capability[ 'subscriber' ] ) // Capabilities |
| 108 |
, $this->menu_tag // Slug |
| 109 |
, array( $this, 'content' ) // Function for output content of page |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
//do_action('wpbm_define_settings_pages', $this->menu_tag ); // Define sub classes, like: page-settings-general.php $this->menu_tag - 'wpbm-settings' - its 'page' parameter in URL |
| 114 |
|
| 115 |
do_action('wpbm_menu_created', $this->menu_tag ); // Define sub classes, like: page-settings-general.php $this->menu_tag - 'wpbm-settings' - its 'page' parameter in URL |
| 116 |
do_action('wpbm_define_nav_tabs', $this->menu_tag ); // Define Nav tabs. |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/** Content of the Menu Page |
| 121 |
* |
| 122 |
Define page S t r u c t u r e, nav T A B s , set N O N C E: wpbm_ajax_admin_nonce field |
| 123 |
|
| 124 |
in ..\any\class\class-admin-page-structure.php |
| 125 |
|
| 126 |
then show page C O N T E N T in files, like page-structure.php |
| 127 |
|
| 128 |
$this->menu_tag - 'wpbm-settings' - its 'page' parameter in URL |
| 129 |
*/ |
| 130 |
public function content() { |
| 131 |
|
| 132 |
do_action('wpbm_page_structure_show', $this->menu_tag ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
/** Hack for changing Root 2nd Submenu Title |
| 138 |
* |
| 139 |
* @global type $submenu |
| 140 |
*/ |
| 141 |
public function change_second_root_menu_title() { |
| 142 |
|
| 143 |
// Change Title of the Main menu inside of submenu |
| 144 |
global $submenu; |
| 145 |
|
| 146 |
if ( ( $this->in_menu == 'root' ) |
| 147 |
&& ( isset( $submenu[ $this->menu_tag ] ) ) |
| 148 |
&& ( isset( $this->menu_title_second ) ) |
| 149 |
) { |
| 150 |
$submenu[ $this->menu_tag ][0][0] = $this->menu_title_second; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** Add Menu |
| 156 |
* |
| 157 |
* @param type $menu_page_title |
| 158 |
* @param type $menu_title |
| 159 |
* @param type $capability |
| 160 |
* @param type $menu_slug |
| 161 |
* @param type $mune_icon_url |
| 162 |
* @param type $page_content |
| 163 |
* @param type $page_css |
| 164 |
* @param type $page_js |
| 165 |
* @return type |
| 166 |
*/ |
| 167 |
protected function create_plugin_menu($menu_page_title, $menu_title, $capability, $menu_slug, $mune_icon_url, $page_content ) { |
| 168 |
/** |
| 169 |
* add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); |
| 170 |
* @return string The resulting page's hook_suffix |
| 171 |
*/ |
| 172 |
|
| 173 |
$page = add_menu_page( $menu_page_title, // The text to be displayed in the title tags of the page when the menu is selected |
| 174 |
$menu_title, // The text to be used for the menu |
| 175 |
$capability, // The capability required for this menu to be displayed to the user. |
| 176 |
$menu_slug, // The slug name to refer to this menu by (should be unique for this menu) |
| 177 |
$page_content, // The function to be called to output the content for this page. |
| 178 |
(($mune_icon_url=='none')?'none':((empty($mune_icon_url))?'':plugins_url($mune_icon_url, WPBM_FILE)) ) // The url to the icon to be used for this menu. Using 'none' would leave div.wp-menu-image empty so an icon can be added as background with CSS. |
| 179 |
, $this->root_position /* @param int $position The position in the menu order this one should appear |
| 180 |
* Positions for Core Menu Items |
| 181 |
2 Dashboard |
| 182 |
4 Separator |
| 183 |
5 Posts |
| 184 |
10 Media |
| 185 |
15 Links |
| 186 |
20 Pages |
| 187 |
25 Comments |
| 188 |
59 Separator |
| 189 |
60 Appearance |
| 190 |
65 Plugins |
| 191 |
70 Users |
| 192 |
75 Tools |
| 193 |
80 Settings |
| 194 |
99 Separator |
| 195 |
*/ |
| 196 |
); |
| 197 |
|
| 198 |
add_action( 'admin_print_styles-' . $page, array( $this, 'load_css' ) ); |
| 199 |
|
| 200 |
add_action( 'admin_print_scripts-' . $page, array( $this, 'load_js' ) ); |
| 201 |
|
| 202 |
return $page; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
/** Add Sub Menu |
| 207 |
* |
| 208 |
* @param type $parent_menu_slug - The slug name for the parent menu (or the file name of a standard WordPress admin page) |
| 209 |
* @param type $menu_page_title |
| 210 |
* @param type $menu_title |
| 211 |
* @param type $capability |
| 212 |
* @param type $menu_slug |
| 213 |
* @param type $page_content |
| 214 |
* @param type $page_css |
| 215 |
* @param type $page_js |
| 216 |
* @return type |
| 217 |
*/ |
| 218 |
protected function create_plugin_submenu( $parent_menu_slug, $menu_page_title, $menu_title, $capability, $menu_slug, $page_content ) { |
| 219 |
|
| 220 |
/** |
| 221 |
* function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ); |
| 222 |
* @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. |
| 223 |
*/ |
| 224 |
|
| 225 |
$page = add_submenu_page( |
| 226 |
$parent_menu_slug, // The slug name for the parent menu (or the file name of a standard WordPress admin page) |
| 227 |
$menu_page_title, // The text to be displayed in the title tags of the page when the menu is selected |
| 228 |
$menu_title, // The text to be used for the menu |
| 229 |
$capability, // The capability required for this menu to be displayed to the user. |
| 230 |
$menu_slug, // The slug name to refer to this menu by (should be unique for this menu) |
| 231 |
$page_content // The function to be called to output the content for this page. |
| 232 |
); |
| 233 |
|
| 234 |
add_action( 'admin_print_styles-' . $page, array( $this, 'load_css' ) ); |
| 235 |
|
| 236 |
add_action( 'admin_print_scripts-' . $page, array( $this, 'load_js' ) ); |
| 237 |
|
| 238 |
return $page; |
| 239 |
} |
| 240 |
} |
| 241 |
|