Skip to content

Commit

Permalink
Fix custom URL for admin area and login page (#1324)
Browse files Browse the repository at this point in the history
Custom URLs for admin area and login page are not handled properly
The path locations should not be hard-coded for neutral paths, referrer and slugs.
WP only provides access to the full URL with site included so the checks must be adapted.
Create a new function `qtranxf_get_admin_base` to.
Fix wrong check for neutral path from login page.
  • Loading branch information
poetter-sebastian committed Apr 26, 2023
1 parent 1855e65 commit 0a7e21e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/language_detect.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ function qtranxf_detect_language( array &$url_info ) {
if ( isset( $_SERVER['HTTP_REFERER'] ) && $parse_referrer ) {
$http_referer = $_SERVER['HTTP_REFERER'];
$url_info['http_referer'] = $http_referer;

// if needed, detect front- vs back-end
$parse_referrer_language = true;
if ( strpos( $http_referer, '/wp-admin' ) !== false ) {
// Attention: the referrer can be a full or partial path.
// TODO: improve check if admin base is found at other positions in URL
if ( strpos( $http_referer, '/' . qtranxf_get_admin_base() ) !== false ) {
$url_info['referer_admin'] = true;
if ( ! isset( $url_info['doing_front_end'] ) ) {
$url_info['doing_front_end'] = false;
Expand Down
5 changes: 3 additions & 2 deletions src/modules/slugs/slugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,11 @@ public function query_vars( $query_vars ) {
}

// If req_uri is empty or if it is a request for ourself, unset error.
if ( empty( $request ) || $req_uri == $self || strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
// TODO: improve string check if admin base is found at other positions in URL.
if ( empty( $request ) || $req_uri == $self || strpos( $_SERVER['PHP_SELF'], qtranxf_get_admin_base() . '/' ) !== false ) {
unset( $_GET['error'] );
unset( $error );
if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], qtranxf_get_admin_base() . '/' ) !== false ) {
unset( $perma_query_vars );
}
$wp->did_permalink = false;
Expand Down
23 changes: 16 additions & 7 deletions src/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ function qtranxf_language_neutral_path( string $path ): bool {
if ( isset( $language_neutral_path_cache[ $path ] ) ) {
return $language_neutral_path_cache[ $path ];
}
if ( preg_match( '#^/(wp-.*\.php|wp-login/|wp-admin/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) {
$language_neutral_path_cache[ $path ] = true;

return true;
}
if ( qtranxf_ignored_file_type( $path ) ) {
// WordPress doesn't provide the partial path to wp-admin or login, so check those from the site URL.
$site_url_path = site_url( $path );
if ( str_starts_with( $site_url_path, admin_url() ) ||
$site_url_path === wp_login_url() ||
preg_match( '#^/(wp-.*\.php|xmlrpc.php|robots.txt|oauth/)#', $path ) ||
qtranxf_ignored_file_type( $path )
) {
$language_neutral_path_cache[ $path ] = true;

return true;
}
$language_neutral_path_cache[ $path ] = false;
Expand Down Expand Up @@ -320,6 +320,15 @@ function qtranxf_get_url_info( string $url ): array {
return $urlinfo;
}

/**
* Return the base admin url of the WordPress backend name e.g. `wp-admin` for a default install.
*
* @return string WordPress backend name
*/
function qtranxf_get_admin_base(): string {
return trim( str_replace( site_url(), '', admin_url() ), '/' );
}

/**
* Complete urlinfo with 'path-base' according to home and site info.
* If they differ, 'doing_front_end' might be set.
Expand Down

0 comments on commit 0a7e21e

Please sign in to comment.