Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom URL for admin area and login page #1324

Merged
merged 8 commits into from
Apr 26, 2023
Merged
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