] * : Print one field, bare, for scripts. One of: slug, source, enabled, url, redirect_mode, redirect_target, email_on_change, slug_status, pending_revert_at. * * [--format=] * : Render the output in a given format. * --- * default: table * options: * - table * - json * - yaml * - csv * --- * * ## FIELDS * * slug The login slug in force. * source Where it comes from: constant-disable, constant-override or db. * enabled Whether the custom login URL is active. * url The login URL that works now. * redirect_mode What logged-out visitors of wp-login.php and wp-admin get: 404, home or custom. * redirect_target The redirect slug (custom mode only). * email_on_change Whether a change emails the admin. * slug_status The validator's verdict on the slug: ok, or warn/block with the reason. * pending_revert_at When an unconfirmed settings-screen change will be reverted (ISO 8601, UTC), or empty. * * ## EXAMPLES * * # Show everything. * $ wp ao login-url get * * # Use the URL in a deployment script. * $ wp ao login-url get --field=url * * # JSON for tooling. * $ wp ao login-url get --format=json * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function get( $args, $assoc_args ) { $info = ( new Custom_Login_Url_Service() )->get_info(); $json = in_array( $assoc_args['format'] ?? 'table', [ 'json', 'yaml' ], true ); // Booleans print as true/false in tables and CSV, and stay real booleans in JSON and YAML. if ( ! $json ) { foreach ( [ 'enabled', 'email_on_change' ] as $key ) { $info[ $key ] = $info[ $key ] ? 'true' : 'false'; } } $formatter = new \WP_CLI\Formatter( $assoc_args, array_keys( $info ) ); $formatter->display_item( $info ); } /** * Set the login slug. * * Uses the same rules as the settings screen: the slug is lowercased and * trimmed, and reserved or colliding slugs are always refused. A slug that * is merely easy to guess is refused unless you pass --force. A change made here * is never put on probation: it clears any pending settings-screen safe-save. * * ## OPTIONS * * * : The new login slug, for example my-new-login. * * [--force] * : Accept a slug the validator only warns about (too common, very short, numeric). Never bypasses a reserved or colliding slug. * * [--notify] * : Email the new login URL to the site admin email. Only sent when the slug really changed. * * ## EXAMPLES * * $ wp ao login-url set river-lamp-ocean * Success: Login URL updated. * * # Use an easy-to-guess slug on purpose, and email the admin. * $ wp ao login-url set dashboard --force --notify * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function set( $args, $assoc_args ) { $this->report( ( new Custom_Login_Url_Service() )->set_slug( (string) $args[0], \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ), \WP_CLI\Utils\get_flag_value( $assoc_args, 'notify', false ) ) ); } /** * Generate random three-word login slugs. * * Words come from a curated list and are picked with random_int(). Every * candidate passes the validator. Without --set nothing is saved. With --set it * also clears any pending settings-screen safe-save. * * ## OPTIONS * * [--set] * : Save one generated slug, the same way "set" does. * * [--count=] * : How many candidates to print (1-50, default 1). Not allowed with --set. * * [--force] * : With --set: passed to "set". * * [--notify] * : With --set: email the new login URL to the site admin email. * * ## EXAMPLES * * # Print three ideas. * $ wp ao login-url generate --count=3 * * # Generate and apply one. * $ wp ao login-url generate --set * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function generate( $args, $assoc_args ) { $service = new Custom_Login_Url_Service(); $set = \WP_CLI\Utils\get_flag_value( $assoc_args, 'set', false ); if ( $set && isset( $assoc_args['count'] ) ) { \WP_CLI::error( __( '--count cannot be used with --set.', 'admin-optimizer' ) ); } if ( ! $set && ( isset( $assoc_args['force'] ) || isset( $assoc_args['notify'] ) ) ) { \WP_CLI::error( __( '--force and --notify only apply together with --set.', 'admin-optimizer' ) ); } if ( $set ) { $lock = $service->get_lock( 'slug' ); if ( '' !== $lock ) { \WP_CLI::error( $lock ); } } $generated = $service->generate_candidates( $set ? 1 : (int) ( $assoc_args['count'] ?? 1 ) ); if ( ! $generated['ok'] ) { \WP_CLI::error( $generated['message'] ); } if ( ! $set ) { foreach ( $generated['slugs'] as $slug ) { \WP_CLI::line( $slug ); } return; } $this->report( $service->set_slug( $generated['slugs'][0], \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ), \WP_CLI\Utils\get_flag_value( $assoc_args, 'notify', false ) ) ); } /** * Remove the custom login URL and restore the default login. For lockouts. * * Clears the stored slug, so wp-login.php and /wp-admin behave normally * again. The module stays enabled and your other settings are kept. If the * lockout comes from a wp-config.php constant, remove or change the constant * instead: this command refuses to run while one controls the login URL. It * also clears any pending settings-screen safe-save. * * ## OPTIONS * * [--notify] * : Email the site admin that the default login is back. Only sent when something was cleared. * * ## EXAMPLES * * $ wp ao login-url reset * Success: Custom login URL cleared. The default wp-login.php and /wp-admin work again. * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function reset( $args, $assoc_args ) { $this->report( ( new Custom_Login_Url_Service() )->reset( \WP_CLI\Utils\get_flag_value( $assoc_args, 'notify', false ) ) ); } /** * Turn the Custom Login URL module on. * * Your stored settings are kept. Clears any pending settings-screen safe-save. * * ## EXAMPLES * * $ wp ao login-url enable * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function enable( $args, $assoc_args ) { $this->report( ( new Custom_Login_Url_Service() )->set_module_enabled( true ) ); } /** * Turn the Custom Login URL module off. * * The default login works again; your stored settings are kept. Clears any * pending settings-screen safe-save. * * ## EXAMPLES * * $ wp ao login-url disable * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function disable( $args, $assoc_args ) { $this->report( ( new Custom_Login_Url_Service() )->set_module_enabled( false ) ); } /** * Choose what logged-out visitors of wp-login.php and wp-admin get. * * ## OPTIONS * * * : 404 (the theme's 404 page, no redirect), home (redirect to the home page) or custom (redirect to ). * --- * options: * - '404' * - home * - custom * --- * * [] * : The redirect slug. Required for custom, not allowed for the other modes. * * ## EXAMPLES * * $ wp ao login-url redirect 404 * $ wp ao login-url redirect home * $ wp ao login-url redirect custom welcome * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ public function redirect( $args, $assoc_args ) { $this->report( ( new Custom_Login_Url_Service() )->set_redirect( (string) $args[0], isset( $args[1] ) ? (string) $args[1] : null ) ); } /** * Print a service result: warnings, then success, or an error (exit 1). * * @param array $result Service result. * * @return void */ private function report( array $result ): void { if ( ! $result['ok'] ) { \WP_CLI::error( $result['message'] ); } foreach ( $result['warnings'] as $warning ) { \WP_CLI::warning( $warning ); } \WP_CLI::success( $result['message'] ); } } // Register `wp ao` once, then this module's subcommand under it. $adminoptim_cli_path = [ 'ao' ]; if ( ! \WP_CLI::get_root_command()->find_subcommand( $adminoptim_cli_path ) ) { \WP_CLI::add_command( 'ao', AO_CLI_Namespace::class ); } \WP_CLI::add_command( 'ao login-url', Custom_Login_Url_CLI::class );