1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Subway WordPress Plugin Package. |
4
|
|
|
* |
5
|
|
|
* (c) Joseph Gabito <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* PHP Version 5.4 |
11
|
|
|
* |
12
|
|
|
* @category Subway\Auth\Redirect |
13
|
|
|
* @package Subway |
14
|
|
|
* @author Joseph G. <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
16
|
|
|
* @version GIT:github.com/codehaiku/subway |
17
|
|
|
* @link github.com/codehaiku/subway The Plugin Repository |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Subway; |
21
|
|
|
|
22
|
|
|
if (! defined('ABSPATH') ) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Registers all the admin settings inside Settings > Subway |
28
|
|
|
* |
29
|
|
|
* @category Subway\Auth\Redirect |
30
|
|
|
* @package Subway |
31
|
|
|
* @author Joseph G. <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
33
|
|
|
* @link github.com/codehaiku/subway The Plugin Repository |
34
|
|
|
* @since 1.0 |
35
|
|
|
*/ |
36
|
|
|
final class AuthRedirect |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Handles our ajax authentication. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public static function handleAuthentication() |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// Set the header type to json. |
48
|
|
|
header('Content-Type: application/json'); |
49
|
|
|
|
50
|
|
|
$is_signin = new \WP_Error( 'broke', __( 'Unable to authenticate user', "subway" ) ); |
51
|
|
|
|
52
|
|
|
$log = filter_input(INPUT_POST, 'log', FILTER_SANITIZE_STRING); |
53
|
|
|
|
54
|
|
|
$pwd = filter_input(INPUT_POST, 'pwd', FILTER_SANITIZE_STRING); |
55
|
|
|
|
56
|
|
|
if (empty($log) && empty($pwd) ) { |
57
|
|
|
|
58
|
|
|
$response['type'] = 'error'; |
|
|
|
|
59
|
|
|
|
60
|
|
|
$response['message'] = esc_html__( |
61
|
|
|
'Username and Password cannot be empty.', |
62
|
|
|
'subway' |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
} else { |
66
|
|
|
|
67
|
|
|
$is_signin = wp_signon(); |
68
|
|
|
|
69
|
|
|
$response = array(); |
70
|
|
|
|
71
|
|
|
if (is_wp_error($is_signin) ) { |
72
|
|
|
|
73
|
|
|
$response['type'] = 'error'; |
74
|
|
|
|
75
|
|
|
$response['message'] = $is_signin->get_error_message(); |
76
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
|
79
|
|
|
$response['type'] = 'success'; |
80
|
|
|
|
81
|
|
|
$response['message'] = esc_html__( |
82
|
|
|
'You have successfully logged-in. Redirecting you in few seconds...', |
83
|
|
|
'subway' |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$subway_redirect_url = AuthRedirect::getLoginRedirectUrl('', $is_signin); |
90
|
|
|
|
91
|
|
|
$response['redirect_url'] = apply_filters( |
92
|
|
|
'subway_login_redirect', |
93
|
|
|
$subway_redirect_url, |
94
|
|
|
$is_signin |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
echo wp_json_encode($response); |
98
|
|
|
|
99
|
|
|
wp_die(); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the filtered redirect url for the current user. |
105
|
|
|
* |
106
|
|
|
* @param string $redirect_to The default redirect callback argument. |
107
|
|
|
* @param mixed $user This holds the meta info of currently logged-in user. |
108
|
|
|
* |
109
|
|
|
* @return string The final redirect url. |
110
|
|
|
*/ |
111
|
|
|
public static function getLoginRedirectUrl( $redirect_to, $user ) |
112
|
|
|
{ |
113
|
|
|
|
114
|
|
|
$subway_redirect_type = get_option('subway_redirect_type'); |
115
|
|
|
|
116
|
|
|
// Redirect the user to default behaviour. |
117
|
|
|
// If there are no redirect type option saved. |
118
|
|
|
if (empty($subway_redirect_type) ) { |
119
|
|
|
|
120
|
|
|
return $redirect_to; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ('default' === $subway_redirect_type ) { |
125
|
|
|
return $redirect_to; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ('page' === $subway_redirect_type ) { |
129
|
|
|
|
130
|
|
|
// Get the page url of the selected page. |
131
|
|
|
// If the admin selected 'Custom Page' in the redirect type settings. |
132
|
|
|
$selected_redirect_page = intval(get_option('subway_redirect_page_id')); |
133
|
|
|
|
134
|
|
|
// Redirect to default WordPress behaviour. |
135
|
|
|
// If the user did not select page. |
136
|
|
|
if (empty($selected_redirect_page) ) { |
137
|
|
|
|
138
|
|
|
return $redirect_to; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Otherwise, get the permalink of the saved page |
142
|
|
|
// and let the user go into that page. |
143
|
|
|
return get_permalink($selected_redirect_page); |
144
|
|
|
|
145
|
|
|
} elseif ('custom_url' === $subway_redirect_type ) { |
146
|
|
|
|
147
|
|
|
// Get the custom url saved in the redirect type settings. |
148
|
|
|
$entered_custom_url = get_option('subway_redirect_custom_url'); |
149
|
|
|
|
150
|
|
|
// Redirect to default WordPress behaviour |
151
|
|
|
// if the user did enter a custom url. |
152
|
|
|
if (empty($entered_custom_url) ) { |
153
|
|
|
|
154
|
|
|
return $redirect_to; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Otherwise, get the custom url saved |
159
|
|
|
// and let the user go into that page. |
160
|
|
|
if (! empty($user->ID) ) { |
161
|
|
|
$entered_custom_url = str_replace( |
162
|
|
|
'%user_id%', $user->ID, |
163
|
|
|
$entered_custom_url |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (! empty($user->user_login) ) { |
168
|
|
|
$entered_custom_url = str_replace( |
169
|
|
|
'%user_name%', $user->user_login, |
170
|
|
|
$entered_custom_url |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $entered_custom_url; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Otherwise, quit and redirect the user back to default WordPress behaviour. |
179
|
|
|
return $redirect_to; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Callback function for the 'login_url' filter defined in Subway.php |
184
|
|
|
* |
185
|
|
|
* @param string $login_url The login url. |
186
|
|
|
* |
187
|
|
|
* @return string The final login url. |
188
|
|
|
*/ |
189
|
|
|
public static function loginUrl( $login_url ) |
190
|
|
|
{ |
191
|
|
|
|
192
|
|
|
$subway_login_page = Options::getRedirectPageUrl(); |
193
|
|
|
|
194
|
|
|
// Return the default login url if there is no log-in page defined. |
195
|
|
|
if (empty($subway_login_page) ) { |
196
|
|
|
return $login_url; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Otherwise, return the Subway login page. |
200
|
|
|
return $subway_login_page; |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The callback function for our logout filter. |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public static function logoutUrl() |
210
|
|
|
{ |
211
|
|
|
|
212
|
|
|
$subway_login_page = Options::getRedirectPageUrl(); |
213
|
|
|
|
214
|
|
|
wp_safe_redirect(esc_url($subway_login_page . '?loggedout=true')); |
215
|
|
|
|
216
|
|
|
Helpers::close(); |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.