Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CLI
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_autologin_to_url
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 send_magic_link
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * CLI commands for:
4 * * getting a URL with an autologin code in it
5 * * sending the magic link email to a user
6 *
7 * Someone else has written a WP CLI package which provides the same function without needing to be installed as a site plugin.
8 *
9 * @see https://github.com/aaemnnosttv/wp-cli-login-command
10 *
11 * @package brianhenryie/bh-wp-autologin-urls
12 */
13
14namespace BrianHenryIE\WP_Autologin_URLs\WP_Includes;
15
16use BrianHenryIE\WP_Autologin_URLs\API_Interface;
17use WP_CLI;
18
19/**
20 * WP CLI sanitizes some input, we sanitize a little more, then call the API functions.
21 */
22class CLI {
23
24    /**
25     * The CLI class is really a small wrapper on the API class.
26     */
27    protected API_Interface $api;
28
29    /**
30     * Constructor.
31     *
32     * @param API_Interface $api The main plugin functions implemented.
33     */
34    public function __construct( API_Interface $api ) {
35        $this->api = $api;
36    }
37
38    /**
39     * Append an autologin code to a URL.
40     *
41     * ## OPTIONS
42     *
43     * <user>
44     * : User id, username/login, or email address.
45     *
46     * [<url>]
47     * : The URL to append to.
48     * ---
49     * default: /
50     * ---
51     *
52     * [<expires_in>]
53     * : Number of seconds the code should be valid. Default WEEK_IN_SECONDS.
54     *
55     * ## EXAMPLES
56     *
57     *   # Add an autologin code to the site root for brianhenryie which expires in one week.
58     *   $ wp autologin-urls get-url brianhenryie
59     *
60     *   # Add an autologin code to the URL /my-account for brianhenryie which expires in five minutes
61     *   $ wp autologin-urls get-url brianhenryie my-account 300
62     *
63     * @see API_Interface::add_autologin_to_url()
64     *
65     * @param string[]             $args The unlabelled command line arguments.
66     * @param array<string,string> $assoc_args The labelled command line arguments.
67     */
68    public function add_autologin_to_url( array $args, array $assoc_args ): void {
69
70        $user = $args[0];
71        $url  = $args[1];
72
73        $parsed_url = wp_parse_url( $url );
74
75        $full_url = get_site_url();
76        if ( isset( $parsed_url['path'] ) ) {
77            $full_url .= '/' . $parsed_url['path'];
78        }
79        $full_url = rtrim( $full_url, '/' ) . '/';
80        if ( isset( $parsed_url['query'] ) ) {
81            $full_url .= '?' . $parsed_url['query'];
82        }
83        if ( isset( $parsed_url['fragment'] ) ) {
84            $full_url .= '#' . $parsed_url['fragment'];
85        }
86
87        $expires_in = isset( $args[2] ) ? intval( $args[2] ) : WEEK_IN_SECONDS;
88
89        $result = $this->api->add_autologin_to_url( $full_url, $user, $expires_in );
90
91        WP_CLI::success( $result );
92    }
93
94    /**
95     * Send a magic login email to a user.
96     *
97     * ## OPTIONS
98     *
99     * <user>
100     * : User id, username/login, or email address.
101     *
102     * <url>
103     * : The URL the link in the email should go to.
104     * ---
105     * default: /
106     * ---
107     *
108     * [<expires_in>]
109     * : Number of seconds the code should be valid.
110     * ---
111     * default: 900
112     * ---
113     *
114     * ## EXAMPLES
115     *
116     *   # Send a magic login link to user brianhenryie, if they exist.
117     *   $ wp autologin-urls send-magic-link brianhenryie
118     *
119     * @see API_Interface::send_magic_link()
120     *
121     * @param string[]             $args The unlabelled command line arguments.
122     * @param array<string,string> $assoc_args The labelled command line arguments.
123     */
124    public function send_magic_link( array $args, array $assoc_args ): void {
125
126        $user       = $args[0];
127        $url        = $args[1];
128        $expires_in = intval( $args[2] );
129
130        $result = $this->api->send_magic_link( $user, $url, $expires_in );
131
132        if ( $result['success'] && isset( $result['wp_user'] ) ) {
133            WP_CLI::success( 'Magic login link sent to user ' . $result['wp_user']->user_login . ' at email ' . $result['wp_user']->user_email );
134        } else {
135            WP_CLI::error( $result['message'] ?? 'Failed to send magic login link.' );
136        }
137    }
138}