Make WordPress Core


Ignore:
Timestamp:
11/10/2025 10:57:40 PM (10 months ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use more meaningful variable names in Admin Menu.

Per the Naming Conventions:

Don’t abbreviate variable names unnecessarily; let the code be unambiguous and self-documenting.

This commit includes renaming of the following variables:

  • $i to $submenu_index.
  • $cap to $capability.
  • $tax to $taxonomy.
  • Several $awaiting_mod_* to $awaiting_moderation_*.
  • Several $ptype_* to $post_type_*.
  • $types to $post_types.
  • $appearance_cap to $appearance_capability.

Follow-up to [12597], [14654], [21199], [23871], [33723], [33867].

Props costdev, SergeyBiryukov.
See #63168.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/menu.php

    r60358 r61200  
    4141if ( ! is_multisite() ) {
    4242        if ( current_user_can( 'update_core' ) ) {
    43                 $cap = 'update_core';
     43                $capability = 'update_core';
    4444        } elseif ( current_user_can( 'update_plugins' ) ) {
    45                 $cap = 'update_plugins';
     45                $capability = 'update_plugins';
    4646        } elseif ( current_user_can( 'update_themes' ) ) {
    47                 $cap = 'update_themes';
     47                $capability = 'update_themes';
    4848        } else {
    49                 $cap = 'update_languages';
    50         }
     49                $capability = 'update_languages';
     50        }
     51
    5152        $submenu['index.php'][10] = array(
    5253                sprintf(
     
    5960                        )
    6061                ),
    61                 $cap,
     62                $capability,
    6263                'update-core.php',
    6364        );
    64         unset( $cap );
     65
     66        unset( $capability );
    6567}
    6668
     
    6971// $menu[5] = Posts.
    7072
    71 $menu[10]                      = array( __( 'Media' ), 'upload_files', 'upload.php', '', 'menu-top menu-icon-media', 'menu-media', 'dashicons-admin-media' );
     73$menu[10] = array( __( 'Media' ), 'upload_files', 'upload.php', '', 'menu-top menu-icon-media', 'menu-media', 'dashicons-admin-media' );
     74
    7275        $submenu['upload.php'][5]  = array( __( 'Library' ), 'upload_files', 'upload.php' );
    7376        $submenu['upload.php'][10] = array( __( 'Add Media File' ), 'upload_files', 'media-new.php' );
    74         $i                         = 15;
    75 foreach ( get_taxonomies_for_attachments( 'objects' ) as $tax ) {
    76         if ( ! $tax->show_ui || ! $tax->show_in_menu ) {
    77                 continue;
    78         }
    79 
    80         $submenu['upload.php'][ $i++ ] = array( esc_attr( $tax->labels->menu_name ), $tax->cap->manage_terms, 'edit-tags.php?taxonomy=' . $tax->name . '&post_type=attachment' );
    81 }
    82         unset( $tax, $i );
    83 
    84 $menu[15]                            = array( __( 'Links' ), 'manage_links', 'link-manager.php', '', 'menu-top menu-icon-links', 'menu-links', 'dashicons-admin-links' );
     77        $submenu_index             = 15;
     78
     79foreach ( get_taxonomies_for_attachments( 'objects' ) as $taxonomy ) {
     80        if ( ! $taxonomy->show_ui || ! $taxonomy->show_in_menu ) {
     81                        continue;
     82        }
     83
     84        $submenu['upload.php'][ $submenu_index++ ] = array( esc_attr( $taxonomy->labels->menu_name ), $taxonomy->cap->manage_terms, 'edit-tags.php?taxonomy=' . $taxonomy->name . '&post_type=attachment' );
     85}
     86
     87        unset( $taxonomy, $submenu_index );
     88
     89$menu[15] = array( __( 'Links' ), 'manage_links', 'link-manager.php', '', 'menu-top menu-icon-links', 'menu-links', 'dashicons-admin-links' );
     90
    8591        $submenu['link-manager.php'][5]  = array( _x( 'All Links', 'admin menu' ), 'manage_links', 'link-manager.php' );
    8692        $submenu['link-manager.php'][10] = array( __( 'Add Link' ), 'manage_links', 'link-add.php' );
     
    9197// Avoid the comment count query for users who cannot edit_posts.
    9298if ( current_user_can( 'edit_posts' ) ) {
    93         $awaiting_mod      = wp_count_comments();
    94         $awaiting_mod      = $awaiting_mod->moderated;
    95         $awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
     99        $awaiting_moderation      = wp_count_comments();
     100        $awaiting_moderation      = $awaiting_moderation->moderated;
     101        $awaiting_moderation_i18n = number_format_i18n( $awaiting_moderation );
    96102        /* translators: %s: Number of comments. */
    97         $awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod ), $awaiting_mod_i18n );
     103        $awaiting_moderation_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_moderation ), $awaiting_moderation_i18n );
    98104
    99105        $menu[25] = array(
    100106                /* translators: %s: Number of comments. */
    101                 sprintf( __( 'Comments %s' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_mod_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_mod_text . '</span></span>' ),
     107                sprintf( __( 'Comments %s' ), '<span class="awaiting-mod count-' . absint( $awaiting_moderation ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_moderation_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_moderation_text . '</span></span>' ),
    102108                'edit_posts',
    103109                'edit-comments.php',
     
    107113                'dashicons-admin-comments',
    108114        );
    109         unset( $awaiting_mod );
     115
     116        unset( $awaiting_moderation );
    110117}
    111118
     
    114121$_wp_last_object_menu = 25; // The index of the last top-level menu in the object menu group.
    115122
    116 $types  = (array) get_post_types(
     123$post_types = (array) get_post_types(
    117124        array(
    118125                'show_ui'      => true,
     
    121128        )
    122129);
    123 $builtin = array( 'post', 'page' );
    124 foreach ( array_merge( $builtin, $types ) as $ptype ) {
    125         $ptype_obj = get_post_type_object( $ptype );
     130$builtin    = array( 'post', 'page' );
     131
     132foreach ( array_merge( $builtin, $post_types ) as $post_type ) {
     133        $post_type_obj = get_post_type_object( $post_type );
     134
    126135        // Check if it should be a submenu.
    127         if ( true !== $ptype_obj->show_in_menu ) {
     136        if ( true !== $post_type_obj->show_in_menu ) {
    128137                continue;
    129138        }
    130         $ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$_wp_last_object_menu; // If we're to use $_wp_last_object_menu, increment it first.
    131         $ptype_for_id        = sanitize_html_class( $ptype );
     139
     140        $post_type_menu_position = is_int( $post_type_obj->menu_position )
     141                ? $post_type_obj->menu_position
     142                : ++$_wp_last_object_menu; // If we're to use $_wp_last_object_menu, increment it first.
     143        $post_type_for_id        = sanitize_html_class( $post_type );
    132144
    133145        $menu_icon = 'dashicons-admin-post';
    134         if ( is_string( $ptype_obj->menu_icon ) ) {
     146        if ( is_string( $post_type_obj->menu_icon ) ) {
    135147                // Special handling for an empty div.wp-menu-image, data:image/svg+xml, and Dashicons.
    136                 if ( 'none' === $ptype_obj->menu_icon || 'div' === $ptype_obj->menu_icon
    137                         || str_starts_with( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' )
    138                         || str_starts_with( $ptype_obj->menu_icon, 'dashicons-' )
     148                if ( 'none' === $post_type_obj->menu_icon || 'div' === $post_type_obj->menu_icon
     149                        || str_starts_with( $post_type_obj->menu_icon, 'data:image/svg+xml;base64,' )
     150                        || str_starts_with( $post_type_obj->menu_icon, 'dashicons-' )
    139151                ) {
    140                         $menu_icon = $ptype_obj->menu_icon;
     152                        $menu_icon = $post_type_obj->menu_icon;
    141153                } else {
    142                         $menu_icon = esc_url( $ptype_obj->menu_icon );
     154                        $menu_icon = esc_url( $post_type_obj->menu_icon );
    143155                }
    144         } elseif ( in_array( $ptype, $builtin, true ) ) {
    145                 $menu_icon = 'dashicons-admin-' . $ptype;
    146         }
    147 
    148         $menu_class = 'menu-top menu-icon-' . $ptype_for_id;
     156        } elseif ( in_array( $post_type, $builtin, true ) ) {
     157                $menu_icon = 'dashicons-admin-' . $post_type;
     158        }
     159
     160        $menu_class = 'menu-top menu-icon-' . $post_type_for_id;
    149161        // 'post' special case.
    150         if ( 'post' === $ptype ) {
     162        if ( 'post' === $post_type ) {
    151163                $menu_class    .= ' open-if-no-js';
    152                 $ptype_file    = 'edit.php';
     164                $post_type_file = 'edit.php';
    153165                $post_new_file  = 'post-new.php';
    154166                $edit_tags_file = 'edit-tags.php?taxonomy=%s';
    155167        } else {
    156                 $ptype_file     = "edit.php?post_type=$ptype";
    157                 $post_new_file  = "post-new.php?post_type=$ptype";
    158                 $edit_tags_file = "edit-tags.php?taxonomy=%s&amp;post_type=$ptype";
    159         }
    160 
    161         if ( in_array( $ptype, $builtin, true ) ) {
    162                 $ptype_menu_id = 'menu-' . $ptype_for_id . 's';
     168                $post_type_file = "edit.php?post_type=$post_type";
     169                $post_new_file  = "post-new.php?post_type=$post_type";
     170                $edit_tags_file = "edit-tags.php?taxonomy=%s&amp;post_type=$post_type";
     171        }
     172
     173        if ( in_array( $post_type, $builtin, true ) ) {
     174                $post_type_menu_id = 'menu-' . $post_type_for_id . 's';
    163175        } else {
    164                 $ptype_menu_id = 'menu-posts-' . $ptype_for_id;
    165         }
     176                $post_type_menu_id = 'menu-posts-' . $post_type_for_id;
     177        }
     178
    166179        /*
    167          * If $ptype_menu_position is already populated or will be populated
     180         * If $post_type_menu_position is already populated or will be populated
    168181         * by a hard-coded value below, increment the position.
    169182         */
    170183        $core_menu_positions = array( 59, 60, 65, 70, 75, 80, 85, 99 );
    171         while ( isset( $menu[ $ptype_menu_position ] ) || in_array( $ptype_menu_position, $core_menu_positions, true ) ) {
    172                 ++$ptype_menu_position;
    173         }
    174 
    175         $menu[ $ptype_menu_position ] = array( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->cap->edit_posts, $ptype_file, '', $menu_class, $ptype_menu_id, $menu_icon );
    176         $submenu[ $ptype_file ][5]    = array( $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $ptype_file );
    177         $submenu[ $ptype_file ][10]   = array( $ptype_obj->labels->add_new_item, $ptype_obj->cap->create_posts, $post_new_file );
    178 
    179         $i = 15;
    180         foreach ( get_taxonomies( array(), 'objects' ) as $tax ) {
    181                 if ( ! $tax->show_ui || ! $tax->show_in_menu || ! in_array( $ptype, (array) $tax->object_type, true ) ) {
     184        while ( isset( $menu[ $post_type_menu_position ] ) || in_array( $post_type_menu_position, $core_menu_positions, true ) ) {
     185                ++$post_type_menu_position;
     186        }
     187
     188        $menu[ $post_type_menu_position ] = array( esc_attr( $post_type_obj->labels->menu_name ), $post_type_obj->cap->edit_posts, $post_type_file, '', $menu_class, $post_type_menu_id, $menu_icon );
     189        $submenu[ $post_type_file ][5]    = array( $post_type_obj->labels->all_items, $post_type_obj->cap->edit_posts, $post_type_file );
     190        $submenu[ $post_type_file ][10]   = array( $post_type_obj->labels->add_new_item, $post_type_obj->cap->create_posts, $post_new_file );
     191
     192        $submenu_index = 15;
     193
     194        foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
     195                if ( ! $taxonomy->show_ui || ! $taxonomy->show_in_menu || ! in_array( $post_type, (array) $taxonomy->object_type, true ) ) {
    182196                        continue;
    183197                }
    184198
    185                 $submenu[ $ptype_file ][ $i++ ] = array( esc_attr( $tax->labels->menu_name ), $tax->cap->manage_terms, sprintf( $edit_tags_file, $tax->name ) );
    186         }
    187 }
    188 unset( $ptype, $ptype_obj, $ptype_for_id, $ptype_menu_position, $menu_icon, $i, $tax, $post_new_file );
     199                $submenu[ $post_type_file ][ $submenu_index++ ] = array( esc_attr( $taxonomy->labels->menu_name ), $taxonomy->cap->manage_terms, sprintf( $edit_tags_file, $taxonomy->name ) );
     200        }
     201}
     202
     203unset( $post_type, $post_type_obj, $post_type_for_id, $post_type_menu_position, $menu_icon, $submenu_index, $taxonomy, $post_new_file );
    189204
    190205$menu[59] = array( '', 'read', 'separator2', '', 'wp-menu-separator' );
    191206
    192 $appearance_cap = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
    193 
    194 $menu[60] = array( __( 'Appearance' ), $appearance_cap, 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'dashicons-admin-appearance' );
     207$appearance_capability = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
     208
     209$menu[60] = array( __( 'Appearance' ), $appearance_capability, 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'dashicons-admin-appearance' );
    195210
    196211$count = '';
     
    199214                $update_data = wp_get_update_data();
    200215        }
     216
    201217        $count = sprintf(
    202218                '<span class="update-plugins count-%s"><span class="theme-count">%s</span></span>',
     
    207223
    208224        /* translators: %s: Number of available theme updates. */
    209         $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ), $appearance_cap, 'themes.php' );
     225        $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ), $appearance_capability, 'themes.php' );
    210226
    211227if ( wp_is_block_theme() ) {
     
    235251if ( current_theme_supports( 'custom-header' ) && current_user_can( 'customize' ) ) {
    236252        $customize_header_url      = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
    237         $submenu['themes.php'][15] = array( _x( 'Header', 'custom image header' ), $appearance_cap, esc_url( $customize_header_url ), '', 'hide-if-no-customize' );
     253        $submenu['themes.php'][15] = array( _x( 'Header', 'custom image header' ), $appearance_capability, esc_url( $customize_header_url ), '', 'hide-if-no-customize' );
    238254}
    239255
    240256if ( current_theme_supports( 'custom-background' ) && current_user_can( 'customize' ) ) {
    241257        $customize_background_url  = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
    242         $submenu['themes.php'][20] = array( _x( 'Background', 'custom background' ), $appearance_cap, esc_url( $customize_background_url ), '', 'hide-if-no-customize' );
    243 }
    244 
    245 unset( $customize_url );
    246 
    247 unset( $appearance_cap );
     258        $submenu['themes.php'][20] = array( _x( 'Background', 'custom background' ), $appearance_capability, esc_url( $customize_background_url ), '', 'hide-if-no-customize' );
     259}
     260
     261unset( $customize_url, $appearance_capability );
    248262
    249263// Add 'Theme File Editor' to the bottom of the Appearance (non-block themes) or Tools (block themes) menu.
Note: See TracChangeset for help on using the changeset viewer.