From da638584ea812c18c154255e2c8feb43a5146f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 17 Aug 2024 13:43:57 -0300 Subject: [PATCH] Add --prefix flag to prepend a path prefix for magic links --- README.md | 9 +++++++++ features/login-create.feature | 11 +++++++++++ src/LoginCommand.php | 22 ++++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index de3c56b..dc8b761 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ Set the URL to redirect to upon successfully logging in. Defaults to `admin_url( > Note: The redirect is executed using [`wp_safe_redirect`](https://developer.wordpress.org/reference/functions/wp_safe_redirect/) which restricts the destination URL using a list of allowed hosts. By default, this is limited to the domain of the site, but can be extended using the [`allowed_redirect_hosts`](https://developer.wordpress.org/reference/hooks/allowed_redirect_hosts/) filter. + +#### `--prefix=` + +Optionally prepend a path prefix to the magic link URL. Defaults to `/`. + #### `--url-only` Outputs the created sign-in URL only. Great for scripting, piping to your clipboard, or anything else you can think of. @@ -86,6 +91,10 @@ Email a magic sign-in link to the given user. Sends a nice HTML email to the us [See above.](#--redirect-urlurl) +#### `--prefix=` + +[See above.](#--prefixpath) + #### `--subject=` Optionally override the default email subject with your own custom string. You may use the `{{ domain }}` placeholder. diff --git a/features/login-create.feature b/features/login-create.feature index 884ac37..67a4a24 100644 --- a/features/login-create.feature +++ b/features/login-create.feature @@ -185,3 +185,14 @@ Feature: Users can generate single-use magic links that will log them in automat """ Location: http://localhost:8080/custom-redirect """ + + @option:prefix + Scenario: It can prepend a path prefix to the magic link URL + Given a WP install + And a PHP built-in web server + And the login plugin is installed and active + And I run `wp login as admin --url-only --prefix=subpath` + Then STDOUT should contain: + """ + http://localhost:8080/subpath/ + """ \ No newline at end of file diff --git a/src/LoginCommand.php b/src/LoginCommand.php index ab25311..579330a 100644 --- a/src/LoginCommand.php +++ b/src/LoginCommand.php @@ -50,6 +50,12 @@ class LoginCommand * default: * --- * + * [--prefix=] + * : The prefixed path added to the log-in URL. + * --- + * default: + * --- + * * [--url-only] * : Output the magic link URL only. * @@ -69,7 +75,7 @@ public function create($_, $assoc) $user = $this->lookupUser($user_locator); $expires = human_time_diff(time(), time() + absint($assoc['expires'])); - $magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url']); + $magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url'], $assoc['prefix']); if (WP_CLI\Utils\get_flag_value($assoc, 'url-only')) { WP_CLI::line($magic_url); @@ -108,6 +114,13 @@ public function create($_, $assoc) * default: * --- * + * [--prefix=] + * : The prefixed path added to the log-in URL. + * --- + * default: + * --- + * + * * [--subject=] * : The email subject field. * --- @@ -132,7 +145,7 @@ public function email($_, $assoc) $user = $this->lookupUser($user_locator); $expires = human_time_diff(time(), time() + absint($assoc['expires'])); - $magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url']); + $magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url'], $assoc['prefix']); $domain = $this->domain(); $subject = $this->mustacheRender( $assoc['subject'], @@ -410,10 +423,11 @@ public function toggle($_) * @param WP_User $user User to create login URL for. * @param int $expires Number of seconds from now until the magic link expires. * @param string $redirect_url URL to redirect to upon successfully logging in. + * @param string $prefix URL to redirect to upon successfully logging in. * * @return string URL */ - private function makeMagicUrl(WP_User $user, $expires, $redirect_url) + private function makeMagicUrl(WP_User $user, $expires, $redirect_url, $prefix) { static::debug("Generating a new magic login for User $user->ID expiring in {$expires} seconds."); @@ -422,7 +436,7 @@ private function makeMagicUrl(WP_User $user, $expires, $redirect_url) $this->persistMagicUrl($magic, $endpoint, $expires); - return $this->homeUrl($endpoint . '/' . $magic->getKey()); + return $this->homeUrl(trailingslashit($prefix) . $endpoint . '/' . $magic->getKey()); } /**