autoptimizeCriticalCSSBase   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 192
Duplicated Lines 3.13 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 6
loc 192
rs 9.36
c 0
b 0
f 0
wmc 38
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 39 7
A setup() 3 19 5
B fetch_options() 0 54 8
B on_upgrade() 3 18 8
A check_upgrade() 0 9 2
A ao_ccss_interval() 0 21 3
A load_requires() 0 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Critical CSS base file (initializes all ccss files).
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
class autoptimizeCriticalCSSBase {
11
    /**
12
     * Main plugin filepath.
13
     * Used for activation/deactivation/uninstall hooks.
14
     *
15
     * @var string
16
     */
17
    protected $filepath = null;
18
19
    public function __construct()
20
    {
21
        // define constant, but only once.
22
        if ( ! defined( 'AO_CCSS_DIR' ) ) {
23
            // Define a constant with the directory to store critical CSS in.
24
            if ( is_multisite() ) {
25
                $blog_id = get_current_blog_id();
26
                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' );
27
            } else {
28
                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' );
29
            }
30
        }
31
        if ( ! defined( 'AO_CCSS_VER' ) ) {
32
            // Define plugin version.
33
            define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION );
34
35
            // Define constants for criticalcss.com base path and API endpoints.
36
            // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option.
37
            define( 'AO_CCSS_URL', 'https://criticalcss.com' );
38
            define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' );
39
            define( 'AO_CCSS_SLEEP', 10 );
40
        }
41
42
        // Define support files locations, in case they are not already defined.
43
        if ( ! defined( 'AO_CCSS_LOCK' ) ) {
44
            define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' );
45
        }
46
        if ( ! defined( 'AO_CCSS_LOG' ) ) {
47
            define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' );
48
        }
49
        if ( ! defined( 'AO_CCSS_DEBUG' ) ) {
50
            define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' );
51
        }
52
53
        $this->filepath = __FILE__;
54
55
        $this->setup();
56
        $this->load_requires();
57
    }
58
59
    public function setup()
60
    {
61
        // get all options.
62
        $all_options = $this->fetch_options();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $all_options is correct as $this->fetch_options() (which targets autoptimizeCriticalCSSBase::fetch_options()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
        foreach ( $all_options as $option => $value ) {
0 ignored issues
show
Bug introduced by
The expression $all_options of type null is not traversable.
Loading history...
64
            ${$option} = $value;
65
        }
66
67
        // make sure the 10 minutes cron schedule is added.
68
        add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) );
69
70
        // check if we need to upgrade.
71
        $this->check_upgrade();
72
73
        // make sure ao_ccss_queue is scheduled OK if an API key is set.
74 View Code Duplication
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
0 ignored issues
show
Bug introduced by
The variable $ao_ccss_key seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
76
        }
77
    }
78
79
    public function load_requires() {
80
        // Required libs, core is always needed.
81
        $criticalcss_core = new autoptimizeCriticalCSSCore();
0 ignored issues
show
Unused Code introduced by
$criticalcss_core is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83
        if ( defined( 'WP_CLI' ) || defined( 'DOING_CRON' ) || is_admin() ) {
84
            // TODO: also include if overridden somehow to force queue processing to be executed?
85
            $criticalcss_cron = new autoptimizeCriticalCSSCron();
0 ignored issues
show
Unused Code introduced by
$criticalcss_cron is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
        }
87
88
        if ( is_admin() ) {
89
            $criticalcss_settings = new autoptimizeCriticalCSSSettings();
0 ignored issues
show
Unused Code introduced by
$criticalcss_settings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        } else {
91
            // enqueuing only done when not wp-admin.
92
            $criticalcss_enqueue = new autoptimizeCriticalCSSEnqueue();
0 ignored issues
show
Unused Code introduced by
$criticalcss_enqueue is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        }
94
    }
