wp/wp-admin/includes/menu.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    40 	 */
    40 	 */
    41 	do_action( '_admin_menu' );
    41 	do_action( '_admin_menu' );
    42 }
    42 }
    43 
    43 
    44 // Create list of page plugin hook names.
    44 // Create list of page plugin hook names.
    45 foreach ($menu as $menu_page) {
    45 foreach ( $menu as $menu_page ) {
    46 	if ( false !== $pos = strpos($menu_page[2], '?') ) {
    46 	if ( false !== $pos = strpos( $menu_page[2], '?' ) ) {
    47 		// Handle post_type=post|page|foo pages.
    47 		// Handle post_type=post|page|foo pages.
    48 		$hook_name = substr($menu_page[2], 0, $pos);
    48 		$hook_name = substr( $menu_page[2], 0, $pos );
    49 		$hook_args = substr($menu_page[2], $pos + 1);
    49 		$hook_args = substr( $menu_page[2], $pos + 1 );
    50 		wp_parse_str($hook_args, $hook_args);
    50 		wp_parse_str( $hook_args, $hook_args );
    51 		// Set the hook name to be the post type.
    51 		// Set the hook name to be the post type.
    52 		if ( isset($hook_args['post_type']) )
    52 		if ( isset( $hook_args['post_type'] ) ) {
    53 			$hook_name = $hook_args['post_type'];
    53 			$hook_name = $hook_args['post_type'];
    54 		else
    54 		} else {
    55 			$hook_name = basename($hook_name, '.php');
    55 			$hook_name = basename( $hook_name, '.php' );
    56 		unset($hook_args);
    56 		}
       
    57 		unset( $hook_args );
    57 	} else {
    58 	} else {
    58 		$hook_name = basename($menu_page[2], '.php');
    59 		$hook_name = basename( $menu_page[2], '.php' );
    59 	}
    60 	}
    60 	$hook_name = sanitize_title($hook_name);
    61 	$hook_name = sanitize_title( $hook_name );
    61 
    62 
    62 	if ( isset($compat[$hook_name]) )
    63 	if ( isset( $compat[ $hook_name ] ) ) {
    63 		$hook_name = $compat[$hook_name];
    64 		$hook_name = $compat[ $hook_name ];
    64 	elseif ( !$hook_name )
    65 	} elseif ( ! $hook_name ) {
    65 		continue;
    66 		continue;
    66 
    67 	}
    67 	$admin_page_hooks[$menu_page[2]] = $hook_name;
    68 
    68 }
    69 	$admin_page_hooks[ $menu_page[2] ] = $hook_name;
    69 unset($menu_page, $compat);
    70 }
       
    71 unset( $menu_page, $compat );
    70 
    72 
    71 $_wp_submenu_nopriv = array();
    73 $_wp_submenu_nopriv = array();
    72 $_wp_menu_nopriv = array();
    74 $_wp_menu_nopriv    = array();
    73 // Loop over submenus and remove pages for which the user does not have privs.
    75 // Loop over submenus and remove pages for which the user does not have privs.
    74 foreach ($submenu as $parent => $sub) {
    76 foreach ( $submenu as $parent => $sub ) {
    75 	foreach ($sub as $index => $data) {
    77 	foreach ( $sub as $index => $data ) {
    76 		if ( ! current_user_can($data[1]) ) {
    78 		if ( ! current_user_can( $data[1] ) ) {
    77 			unset($submenu[$parent][$index]);
    79 			unset( $submenu[ $parent ][ $index ] );
    78 			$_wp_submenu_nopriv[$parent][$data[2]] = true;
    80 			$_wp_submenu_nopriv[ $parent ][ $data[2] ] = true;
    79 		}
    81 		}
    80 	}
    82 	}
    81 	unset($index, $data);
    83 	unset( $index, $data );
    82 
    84 
    83 	if ( empty($submenu[$parent]) )
    85 	if ( empty( $submenu[ $parent ] ) ) {
    84 		unset($submenu[$parent]);
    86 		unset( $submenu[ $parent ] );
    85 }
    87 	}
    86 unset($sub, $parent);
    88 }
       
    89 unset( $sub, $parent );
    87 
    90 
    88 /*
    91 /*
    89  * Loop over the top-level menu.
    92  * Loop over the top-level menu.
    90  * Menus for which the original parent is not accessible due to lack of privileges
    93  * Menus for which the original parent is not accessible due to lack of privileges
    91  * will have the next submenu in line be assigned as the new menu parent.
    94  * will have the next submenu in line be assigned as the new menu parent.
    92  */
    95  */
    93 foreach ( $menu as $id => $data ) {
    96 foreach ( $menu as $id => $data ) {
    94 	if ( empty($submenu[$data[2]]) )
    97 	if ( empty( $submenu[ $data[2] ] ) ) {
    95 		continue;
    98 		continue;
    96 	$subs = $submenu[$data[2]];
    99 	}
    97 	$first_sub = reset( $subs );
   100 	$subs       = $submenu[ $data[2] ];
       
   101 	$first_sub  = reset( $subs );
    98 	$old_parent = $data[2];
   102 	$old_parent = $data[2];
    99 	$new_parent = $first_sub[2];
   103 	$new_parent = $first_sub[2];
   100 	/*
   104 	/*
   101 	 * If the first submenu is not the same as the assigned parent,
   105 	 * If the first submenu is not the same as the assigned parent,
   102 	 * make the first submenu the new parent.
   106 	 * make the first submenu the new parent.
   103 	 */
   107 	 */
   104 	if ( $new_parent != $old_parent ) {
   108 	if ( $new_parent != $old_parent ) {
   105 		$_wp_real_parent_file[$old_parent] = $new_parent;
   109 		$_wp_real_parent_file[ $old_parent ] = $new_parent;
   106 		$menu[$id][2] = $new_parent;
   110 		$menu[ $id ][2]                      = $new_parent;
   107 
   111 
   108 		foreach ($submenu[$old_parent] as $index => $data) {
   112 		foreach ( $submenu[ $old_parent ] as $index => $data ) {
   109 			$submenu[$new_parent][$index] = $submenu[$old_parent][$index];
   113 			$submenu[ $new_parent ][ $index ] = $submenu[ $old_parent ][ $index ];
   110 			unset($submenu[$old_parent][$index]);
   114 			unset( $submenu[ $old_parent ][ $index ] );
   111 		}
   115 		}
   112 		unset($submenu[$old_parent], $index);
   116 		unset( $submenu[ $old_parent ], $index );
   113 
   117 
   114 		if ( isset($_wp_submenu_nopriv[$old_parent]) )
   118 		if ( isset( $_wp_submenu_nopriv[ $old_parent ] ) ) {
   115 			$_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
   119 			$_wp_submenu_nopriv[ $new_parent ] = $_wp_submenu_nopriv[ $old_parent ];
   116 	}
   120 		}
   117 }
   121 	}
   118 unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
   122 }
       
   123 unset( $id, $data, $subs, $first_sub, $old_parent, $new_parent );
   119 
   124 
   120 if ( is_network_admin() ) {
   125 if ( is_network_admin() ) {
   121 
   126 
   122 	/**
   127 	/**
   123 	 * Fires before the administration menu loads in the Network Admin.
   128 	 * Fires before the administration menu loads in the Network Admin.
   152 /*
   157 /*
   153  * Remove menus that have no accessible submenus and require privileges
   158  * Remove menus that have no accessible submenus and require privileges
   154  * that the user does not have. Run re-parent loop again.
   159  * that the user does not have. Run re-parent loop again.
   155  */
   160  */
   156 foreach ( $menu as $id => $data ) {
   161 foreach ( $menu as $id => $data ) {
   157 	if ( ! current_user_can($data[1]) )
   162 	if ( ! current_user_can( $data[1] ) ) {
   158 		$_wp_menu_nopriv[$data[2]] = true;
   163 		$_wp_menu_nopriv[ $data[2] ] = true;
       
   164 	}
   159 
   165 
   160 	/*
   166 	/*
   161 	 * If there is only one submenu and it is has same destination as the parent,
   167 	 * If there is only one submenu and it is has same destination as the parent,
   162 	 * remove the submenu.
   168 	 * remove the submenu.
   163 	 */
   169 	 */
   164 	if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) {
   170 	if ( ! empty( $submenu[ $data[2] ] ) && 1 == count( $submenu[ $data[2] ] ) ) {
   165 		$subs = $submenu[$data[2]];
   171 		$subs      = $submenu[ $data[2] ];
   166 		$first_sub = reset( $subs );
   172 		$first_sub = reset( $subs );
   167 		if ( $data[2] == $first_sub[2] )
   173 		if ( $data[2] == $first_sub[2] ) {
   168 			unset( $submenu[$data[2]] );
   174 			unset( $submenu[ $data[2] ] );
       
   175 		}
   169 	}
   176 	}
   170 
   177 
   171 	// If submenu is empty...
   178 	// If submenu is empty...
   172 	if ( empty($submenu[$data[2]]) ) {
   179 	if ( empty( $submenu[ $data[2] ] ) ) {
   173 		// And user doesn't have privs, remove menu.
   180 		// And user doesn't have privs, remove menu.
   174 		if ( isset( $_wp_menu_nopriv[$data[2]] ) ) {
   181 		if ( isset( $_wp_menu_nopriv[ $data[2] ] ) ) {
   175 			unset($menu[$id]);
   182 			unset( $menu[ $id ] );
   176 		}
   183 		}
   177 	}
   184 	}
   178 }
   185 }
   179 unset($id, $data, $subs, $first_sub);
   186 unset( $id, $data, $subs, $first_sub );
   180 
   187 
   181 /**
   188 /**
   182  *
       
   183  * @param string $add
   189  * @param string $add
   184  * @param string $class
   190  * @param string $class
   185  * @return string
   191  * @return string
   186  */
   192  */
   187 function add_cssclass($add, $class) {
   193 function add_cssclass( $add, $class ) {
   188 	$class = empty($class) ? $add : $class .= ' ' . $add;
   194 	$class = empty( $class ) ? $add : $class .= ' ' . $add;
   189 	return $class;
   195 	return $class;
   190 }
   196 }
   191 
   197 
   192 /**
   198 /**
   193  *
       
   194  * @param array $menu
   199  * @param array $menu
   195  * @return array
   200  * @return array
   196  */
   201  */
   197 function add_menu_classes($menu) {
   202 function add_menu_classes( $menu ) {
   198 	$first = $lastorder = false;
   203 	$first = $lastorder = false;
   199 	$i = 0;
   204 	$i     = 0;
   200 	$mc = count($menu);
   205 	$mc    = count( $menu );
   201 	foreach ( $menu as $order => $top ) {
   206 	foreach ( $menu as $order => $top ) {
   202 		$i++;
   207 		$i++;
   203 
   208 
   204 		if ( 0 == $order ) { // dashboard is always shown/single
   209 		if ( 0 == $order ) { // dashboard is always shown/single
   205 			$menu[0][4] = add_cssclass('menu-top-first', $top[4]);
   210 			$menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
   206 			$lastorder = 0;
   211 			$lastorder  = 0;
   207 			continue;
   212 			continue;
   208 		}
   213 		}
   209 
   214 
   210 		if ( 0 === strpos($top[2], 'separator') && false !== $lastorder ) { // if separator
   215 		if ( 0 === strpos( $top[2], 'separator' ) && false !== $lastorder ) { // if separator
   211 			$first = true;
   216 			$first                 = true;
   212 			$c = $menu[$lastorder][4];
   217 			$c                     = $menu[ $lastorder ][4];
   213 			$menu[$lastorder][4] = add_cssclass('menu-top-last', $c);
   218 			$menu[ $lastorder ][4] = add_cssclass( 'menu-top-last', $c );
   214 			continue;
   219 			continue;
   215 		}
   220 		}
   216 
   221 
   217 		if ( $first ) {
   222 		if ( $first ) {
   218 			$c = $menu[$order][4];
   223 			$c                 = $menu[ $order ][4];
   219 			$menu[$order][4] = add_cssclass('menu-top-first', $c);
   224 			$menu[ $order ][4] = add_cssclass( 'menu-top-first', $c );
   220 			$first = false;
   225 			$first             = false;
   221 		}
   226 		}
   222 
   227 
   223 		if ( $mc == $i ) { // last item
   228 		if ( $mc == $i ) { // last item
   224 			$c = $menu[$order][4];
   229 			$c                 = $menu[ $order ][4];
   225 			$menu[$order][4] = add_cssclass('menu-top-last', $c);
   230 			$menu[ $order ][4] = add_cssclass( 'menu-top-last', $c );
   226 		}
   231 		}
   227 
   232 
   228 		$lastorder = $order;
   233 		$lastorder = $order;
   229 	}
   234 	}
   230 
   235 
   236 	 * @param array $menu Associative array of administration menu items.
   241 	 * @param array $menu Associative array of administration menu items.
   237 	 */
   242 	 */
   238 	return apply_filters( 'add_menu_classes', $menu );
   243 	return apply_filters( 'add_menu_classes', $menu );
   239 }
   244 }
   240 
   245 
   241 uksort($menu, "strnatcasecmp"); // make it all pretty
   246 uksort( $menu, 'strnatcasecmp' ); // make it all pretty
   242 
   247 
   243 /**
   248 /**
   244  * Filters whether to enable custom ordering of the administration menu.
   249  * Filters whether to enable custom ordering of the administration menu.
   245  *
   250  *
   246  * See the {@see 'menu_order'} filter for reordering menu items.
   251  * See the {@see 'menu_order'} filter for reordering menu items.
   252 if ( apply_filters( 'custom_menu_order', false ) ) {
   257 if ( apply_filters( 'custom_menu_order', false ) ) {
   253 	$menu_order = array();
   258 	$menu_order = array();
   254 	foreach ( $menu as $menu_item ) {
   259 	foreach ( $menu as $menu_item ) {
   255 		$menu_order[] = $menu_item[2];
   260 		$menu_order[] = $menu_item[2];
   256 	}
   261 	}
   257 	unset($menu_item);
   262 	unset( $menu_item );
   258 	$default_menu_order = $menu_order;
   263 	$default_menu_order = $menu_order;
   259 
   264 
   260 	/**
   265 	/**
   261 	 * Filters the order of administration menu items.
   266 	 * Filters the order of administration menu items.
   262 	 *
   267 	 *
   267 	 *
   272 	 *
   268 	 * @since 2.8.0
   273 	 * @since 2.8.0
   269 	 *
   274 	 *
   270 	 * @param array $menu_order An ordered array of menu items.
   275 	 * @param array $menu_order An ordered array of menu items.
   271 	 */
   276 	 */
   272 	$menu_order = apply_filters( 'menu_order', $menu_order );
   277 	$menu_order         = apply_filters( 'menu_order', $menu_order );
   273 	$menu_order = array_flip($menu_order);
   278 	$menu_order         = array_flip( $menu_order );
   274 	$default_menu_order = array_flip($default_menu_order);
   279 	$default_menu_order = array_flip( $default_menu_order );
   275 
   280 
   276 	/**
   281 	/**
   277 	 *
       
   278 	 * @global array $menu_order
   282 	 * @global array $menu_order
   279 	 * @global array $default_menu_order
   283 	 * @global array $default_menu_order
   280 	 *
   284 	 *
   281 	 * @param array $a
   285 	 * @param array $a
   282 	 * @param array $b
   286 	 * @param array $b
   283 	 * @return int
   287 	 * @return int
   284 	 */
   288 	 */
   285 	function sort_menu($a, $b) {
   289 	function sort_menu( $a, $b ) {
   286 		global $menu_order, $default_menu_order;
   290 		global $menu_order, $default_menu_order;
   287 		$a = $a[2];
   291 		$a = $a[2];
   288 		$b = $b[2];
   292 		$b = $b[2];
   289 		if ( isset($menu_order[$a]) && !isset($menu_order[$b]) ) {
   293 		if ( isset( $menu_order[ $a ] ) && ! isset( $menu_order[ $b ] ) ) {
   290 			return -1;
   294 			return -1;
   291 		} elseif ( !isset($menu_order[$a]) && isset($menu_order[$b]) ) {
   295 		} elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
   292 			return 1;
   296 			return 1;
   293 		} elseif ( isset($menu_order[$a]) && isset($menu_order[$b]) ) {
   297 		} elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
   294 			if ( $menu_order[$a] == $menu_order[$b] )
   298 			if ( $menu_order[ $a ] == $menu_order[ $b ] ) {
   295 				return 0;
   299 				return 0;
   296 			return ($menu_order[$a] < $menu_order[$b]) ? -1 : 1;
   300 			}
       
   301 			return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1;
   297 		} else {
   302 		} else {
   298 			return ($default_menu_order[$a] <= $default_menu_order[$b]) ? -1 : 1;
   303 			return ( $default_menu_order[ $a ] <= $default_menu_order[ $b ] ) ? -1 : 1;
   299 		}
   304 		}
   300 	}
   305 	}
   301 
   306 
   302 	usort($menu, 'sort_menu');
   307 	usort( $menu, 'sort_menu' );
   303 	unset($menu_order, $default_menu_order);
   308 	unset( $menu_order, $default_menu_order );
   304 }
   309 }
   305 
   310 
   306 // Prevent adjacent separators
   311 // Prevent adjacent separators
   307 $prev_menu_was_separator = false;
   312 $prev_menu_was_separator = false;
   308 foreach ( $menu as $id => $data ) {
   313 foreach ( $menu as $id => $data ) {
   324 unset( $id, $data, $prev_menu_was_separator );
   329 unset( $id, $data, $prev_menu_was_separator );
   325 
   330 
   326 // Remove the last menu item if it is a separator.
   331 // Remove the last menu item if it is a separator.
   327 $last_menu_key = array_keys( $menu );
   332 $last_menu_key = array_keys( $menu );
   328 $last_menu_key = array_pop( $last_menu_key );
   333 $last_menu_key = array_pop( $last_menu_key );
   329 if ( !empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][ 4 ] )
   334 if ( ! empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][4] ) {
   330 	unset( $menu[ $last_menu_key ] );
   335 	unset( $menu[ $last_menu_key ] );
       
   336 }
   331 unset( $last_menu_key );
   337 unset( $last_menu_key );
   332 
   338 
   333 if ( !user_can_access_admin_page() ) {
   339 if ( ! user_can_access_admin_page() ) {
   334 
   340 
   335 	/**
   341 	/**
   336 	 * Fires when access to an admin page is denied.
   342 	 * Fires when access to an admin page is denied.
   337 	 *
   343 	 *
   338 	 * @since 2.5.0
   344 	 * @since 2.5.0
   340 	do_action( 'admin_page_access_denied' );
   346 	do_action( 'admin_page_access_denied' );
   341 
   347 
   342 	wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
   348 	wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
   343 }
   349 }
   344 
   350 
   345 $menu = add_menu_classes($menu);
   351 $menu = add_menu_classes( $menu );