diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index afa36a2f2b51a..8ce8d37bbdf73 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -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(); +} \ No newline at end of file diff --git a/tests/phpunit/tests/admin/includesMisc.php b/tests/phpunit/tests/admin/includesMisc.php index 2903ea1ca6e78..195a6ddda7480 100644 --- a/tests/phpunit/tests/admin/includesMisc.php +++ b/tests/phpunit/tests/admin/includesMisc.php @@ -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() ); + } }