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

Page_Redirect::index()   D

Complexity

Conditions 14
Paths 102

Size

Total Lines 82
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 34
nc 102
nop 0
dl 0
loc 82
rs 4.9189
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * @package Subway
11
 */
12
13
namespace Subway;
14
15
if ( ! defined( 'ABSPATH' ) ) {
16
	return;
17
}
18
/**
19
 * Class Subway\Page_Redirect handles the 'front-end' redirection of Subway.
20
 *
21
 * @since  2.0
22
 */
23
final class Page_Redirect {
24
25
	/**
26
	 * Redirects pages into our login page.
27
	 *
28
	 * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
	 */
30
	public static function index() {
31
32
		// Only execute for non logged in users.
33
		if ( is_user_logged_in() ) {
34
			return;
35
		}
36
37
		$queried_id = get_queried_object_id();
38
39
		$current_post = get_post( absint( $queried_id ) );
40
41
		$login_page_id = absint( get_option( 'subway_login_page' ) );
42
43
		$excluded_page = Options::get_public_post_ids();
44
45
		// Already escaped inside 'subway_get_redirect_page_url'.
46
		$redirect_page = Options::get_redirect_page_url(); // WPCS XSS OK.
47
48
		// Exit if site is public.
49
		if ( Options::is_public_site() ) {
50
			return;
51
		}
52
53
		// Check if redirect page is empty or not.
54
		if ( empty( $redirect_page ) ) {
55
			return;
56
		}
57
58
		// Check if buddypress activate page.
59
		if ( function_exists( 'bp_is_activation_page' ) ) {
60
			if ( bp_is_activation_page() ) {
61
				return;
62
			}
63
		}
64
65
		// Check if buddypress registration page.
66
		if ( function_exists( 'bp_is_register_page' ) ) {
67
			if ( bp_is_register_page() ) {
68
				return;
69
			}
70
		}
71
72
		// Assign 0 value to empty $post->ID to prevent exception.
73
		// This applies to custom WordPress pages such as BP Members Page and Groups.
74
		if ( empty( $current_post ) ) {
75
			$current_post = new \stdclass;
76
			$current_post->ID = 0;
77
		}
78
79
		$current_page_id = absint( $current_post->ID );
80
81
		// Check if $current_page_id && $selected_blog_id is equal to each other.
82
		// If that is the case, get the page ID instead of global $post->ID that the query returns.
83
		// The ID of the first post object inside the loop is not correct.
84
		$blog_id = absint( get_option( 'page_for_posts' ) );
85
86
		if ( is_home() ) {
87
			if ( $blog_id === $login_page_id ) {
88
				$current_page_id = $blog_id;
89
			}
90
		}
91
92
		// Only execute the script for non-loggedin visitors.
93
		if ( ! is_user_logged_in() ) {
94
95
			if ( $current_page_id !== $login_page_id ) {
96
97
				if ( ! in_array( $current_page_id, $excluded_page, true ) ) {
98
99
					wp_safe_redirect(
100
						add_query_arg(
101
							array( '_redirected' => 'yes' ),
102
							$redirect_page
103
						)
104
					);
105
106
					die();
107
108
				}
109
			}
110
		}
111
	}
112
113
}
114
115