Skip to content
Merged
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
16 changes: 16 additions & 0 deletions src/php/Admin/Menus/Edit_Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,25 @@ private function register_without_menu_item(): void {

if ( $hook ) {
add_action( 'load-' . $hook, array( $this, 'load' ) );
add_action( 'load-' . $hook, array( $this, 'set_page_title' ) );
}
}

/**
* Give the edit screen its title.
*
* WordPress looks a page's title up in the admin menu, where this page is not
* registered, and the admin header requires one.
*
* @return void
*/
public function set_page_title(): void {
global $title;

// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the admin header reads the page title from this global.
$title = $this->title;
}

/**
* Mark the snippet being edited in the menu.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/Admin/Menus/Edit_Menu_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,43 @@ public function test_hookname_matches_the_registered_page(): void {
$this->assertContains( $registered, $menu->get_hooknames() );
$this->assertNotFalse( has_action( 'load-' . $registered, [ $menu, 'load' ] ), 'the load hook is bound to the same name' );
}

/**
* The edit screen has a title for the admin header.
*
* WordPress looks a page's title up in the admin menu, where the edit page is
* not registered, and the admin header passes the result to strip_tags().
*
* @return void
*/
public function test_edit_screen_has_a_title(): void {
global $title, $pagenow, $plugin_page;

$edit_slug = code_snippets()->get_menu_slug( 'edit' );
$_GET['page'] = $edit_slug;
$_GET['id'] = '11';

$menu = new Edit_Menu();
$menu->register();

$load_hook = 'load-' . $menu->get_hookname();
remove_action( $load_hook, [ $menu, 'load' ] );

$previous_pagenow = $pagenow;

// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- the title lookup reads the current page from these globals.
$pagenow = 'admin.php';
$plugin_page = $edit_slug;
$title = null;

try {
do_action( $load_hook );
$this->assertSame( $menu->title, get_admin_page_title() );
} finally {
$pagenow = $previous_pagenow;
$plugin_page = null;
$title = null;
}
// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
Loading