Passed
Push — master ( 91003a...b0b2ca )
by Joseph
07:50 queued 04:02
created

PageRedirect::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
 * PHP Version 5.4
11
 * 
12
 * @category Subway\Page\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
 * Subway Option Methods.
28
 *
29
 * @category Subway\Page\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 PageRedirect
37
{
38
39
    /**
40
     * Redirects pages into our login page.
41
     *
42
     * @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...
43
     */
44
    public static function index() 
45
    {
46
47
        // Only execute for non logged in users.
48
        if (is_user_logged_in() ) {
49
            return;
50
        }
51
52
        $queried_id = get_queried_object_id();
53
54
        $current_post = get_post(absint($queried_id));
55
56
        $login_page_id = absint(get_option('subway_login_page'));
57
58
        $excluded_page = Options::getPublicPostsIdentifiers();
59
60
        // Already escaped inside 'subway_get_redirect_page_url'.
61
        $redirect_page = Options::getRedirectPageUrl(); // WPCS XSS OK.
62
63
        // Exit if site is public.
64
        if (Options::isPublicSite() ) {
65
            return;
66
        }
67
68
        // Check if redirect page is empty or not.
69
        if (empty($redirect_page) ) {
70
            return;
71
        }
72
73
        // Check if buddypress activate page.
74
        if (function_exists('bp_is_activation_page') ) {
75
            if (bp_is_activation_page() ) {
76
                return;
77
            }
78
        }
79
80
        // Check if buddypress registration page.
81
        if (function_exists('bp_is_register_page') ) {
82
            if (bp_is_register_page() ) {
83
                return;
84
            }
85
        }
86
87
        // Assign 0 value to empty $post->ID to prevent exception.
88
        // This applies to custom WordPress pages such as BP Members Page and Groups.
89
        if (empty($current_post) ) {
90
            $current_post = new \stdclass;
91
            $current_post->ID = 0;
92
        }
93
94
        $current_page_id = absint($current_post->ID);
95
96
        // Check if $current_page_id && $selected_blog_id is equal to each other.
97
        // Get the page ID instead of global $post->ID that the query returns.
98
        // The ID of the first post object inside the loop is not correct.
99
        $blog_id = absint(get_option('page_for_posts'));
100
101
        if (is_home() ) {
102
            if ($blog_id === $login_page_id ) {
103
                $current_page_id = $blog_id;
104
            }
105
        }
106
107
        // Only execute the script for non-loggedin visitors.
108
        if (! is_user_logged_in() ) {
109
110
            if ($current_page_id !== $login_page_id ) {
111
112
                if (! in_array($current_page_id, $excluded_page, true) ) {
113
114
                    wp_safe_redirect(
115
                        add_query_arg(
116
                            array( '_redirected' => 'yes' ),
117
                            $redirect_page
118
                        )
119
                    );
120
121
                    Helpers::close();
122
                }
123
            }
124
        }
125
    }
126
127
}
128
129