From a341556bb3904befa70f0d9c2773d74b024b1885 Mon Sep 17 00:00:00 2001 From: poetter-sebastian Date: Mon, 17 Apr 2023 13:39:39 +0200 Subject: [PATCH 1/8] Fixed custom url error for admin area --- src/language_detect.php | 5 ++++- src/modules/slugs/slugs.php | 7 +++++-- src/url.php | 5 ++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/language_detect.php b/src/language_detect.php index 2cb2c534..19902385 100644 --- a/src/language_detect.php +++ b/src/language_detect.php @@ -30,9 +30,12 @@ function qtranxf_detect_language( array &$url_info ) { $http_referer = $_SERVER['HTTP_REFERER']; $url_info['http_referer'] = $http_referer; + //fix custom admin url error + preg_match('/\/.+\/(.+)\//', admin_url(), $adminNeedle); + // if needed, detect front- vs back-end $parse_referrer_language = true; - if ( strpos( $http_referer, '/wp-admin' ) !== false ) { + if ( strpos( $http_referer, '/'.$adminNeedle[1] ) !== false ) { $url_info['referer_admin'] = true; if ( ! isset( $url_info['doing_front_end'] ) ) { $url_info['doing_front_end'] = false; diff --git a/src/modules/slugs/slugs.php b/src/modules/slugs/slugs.php index b66173bc..92401885 100644 --- a/src/modules/slugs/slugs.php +++ b/src/modules/slugs/slugs.php @@ -325,11 +325,14 @@ public function query_vars( $query_vars ) { } ); } + //fix custom admin url error + preg_match('/\/.+\/(.+)\//', admin_url(), $adminNeedle); + // 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 ) { + if ( empty( $request ) || $req_uri == $self || strpos( $_SERVER['PHP_SELF'], $adminNeedle[1].'/' ) !== 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'], $adminNeedle[1].'/' ) !== false ) { unset( $perma_query_vars ); } $wp->did_permalink = false; diff --git a/src/url.php b/src/url.php index 81f79437..3552df08 100644 --- a/src/url.php +++ b/src/url.php @@ -283,7 +283,10 @@ 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 ) ) { + //fix custom admin url error + preg_match('/\/.+\/(.+)\//', admin_url(), $adminNeedle); + + if ( preg_match( '#^/(wp-.*\.php|wp-login/|'.$adminNeedle[1].'/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) { $language_neutral_path_cache[ $path ] = true; return true; From 5436053456afd90ad8a471fcd56b139cb83421aa Mon Sep 17 00:00:00 2001 From: poetter-sebastian Date: Wed, 19 Apr 2023 15:00:08 +0200 Subject: [PATCH 2/8] =?UTF-8?q?Replaced=20the=20regex=20function=20with=20?= =?UTF-8?q?str=5Freplace=20according=20to=20#1324=20Creaded=20the=20functi?= =?UTF-8?q?on=20qtranxf=5Fget=5Flogin=5Fbase=20for=20login=20name=20Create?= =?UTF-8?q?d=20the=20function=C2=B4qtranxf=5Fget=5Fadmin=5Fbase=20for=20ba?= =?UTF-8?q?ckend=20name=20Creaded=20test=20function=20for=20str=5Freplace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dev/qtx-test-test-urlBases.php | 83 ++++++++++++++++++++++++++++++++++ src/language_detect.php | 4 +- src/modules/slugs/slugs.php | 7 +-- src/url.php | 34 ++++++++++++-- 4 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 dev/qtx-test-test-urlBases.php diff --git a/dev/qtx-test-test-urlBases.php b/dev/qtx-test-test-urlBases.php new file mode 100644 index 00000000..33b6dca7 --- /dev/null +++ b/dev/qtx-test-test-urlBases.php @@ -0,0 +1,83 @@ +did_permalink = false; diff --git a/src/url.php b/src/url.php index 3552df08..0df0d82a 100644 --- a/src/url.php +++ b/src/url.php @@ -283,10 +283,8 @@ function qtranxf_language_neutral_path( string $path ): bool { if ( isset( $language_neutral_path_cache[ $path ] ) ) { return $language_neutral_path_cache[ $path ]; } - //fix custom admin url error - preg_match('/\/.+\/(.+)\//', admin_url(), $adminNeedle); - if ( preg_match( '#^/(wp-.*\.php|wp-login/|'.$adminNeedle[1].'/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) { + if ( preg_match( '#^/(wp-.*\.php|'.qtranxf_get_login_base().'/|'.qtranxf_get_admin_base().'/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) { $language_neutral_path_cache[ $path ] = true; return true; @@ -323,6 +321,36 @@ function qtranxf_get_url_info( string $url ): array { return $urlinfo; } +/** + * Returns the base admin url of the WordPress backend name e.g. wp-admin + * If the $admin_base is empty it returns the standard WordPress backend name + * + * @author Sebastian Poetter https://github.com/poetter-sebastian + * @link https://github.com/qtranslate/qtranslate-xt/pull/1324 repo pull request + * + * @return string WordPress backend name + */ +function qtranxf_get_admin_base():string +{ + $admin_base = str_replace( [site_url(), '/'], '', admin_url() ); + return strlen($admin_base) == 0? 'wp-admin' : $admin_base; +} + +/** + * Returns the base admin url of the WordPress backend login url e.g. wp-login + * If the $login_base is empty it returns the standard WordPress backend login name + * + * @author Sebastian Poetter https://github.com/poetter-sebastian + * @link https://github.com/qtranslate/qtranslate-xt/pull/1324 repo pull request + * + * @return string WordPress backend login name + */ +function qtranxf_get_login_base():string +{ + $login_base = str_replace( [site_url(), '/', '.php'], '', wp_login_url() ); + return strlen($login_base) == 0? 'wp-login' : $login_base; +} + /** * Complete urlinfo with 'path-base' according to home and site info. * If they differ, 'doing_front_end' might be set. From 3325255f24df7f4b10f71d3f852b5b51be08bce6 Mon Sep 17 00:00:00 2001 From: poetter-sebastian Date: Thu, 20 Apr 2023 01:08:30 +0200 Subject: [PATCH 3/8] Simplified the admin_base and login_base functions Removed unnecessary test file --- dev/qtx-test-test-urlBases.php | 83 ---------------------------------- src/url.php | 10 ++-- 2 files changed, 3 insertions(+), 90 deletions(-) delete mode 100644 dev/qtx-test-test-urlBases.php diff --git a/dev/qtx-test-test-urlBases.php b/dev/qtx-test-test-urlBases.php deleted file mode 100644 index 33b6dca7..00000000 --- a/dev/qtx-test-test-urlBases.php +++ /dev/null @@ -1,83 +0,0 @@ - Date: Sun, 23 Apr 2023 18:24:11 +0200 Subject: [PATCH 4/8] Reformat PHP --- src/language_detect.php | 2 +- src/modules/slugs/slugs.php | 4 ++-- src/url.php | 12 +++++------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/language_detect.php b/src/language_detect.php index 9a056044..0ae757e9 100644 --- a/src/language_detect.php +++ b/src/language_detect.php @@ -33,7 +33,7 @@ function qtranxf_detect_language( array &$url_info ) { // if needed, detect front- vs back-end $parse_referrer_language = true; - if ( strpos( $http_referer, '/'.qtranxf_get_admin_base() ) !== false ) { + 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; diff --git a/src/modules/slugs/slugs.php b/src/modules/slugs/slugs.php index 5a4a0bb4..735c1058 100644 --- a/src/modules/slugs/slugs.php +++ b/src/modules/slugs/slugs.php @@ -326,10 +326,10 @@ 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'], qtranxf_get_admin_base().'/' ) !== false ) { + 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'], qtranxf_get_admin_base().'/' ) !== false ) { + if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], qtranxf_get_admin_base() . '/' ) !== false ) { unset( $perma_query_vars ); } $wp->did_permalink = false; diff --git a/src/url.php b/src/url.php index 38e70022..867e480c 100644 --- a/src/url.php +++ b/src/url.php @@ -284,7 +284,7 @@ function qtranxf_language_neutral_path( string $path ): bool { return $language_neutral_path_cache[ $path ]; } - if ( preg_match( '#^/(wp-.*\.php|'.qtranxf_get_login_base().'/|'.qtranxf_get_admin_base().'/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) { + if ( preg_match( '#^/(wp-.*\.php|' . qtranxf_get_login_base() . '/|' . qtranxf_get_admin_base() . '/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) { $language_neutral_path_cache[ $path ] = true; return true; @@ -329,9 +329,8 @@ function qtranxf_get_url_info( string $url ): array { * * @return string WordPress backend name */ -function qtranxf_get_admin_base():string -{ - return trim( str_replace( site_url(), '', admin_url() ), '/'); +function qtranxf_get_admin_base(): string { + return trim( str_replace( site_url(), '', admin_url() ), '/' ); } /** @@ -342,9 +341,8 @@ function qtranxf_get_admin_base():string * * @return string WordPress backend login name */ -function qtranxf_get_login_base():string -{ - return trim( str_replace( site_url(), '', wp_login_url() ), '/'); +function qtranxf_get_login_base(): string { + return trim( str_replace( site_url(), '', wp_login_url() ), '/' ); } /** From 25ddfb68538d8159d1d9186a053a08fc7b8fc35e Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Sun, 23 Apr 2023 19:49:36 +0200 Subject: [PATCH 5/8] Clean up PHPDoc without author and link --- src/url.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/url.php b/src/url.php index 867e480c..e8e41a7f 100644 --- a/src/url.php +++ b/src/url.php @@ -322,10 +322,7 @@ function qtranxf_get_url_info( string $url ): array { } /** - * Returns the base admin url of the WordPress backend name e.g. wp-admin - * - * @author Sebastian Poetter https://github.com/poetter-sebastian - * @link https://github.com/qtranslate/qtranslate-xt/pull/1324 repo pull request + * Return the base admin url of the WordPress backend name e.g. wp-admin. * * @return string WordPress backend name */ @@ -334,10 +331,7 @@ function qtranxf_get_admin_base(): string { } /** - * Returns the base admin url of the WordPress backend login url e.g. wp-login.php - * - * @author Sebastian Poetter https://github.com/poetter-sebastian - * @link https://github.com/qtranslate/qtranslate-xt/pull/1324 repo pull request + * Return the base admin url of the WordPress backend login url e.g. wp-login.php. * * @return string WordPress backend login name */ From b6195286278671dd4fc70e3c039d7f80c96cc9aa Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Sun, 23 Apr 2023 23:38:50 +0200 Subject: [PATCH 6/8] Rewrite neutral path checks for admin and login URL --- src/url.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/url.php b/src/url.php index e8e41a7f..011b4777 100644 --- a/src/url.php +++ b/src/url.php @@ -283,15 +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|' . qtranxf_get_login_base() . '/|' . qtranxf_get_admin_base() . '/|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; From a1906186ca2cb1ce5b8a6f89331aef6894a5ccda Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Mon, 24 Apr 2023 23:35:24 +0200 Subject: [PATCH 7/8] Remove `qtranxf_get_login_base` --- src/url.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/url.php b/src/url.php index 011b4777..f6ecae9f 100644 --- a/src/url.php +++ b/src/url.php @@ -321,7 +321,7 @@ function qtranxf_get_url_info( string $url ): array { } /** - * Return the base admin url of the WordPress backend name e.g. wp-admin. + * Return the base admin url of the WordPress backend name e.g. `wp-admin` for a default install. * * @return string WordPress backend name */ @@ -329,15 +329,6 @@ function qtranxf_get_admin_base(): string { return trim( str_replace( site_url(), '', admin_url() ), '/' ); } -/** - * Return the base admin url of the WordPress backend login url e.g. wp-login.php. - * - * @return string WordPress backend login name - */ -function qtranxf_get_login_base(): string { - return trim( str_replace( site_url(), '', wp_login_url() ), '/' ); -} - /** * Complete urlinfo with 'path-base' according to home and site info. * If they differ, 'doing_front_end' might be set. From bcd231ef1ccb5a6625bf0a27d7031e5b21459346 Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Thu, 27 Apr 2023 00:53:56 +0200 Subject: [PATCH 8/8] Add comments and TODOs --- src/language_detect.php | 4 ++-- src/modules/slugs/slugs.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/language_detect.php b/src/language_detect.php index 0ae757e9..f6e00e7c 100644 --- a/src/language_detect.php +++ b/src/language_detect.php @@ -29,10 +29,10 @@ 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; + // 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'] ) ) { diff --git a/src/modules/slugs/slugs.php b/src/modules/slugs/slugs.php index 735c1058..f21fc17f 100644 --- a/src/modules/slugs/slugs.php +++ b/src/modules/slugs/slugs.php @@ -326,6 +326,7 @@ public function query_vars( $query_vars ) { } // If req_uri is empty or if it is a request for ourself, unset error. + // 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 );