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
83 changes: 83 additions & 0 deletions dev/qtx-test-test-urlBases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* @param array $urls
* @param array $exampleAdmin
* @param array $exampleAdminSolves
*
* @return void
*/
/**
* @param array $urls
* @param array $example
* @param array $exampleSolves
*
* @return void
*/
function qtranxf_check_url_replacement(array $urls, array $example, array $exampleSolves)
{
for ($i = 0; $i < count($example); $i++) {
$replace = str_replace([$urls[$i], '/', '.php'], '', $example[$i]);
if (strlen($replace) == 0 || $replace != $exampleSolves[$i]) {
qtranxf_tst_log(__FUNCTION__ . ': exit on url: ' . $example[$i] . '. Returned: "' . $replace
. '" but it should be "' . $exampleSolves[$i]. '"');
exit();
}
}
}

function qtranxf_run_test_urlBases() {
$urls = [
'https://localhost/wordpress/',
'https://www.localhost.de/',
'https://www.kl12354.com/234kdsfgk4534o5/wordpress/',
'https://www.dog.io/',
'https://127.0.0.1/',
'https://www.wordpress.com/'
];

$exampleAdmin = [
'https://localhost/wordpress/admin-page/',
'https://www.localhost.de/testo_ad#1min/',
'https://www.kl12354.com/234kdsfgk4534o5/wordpress/testo_admin/',
'https://www.dog.io/testo-cutsom_url/',
'https://127.0.0.1/wp-admin/', //default
'https://www.wordpress.com/wp-admin/' //default
];

$exampleAdminSolves = [
'admin-page',
'testo_ad#1min',
'testo_admin',
'testo-cutsom_url',
'wp-admin',
'wp-admin'
];

$exampleLogin = [
'https://localhost/wordpress/secret-login/',
'https://www.localhost.de/testo_login/',
'https://www.kl12354.com/234kdsfgk4534o5/wordpress/testo_login/',
'https://www.dog.io/testo-cutsom_url/',
'https://127.0.0.1/wp-login.php', //default
'https://www.wordpress.com/wp-login.php' //default
];

$exampleLoginSolves = [
'secret-login',
'testo_login',
'testo_login',
'testo-cutsom_url',
'wp-login',
'wp-login'
];

qtranxf_check_url_replacement($urls, $exampleAdmin, $exampleAdminSolves);

qtranxf_check_url_replacement($urls, $exampleLogin, $exampleLoginSolves);
}

qtranxf_run_test_urlBases();
poetter-sebastian marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion src/language_detect.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ function qtranxf_detect_language( array &$url_info ) {
$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 ) {
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
4 changes: 2 additions & 2 deletions src/modules/slugs/slugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], 'wp-admin/' ) !== 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'], 'wp-admin/' ) !== false ) {
if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], qtranxf_get_admin_base().'/' ) !== false ) {
herrvigg marked this conversation as resolved.
Show resolved Hide resolved
unset( $perma_query_vars );
}
$wp->did_permalink = false;
Expand Down
33 changes: 32 additions & 1 deletion src/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ 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 ) ) {

if ( preg_match( '#^/(wp-.*\.php|'.qtranxf_get_login_base().'/|'.qtranxf_get_admin_base().'/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) {
herrvigg marked this conversation as resolved.
Show resolved Hide resolved
$language_neutral_path_cache[ $path ] = true;

return true;
Expand Down Expand Up @@ -320,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
herrvigg marked this conversation as resolved.
Show resolved Hide resolved
*
* @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;
poetter-sebastian marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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
herrvigg marked this conversation as resolved.
Show resolved Hide resolved
*
* @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;
poetter-sebastian marked this conversation as resolved.
Show resolved Hide resolved
}

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