|
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
|
|
|
* @package Subway |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Subway; |
|
14
|
|
|
|
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
16
|
|
|
return; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Subway\Auth_Redirect handles our redirection after logging in. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Auth_Redirect { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Handles our ajax authentication. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function handle_authentication() { |
|
32
|
|
|
|
|
33
|
|
|
// Set the header type to json. |
|
34
|
|
|
header( 'Content-Type: application/json' ); |
|
35
|
|
|
|
|
36
|
|
|
$log = filter_input( INPUT_POST, 'log', FILTER_SANITIZE_STRING ); |
|
37
|
|
|
|
|
38
|
|
|
$pwd = filter_input( INPUT_POST, 'pwd', FILTER_SANITIZE_STRING ); |
|
39
|
|
|
|
|
40
|
|
|
if ( empty( $log ) && empty( $pwd ) ) { |
|
41
|
|
|
|
|
42
|
|
|
$response['type'] = 'error'; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$response['message'] = esc_html__( 'Username and Password cannot be empty.', 'subway' ); |
|
45
|
|
|
|
|
46
|
|
|
} else { |
|
47
|
|
|
|
|
48
|
|
|
$is_signin = wp_signon(); |
|
49
|
|
|
|
|
50
|
|
|
$response = array(); |
|
51
|
|
|
|
|
52
|
|
|
if ( is_wp_error( $is_signin ) ) { |
|
53
|
|
|
|
|
54
|
|
|
$response['type'] = 'error'; |
|
55
|
|
|
|
|
56
|
|
|
$response['message'] = $is_signin->get_error_message(); |
|
57
|
|
|
|
|
58
|
|
|
} else { |
|
59
|
|
|
|
|
60
|
|
|
$response['type'] = 'success'; |
|
61
|
|
|
|
|
62
|
|
|
$response['message'] = esc_html__( 'You have successfully logged-in. Redirecting you in few seconds...' ); |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$subway_redirect_url = Auth_Redirect::get_login_redirect_url( $redirect_to = '', $request = '', $user = 1 ); |
|
68
|
|
|
|
|
69
|
|
|
$response['redirect_url'] = apply_filters( 'subway_login_redirect', $subway_redirect_url ); |
|
70
|
|
|
|
|
71
|
|
|
echo wp_json_encode( $response ); |
|
72
|
|
|
|
|
73
|
|
|
wp_die(); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the filtered redirect url for the current user. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $redirect_to The default redirect callback argument. |
|
81
|
|
|
* @param string $request The default redirect request callback argument. |
|
82
|
|
|
* @param integer $user The current user logging in. |
|
83
|
|
|
* @return string The final redirect url. |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function get_login_redirect_url( $redirect_to, $request, $user ) { |
|
86
|
|
|
|
|
87
|
|
|
$subway_redirect_type = get_option( 'subway_redirect_type' ); |
|
88
|
|
|
|
|
89
|
|
|
// Redirect the user to default behaviour if there are no redirect type option saved. |
|
90
|
|
|
if ( empty( $subway_redirect_type ) ) { |
|
91
|
|
|
|
|
92
|
|
|
return $redirect_to; |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ( 'default' === $subway_redirect_type ) { |
|
97
|
|
|
return $redirect_to; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( 'page' === $subway_redirect_type ) { |
|
101
|
|
|
|
|
102
|
|
|
// Get the page url of the selected page if the admin selected 'Custom Page' in the redirect type settings. |
|
103
|
|
|
$selected_redirect_page = intval( get_option( 'subway_redirect_page_id' ) ); |
|
104
|
|
|
|
|
105
|
|
|
// Redirect to default WordPress behaviour if the user did not select page. |
|
106
|
|
|
if ( empty( $selected_redirect_page ) ) { |
|
107
|
|
|
|
|
108
|
|
|
return $redirect_to; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Otherwise, get the permalink of the saved page and let the user go into that page. |
|
112
|
|
|
return get_permalink( $selected_redirect_page ); |
|
113
|
|
|
|
|
114
|
|
|
} elseif ( 'custom_url' === $subway_redirect_type ) { |
|
115
|
|
|
|
|
116
|
|
|
// Get the custom url saved in the redirect type settings. |
|
117
|
|
|
$entered_custom_url = get_option( 'subway_redirect_custom_url' ); |
|
118
|
|
|
|
|
119
|
|
|
// Redirect to default WordPress behaviour if the user did enter a custom url. |
|
120
|
|
|
if ( empty( $entered_custom_url ) ) { |
|
121
|
|
|
|
|
122
|
|
|
return $redirect_to; |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Otherwise, get the custom url saved and let the user go into that page. |
|
127
|
|
|
$current_user = wp_get_current_user(); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
if ( ! empty( $user->ID ) ) { |
|
130
|
|
|
$entered_custom_url = str_replace( '%user_id%', $user->ID, $entered_custom_url ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ( ! empty( $user->user_login ) ) { |
|
134
|
|
|
$entered_custom_url = str_replace( '%user_name%', $user->user_login, $entered_custom_url ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $entered_custom_url; |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// Otherwise, quit and redirect the user back to default WordPress behaviour. |
|
142
|
|
|
return $redirect_to; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Callback function for the 'login_url' filter defined in Subway.php |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $login_url The login url. |
|
149
|
|
|
* @return string The final login url. |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function login_url( $login_url ) { |
|
152
|
|
|
|
|
153
|
|
|
$subway_login_page = Options::get_redirect_page_url(); |
|
154
|
|
|
|
|
155
|
|
|
// Return the default login url if there is no log-in page defined. |
|
156
|
|
|
if ( empty( $subway_login_page ) ) { |
|
157
|
|
|
return $login_url; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// Otherwise, return the Subway login page. |
|
161
|
|
|
return $subway_login_page; |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* The callback function for our logout filter. |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function logout_url() { |
|
171
|
|
|
|
|
172
|
|
|
$subway_login_page = Options::get_redirect_page_url(); |
|
173
|
|
|
|
|
174
|
|
|
wp_safe_redirect( esc_url( $subway_login_page . '?loggedout=true' ) ); |
|
175
|
|
|
|
|
176
|
|
|
exit; |
|
177
|
|
|
|
|
178
|
|
|
return; |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.