Passed
Pull Request — master (#4)
by Joseph
02:09
created

Auth_Redirect   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 159
rs 10
c 1
b 0
f 0
wmc 16
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle_authentication() 0 45 4
C get_login_redirect_url() 0 59 9
A login_url() 0 13 2
A logout_url() 0 11 1
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';
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...
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();
0 ignored issues
show
Unused Code introduced by
$current_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
return; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
179
180
	}
181
182
}
183