Passed
Push — master ( 38ae5c...d76375 )
by Stiofan
04:14
created

WPInv_Cache_Helper::init_hooks()   C

Complexity

Conditions 13
Paths 69

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 31
nc 69
nop 0
dl 0
loc 49
rs 5.1401
c 0
b 0
f 0

How to fix   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
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
/**
7
 * WPInv_Cache_Helper class.
8
 *
9
 * @class   WPInv_Cache_Helper
10
 */
11
class WPInv_Cache_Helper {
12
13
    /**
14
     * Hook in methods.
15
     */
16
    public static function init() {
17
        add_action( 'init', array( __CLASS__, 'init_hooks' ), 0 );
18
        add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
19
    }
20
21
    public static function init_hooks() {
22
        if ( false === ( $page_uris = get_transient( 'wpinv_cache_excluded_uris' ) ) ) {
23
            $checkout_page = wpinv_get_option( 'checkout_page', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
            $success_page  = wpinv_get_option( 'success_page', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
            $failure_page  = wpinv_get_option( 'failure_page', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
            $history_page  = wpinv_get_option( 'invoice_history_page', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
            if ( empty( $checkout_page ) || empty( $success_page ) || empty( $failure_page ) || empty( $history_page ) ) {
28
                return;
29
            }
30
31
            $page_uris   = array();
32
33
            // Exclude querystring when using page ID
34
            $page_uris[] = 'p=' . $checkout_page;
35
            $page_uris[] = 'p=' . $success_page;
36
            $page_uris[] = 'p=' . $failure_page;
37
            $page_uris[] = 'p=' . $history_page;
38
39
            // Exclude permalinks
40
            $checkout_page  = get_post( $checkout_page );
41
            $success_page   = get_post( $success_page );
42
            $failure_page   = get_post( $failure_page );
43
            $history_page   = get_post( $history_page );
44
45
            if ( ! is_null( $checkout_page ) ) {
46
                $page_uris[] = '/' . $checkout_page->post_name;
47
            }
48
            if ( ! is_null( $success_page ) ) {
49
                $page_uris[] = '/' . $success_page->post_name;
50
            }
51
            if ( ! is_null( $failure_page ) ) {
52
                $page_uris[] = '/' . $failure_page->post_name;
53
            }
54
            if ( ! is_null( $history_page ) ) {
55
                $page_uris[] = '/' . $history_page->post_name;
56
            }
57
58
            set_transient( 'wpinv_cache_excluded_uris', $page_uris );
59
        }
60
61
        if ( is_array( $page_uris ) ) {
62
            foreach( $page_uris as $uri ) {
63
                if ( strstr( $_SERVER['REQUEST_URI'], $uri ) ) {
64
                    self::nocache();
65
                    break;
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * Set nocache constants and headers.
73
     * @access private
74
     */
75
    private static function nocache() {
76
        if ( ! defined( 'DONOTCACHEPAGE' ) ) {
77
            define( "DONOTCACHEPAGE", true );
78
        }
79
        if ( ! defined( 'DONOTCACHEOBJECT' ) ) {
80
            define( "DONOTCACHEOBJECT", true );
81
        }
82
        if ( ! defined( 'DONOTCACHEDB' ) ) {
83
            define( "DONOTCACHEDB", true );
84
        }
85
        nocache_headers();
86
    }
87
    
88
    /**
89
     * notices function.
90
     */
91
    public static function notices() {
92
        if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) {
93
            return;
94
        }
95
96
        $config   = w3_instance( 'W3_Config' );
97
        $enabled  = $config->get_integer( 'dbcache.enabled' );
98
        $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) );
99
100
        if ( $enabled && ! in_array( '_wp_session_', $settings ) ) {
101
            ?>
102
            <div class="error">
103
                <p><?php printf( __( 'In order for <strong>database caching</strong> to work with Invoicing you must add %1$s to the "Ignored Query Strings" option in <a href="%2$s">W3 Total Cache settings</a>.', 'invoicing' ), '<code>_wp_session_</code>', admin_url( 'admin.php?page=w3tc_dbcache' ) ); ?></p>
104
            </div>
105
            <?php
106
        }
107
    }
108
}
109
110
WPInv_Cache_Helper::init();
111