95
96
    public static function fetch_options() {
97
        static $autoptimize_ccss_options = null;
98
99
        if ( null === $autoptimize_ccss_options ) {
100
            // not cached yet, fetching from WordPress options.
101
            $autoptimize_ccss_options['ao_css_defer']          = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' );
102
            $autoptimize_ccss_options['ao_css_defer_inline']   = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer_inline' );
103
            $autoptimize_ccss_options['ao_ccss_rules_raw']     = get_option( 'autoptimize_ccss_rules', false );
104
            $autoptimize_ccss_options['ao_ccss_additional']    = get_option( 'autoptimize_ccss_additional' );
105
            $autoptimize_ccss_options['ao_ccss_queue_raw']     = get_option( 'autoptimize_ccss_queue', false );
106
            $autoptimize_ccss_options['ao_ccss_viewport']      = get_option( 'autoptimize_ccss_viewport', false );
107
            $autoptimize_ccss_options['ao_ccss_finclude']      = get_option( 'autoptimize_ccss_finclude', false );
108
            $autoptimize_ccss_options['ao_ccss_rtimelimit']    = get_option( 'autoptimize_ccss_rtimelimit', '30' );
109
            $autoptimize_ccss_options['ao_ccss_noptimize']     = get_option( 'autoptimize_ccss_noptimize', false );
110
            $autoptimize_ccss_options['ao_ccss_debug']         = get_option( 'autoptimize_ccss_debug', false );
111
            $autoptimize_ccss_options['ao_ccss_key']           = get_option( 'autoptimize_ccss_key' );
112
            $autoptimize_ccss_options['ao_ccss_keyst']         = get_option( 'autoptimize_ccss_keyst' );
113
            $autoptimize_ccss_options['ao_ccss_loggedin']      = get_option( 'autoptimize_ccss_loggedin', '1' );
114
            $autoptimize_ccss_options['ao_ccss_forcepath']     = get_option( 'autoptimize_ccss_forcepath', '1' );
115
            $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' );
116
            $autoptimize_ccss_options['ao_ccss_deferjquery']   = get_option( 'autoptimize_ccss_deferjquery', false );
117
            $autoptimize_ccss_options['ao_ccss_domain']        = get_option( 'autoptimize_ccss_domain' );
118
            $autoptimize_ccss_options['ao_ccss_unloadccss']    = get_option( 'autoptimize_ccss_unloadccss', false );
119
120
            if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) {
121
                $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] );
122
            } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) {
123
                // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain).
124
                update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) );
125
            }
126
127
            // Setup the rules array.
128
            if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) {
129
                $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array();
130
                $autoptimize_ccss_options['ao_ccss_rules']['types'] = array();
131
            } else {
132
                $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true );
133
            }
134
135
            // Setup the queue array.
136
            if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) {
137
                $autoptimize_ccss_options['ao_ccss_queue'] = array();
138
            } else {
139
                $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true );
140
            }
141
142
            // Override API key if constant is defined.
143
            if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) {
144
                $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY;
145
            }
146
        }
147
148
        return $autoptimize_ccss_options;
149
    }
150
151
    public function on_upgrade() {
152
        global $ao_ccss_key;
153
154
        // Create the cache directory if it doesn't exist already.
155
        if ( ! file_exists( AO_CCSS_DIR ) ) {
156
            mkdir( AO_CCSS_DIR, 0755, true );
157
        }
158
159
        // Create a scheduled event for the queue.
160 View Code Duplication
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
162
        }
163
164
        // Create a scheduled event for log maintenance.
165
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) {
166
            wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' );
167
        }
168
    }
169
170
    public function check_upgrade() {
171
        $db_version = get_option( 'autoptimize_ccss_version', '' );
172
        if ( AO_CCSS_VER !== $db_version ) {
173
            // check schedules & re-schedule if needed.
174
            $this->on_upgrade();
175
            // and update db_version.
176
            update_option( 'autoptimize_ccss_version', AO_CCSS_VER );
177
        }
178
    }
179
180
    public function ao_ccss_interval( $schedules ) {
181
        // Let interval be configurable.
182
        if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) {
183
            $intsec = 600;
184
        } else {
185
            $intsec = AO_CCSS_DEBUG_INTERVAL;
186
            if ( $intsec >= 120 ) {
187
                $inttxt = $intsec / 60 . ' minutes';
188
            } else {
189
                $inttxt = $intsec . ' second(s)';
190
            }
191
            autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 );
192
        }
193
194
        // Attach interval to schedule.
195
        $schedules['ao_ccss'] = array(
196
            'interval' => $intsec,
197
            'display'  => __( 'Autoptimize CriticalCSS' ),
198
        );
199
        return $schedules;
200
    }
201
}
202