Changeset 64351
- Timestamp:
- 10/15/2026 03:20:06 AM (4 weeks ago)
- Location:
- trunk
- Files:
-
- 4 edited
-
src/wp-admin/includes/menu.php (modified) (1 diff)
-
src/wp-admin/includes/plugin.php (modified) (5 diffs)
-
tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon (modified) (1 diff)
-
tests/phpunit/tests/admin/includesPlugin.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/menu.php
r63941 r64351 377 377 unset( $last_menu_key ); 378 378 379 if ( ! user_can_access_admin_page() ) { 379 if ( ! wp_admin_page_exists() ) { 380 381 /** 382 * Fires when a request is made to access an admin page that does not exist. 383 * 384 * @since 7.2.0 385 */ 386 do_action( 'wp_admin_page_not_found' ); 387 388 wp_die( __( 'The requested page does not exist.' ), 404 ); 389 } elseif ( ! user_can_access_admin_page() ) { 380 390 381 391 /** -
trunk/src/wp-admin/includes/plugin.php
r64235 r64351 1478 1478 * @since 5.3.0 Added the `$position` parameter. 1479 1479 * 1480 * @global array $submenu 1481 * @global array $menu 1482 * @global array $_wp_real_parent_file 1483 * @global bool $_wp_submenu_nopriv 1484 * @global array $_registered_pages 1485 * @global array $_parent_pages 1480 * @global array $submenu 1481 * @global array $menu 1482 * @global array $_wp_real_parent_file 1483 * @global array<string, array<string, true>> $_wp_submenu_nopriv 1484 * @global array<string, true> $_wp_nopriv_pages 1485 * @global array<string, true> $_registered_pages 1486 * @global array $_parent_pages 1486 1487 * 1487 1488 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard … … 1502 1503 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) { 1503 1504 global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, 1504 $_ registered_pages, $_parent_pages;1505 $_wp_nopriv_pages, $_registered_pages, $_parent_pages; 1505 1506 1506 1507 $menu_slug = plugin_basename( $menu_slug ); … … 1513 1514 if ( ! current_user_can( $capability ) ) { 1514 1515 $_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true; 1516 $_wp_nopriv_pages[ $menu_slug ] = true; 1515 1517 return false; 1516 1518 } … … 2164 2166 2165 2167 /** 2168 * Determines whether the requested admin page is registered. 2169 * 2170 * This does not check whether the current user has the capability to access 2171 * the page, only whether the page itself exists. Pages registered for a 2172 * capability the current user lacks are recorded in the no-privilege 2173 * registries rather than in $_registered_pages, and are still considered 2174 * to exist so that the caller can report a permission error instead of a 2175 * "page not found" error. 2176 * 2177 * @since 7.2.0 2178 * 2179 * @global string|null $plugin_page The plugin page slug being loaded. 2180 * @global array<string, true> $_registered_pages Array of all registered admin page hooks. 2181 * @global array<string, true> $_wp_menu_nopriv Array of top-level menu slugs the current user cannot access. 2182 * @global array<string, array<string, true>> $_wp_submenu_nopriv Array of submenu slugs the current user cannot access, keyed by parent slug. 2183 * @global array<string, true> $_wp_nopriv_pages Array of page slugs registered for a capability the current user lacks. 2184 * 2185 * @return bool True if the admin page exists, false otherwise. 2186 */ 2187 function wp_admin_page_exists(): bool { 2188 global $plugin_page, $_registered_pages, $_wp_menu_nopriv, $_wp_submenu_nopriv, 2189 $_wp_nopriv_pages; 2190 2191 if ( ! isset( $plugin_page ) ) { 2192 return true; 2193 } 2194 2195 $hookname = get_plugin_page_hookname( $plugin_page, get_admin_page_parent() ); 2196 2197 if ( isset( $_registered_pages[ $hookname ] ) ) { 2198 return true; 2199 } 2200 2201 // The page may be registered for a capability the current user lacks. 2202 if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) || isset( $_wp_nopriv_pages[ $plugin_page ] ) ) { 2203 return true; 2204 } 2205 2206 foreach ( (array) $_wp_submenu_nopriv as $nopriv_submenus ) { 2207 if ( isset( $nopriv_submenus[ $plugin_page ] ) ) { 2208 return true; 2209 } 2210 } 2211 2212 return false; 2213 } 2214 2215 /** 2166 2216 * Gets the hook name for the administrative page of a plugin. 2167 2217 * … … 2203 2253 * @since 1.5.0 2204 2254 * 2205 * @global string $pagenow The filename of the current screen.2206 * @global array $menu2207 * @global array $submenu2208 * @global array $_wp_menu_nopriv2209 * @global array $_wp_submenu_nopriv2210 * @global string $plugin_page2211 * @global array $_registered_pages2255 * @global string $pagenow The filename of the current screen. 2256 * @global array $menu The top-level admin menu items. 2257 * @global array $submenu The admin submenu items, keyed by parent slug. 2258 * @global array<string, true> $_wp_menu_nopriv Array of top-level menu slugs the current user cannot access. 2259 * @global array<string, array<string, true>> $_wp_submenu_nopriv Array of submenu slugs the current user cannot access, keyed by parent slug. 2260 * @global string|null $plugin_page The plugin page slug being loaded. 2261 * @global array<string, true> $_registered_pages Array of all registered admin page hooks. 2212 2262 * 2213 2263 * @return bool True if the current user can access the admin page, false otherwise. 2214 2264 */ 2215 function user_can_access_admin_page() {2265 function user_can_access_admin_page(): bool { 2216 2266 global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv, 2217 2267 $plugin_page, $_registered_pages; -
trunk/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon
r63022 r64351 25 25 path: ../../../src/wp-admin/includes/class-wp-debug-data.php 26 26 - 27 message: '#^Cannot access offset mixed on bool\.$#'28 identifier: offsetAccess.nonOffsetAccessible29 count: 130 path: ../../../src/wp-admin/includes/plugin.php31 -32 27 message: '#^Cannot access offset ''new_version'' on bool\.$#' 33 28 identifier: offsetAccess.nonOffsetAccessible -
trunk/tests/phpunit/tests/admin/includesPlugin.php
r63608 r64351 27 27 */ 28 28 private function reset_menu_globals() { 29 global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages; 30 31 $menu = array(); 32 $submenu = array(); 33 $admin_page_hooks = array(); 34 $_registered_pages = array(); 35 $_parent_pages = array(); 29 global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, 30 $_wp_menu_nopriv, $_wp_submenu_nopriv, $_wp_nopriv_pages; 31 32 $menu = array(); 33 $submenu = array(); 34 $admin_page_hooks = array(); 35 $_registered_pages = array(); 36 $_parent_pages = array(); 37 $_wp_menu_nopriv = array(); 38 $_wp_submenu_nopriv = array(); 39 $_wp_nopriv_pages = array(); 40 41 unset( $GLOBALS['plugin_page'] ); 36 42 } 37 43 … … 96 102 97 103 wp_set_current_user( $current_user ); 104 } 105 106 /** 107 * @ticket 14060 108 * 109 * @covers ::wp_admin_page_exists 110 */ 111 public function test_wp_admin_page_exists_returns_true_when_no_plugin_page_is_set() { 112 unset( $GLOBALS['plugin_page'] ); 113 114 $this->assertTrue( wp_admin_page_exists() ); 115 } 116 117 /** 118 * @ticket 14060 119 * 120 * @covers ::wp_admin_page_exists 121 */ 122 public function test_wp_admin_page_exists_returns_true_for_a_registered_page() { 123 global $pagenow, $plugin_page; 124 125 wp_set_current_user( self::$admin_id ); 126 127 add_options_page( 'Test Settings', 'Test Settings', 'manage_options', 'testsettings', 'mt_settings_page' ); 128 129 $original_pagenow = $pagenow; 130 $pagenow = 'options-general.php'; 131 $plugin_page = 'testsettings'; 132 133 $exists = wp_admin_page_exists(); 134 135 $pagenow = $original_pagenow; 136 137 $this->assertTrue( $exists ); 138 } 139 140 /** 141 * @ticket 14060 142 * 143 * @covers ::wp_admin_page_exists 144 */ 145 public function test_wp_admin_page_exists_returns_false_for_an_unregistered_page() { 146 global $plugin_page, $_registered_pages; 147 148 $plugin_page = 'does-not-exist'; 149 $_registered_pages = array(); 150 151 $this->assertFalse( wp_admin_page_exists() ); 152 } 153 154 /** 155 * Pages the current user cannot access are not added to $_registered_pages, 156 * but they do exist and must not be reported as missing. 157 * 158 * @ticket 14060 159 * 160 * @covers ::wp_admin_page_exists 161 */ 162 public function test_wp_admin_page_exists_returns_true_for_a_registered_page_the_user_cannot_access() { 163 global $plugin_page; 164 165 wp_set_current_user( self::$admin_id ); 166 167 add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' ); 168 169 // Register a submenu page requiring a capability no user has. 170 wp_set_current_user( 0 ); 171 add_submenu_page( 'mt-top-level-handle', 'No Priv', 'No Priv', 'do_not_allow', 'mt-no-priv', 'mt_no_priv_page' ); 172 173 $plugin_page = 'mt-no-priv'; 174 175 $this->assertTrue( wp_admin_page_exists() ); 176 } 177 178 /** 179 * The no-privilege menu registries are reset in menu.php after the 180 * `_admin_menu` hook fires, so pages registered before that must still 181 * be considered to exist. 182 * 183 * @ticket 14060 184 * 185 * @covers ::wp_admin_page_exists 186 */ 187 public function test_wp_admin_page_exists_returns_true_for_an_inaccessible_page_after_nopriv_registries_are_reset() { 188 global $plugin_page, $_wp_menu_nopriv, $_wp_submenu_nopriv; 189 190 wp_set_current_user( 0 ); 191 192 add_submenu_page( 'options-general.php', 'No Priv', 'No Priv', 'do_not_allow', 'mt-no-priv', 'mt_no_priv_page' ); 193 194 // Simulate menu.php resetting the registries. 195 $_wp_menu_nopriv = array(); 196 $_wp_submenu_nopriv = array(); 197 198 $plugin_page = 'mt-no-priv'; 199 200 $this->assertTrue( wp_admin_page_exists() ); 98 201 } 99 202
Note:
See TracChangeset
for help on using the changeset viewer.