Skip to content

Commit

Permalink
Add --prefix flag to prepend a path prefix for magic links
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyc committed Aug 18, 2024
1 parent d88c5a7 commit da63858
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<path>`

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.
Expand All @@ -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=<path>`

[See above.](#--prefixpath)

#### `--subject=<email-subject>`

Optionally override the default email subject with your own custom string. You may use the `{{ domain }}` placeholder.
Expand Down
11 changes: 11 additions & 0 deletions features/login-create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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/
"""
22 changes: 18 additions & 4 deletions src/LoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class LoginCommand
* default:
* ---
*
* [--prefix=<path>]
* : The prefixed path added to the log-in URL.
* ---
* default:
* ---
*
* [--url-only]
* : Output the magic link URL only.
*
Expand All @@ -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);
Expand Down Expand Up @@ -108,6 +114,13 @@ public function create($_, $assoc)
* default:
* ---
*
* [--prefix=<prefix>]
* : The prefixed path added to the log-in URL.
* ---
* default:
* ---
*
*
* [--subject=<email-subject>]
* : The email subject field.
* ---
Expand All @@ -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'],
Expand Down Expand Up @@ -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.");

Expand All @@ -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());
}

/**
Expand Down

0 comments on commit da63858

Please sign in to comment.