Passed
Push — master ( 54f7d9...dbb97a )
by Joseph
20s
created

PageRedirect::index()   F

Complexity

Conditions 23
Paths 412

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
nc 412
nop 0
dl 0
loc 110
rs 0.6533
c 1
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
        // Additional filter for custom pages, taxonomy, links, parameterized urls, etc.
48
        // Callback function/method must return true (boolean) to disable redirect.
49
        $should_exit = apply_filters( 'subway/classes/subway-page-redirect/index/before-page-redirect', false );
50
51
        if ( $should_exit ) {
52
            return;
53
        }
54
55
        // Only execute for non logged in users.
56
        if (is_user_logged_in() ) {
57
            return;
58
        }
59
        
60
        $is_private = false;
61
62
        $queried_id = get_queried_object_id();
63
64
        $current_post = get_post(absint($queried_id));
65
66
        $login_page_id = absint(get_option('subway_login_page'));
67
68
        $excluded_page = Options::getPublicPostsIdentifiers();
69
70
        // Already escaped inside 'subway_get_redirect_page_url'.
71
        $redirect_page = Options::getRedirectPageUrl(); // WPCS XSS OK.
72
73
        if ( !empty( $current_post ) ) {
74
		  $is_private = Metabox::isPostPrivate( $current_post->ID );
75
        }
76
77
        // do_action( 'subway/classes/subway-page-redirect/index/before_page_redirect' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
79
        // Turn off if is in 'wc-ajax' (woocommerce)
80
        if ( function_exists( 'is_ajax') ) {
81
            if ( is_ajax() ) {
82
               return;
83
            }
84
        }
85
86
        // Same for normal ajax requests.
87
        if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX  ) {
88
            return;
89
        }
90
91
        // Exit if site is public.
92
        if ( Options::isPublicSite() ) {
93
            return;
94
        }
95
96
        // Check if redirect page is empty or not.
97
        if ( empty( $redirect_page ) ) {
98
            return;
99
        }
100
101
        // Check if buddypress activate page.
102
        if (function_exists('bp_is_activation_page') ) {
103
            if (bp_is_activation_page() ) {
104
                return;
105
            }
106
        }
107
108
        // Check if buddypress registration page.
109
        if (function_exists('bp_is_register_page') ) {
110
            if (bp_is_register_page() ) {
111
                return;
112
            }
113
        }
114
115
        // Assign 0 value to empty $post->ID to prevent exception.
116
        // This applies to custom WordPress pages such as BP Members Page and Groups.
117
        if (empty($current_post) ) {
118
            $current_post = new \stdclass;
119
            $current_post->ID = 0;
120
        }
121
122
        $current_page_id = absint($current_post->ID);
123
124
        // Check if $current_page_id && $selected_blog_id is equal to each other.
125
        // Get the page ID instead of global $post->ID that the query returns.
126
        // The ID of the first post object inside the loop is not correct.
127
        $blog_id = absint(get_option('page_for_posts'));
128
129
        if (is_home() ) {
130
            if ($blog_id === $login_page_id ) {
131
                $current_page_id = $blog_id;
132
            }
133
        }
134
135
        // Only execute the script for non-loggedin visitors.
136
        if ( ! is_user_logged_in() ) {
137
138
            if ($current_page_id !== $login_page_id ) {
139
140
                if ( ! in_array( $current_page_id, $excluded_page, true ) || in_array( $current_page_id, $excluded_page, true ) && true === $is_private ) {
141
142
                    wp_safe_redirect(
143
                        add_query_arg(
144
                            array( '_redirected' => 'yes' ),
145
                            $redirect_page
146
                        )
147
                    );
148
149
                    Helpers::close();
150
                }
151
            }
152
        }
153
    }
154
155
}
156