Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/wp-admin/includes/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,17 @@ function sort_menu( $a, $b ) {
}
unset( $last_menu_key );

if ( ! user_can_access_admin_page() ) {
if ( ! admin_page_exists() ) {

/**
* Fires when access to an admin page which does not exist.
*
* @since 7.0.0
*/
do_action( 'admin_page_not_found' );

wp_die( __( 'The requested page does not exist.' ), 404 );
} elseif ( ! user_can_access_admin_page() ) {

/**
* Fires when access to an admin page is denied.
Expand Down
36 changes: 34 additions & 2 deletions src/wp-admin/includes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,33 @@ function get_plugin_page_hook( $plugin_page, $parent_page ) {
}
}

/**
* Checks if admin page exists, if the user would have the required capabilities.
*
* @since 7.0.0
*
* @global string $admin_page_parent The parent slug of the current admin page.
* @global string $plugin_page The plugin page slug being loaded.
* @global array<string, true> $_registered_pages Array of all registered admin page hooks.
*
* @return bool True if the admin page exists, false otherwise.
*/
function admin_page_exists() {
global $admin_page_parent, $plugin_page, $_registered_pages;

$admin_page_parent ??= get_admin_page_parent();

if ( isset( $plugin_page ) ) {
$hookname = get_plugin_page_hookname( $plugin_page, $admin_page_parent );

if ( ! isset( $_registered_pages[ $hookname ] ) ) {
return false;
}
}

return true;
}

/**
* Gets the hook name for the administrative page of a plugin.
*
Expand Down Expand Up @@ -2169,14 +2196,15 @@ function get_plugin_page_hookname( $plugin_page, $parent_page ) {
* @global array $_wp_submenu_nopriv
* @global string $plugin_page
* @global array $_registered_pages
* @global string $admin_page_parent
*
* @return bool True if the current user can access the admin page, false otherwise.
*/
function user_can_access_admin_page() {
global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
$plugin_page, $_registered_pages;
$plugin_page, $_registered_pages, $admin_page_parent;

$parent = get_admin_page_parent();
$parent = $admin_page_parent ?? get_admin_page_parent();

if ( ! isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $parent ][ $pagenow ] ) ) {
return false;
Expand All @@ -2189,6 +2217,10 @@ function user_can_access_admin_page() {

$hookname = get_plugin_page_hookname( $plugin_page, $parent );

if ( ! admin_page_exists() ) {
return false;
}

if ( ! isset( $_registered_pages[ $hookname ] ) ) {
return false;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/phpunit/tests/admin/includesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,69 @@ public function test_menu_page_url() {
wp_set_current_user( $current_user );
}

/**
* @ticket 14060
*
* @covers ::admin_page_exists
*/
public function test_admin_page_exists_returns_true_when_plugin_page_not_set() {
global $plugin_page, $admin_page_parent, $_registered_pages;

$previous_plugin_page = $plugin_page;
unset( $plugin_page );

$this->assertTrue( admin_page_exists() );

$plugin_page = $previous_plugin_page;
}

/**
* @ticket 14060
*
* @covers ::admin_page_exists
*/
public function test_admin_page_exists_returns_true_when_plugin_page_registered() {
global $plugin_page, $admin_page_parent, $_registered_pages;

$previous_plugin_page = $plugin_page ?? null;
$previous_admin_page_parent = $admin_page_parent ?? null;
$previous_registered_pages = $_registered_pages;

$plugin_page = 'admin-page-exists-test';
$admin_page_parent = 'options-general.php';
$hookname = get_plugin_page_hookname( $plugin_page, $admin_page_parent );
$_registered_pages[ $hookname ] = true;

$this->assertTrue( admin_page_exists() );

$plugin_page = $previous_plugin_page;
$admin_page_parent = $previous_admin_page_parent;
$_registered_pages = $previous_registered_pages;
}

/**
* @ticket 14060
*
* @covers ::admin_page_exists
*/
public function test_admin_page_exists_returns_false_when_plugin_page_not_registered() {
global $plugin_page, $admin_page_parent, $_registered_pages;

$previous_plugin_page = $plugin_page ?? null;
$previous_admin_page_parent = $admin_page_parent ?? null;
$previous_registered_pages = $_registered_pages;

$plugin_page = 'nonexistent-plugin-page-slug';
$admin_page_parent = 'options-general.php';
$_registered_pages = array();

$this->assertFalse( admin_page_exists() );

$plugin_page = $previous_plugin_page;
$admin_page_parent = $previous_admin_page_parent;
$_registered_pages = $previous_registered_pages;
}

/**
* Tests the position parameter.
*
Expand Down
Loading