/
CRG_Login.php
252 lines (219 loc) · 8.35 KB
/
CRG_Login.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/*
Plugin Name: CRG Login
Plugin URI: http://customrayguns.com
Description: The BEST way to login register your users.
Version: 3.0
Author: Custom Ray Guns
Author URI: http://customrayguns.com
*/
//die('crg-login');
if (isset($_POST['crg_login_email'])){$crg_login_email = $_POST['crg_login_email'];}
//Detects if on the login page, then replaces Username w/ Email
if($_SERVER["PHP_SELF"]=='/wp-login.php'){
ob_start('kill_wp_attempt_focus');
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
}
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text === 'Username') {
$translated_text = 'Email';
}
return $translated_text;
}
function kill_wp_attempt_focus($in){
return preg_replace('/function wp_attempt_focus/','function wp_attempt_focus(){} function wp_attempt_focus_killed',$in);
}
//session_start();
if ((isset($_SESSION['crg_login_redirect_url'])) and (isset($_SESSION['crg_login_email'])) ) {
if($pagenow==='wp-login.php'){
add_action ('shutdown','crg_login_fill');}
}
function crg_login_fill(){
echo ("
<script>
jQuery(document).ready(function() {
//jQuery(\"label[for='user_login']\").html(\"E-Mailo<br /><input type = 'text' id = 'user_login' name = 'user_login' />\");
jQuery('#user_login').val('" . $_SESSION['crg_login_email'] . "');
jQuery('#user_pass').val('');
jQuery('#user_pass').focus();
});
</script>
");
}
add_action('init', 'crg_redirect');
function crg_redirect(){
if(is_user_logged_in()){
if (isset($_SESSION['crg_login_redirect_url'])){
$url = $_SESSION['crg_login_redirect_url'];
unset($_SESSION['crg_login_redirect_url']);
wp_redirect($url);
} } }
//jQuery is required for this plugin:
//if (wp_script_is('jquery','enqueued')){return;}else{wp_enqueue_script('jquery');}
//launches crg_login logic:
add_action('setup_theme', 'launch_crg_login');
function launch_crg_login(){$crg_login_controller = new crg_login_controller;}
//launches the sidebar widget:
add_action( 'widgets_init', function(){register_widget( 'crg_login_widget' );});
//The login controller controls the main functionality of the plugin.
class crg_login_controller{
function __construct(){
global $wpdb;
if (isset($_POST['crg_login_email'])){$crg_login_email = $_POST['crg_login_email'];}
//the user has entered an email address, but no password is entered:
if( ((isset($_POST['crg_login_email'])) && (!( isset($_POST['crg_login_password']) ) ) )){
if (email_exists($crg_login_email)){
//The user has entered an existing email
$login_link = wp_login_url(get_permalink());
$url = getUrl($_SERVER);
$_SESSION['crg_login_email'] = $_POST['crg_login_email'];
if (!(isset($_SESSION['crg_login_redirect_url']))){$_SESSION['crg_login_redirect_url'] = $_SERVER['HTTP_REFERER'];}
wp_redirect($login_link);
die();
//the user has entered an email address, that is NOT in the database:
}else{
$password = wp_generate_password();
$user_id = wp_create_user( $crg_login_email, $password, $crg_login_email );
$user = new WP_User( $user_id );
wp_set_auth_cookie( $user->ID, TRUE );
//either 'administrator', 'subscriber', 'editor', 'author', 'contributor':
$user->set_role( 'subscriber' );
$first_name = $_POST['crg_login_first_name'];
$last_name = $_POST['crg_login_last_name'];
$user_id = wp_update_user( array( 'ID' => $user_id, 'first_name' => $first_name, 'last_name' => $last_name ) );
$creds = array();
$creds['user_login'] = $crg_login_email;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, true );
$user = get_user_by( 'email', $crg_login_email );
$x = $user->ID;
$xyz = crg_trim_email($crg_login_email);
update_user_meta( $x, 'nickname', $xyz);
$wpdb->query("UPDATE $wpdb->users SET display_name = '$xyz' WHERE ID = $x");
if (isset($_SESSION['crg_login_redirect_url'])){
$crg_login_redirect_url = $_SESSION['crg_login_redirect_url'];
wp_redirect($crg_login_redirect_url);
die();
}
//gets the current url:
$url = getUrl($_SERVER);
wp_redirect($url);
die();
}
}
//error condition:
if( ((isset($_POST['crg_login_email'])) && ( isset($_POST['crg_login_password']) ) )){
die("ERROR:both here");
} } }
//This function returns the current URL:
function url_origin($s, $use_forwarded_host=false){
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = ($use_forwarded_host && isset($s['HTTP_X_FORWARDED_HOST'])) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function getUrl($s, $use_forwarded_host=false){
return url_origin($s, $use_forwarded_host) . $s['REQUEST_URI'];
}
//This function returns the first part of an email. i.e. "jiminac@gmail.com" is trimmed to "jiminac" :
function crg_trim_email($email){
$trimmed = '';
$arr1 = str_split($email);
foreach ($arr1 as $l){
if ($l == "@"){break;}
$trimmed = $trimmed . $l;
}
return $trimmed;
}
//crg_login_widget is the sidebar widget for this plugin:
class crg_login_widget extends WP_Widget {
function __construct() {
parent::__construct(
'crg_login_widget', // Base ID
__('CRG Login', 'crg_text_domain'), // Name
array( 'description' => __( 'The best way to login users.', 'crg_text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
* @see WP_Widget::widget()
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
echo $args['before_title'];
if ( is_user_logged_in() ) {
//The user is NOT logged in:
$user_ID = get_current_user_id();
$key = 'nickname';
$single = true;
$user_nickname = get_user_meta( $user_ID, $key, $single );
_e( 'Welcome, ', 'crg_text_domain' );
echo ("<a href = '/wp-admin/profile.php' >" . $user_nickname . "</a>!");
echo $args['after_title'];
//The user IS logged in:
}else{
_e( 'Sign up or login:', 'crg_text_domain' );
echo $args['after_title'];
echo "<div id = 'crg_js_disabled'>";
_e('JavaScript disabled on this browser.');
echo "<br /><a href = '";
echo site_url();
echo "/wp-login.php'>";
_e('Click here to login.','crg_text_domain');
echo "</a></div><div id = 'crg_login_widget_form' style = 'display:none;'><form method = 'post' id = 'crg_email_form' ><input type = 'text' name = 'crg_login_email' id = 'crg_email' placeholder = '";
_e('Enter your email here.', 'crg_text_domain');
echo "' /><br /><input type = 'submit' value = '";
_e("Submit", 'crg_text_domain');
echo "' name = 'crg_login_submit_button' id = 'crg_login_submit_button'/></form></div>";
}
echo $args['after_widget'];
$jquery_output =
<<<JQUERY_OUTPUT_START_STOP
<script>
jQuery('document').ready(function() {
jQuery('#crg_js_disabled').hide();
jQuery('#crg_login_widget_form').show();
});
</script>
JQUERY_OUTPUT_START_STOP;
echo $jquery_output;
}
/**
* Back-end widget form.
* @see WP_Widget::form()
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}else {
$title = __( 'New title', 'crg_text_domain' );
}
?>
<p>
<label for="crg_login_social_login">Use Social Logins?</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
* @see WP_Widget::update()
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}