Skip to content

Commit 17aa4b5

Browse files
committed
Add --prefix flag to prepend a path prefix for magic links
1 parent d88c5a7 commit 17aa4b5

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ Set the URL to redirect to upon successfully logging in. Defaults to `admin_url(
6262

6363
> 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.
6464
65+
66+
#### `--prefix=<path>`
67+
68+
Optionally prepend a path prefix to the magic link URL. Defaults to `/`.
69+
6570
#### `--url-only`
6671

6772
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
8691

8792
[See above.](#--redirect-urlurl)
8893

94+
#### `--prefix=<path>`
95+
96+
[See above.](#--prefix-path)
97+
8998
#### `--subject=<email-subject>`
9099

91100
Optionally override the default email subject with your own custom string. You may use the `{{ domain }}` placeholder.

features/login-create.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,14 @@ Feature: Users can generate single-use magic links that will log them in automat
185185
"""
186186
Location: http://localhost:8080/custom-redirect
187187
"""
188+
189+
@option:prefix
190+
Scenario: It can prepend a path prefix to the magic link URL
191+
Given a WP install
192+
And a PHP built-in web server
193+
And the login plugin is installed and active
194+
And I run `wp login as admin --url-only --prefix=subpath`
195+
Then STDOUT should contain:
196+
"""
197+
http://localhost:8080/subpath/
198+
"""

src/LoginCommand.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class LoginCommand
5050
* default:
5151
* ---
5252
*
53+
* [--prefix=<path>]
54+
* : The prefixed path added to the log-in URL.
55+
* ---
56+
* default:
57+
* ---
58+
*
5359
* [--url-only]
5460
* : Output the magic link URL only.
5561
*
@@ -69,7 +75,7 @@ public function create($_, $assoc)
6975

7076
$user = $this->lookupUser($user_locator);
7177
$expires = human_time_diff(time(), time() + absint($assoc['expires']));
72-
$magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url']);
78+
$magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url'], $assoc['prefix']);
7379

7480
if (WP_CLI\Utils\get_flag_value($assoc, 'url-only')) {
7581
WP_CLI::line($magic_url);
@@ -108,6 +114,13 @@ public function create($_, $assoc)
108114
* default:
109115
* ---
110116
*
117+
* [--prefix=<prefix>]
118+
* : The prefixed path added to the log-in URL.
119+
* ---
120+
* default:
121+
* ---
122+
*
123+
*
111124
* [--subject=<email-subject>]
112125
* : The email subject field.
113126
* ---
@@ -132,7 +145,7 @@ public function email($_, $assoc)
132145

133146
$user = $this->lookupUser($user_locator);
134147
$expires = human_time_diff(time(), time() + absint($assoc['expires']));
135-
$magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url']);
148+
$magic_url = $this->makeMagicUrl($user, $assoc['expires'], $assoc['redirect-url'], $assoc['prefix']);
136149
$domain = $this->domain();
137150
$subject = $this->mustacheRender(
138151
$assoc['subject'],
@@ -410,10 +423,11 @@ public function toggle($_)
410423
* @param WP_User $user User to create login URL for.
411424
* @param int $expires Number of seconds from now until the magic link expires.
412425
* @param string $redirect_url URL to redirect to upon successfully logging in.
426+
* @param string $prefix URL to redirect to upon successfully logging in.
413427
*
414428
* @return string URL
415429
*/
416-
private function makeMagicUrl(WP_User $user, $expires, $redirect_url)
430+
private function makeMagicUrl(WP_User $user, $expires, $redirect_url, $prefix)
417431
{
418432
static::debug("Generating a new magic login for User $user->ID expiring in {$expires} seconds.");
419433

@@ -422,7 +436,7 @@ private function makeMagicUrl(WP_User $user, $expires, $redirect_url)
422436

423437
$this->persistMagicUrl($magic, $endpoint, $expires);
424438

425-
return $this->homeUrl($endpoint . '/' . $magic->getKey());
439+
return $this->homeUrl(trailingslashit($prefix) . $endpoint . '/' . $magic->getKey());
426440
}
427441

428442
/**

0 commit comments

Comments
 (0)