WPInv_Cache_Helper::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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', '' );
24
            $success_page  = wpinv_get_option( 'success_page', '' );
25
            $failure_page  = wpinv_get_option( 'failure_page', '' );
26
            $history_page  = wpinv_get_option( 'invoice_history_page', '' );
27
            $subscr_page   = wpinv_get_option( 'invoice_subscription_page', '' );
28
            if ( empty( $checkout_page ) || empty( $success_page ) || empty( $failure_page ) || empty( $history_page ) || empty( $subscr_page ) ) {
29
                return;
30
            }
31
32
            $page_uris   = array();
33
34
            // Exclude querystring when using page ID
35
            $page_uris[] = 'p=' . $checkout_page;
36
            $page_uris[] = 'p=' . $success_page;
37
            $page_uris[] = 'p=' . $failure_page;
38
            $page_uris[] = 'p=' . $history_page;
39
            $page_uris[] = 'p=' . $subscr_page;
40
41
            // Exclude permalinks
42
            $checkout_page  = get_post( $checkout_page );
0 ignored issues
show
Bug introduced by
It seems like $checkout_page can also be of type string; however, parameter $post of get_post() does only seem to accept WP_Post|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $checkout_page  = get_post( /** @scrutinizer ignore-type */ $checkout_page );
Loading history...
43
            $success_page   = get_post( $success_page );
44
            $failure_page   = get_post( $failure_page );
45
            $history_page   = get_post( $history_page );
46
            $subscr_page    = get_post( $subscr_page );
47
48
            if ( ! is_null( $checkout_page ) ) {
49
                $page_uris[] = '/' . $checkout_page->post_name;
50
            }
51
            if ( ! is_null( $success_page ) ) {
52
                $page_uris[] = '/' . $success_page->post_name;
53
            }
54
            if ( ! is_null( $failure_page ) ) {
55
                $page_uris[] = '/' . $failure_page->post_name;
56
            }
57
            if ( ! is_null( $history_page ) ) {
58
                $page_uris[] = '/' . $history_page->post_name;
59
            }
60
            if ( ! is_null( $subscr_page ) ) {
61
                $page_uris[] = '/' . $subscr_page->post_name;
62
            }
63
64
            set_transient( 'wpinv_cache_excluded_uris', $page_uris );
65
        }
66
67
        if ( is_array( $page_uris ) ) {
68
            foreach ( $page_uris as $uri ) {
69
                if ( strstr( $_SERVER['REQUEST_URI'], $uri ) ) {
70
                    self::nocache();
71
                    break;
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * Set nocache constants and headers.
79
     * @access private
80
     */
81
    private static function nocache() {
82
        if ( ! defined( 'DONOTCACHEPAGE' ) ) {
83
            define( 'DONOTCACHEPAGE', true );
84
        }
85
        if ( ! defined( 'DONOTCACHEOBJECT' ) ) {
86
            define( 'DONOTCACHEOBJECT', true );
87
        }
88
        if ( ! defined( 'DONOTCACHEDB' ) ) {
89
            define( 'DONOTCACHEDB', true );
90
        }
91
        nocache_headers();
92
    }
93
94
    /**
95
     * notices function.
96
     */
97
    public static function notices() {
98
        if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) {
99
            return;
100
        }
101
102
        $config   = w3_instance( 'W3_Config' );
103
        $enabled  = $config->get_integer( 'dbcache.enabled' );
104
        $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) );
105
106
        if ( $enabled && ! in_array( '_wp_session_', $settings ) ) {
107
            ?>
108
            <div class="error">
109
                <p><?php printf( wp_kses_post( __( '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>', esc_url( admin_url( 'admin.php?page=w3tc_dbcache' ) ) ); ?></p>
110
            </div>
111
            <?php
112
        }
113
    }
114
}
115
116
WPInv_Cache_Helper::init();
117