| 1 |
<?php |
| 2 |
namespace dologin; |
| 3 |
|
| 4 |
defined( 'WPINC' ) || exit; |
| 5 |
|
| 6 |
use WP_CLI; |
| 7 |
|
| 8 |
/** |
| 9 |
* Passwordless API CLI |
| 10 |
*/ |
| 11 |
class CLI extends Instance { |
| 12 |
public function __construct() { |
| 13 |
defined( 'debug' ) && debug( 'CLI init' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* List all passwordless links |
| 18 |
* |
| 19 |
* ## OPTIONS |
| 20 |
* |
| 21 |
* ## EXAMPLES |
| 22 |
* |
| 23 |
* # List all passwordless link |
| 24 |
* $ wp dologin list |
| 25 |
*/ |
| 26 |
public function list() { |
| 27 |
$list = $this->cls( 'Admin' )->pswdless_log(); |
| 28 |
foreach ( $list as $k => $v ) { |
| 29 |
$list[ $k ] = (array) $v; |
| 30 |
$user = get_user_by( 'id', $v->user_id ); |
| 31 |
$list[ $k ]['Login_Name'] = $user ? $user->user_login : 'N/A'; |
| 32 |
$list[ $k ]['Expiration'] = $v->expired_at > time() ? Util::readable_time( $v->expired_at - time(), 3600, false ) : __( 'Expired', 'dologin' ); |
| 33 |
$list[ $k ]['Created_At'] = Util::readable_time( $v->dateline ); |
| 34 |
} |
| 35 |
if ( $list ) { |
| 36 |
WP_CLI\Utils\format_items( 'table', $list, array( 'id', 'Login_Name', 'Expiration', 'Created_At', 'onetime', 'active', 'count' ) ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generate a passwordless link for one username |
| 42 |
* |
| 43 |
* ## OPTIONS |
| 44 |
* |
| 45 |
* ## EXAMPLES |
| 46 |
* |
| 47 |
* # Generate a passwordless link for one username |
| 48 |
* $ wp dologin gen root |
| 49 |
*/ |
| 50 |
public function gen( $args ) { |
| 51 |
$uname = $args[0]; |
| 52 |
$user = get_user_by( 'login', $uname ); |
| 53 |
if ( ! $user ) { |
| 54 |
WP_CLI::error( __( 'No related user.', 'dologin' ) ); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$link = $this->cls( 'Pswdless' )->gen_link( 'CLI-' . $user->display_name, $user->ID, true ); |
| 59 |
WP_CLI::success( 'Link generated: ' . $link ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Delete a passwordless link w/ the ID in list |
| 64 |
* |
| 65 |
* ## OPTIONS |
| 66 |
* |
| 67 |
* ## EXAMPLES |
| 68 |
* |
| 69 |
* # Delete the passwordless link (ID is 5) |
| 70 |
* $ wp dologin del 5 |
| 71 |
*/ |
| 72 |
public function del( $args ) { |
| 73 |
$id = $args[0]; |
| 74 |
if ( ! $id ) { |
| 75 |
WP_CLI::error( __( 'No ID to delete.', 'dologin' ) ); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$this->cls( 'Pswdless' )->del_link( $id ); |
| 80 |
WP_CLI::success( 'Delete passwordless link successfully. ID = ' . $id ); |
| 81 |
} |
| 82 |
} |
| 83 |
|