From 55bb567e0f04644a7bad5f25e696983963d4dcf1 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 14 Jul 2017 23:00:55 +0000 Subject: [PATCH] Multisite: Introduce a `self_admin_url` filter to adjust the URL to an administration panel. Props j.hoffmann. Fixes #37446. git-svn-id: https://develop.svn.wordpress.org/trunk@41060 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 24 +++++++++--- tests/phpunit/tests/link/adminUrl.php | 56 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/tests/link/adminUrl.php diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 4d3a37ca5d..9f8cb01e75 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -3397,12 +3397,24 @@ function user_admin_url( $path = '', $scheme = 'admin' ) { * @return string Admin URL link with optional path appended. */ function self_admin_url( $path = '', $scheme = 'admin' ) { - if ( is_network_admin() ) - return network_admin_url($path, $scheme); - elseif ( is_user_admin() ) - return user_admin_url($path, $scheme); - else - return admin_url($path, $scheme); + if ( is_network_admin() ) { + $url = network_admin_url( $path, $scheme ); + } elseif ( is_user_admin() ) { + $url = user_admin_url( $path, $scheme ); + } else { + $url = admin_url( $path, $scheme ); + } + + /** + * Filters the admin URL for the current site or network depending on context. + * + * @since 4.9.0 + * + * @param string $url The complete URL including scheme and path. + * @param string $path Path relative to the URL. Blank string if no path is specified. + * @param string $scheme The scheme to use. + */ + return apply_filters( 'self_admin_url', $url, $path, $scheme ); } /** diff --git a/tests/phpunit/tests/link/adminUrl.php b/tests/phpunit/tests/link/adminUrl.php new file mode 100644 index 0000000000..f736007a2e --- /dev/null +++ b/tests/phpunit/tests/link/adminUrl.php @@ -0,0 +1,56 @@ +assertSame( admin_url(), self_admin_url() ); + } + + /** + * @ticket 37446 + */ + public function test_self_admin_url_with_path() { + $path = 'options-general.php'; + + $this->assertSame( admin_url( $path ), self_admin_url( $path ) ); + } + + /** + * @ticket 37446 + */ + public function test_self_admin_url_with_path_and_scheme() { + $path = 'options-general.php'; + $scheme = 'https'; + + $this->assertSame( admin_url( $path, $scheme ), self_admin_url( $path, $scheme ) ); + } + + /** + * @ticket 37446 + */ + public function test_self_admin_url_filtered() { + $path = 'options-general.php'; + $scheme = 'https'; + + add_filter( 'self_admin_url', array( $this, 'filter_self_admin_url' ), 10, 3 ); + $result = self_admin_url( $path, $scheme ); + remove_filter( 'self_admin_url', array( $this, 'filter_self_admin_url' ), 10 ); + + $expected = home_url( '/global-admin/' . $path, $scheme ); + + $this->assertSame( $expected, $result ); + } + + /** + * @ticket 37446 + */ + public function filter_self_admin_url( $url, $path, $scheme ) { + return home_url( path_join( '/global-admin/', $path ), $scheme ); + } +}