wp/wp-admin/includes/menu.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     1 <?php
     1 <?php
     2 
       
     3 /**
     2 /**
     4  * Build Administration Menu.
     3  * Build Administration Menu.
     5  *
     4  *
     6  * @package WordPress
     5  * @package WordPress
     7  * @subpackage Administration
     6  * @subpackage Administration
     8  */
     7  */
     9 
     8 
    10 if ( is_network_admin() )
     9 if ( is_network_admin() ) {
    11 	do_action('_network_admin_menu');
    10 
    12 elseif ( is_user_admin() )
    11 	/**
    13 	do_action('_user_admin_menu');
    12 	 * Fires before the administration menu loads in the Network Admin.
    14 else
    13 	 *
    15 	do_action('_admin_menu');
    14 	 * The hook fires before menus and sub-menus are removed based on user privileges.
       
    15 	 *
       
    16 	 * @private
       
    17 	 * @since 3.1.0
       
    18 	 */
       
    19 	do_action( '_network_admin_menu' );
       
    20 } elseif ( is_user_admin() ) {
       
    21 
       
    22 	/**
       
    23 	 * Fires before the administration menu loads in the User Admin.
       
    24 	 *
       
    25 	 * The hook fires before menus and sub-menus are removed based on user privileges.
       
    26 	 *
       
    27 	 * @private
       
    28 	 * @since 3.1.0
       
    29 	 */
       
    30 	do_action( '_user_admin_menu' );
       
    31 } else {
       
    32 
       
    33 	/**
       
    34 	 * Fires before the administration menu loads in the admin.
       
    35 	 *
       
    36 	 * The hook fires before menus and sub-menus are removed based on user privileges.
       
    37 	 *
       
    38 	 * @private
       
    39 	 * @since 2.2.0
       
    40 	 */
       
    41 	do_action( '_admin_menu' );
       
    42 }
    16 
    43 
    17 // Create list of page plugin hook names.
    44 // Create list of page plugin hook names.
    18 foreach ($menu as $menu_page) {
    45 foreach ($menu as $menu_page) {
    19 	if ( false !== $pos = strpos($menu_page[2], '?') ) {
    46 	if ( false !== $pos = strpos($menu_page[2], '?') ) {
    20 		// Handle post_type=post|page|foo pages.
    47 		// Handle post_type=post|page|foo pages.
    42 unset($menu_page, $compat);
    69 unset($menu_page, $compat);
    43 
    70 
    44 $_wp_submenu_nopriv = array();
    71 $_wp_submenu_nopriv = array();
    45 $_wp_menu_nopriv = array();
    72 $_wp_menu_nopriv = array();
    46 // Loop over submenus and remove pages for which the user does not have privs.
    73 // Loop over submenus and remove pages for which the user does not have privs.
    47 foreach ( array( 'submenu' ) as $sub_loop ) {
    74 foreach ($submenu as $parent => $sub) {
    48 	foreach ($$sub_loop as $parent => $sub) {
    75 	foreach ($sub as $index => $data) {
    49 		foreach ($sub as $index => $data) {
    76 		if ( ! current_user_can($data[1]) ) {
    50 			if ( ! current_user_can($data[1]) ) {
    77 			unset($submenu[$parent][$index]);
    51 				unset(${$sub_loop}[$parent][$index]);
    78 			$_wp_submenu_nopriv[$parent][$data[2]] = true;
    52 				$_wp_submenu_nopriv[$parent][$data[2]] = true;
    79 		}
    53 			}
    80 	}
    54 		}
    81 	unset($index, $data);
    55 		unset($index, $data);
    82 
    56 
    83 	if ( empty($submenu[$parent]) )
    57 		if ( empty(${$sub_loop}[$parent]) )
    84 		unset($submenu[$parent]);
    58 			unset(${$sub_loop}[$parent]);
    85 }
    59 	}
    86 unset($sub, $parent);
    60 	unset($sub, $parent);
    87 
    61 }
    88 /*
    62 unset($sub_loop);
    89  * Loop over the top-level menu.
    63 
    90  * Menus for which the original parent is not accessible due to lack of privileges
    64 // Loop over the top-level menu.
    91  * will have the next submenu in line be assigned as the new menu parent.
    65 // Menus for which the original parent is not accessible due to lack of privs will have the next
    92  */
    66 // submenu in line be assigned as the new menu parent.
       
    67 foreach ( $menu as $id => $data ) {
    93 foreach ( $menu as $id => $data ) {
    68 	if ( empty($submenu[$data[2]]) )
    94 	if ( empty($submenu[$data[2]]) )
    69 		continue;
    95 		continue;
    70 	$subs = $submenu[$data[2]];
    96 	$subs = $submenu[$data[2]];
    71 	$first_sub = array_shift($subs);
    97 	$first_sub = reset( $subs );
    72 	$old_parent = $data[2];
    98 	$old_parent = $data[2];
    73 	$new_parent = $first_sub[2];
    99 	$new_parent = $first_sub[2];
    74 	// If the first submenu is not the same as the assigned parent,
   100 	/*
    75 	// make the first submenu the new parent.
   101 	 * If the first submenu is not the same as the assigned parent,
       
   102 	 * make the first submenu the new parent.
       
   103 	 */
    76 	if ( $new_parent != $old_parent ) {
   104 	if ( $new_parent != $old_parent ) {
    77 		$_wp_real_parent_file[$old_parent] = $new_parent;
   105 		$_wp_real_parent_file[$old_parent] = $new_parent;
    78 		$menu[$id][2] = $new_parent;
   106 		$menu[$id][2] = $new_parent;
    79 
   107 
    80 		foreach ($submenu[$old_parent] as $index => $data) {
   108 		foreach ($submenu[$old_parent] as $index => $data) {
    87 			$_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
   115 			$_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
    88 	}
   116 	}
    89 }
   117 }
    90 unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
   118 unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
    91 
   119 
    92 if ( is_network_admin() )
   120 if ( is_network_admin() ) {
    93 	do_action('network_admin_menu', '');
   121 
    94 elseif ( is_user_admin() )
   122 	/**
    95 	do_action('user_admin_menu', '');
   123 	 * Fires before the administration menu loads in the Network Admin.
    96 else
   124 	 *
    97 	do_action('admin_menu', '');
   125 	 * @since 3.1.0
    98 
   126 	 *
    99 // Remove menus that have no accessible submenus and require privs that the user does not have.
   127 	 * @param string $context Empty context.
   100 // Run re-parent loop again.
   128 	 */
       
   129 	do_action( 'network_admin_menu', '' );
       
   130 } elseif ( is_user_admin() ) {
       
   131 
       
   132 	/**
       
   133 	 * Fires before the administration menu loads in the User Admin.
       
   134 	 *
       
   135 	 * @since 3.1.0
       
   136 	 *
       
   137 	 * @param string $context Empty context.
       
   138 	 */
       
   139 	do_action( 'user_admin_menu', '' );
       
   140 } else {
       
   141 
       
   142 	/**
       
   143 	 * Fires before the administration menu loads in the admin.
       
   144 	 *
       
   145 	 * @since 1.5.0
       
   146 	 *
       
   147 	 * @param string $context Empty context.
       
   148 	 */
       
   149 	do_action( 'admin_menu', '' );
       
   150 }
       
   151 
       
   152 /*
       
   153  * Remove menus that have no accessible submenus and require privileges
       
   154  * that the user does not have. Run re-parent loop again.
       
   155  */
   101 foreach ( $menu as $id => $data ) {
   156 foreach ( $menu as $id => $data ) {
   102 	if ( ! current_user_can($data[1]) )
   157 	if ( ! current_user_can($data[1]) )
   103 		$_wp_menu_nopriv[$data[2]] = true;
   158 		$_wp_menu_nopriv[$data[2]] = true;
   104 
   159 
   105 	// If there is only one submenu and it is has same destination as the parent,
   160 	/*
   106 	// remove the submenu.
   161 	 * If there is only one submenu and it is has same destination as the parent,
       
   162 	 * remove the submenu.
       
   163 	 */
   107 	if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) {
   164 	if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) {
   108 		$subs = $submenu[$data[2]];
   165 		$subs = $submenu[$data[2]];
   109 		$first_sub = array_shift($subs);
   166 		$first_sub = reset( $subs );
   110 		if ( $data[2] == $first_sub[2] )
   167 		if ( $data[2] == $first_sub[2] )
   111 			unset( $submenu[$data[2]] );
   168 			unset( $submenu[$data[2]] );
   112 	}
   169 	}
   113 
   170 
   114 	// If submenu is empty...
   171 	// If submenu is empty...
   175 		}
   232 		}
   176 
   233 
   177 		$lastorder = $order;
   234 		$lastorder = $order;
   178 	}
   235 	}
   179 
   236 
       
   237 	/**
       
   238 	 * Filter administration menus array with classes added for top-level items.
       
   239 	 *
       
   240 	 * @since 2.7.0
       
   241 	 *
       
   242 	 * @param array $menu Associative array of administration menu items.
       
   243 	 */
   180 	return apply_filters( 'add_menu_classes', $menu );
   244 	return apply_filters( 'add_menu_classes', $menu );
   181 }
   245 }
   182 
   246 
   183 uksort($menu, "strnatcasecmp"); // make it all pretty
   247 uksort($menu, "strnatcasecmp"); // make it all pretty
   184 
   248 
   185 if ( apply_filters('custom_menu_order', false) ) {
   249 /**
       
   250  * Filter whether to enable custom ordering of the administration menu.
       
   251  *
       
   252  * See the 'menu_order' filter for reordering menu items.
       
   253  *
       
   254  * @since 2.8.0
       
   255  *
       
   256  * @param bool $custom Whether custom ordering is enabled. Default false.
       
   257  */
       
   258 if ( apply_filters( 'custom_menu_order', false ) ) {
   186 	$menu_order = array();
   259 	$menu_order = array();
   187 	foreach ( $menu as $menu_item ) {
   260 	foreach ( $menu as $menu_item ) {
   188 		$menu_order[] = $menu_item[2];
   261 		$menu_order[] = $menu_item[2];
   189 	}
   262 	}
   190 	unset($menu_item);
   263 	unset($menu_item);
   191 	$default_menu_order = $menu_order;
   264 	$default_menu_order = $menu_order;
   192 	$menu_order = apply_filters('menu_order', $menu_order);
   265 
       
   266 	/**
       
   267 	 * Filter the order of administration menu items.
       
   268 	 *
       
   269 	 * A truthy value must first be passed to the 'custom_menu_order' filter
       
   270 	 * for this filter to work. Use the following to enable custom menu ordering:
       
   271 	 *
       
   272 	 *     add_filter( 'custom_menu_order', '__return_true' );
       
   273 	 *
       
   274 	 * @since 2.8.0
       
   275 	 *
       
   276 	 * @param array $menu_order An ordered array of menu items.
       
   277 	 */
       
   278 	$menu_order = apply_filters( 'menu_order', $menu_order );
   193 	$menu_order = array_flip($menu_order);
   279 	$menu_order = array_flip($menu_order);
   194 	$default_menu_order = array_flip($default_menu_order);
   280 	$default_menu_order = array_flip($default_menu_order);
   195 
   281 
   196 	function sort_menu($a, $b) {
   282 	function sort_menu($a, $b) {
   197 		global $menu_order, $default_menu_order;
   283 		global $menu_order, $default_menu_order;
   220 if ( !empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][ 4 ] )
   306 if ( !empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][ 4 ] )
   221 	unset( $menu[ $last_menu_key ] );
   307 	unset( $menu[ $last_menu_key ] );
   222 unset( $last_menu_key );
   308 unset( $last_menu_key );
   223 
   309 
   224 if ( !user_can_access_admin_page() ) {
   310 if ( !user_can_access_admin_page() ) {
   225 	do_action('admin_page_access_denied');
   311 
   226 	wp_die( __('You do not have sufficient permissions to access this page.') );
   312 	/**
       
   313 	 * Fires when access to an admin page is denied.
       
   314 	 *
       
   315 	 * @since 2.5.0
       
   316 	 */
       
   317 	do_action( 'admin_page_access_denied' );
       
   318 
       
   319 	wp_die( __( 'You do not have sufficient permissions to access this page.' ), 403 );
   227 }
   320 }
   228 
   321 
   229 $menu = add_menu_classes($menu);
   322 $menu = add_menu_classes($menu);