Passed
Pull Request — master (#8)
by Joseph
04:07
created

AuthRedirect::handleAuthentication()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 55
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 25
nc 3
nop 0
dl 0
loc 55
rs 9.078
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $log = filter_input(INPUT_POST, 'log', FILTER_SANITIZE_STRING);
51
52
        $pwd = filter_input(INPUT_POST, 'pwd', FILTER_SANITIZE_STRING);
53
54
        if (empty($log) && empty($pwd) ) {
55
56
            $response['type'] = 'error';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
57
58
            $response['message'] = esc_html__(
59
                'Username and Password cannot be empty.', 
60
                'subway'
61
            );
62
63
        } else {
64
65
            $is_signin = wp_signon();
66
67
            $response = array();
68
69
            if (is_wp_error($is_signin) ) {
70
71
                $response['type'] = 'error';
72
73
                $response['message'] = $is_signin->get_error_message();
74
75
            } else {
76
77
                $response['type'] = 'success';
78
79
                $response['message'] = esc_html__(
80
                    'You have successfully logged-in. 
81
                	Redirecting you in few seconds...'
82
                );
83
84
            }
85
        }
86
        
87
        $subway_redirect_url = AuthRedirect::getLoginRedirectUrl('');
88
89
        $response['redirect_url'] = apply_filters(
90
            'subway_login_redirect', 
91
            $subway_redirect_url
92
        );
93
94
        echo wp_json_encode($response);
95
96
        wp_die();
97
98
    }
99
100
    /**
101
     * Returns the filtered redirect url for the current user.
102
     *
103
     * @param string $redirect_to The default redirect callback argument.
104
     * 
105
     * @return string               The final redirect url.
106
     */
107
    public static function getLoginRedirectUrl( $redirect_to ) 
108
    {
109
110
        $subway_redirect_type = get_option('subway_redirect_type');
111
112
        // Redirect the user to default behaviour.
113
        // If there are no redirect type option saved.
114
        if (empty($subway_redirect_type) ) {
115
116
            return $redirect_to;
117
118
        }
119
120
        if ('default' === $subway_redirect_type ) {
121
            return $redirect_to;
122
        }
123
124
        if ('page' === $subway_redirect_type ) {
125
126
            // Get the page url of the selected page.
127
            // If the admin selected 'Custom Page' in the redirect type settings.
128
            $selected_redirect_page = intval(get_option('subway_redirect_page_id'));
129
130
            // Redirect to default WordPress behaviour.
131
            // If the user did not select page.
132
            if (empty($selected_redirect_page) ) {
133
134
                return $redirect_to;
135
            }
136
137
            // Otherwise, get the permalink of the saved page 
138
            // and let the user go into that page.
139
            return get_permalink($selected_redirect_page);
140
141
        } elseif ('custom_url' === $subway_redirect_type ) {
142
143
            // Get the custom url saved in the redirect type settings.
144
            $entered_custom_url = get_option('subway_redirect_custom_url');
145
146
            // Redirect to default WordPress behaviour 
147
            // if the user did enter a custom url.
148
            if (empty($entered_custom_url) ) {
149
150
                return $redirect_to;
151
152
            }
153
154
            // Otherwise, get the custom url saved 
155
            // and let the user go into that page.
156
            $current_user = wp_get_current_user();
157
158
            if (! empty($current_user->ID) ) {
159
                $entered_custom_url = str_replace(
160
                    '%user_id%', $current_user->ID, 
161
                    $entered_custom_url
162
                );
163
            }
164
165
            if (! empty($current_user->user_login) ) {
166
                $entered_custom_url = str_replace(
167
                    '%user_name%', $current_user->user_login, 
168
                    $entered_custom_url
169
                );
170
            }
171
172
            return $entered_custom_url;
173
174
        }
175
176
        // Otherwise, quit and redirect the user back to default WordPress behaviour.
177
        return $redirect_to;
178
    }
179
180
    /**
181
     * Callback function for the 'login_url' filter defined in Subway.php
182
     *
183
     * @param string $login_url The login url.
184
     * 
185
     * @return string            The final login url.
186
     */
187
    public static function loginUrl( $login_url  ) 
188
    {
189
190
        $subway_login_page = Options::getRedirectPageUrl();
191
192
        // Return the default login url if there is no log-in page defined.
193
        if (empty($subway_login_page) ) {
194
            return $login_url;
195
        }
196
197
        // Otherwise, return the Subway login page.
198
        return $subway_login_page;
199
200
    }
201
202
    /**
203
     * The callback function for our logout filter.
204
     *
205
     * @return void
206
     */
207
    public static function logoutUrl() 
208
    {
209
210
        $subway_login_page = Options::getRedirectPageUrl();
211
212
        wp_safe_redirect(esc_url($subway_login_page . '?loggedout=true'));
213
214
        Helpers::close();
215
216
    }
217
218
}
219