Coding Standards: Use strict comparison in `wp-admin/includes/menu.php`.

Includes minor code layout fixes for better readability.

Follow-up to [3536], [4093], [9578], [11092], [18034].

Props aristath, poena, afercia, SergeyBiryukov.
See #57839.
Built from https://develop.svn.wordpress.org/trunk@55873


git-svn-id: http://core.svn.wordpress.org/trunk@55385 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-05-31 14:53:26 +00:00
parent f3b93e6b3a
commit 3d1140c898
2 changed files with 30 additions and 14 deletions

View File

@ -44,11 +44,13 @@ if ( is_network_admin() ) {
// Create list of page plugin hook names. // Create list of page plugin hook names.
foreach ( $menu as $menu_page ) { foreach ( $menu as $menu_page ) {
$pos = strpos( $menu_page[2], '?' ); $pos = strpos( $menu_page[2], '?' );
if ( false !== $pos ) { if ( false !== $pos ) {
// Handle post_type=post|page|foo pages. // Handle post_type=post|page|foo pages.
$hook_name = substr( $menu_page[2], 0, $pos ); $hook_name = substr( $menu_page[2], 0, $pos );
$hook_args = substr( $menu_page[2], $pos + 1 ); $hook_args = substr( $menu_page[2], $pos + 1 );
wp_parse_str( $hook_args, $hook_args ); wp_parse_str( $hook_args, $hook_args );
// Set the hook name to be the post type. // Set the hook name to be the post type.
if ( isset( $hook_args['post_type'] ) ) { if ( isset( $hook_args['post_type'] ) ) {
$hook_name = $hook_args['post_type']; $hook_name = $hook_args['post_type'];
@ -59,6 +61,7 @@ foreach ( $menu as $menu_page ) {
} else { } else {
$hook_name = basename( $menu_page[2], '.php' ); $hook_name = basename( $menu_page[2], '.php' );
} }
$hook_name = sanitize_title( $hook_name ); $hook_name = sanitize_title( $hook_name );
if ( isset( $compat[ $hook_name ] ) ) { if ( isset( $compat[ $hook_name ] ) ) {
@ -98,17 +101,20 @@ foreach ( $menu as $id => $data ) {
if ( empty( $submenu[ $data[2] ] ) ) { if ( empty( $submenu[ $data[2] ] ) ) {
continue; continue;
} }
$subs = $submenu[ $data[2] ]; $subs = $submenu[ $data[2] ];
$first_sub = reset( $subs ); $first_sub = reset( $subs );
$old_parent = $data[2]; $old_parent = $data[2];
$new_parent = $first_sub[2]; $new_parent = $first_sub[2];
/* /*
* If the first submenu is not the same as the assigned parent, * If the first submenu is not the same as the assigned parent,
* make the first submenu the new parent. * make the first submenu the new parent.
*/ */
if ( $new_parent != $old_parent ) { if ( $new_parent !== $old_parent ) {
$_wp_real_parent_file[ $old_parent ] = $new_parent; $_wp_real_parent_file[ $old_parent ] = $new_parent;
$menu[ $id ][2] = $new_parent;
$menu[ $id ][2] = $new_parent;
foreach ( $submenu[ $old_parent ] as $index => $data ) { foreach ( $submenu[ $old_parent ] as $index => $data ) {
$submenu[ $new_parent ][ $index ] = $submenu[ $old_parent ][ $index ]; $submenu[ $new_parent ][ $index ] = $submenu[ $old_parent ][ $index ];
@ -171,7 +177,8 @@ foreach ( $menu as $id => $data ) {
if ( ! empty( $submenu[ $data[2] ] ) && 1 === count( $submenu[ $data[2] ] ) ) { if ( ! empty( $submenu[ $data[2] ] ) && 1 === count( $submenu[ $data[2] ] ) ) {
$subs = $submenu[ $data[2] ]; $subs = $submenu[ $data[2] ];
$first_sub = reset( $subs ); $first_sub = reset( $subs );
if ( $data[2] == $first_sub[2] ) {
if ( $data[2] === $first_sub[2] ) {
unset( $submenu[ $data[2] ] ); unset( $submenu[ $data[2] ] );
} }
} }
@ -217,32 +224,36 @@ function add_menu_classes( $menu ) {
$first_item = false; $first_item = false;
$last_order = false; $last_order = false;
$items_count = count( $menu ); $items_count = count( $menu );
$i = 0;
$i = 0;
foreach ( $menu as $order => $top ) { foreach ( $menu as $order => $top ) {
$i++; $i++;
if ( 0 == $order ) { // Dashboard is always shown/single. if ( 0 === $order ) { // Dashboard is always shown/single.
$menu[0][4] = add_cssclass( 'menu-top-first', $top[4] ); $menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
$last_order = 0; $last_order = 0;
continue; continue;
} }
if ( str_starts_with( $top[2], 'separator' ) && false !== $last_order ) { // If separator. if ( str_starts_with( $top[2], 'separator' ) && false !== $last_order ) { // If separator.
$first_item = true; $first_item = true;
$classes = $menu[ $last_order ][4]; $classes = $menu[ $last_order ][4];
$menu[ $last_order ][4] = add_cssclass( 'menu-top-last', $classes ); $menu[ $last_order ][4] = add_cssclass( 'menu-top-last', $classes );
continue; continue;
} }
if ( $first_item ) { if ( $first_item ) {
$classes = $menu[ $order ][4]; $first_item = false;
$classes = $menu[ $order ][4];
$menu[ $order ][4] = add_cssclass( 'menu-top-first', $classes ); $menu[ $order ][4] = add_cssclass( 'menu-top-first', $classes );
$first_item = false;
} }
if ( $i === $items_count ) { // Last item. if ( $i === $items_count ) { // Last item.
$classes = $menu[ $order ][4]; $classes = $menu[ $order ][4];
$menu[ $order ][4] = add_cssclass( 'menu-top-last', $classes ); $menu[ $order ][4] = add_cssclass( 'menu-top-last', $classes );
} }
@ -272,10 +283,12 @@ uksort( $menu, 'strnatcasecmp' ); // Make it all pretty.
*/ */
if ( apply_filters( 'custom_menu_order', false ) ) { if ( apply_filters( 'custom_menu_order', false ) ) {
$menu_order = array(); $menu_order = array();
foreach ( $menu as $menu_item ) { foreach ( $menu as $menu_item ) {
$menu_order[] = $menu_item[2]; $menu_order[] = $menu_item[2];
} }
unset( $menu_item ); unset( $menu_item );
$default_menu_order = $menu_order; $default_menu_order = $menu_order;
/** /**
@ -290,8 +303,9 @@ if ( apply_filters( 'custom_menu_order', false ) ) {
* *
* @param array $menu_order An ordered array of menu items. * @param array $menu_order An ordered array of menu items.
*/ */
$menu_order = apply_filters( 'menu_order', $menu_order ); $menu_order = apply_filters( 'menu_order', $menu_order );
$menu_order = array_flip( $menu_order ); $menu_order = array_flip( $menu_order );
$default_menu_order = array_flip( $default_menu_order ); $default_menu_order = array_flip( $default_menu_order );
/** /**
@ -304,14 +318,16 @@ if ( apply_filters( 'custom_menu_order', false ) ) {
*/ */
function sort_menu( $a, $b ) { function sort_menu( $a, $b ) {
global $menu_order, $default_menu_order; global $menu_order, $default_menu_order;
$a = $a[2]; $a = $a[2];
$b = $b[2]; $b = $b[2];
if ( isset( $menu_order[ $a ] ) && ! isset( $menu_order[ $b ] ) ) { if ( isset( $menu_order[ $a ] ) && ! isset( $menu_order[ $b ] ) ) {
return -1; return -1;
} elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) { } elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
return 1; return 1;
} elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) { } elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
if ( $menu_order[ $a ] == $menu_order[ $b ] ) { if ( $menu_order[ $a ] === $menu_order[ $b ] ) {
return 0; return 0;
} }
return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1; return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1;

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-alpha-55872'; $wp_version = '6.3-alpha-55873';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.