forked from pluginpizza/automatic-login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
121 lines (100 loc) · 3.29 KB
/
plugin.php
File metadata and controls
121 lines (100 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Plugin Name: Automatic Login
* Description: Log in automatically on your locally hosted WordPress install.
* Version: 1.0.0
* Author: Functions File, Barry Ceelen
* Author URI: https://github.com/functionsfile
* Plugin URI: https://github.com/functionsfile/automatic-login
* License: GPLv3+
* GitHub Plugin URI: functionsfile/automatic-login
*
* @package AutomaticLogin
*/
namespace AutomaticLogin;
// Maybe login automatically.
add_action( 'init', __NAMESPACE__ . '\maybe_login' );
// Maybe check the "Remember Me" checkbox on the login form.
add_action( 'login_footer', __NAMESPACE__ . '\maybe_check_rememberme' );
// Maybe remember me for 399 days.
add_action( 'auth_cookie_expiration', __NAMESPACE__ . '\maybe_set_very_long_auth_cookie_expiration', 10, 3 );
/**
* Maybe login automatically.
*
* Log in automatically when visiting a WordPress admin URL if the following
* conditions are met:
*
* - The 'WP_ENVIRONMENT_TYPE' constant is defined as 'local'.
* - A user exists with the password 'password' and the username is either
* 'admin' or 'wpsnapshots'.
*
* Alternatively, the `AUTOMATIC_LOGIN_USER_LOGIN` and `AUTOMATIC_LOGIN_USER_PASSWORD`
* constants can be used to define the login credenitals for Automatic Login.
*
* define( 'AUTOMATIC_LOGIN_USER_NAME', 'mycoolusername' );
* define( 'AUTOMATIC_LOGIN_USER_PASSWORD', 'mycoolpassword' );
*
* Note that Automatic Login does not create a user for you, the user should
* already exist.
*
* @return void
*/
function maybe_login() {
if ( ! is_admin() || is_user_logged_in() ) {
return;
}
if ( ! function_exists( 'wp_get_environment_type' ) || 'local' !== wp_get_environment_type() ) {
return;
}
$user_logins = [ 'admin', 'wpsnapshots' ];
if ( defined( 'AUTOMATIC_LOGIN_USER_LOGIN' ) && is_string( AUTOMATIC_LOGIN_USER_LOGIN ) ) {
array_unshift( $user_logins, AUTOMATIC_LOGIN_USER_LOGIN );
}
foreach ( $user_logins as $user_login ) {
$user_password = 'password';
if ( defined( 'AUTOMATIC_LOGIN_USER_PASSWORD' ) && is_string( AUTOMATIC_LOGIN_USER_PASSWORD ) ) {
$user_password = AUTOMATIC_LOGIN_USER_PASSWORD;
}
$user = wp_signon(
array(
'user_login' => $user_login,
'user_password' => $user_password,
'remember' => true,
)
);
if ( ! is_wp_error( $user ) && isset( $_SERVER['REQUEST_URI'] ) ) {
wp_safe_redirect( $_SERVER['REQUEST_URI'] );
exit();
}
}
}
/**
* Maybe check the "Remember Me" checkbox on the login form.
*
* @return void
*/
function maybe_check_rememberme() {
if ( ! function_exists( 'wp_get_environment_type' ) || 'local' !== wp_get_environment_type() ) {
return;
}
echo '<script>if(document.getElementById("rememberme")){document.getElementById("rememberme").checked=true;}</script>';
}
/**
* Maybe remember me for 399 days.
*
* @see https://chromestatus.com/feature/4887741241229312
*
* @param int $length Duration of the expiration period in seconds.
* @param int $user_id User ID.
* @param bool $remember Whether to remember the user login. Default false.
* @return int
*/
function maybe_set_very_long_auth_cookie_expiration( $length, $user_id, $remember ) {
if ( ! function_exists( 'wp_get_environment_type' ) || 'local' !== wp_get_environment_type() ) {
return;
}
if ( $remember ) {
$length = 399 * DAY_IN_SECONDS;
}
return $length;
}