Skip to content
Draft
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
39 changes: 39 additions & 0 deletions src/wp-admin/includes/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1633,3 +1633,42 @@ function wp_check_php_version() {

return $response;
}

/**
* Returns the URL of the current admin page.
*
* @since 6.8.0
*
* @return string|false The URL of the current admin page, or false if not on an admin page.
*/
function get_current_admin_page_url() {
if ( ! is_admin() ) {
return false;
}

global $pagenow;

$url = $pagenow;
$query_string = $_SERVER['QUERY_STRING'];

if ( ! empty( $query_string ) ) {
$url .= '?' . $query_string;
}

return $url;
}

/**
* Returns the current admin hook.
*
* @since 6.8.0
*
* @return string|false The current admin hook, or false if not on an admin page.
*/
function get_current_admin_hook() {
if ( ! is_admin() ) {
return false;
}

return current_filter();
}
53 changes: 53 additions & 0 deletions tests/phpunit/tests/admin/includesMisc.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,57 @@ function () {
update_option_new_admin_email( 'old@example.com', 'new@example.com' );
$this->assertSame( 'Filtered Admin Email Address', $mailer->get_sent()->subject );
}

/**
* @ticket 27888
*
* @covers ::get_current_admin_page_url
*/
public function test_get_current_admin_page_url() {
$this->assertFalse( get_current_admin_page_url() );

set_current_screen( 'edit.php' );
global $pagenow;
$pagenow = 'edit.php';
$_SERVER['QUERY_STRING'] = 'post_type=page&orderby=title';

$this->assertSame( 'edit.php?post_type=page&orderby=title', get_current_admin_page_url() );

$_SERVER['QUERY_STRING'] = '';
$this->assertSame( 'edit.php', get_current_admin_page_url() );

$_SERVER['QUERY_STRING'] = '';
set_current_screen( 'front' );
}

/**
* Data provider for test_get_current_admin_hook
*
* @return array Test data.
*/
public static function current_filters() {
return array(
array( '', array( '' ) ),
array( 'some_hook', array( 'some_hook' ) ),
array( 'another_hook', array( 'some_hook', 'another_hook' ) ),
);
}

/**
* @ticket 27888
*
* @dataProvider current_filters
* @covers ::get_current_admin_hook
*
* @param string $expected Expected value.
* @param string $mock_current_filter Mock value for $wp_current_filter.
*/
public function test_get_current_admin_hook( $expected, $mock_current_filter ) {
set_current_screen( 'edit.php' );

global $wp_current_filter;
$wp_current_filter = $mock_current_filter;

$this->assertSame( $expected, get_current_admin_hook() );
}
}
Loading