| 1 |
<?php |
| 2 |
/* wp-admin-menu-classes.php |
| 3 |
|
| 4 |
Classes to encapsulate access to admin menu global arrays $menu and $submenu |
| 5 |
|
| 6 |
See: |
| 7 |
-- http://core.trac.wordpress.org/ticket/12718 |
| 8 |
-- http://core.trac.wordpress.org/ticket/11517 |
| 9 |
|
| 10 |
version: 1.05 - Renamed "delete_*" functions to "remove_*" (Oct 6 2010) |
| 11 |
version: 1.04 - Another significant update. Attempted to make PHP 4.x compatible (Sept 30 2010) |
| 12 |
Added hookname property to both section and item |
| 13 |
Renamed property with list of items to be "items" instead of "submenus" |
| 14 |
version: 1.03 - Major Rewrite over v1.02. Designed for hopeful inclusion within WordPress v3.1 (Sept 29 2010) |
| 15 |
version: 1.02 - Added remove_admin_menu_item(), changed "find-by"=="title" to search on RegEx (Sept 27 2010) |
| 16 |
|
| 17 |
Designed for hopeful inclusion within WordPress v3.1 |
| 18 |
|
| 19 |
Examples of use: |
| 20 |
// This example creates one menu in place of Dashboard called "My Menu", adds a few things, and removes all else. |
| 21 |
// This example assumes this might only be done for end users, not administrators |
| 22 |
|
| 23 |
$dashboard = rename_admin_menu_section('Dashboard','My Menu'); |
| 24 |
|
| 25 |
// // Alternate approach |
| 26 |
// remove_admin_menu_item('index.php'); // Dashboard |
| 27 |
// $dashboard = add_admin_menu_section(array( |
| 28 |
// 'title' => 'My Menu', |
| 29 |
// 'slug' => 'index.php', |
| 30 |
// )); |
| 31 |
|
| 32 |
remove_admin_menu_item($dashboard,'index.php'); // Dashboard |
| 33 |
remove_admin_menu_item($dashboard,'update-core.php'); // Updates |
| 34 |
|
| 35 |
$movies = "edit.php?post_type=movie"; |
| 36 |
copy_admin_menu_item($dashboard,$movies); |
| 37 |
$movie_genre = 'edit-tags.php?taxonomy=movie-genre&post_type=movie'; |
| 38 |
copy_admin_menu_item($dashboard,$movies,$movie_genre); |
| 39 |
rename_admin_menu_item($dashboard,$movie_genre,'Movie Genre'); |
| 40 |
remove_admin_menu_item($movies); |
| 41 |
remove_admin_menu_item($movies,$movie_genre); |
| 42 |
remove_admin_menu_item($movies,'post-new.php?post_type=movie'); |
| 43 |
remove_admin_menu_section($movies); |
| 44 |
|
| 45 |
$actors = "edit.php?post_type=actor"; |
| 46 |
copy_admin_menu_item($dashboard,$actors); |
| 47 |
remove_admin_menu_item($actors); |
| 48 |
remove_admin_menu_item($actors,'post-new.php?post_type=actor'); |
| 49 |
//remove_admin_menu_section($actors); |
| 50 |
|
| 51 |
rename_admin_menu_item($dashboard,'Pages','Other Pages'); |
| 52 |
|
| 53 |
remove_admin_menu_section('edit.php'); // Posts |
| 54 |
remove_admin_menu_section('upload.php'); // Media |
| 55 |
remove_admin_menu_section('link-manager.php'); // Links |
| 56 |
remove_admin_menu_section('edit-comments.php'); // Comments |
| 57 |
remove_admin_menu_section('edit.php?post_type=page'); // Pages |
| 58 |
remove_admin_menu_section('plugins.php'); // Plugins |
| 59 |
remove_admin_menu_section('themes.php'); // Appearance |
| 60 |
remove_admin_menu_section('users.php'); // Users |
| 61 |
remove_admin_menu_section('tools.php'); // Tools |
| 62 |
remove_admin_menu_section('options-general.php'); // Settings |
| 63 |
|
| 64 |
*/ |
| 65 |
|
| 66 |
|
| 67 |
function add_admin_menu_section($section,$args=array()) { |
| 68 |
$new_section = new WP_AdminMenuSection(); |
| 69 |
return $new_section->initialize($section,$args); |
| 70 |
} |
| 71 |
|
| 72 |
function add_admin_menu_item($section,$item,$args=array()) { |
| 73 |
$section = $temp = get_admin_menu_section($section); |
| 74 |
$item = ($section ? $section->add_item($item,$args) : false); |
| 75 |
return $item; |
| 76 |
} |
| 77 |
function remove_admin_menu_item($section,$item=false) { |
| 78 |
if (!$item) |
| 79 |
$item = $section; // These slugs are often identical |
| 80 |
$section = get_admin_menu_section($section); |
| 81 |
if ($section) |
| 82 |
$section->remove_item($item); |
| 83 |
} |
| 84 |
function remove_admin_menu_section($section) { |
| 85 |
$section = get_admin_menu_section($section); |
| 86 |
if ($section) |
| 87 |
$section->delete(); |
| 88 |
} |
| 89 |
function rename_admin_menu_section($section,$new_title) { |
| 90 |
$section = get_admin_menu_section($section); |
| 91 |
if ($section) |
| 92 |
$section->set_title($new_title); |
| 93 |
return $section; |
| 94 |
} |
| 95 |
function rename_admin_menu_item($section,$item,$new_title) { |
| 96 |
$item = get_admin_menu_item($section,$item); |
| 97 |
if ($item) |
| 98 |
$item->set_title($new_title); |
| 99 |
return $item; |
| 100 |
} |
| 101 |
function swap_admin_menu_sections($from_section,$to_section) { |
| 102 |
$from_section = get_admin_menu_section($from_section); |
| 103 |
if ($from_section) |
| 104 |
$from_section->swap_with($to_section); |
| 105 |
return $section; |
| 106 |
} |
| 107 |
function get_admin_menu_section($section) { |
| 108 |
if (!is_a($section,'WP_AdminMenuSection')) |
| 109 |
$section = new WP_AdminMenuSection($section); |
| 110 |
return $section; |
| 111 |
} |
| 112 |
function get_admin_menu_item($section,$item) { |
| 113 |
$section = get_admin_menu_section($section); |
| 114 |
if (!is_a($item,'WP_AdminMenuItem')) { |
| 115 |
$item = $section->find_item($item); |
| 116 |
} |
| 117 |
return $item; |
| 118 |
} |
| 119 |
function copy_admin_menu_item($to_section,$from_section,$item=false) { |
| 120 |
if (!$item) |
| 121 |
$item = $from_section; // These slugs are often identical |
| 122 |
$to_section = get_admin_menu_section($to_section); |
| 123 |
$item = get_admin_menu_item($from_section,$item); |
| 124 |
add_admin_menu_item($to_section,$item); |
| 125 |
} |
| 126 |
|
| 127 |
class WP_AdminMenuSection { |
| 128 |
var $index = 0; |
| 129 |
var $items=array(); |
| 130 |
var $hookname; |
| 131 |
function __construct($section=false) { |
| 132 |
$this->WP_AdminMenuSection($section); |
| 133 |
} |
| 134 |
function WP_AdminMenuSection($section=false) { |
| 135 |
if ($section) { // $section=false when we need to add one. Static methods would be nicer. |
| 136 |
if (is_a($section,'WP_AdminMenuSection')) { |
| 137 |
$this->index = &$section->index; |
| 138 |
$this->items = &$section->items; |
| 139 |
$this->hookname = &$section->hookname; |
| 140 |
} else { |
| 141 |
global $menu; |
| 142 |
global $submenu; |
| 143 |
$found = false; |
| 144 |
foreach($menu as $index => $section_array) { |
| 145 |
if ($section==$section_array[2] || // Find by File/Slug |
| 146 |
$section==$section_array[0] || // Find by Title |
| 147 |
@preg_match("#^{$section}$#",$section_array[0])) { // Find by Title via RegEx |
| 148 |
$found = $index; |
| 149 |
} else { |
| 150 |
continue; |
| 151 |
} |
| 152 |
break; |
| 153 |
} |
| 154 |
$this->index = $found; |
| 155 |
if ($found) |
| 156 |
$this->refresh(); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
function initialize($section,$args=array()) { |
| 161 |
$args = wp_parse_args($args,array( |
| 162 |
'where' => 'bottom' // top or bottom |
| 163 |
)); |
| 164 |
$section = wp_parse_args($section,array( |
| 165 |
'title' => false, |
| 166 |
'slug' => false, |
| 167 |
'page_title' => false, |
| 168 |
'icon_src' => false, |
| 169 |
'function' => false, |
| 170 |
'capability' => 'edit_posts', |
| 171 |
)); |
| 172 |
if (!$section['page_title']) |
| 173 |
$section['page_title'] = strip_tags($section['title']); |
| 174 |
switch ($args['where']) { |
| 175 |
case 'bottom': |
| 176 |
$this->hookname = add_menu_page( |
| 177 |
$section['page_title'], |
| 178 |
$section['title'], |
| 179 |
$section['capability'], |
| 180 |
$section['slug'], |
| 181 |
$section['function'], |
| 182 |
$section['icon_src'] ); |
| 183 |
break; |
| 184 |
case 'after': // TODO: Implement this |
| 185 |
case 'before': // TODO: Implement this |
| 186 |
case 'top': // TODO: Implement this |
| 187 |
default: |
| 188 |
wp_die("where='{$args['where']}' not yet implemented in WP_AdminMenuSection->initialize()."); |
| 189 |
} |
| 190 |
$this->refresh($section['slug']); |
| 191 |
return $this; |
| 192 |
} |
| 193 |
|
| 194 |
function add_item($item,$args=array()) { |
| 195 |
$last_index = ''; |
| 196 |
$args = wp_parse_args($args,array( |
| 197 |
'where' => 'bottom' // top or bottom |
| 198 |
)); |
| 199 |
global $submenu; |
| 200 |
$slug = $this->get_slug(); |
| 201 |
if (!isset($submenu[$slug])) |
| 202 |
$submenu[$slug] = array(); |
| 203 |
$item_list = &$submenu[$slug]; |
| 204 |
if (is_a($item,'WP_AdminMenuItem')) { |
| 205 |
$item_type = OBJECT; |
| 206 |
$item_array = $item->get_array(); |
| 207 |
} else if ($this->is_item_array($item)) { |
| 208 |
$item_type = ARRAY_N; |
| 209 |
$item_array = $item; |
| 210 |
} else { |
| 211 |
$item_type = ARRAY_A; |
| 212 |
$item = wp_parse_args($item,array( |
| 213 |
'title' => false, |
| 214 |
'slug' => false, |
| 215 |
'page_title' => false, |
| 216 |
'function' => false, |
| 217 |
'capability' => 'edit_posts', |
| 218 |
)); |
| 219 |
if (!$item['page_title']) |
| 220 |
$item['page_title'] = $item['title']; |
| 221 |
$item['hookname'] = add_submenu_page( $slug, $item['page_title'], $item['title'], $item['capability'], $item['slug'], $item['function']); |
| 222 |
if ($args['where']!='bottom') // If 'bottom', do nothing more./ |
| 223 |
$item_array = array_pop($item_list); |
| 224 |
} |
| 225 |
switch ($args['where']) { |
| 226 |
case 'top': // No, array_unshift() won't do this instead. |
| 227 |
$last_index = $this->get_last_item_index()+5; // Menus typically go in increments of 5. |
| 228 |
$item_list[$last_index] = null; // Create a placeholder at end to allow us to shift them all up |
| 229 |
$item_indexes = array_keys($item_list); |
| 230 |
$new_item_list = array(); |
| 231 |
$new_item_list[$item_indexes[0]] = $item_array; // Finally add the item array to the beginning. |
| 232 |
for($i = 1; $i<count($item_indexes); $i++) { |
| 233 |
$new_item_list[$item_indexes[$i]] = $item_list[$item_indexes[$i-1]]; |
| 234 |
} |
| 235 |
$item_list = $new_item_list; |
| 236 |
break; |
| 237 |
case 'bottom': |
| 238 |
if ($item_type != ARRAY_A) { |
| 239 |
// If it's an associative array we need to add it, otherwise it's already part of the menu. |
| 240 |
$last_index = $this->get_last_item_index()+5; |
| 241 |
$item_list[$last_index] = $item_array; |
| 242 |
} |
| 243 |
break; |
| 244 |
case 'after': // TODO: Implement this |
| 245 |
case 'before': // TODO: Implement this |
| 246 |
default: |
| 247 |
wp_die("where='{$args['where']}' not yet implemented in WP_AdminMenuSection->add_item()."); |
| 248 |
} |
| 249 |
$this->refresh(); |
| 250 |
return new WP_AdminMenuItem($slug,$last_index); |
| 251 |
} |
| 252 |
function rename_item($item,$new_title) { |
| 253 |
$this->find_item($item)->set_title($new_title); |
| 254 |
} |
| 255 |
function swap_with($section) { |
| 256 |
$with = get_admin_menu_section($section); |
| 257 |
global $menu; |
| 258 |
$temp = $menu[$this->index]; |
| 259 |
$menu[$this->index] = $menu[$with->index]; |
| 260 |
$menu[$with->index] = $temp; |
| 261 |
$temp = $this->index; |
| 262 |
$this->index = $with->index; |
| 263 |
$with->index = $temp; |
| 264 |
$temp = $this->items; |
| 265 |
$this->items = $with->items; |
| 266 |
$with->items = $temp; |
| 267 |
} |
| 268 |
function delete() { |
| 269 |
global $submenu; |
| 270 |
unset($submenu[$this->get_slug()]); |
| 271 |
global $menu; |
| 272 |
unset($menu[$this->index]); |
| 273 |
} |
| 274 |
function remove_item($item) { |
| 275 |
global $submenu; |
| 276 |
$index = $this->find_item_index($item); |
| 277 |
$item = $this->find_item($index); |
| 278 |
unset($submenu[$item->parent_slug][$index]); |
| 279 |
unset($this->items[$index]); |
| 280 |
|
| 281 |
} |
| 282 |
function find_item($item) { |
| 283 |
if (!is_a($item,'WP_AdminMenuItem')) { |
| 284 |
$index = (is_numeric($item) ? $item : $this->find_item_index($item)); |
| 285 |
$item = ($index!==false ? $this->items[$index] : false); |
| 286 |
} |
| 287 |
return $item; |
| 288 |
} |
| 289 |
function find_item_index($item) { |
| 290 |
if (is_a($item,'WP_AdminMenuSection')) { |
| 291 |
wp_die('Unexpected: WP_AdminMenuSection passed when WP_AdminMenuItem or WP_AdminMenuItem->slug expected.'); |
| 292 |
} |
| 293 |
if (is_a($item,'WP_AdminMenuItem')) { |
| 294 |
$item = $item->get_slug(); |
| 295 |
} |
| 296 |
foreach($this->items as $index => $item_obj) { |
| 297 |
if ($item==$item_obj->get_slug()) { |
| 298 |
break; |
| 299 |
} else if (preg_match("#^{$item}$#",$item_obj->get_title())) { |
| 300 |
break; |
| 301 |
} else { |
| 302 |
$index = false; |
| 303 |
} |
| 304 |
} |
| 305 |
return $index; |
| 306 |
} |
| 307 |
function get_title() { |
| 308 |
return $GLOBALS['menu'][$this->index][0]; |
| 309 |
} |
| 310 |
function set_title($new_title) { |
| 311 |
$GLOBALS['menu'][$this->index][0] = $new_title; |
| 312 |
} |
| 313 |
function get_capability() { |
| 314 |
return $GLOBALS['menu'][$this->index][1]; |
| 315 |
} |
| 316 |
function set_capability($new_capability) { |
| 317 |
$GLOBALS['menu'][$this->index][1] = $new_capability; |
| 318 |
} |
| 319 |
function get_file() { // 'slug' & 'file' are synonyms for admin menu |
| 320 |
return $GLOBALS['menu'][$this->index][2]; |
| 321 |
} |
| 322 |
function set_file($new_file) { |
| 323 |
$GLOBALS['menu'][$this->index][2] = $new_file; |
| 324 |
} |
| 325 |
function get_slug() { // 'slug' & 'file' are synonyms for admin menu |
| 326 |
return $this->get_file(); |
| 327 |
} |
| 328 |
function set_slug($new_slug) { |
| 329 |
$this->set_file($new_slug); |
| 330 |
} |
| 331 |
function get_unused() { |
| 332 |
return $GLOBALS['menu'][$this->index][3]; |
| 333 |
} |
| 334 |
function set_unused($new_unused) { |
| 335 |
$GLOBALS['menu'][$this->index][3] = $new_unused; |
| 336 |
} |
| 337 |
function get_class() { |
| 338 |
return $GLOBALS['menu'][$this->index][4]; |
| 339 |
} |
| 340 |
function set_class($new_class) { |
| 341 |
$GLOBALS['menu'][$this->index][4] = $new_class; |
| 342 |
} |
| 343 |
function get_id() { |
| 344 |
return $GLOBALS['menu'][$this->index][5]; |
| 345 |
} |
| 346 |
function set_id($new_id) { |
| 347 |
$GLOBALS['menu'][$this->index][5] = $new_id; |
| 348 |
} |
| 349 |
function get_icon_src() { |
| 350 |
return $GLOBALS['menu'][$this->index][6]; |
| 351 |
} |
| 352 |
function set_icon_src($new_icon_src) { |
| 353 |
$GLOBALS['menu'][$this->index][6] = $new_icon_src; |
| 354 |
} |
| 355 |
function is_item_array($item) { |
| 356 |
$is_item_array = true; |
| 357 |
if (!is_array($item)) { |
| 358 |
$is_item_array = false; |
| 359 |
} else { |
| 360 |
foreach(array_keys($item) as $key) { |
| 361 |
if (!is_numeric($key)) { |
| 362 |
$is_item_array = false; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
return $is_item_array; |
| 368 |
} |
| 369 |
function get_last_item_index() { |
| 370 |
global $submenu; |
| 371 |
$slug = $this->get_slug(); |
| 372 |
return ($slug ? end(array_keys($submenu[$slug])) : false); |
| 373 |
} |
| 374 |
function refresh($section=false) { // This in case something external changes the submenu indexes |
| 375 |
if (!$section) { |
| 376 |
$this->items = $this->get_items($this); |
| 377 |
} else { |
| 378 |
$this->items = $this->get_items($section); |
| 379 |
$this->hookname = get_plugin_page_hookname($section,''); |
| 380 |
$this->index = $this->find_index($section); |
| 381 |
} |
| 382 |
} |
| 383 |
function find_index($section) { |
| 384 |
global $menu; |
| 385 |
$found = false; |
| 386 |
foreach($menu as $index => $section_array) { |
| 387 |
if ($section==$section_array[2]) { |
| 388 |
$found = true; |
| 389 |
break; |
| 390 |
} |
| 391 |
} |
| 392 |
return ($found ? $index : false); |
| 393 |
} |
| 394 |
function get_items($section) { |
| 395 |
if (is_a($section,'WP_AdminMenuSection')) |
| 396 |
$slug = $section->get_slug(); |
| 397 |
else if (is_string($section)) |
| 398 |
$slug = $section; |
| 399 |
global $submenu; |
| 400 |
$items = array(); |
| 401 |
if (isset($submenu[$slug])) { |
| 402 |
foreach($submenu[$slug] as $index => $item) { |
| 403 |
$items[$index] = new WP_AdminMenuItem($slug,$index); |
| 404 |
} |
| 405 |
} |
| 406 |
return $items; |
| 407 |
} |
| 408 |
} |
| 409 |
class WP_AdminMenuItem { |
| 410 |
var $index; |
| 411 |
var $parent_slug; |
| 412 |
var $hookname; |
| 413 |
function __construct($parent_slug,$slug) { |
| 414 |
$this->WP_AdminMenuItem($parent_slug,$slug); |
| 415 |
} |
| 416 |
function WP_AdminMenuItem($parent_slug,$slug) { |
| 417 |
$this->parent_slug = $parent_slug; |
| 418 |
if (is_numeric($slug)) { // $slug is designed for future non-legacy use |
| 419 |
$this->index = $slug; |
| 420 |
$slug = $this->get_slug(); |
| 421 |
} else { |
| 422 |
$section = new WP_AdminMenuSection($parent_slug); |
| 423 |
$item = $section->find_item($slug); |
| 424 |
if ($item) |
| 425 |
$this->index = $item->index; |
| 426 |
} |
| 427 |
$this->hookname = get_plugin_page_hookname($slug,$parent_slug); |
| 428 |
} |
| 429 |
function get_array() { // Only here because WP_AdminMenuSection really needed it. |
| 430 |
return $GLOBALS['submenu'][$this->parent_slug][$this->index]; |
| 431 |
} |
| 432 |
function get_title() { |
| 433 |
return $GLOBALS['submenu'][$this->parent_slug][$this->index][0]; |
| 434 |
} |
| 435 |
function set_title($new_title) { |
| 436 |
$GLOBALS['submenu'][$this->parent_slug][$this->index][0] = $new_title; |
| 437 |
} |
| 438 |
function get_capability() { |
| 439 |
return $GLOBALS['submenu'][$this->parent_slug][$this->index][1]; |
| 440 |
} |
| 441 |
function set_capability($new_capability) { |
| 442 |
$GLOBALS['submenu'][$this->parent_slug][$this->index][1] = $new_capability; |
| 443 |
} |
| 444 |
function get_slug() { // 'slug' & 'file' are synonyms for admin menu |
| 445 |
return html_entity_decode($GLOBALS['submenu'][$this->parent_slug][$this->index][2]); |
| 446 |
} |
| 447 |
function set_slug($new_slug) { |
| 448 |
$GLOBALS['submenu'][$this->parent_slug][$this->index][2] = $new_slug; |
| 449 |
} |
| 450 |
function get_file() { // 'slug' & 'file' are synonyms for admin menu |
| 451 |
return $this->get_slug(); |
| 452 |
} |
| 453 |
function set_file($new_file) { |
| 454 |
$this->set_slug($new_slug); |
| 455 |
} |
| 456 |
} |
| 457 |
|