Completed
Push — master ( 7edd50...ca6328 )
by frank
01:38
created

do_2_2_settings_update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles version updates and should only be instantiated in autoptimize.php if/when needed.
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
class autoptimizeVersionUpdatesHandler
11
{
12
    /**
13
     * The current major version string.
14
     *
15
     * @var string
16
     */
17
    protected $current_major_version = null;
18
19
    public function __construct( $current_version )
20
    {
21
        $this->current_major_version = substr( $current_version, 0, 3 );
22
    }
23
24
    /**
25
     * Runs all needed upgrade procedures (depending on the
26
     * current major version specified during class instantiation)
27
     */
28
    public function run_needed_major_upgrades()
29
    {
30
        $major_update = false;
31
32
        switch ( $this->current_major_version ) {
33
            case '1.6':
34
                $this->upgrade_from_1_6();
35
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update 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...
36
                // No break, intentionally, so all upgrades are ran during a single request...
37
            case '1.7':
38
                $this->upgrade_from_1_7();
39
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update 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...
40
                // No break, intentionally, so all upgrades are ran during a single request...
41
            case '1.9':
42
                $this->upgrade_from_1_9();
43
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update 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...
44
                // No break, intentionally, so all upgrades are ran during a single request...
45
            case '2.2':
46
                $this->upgrade_from_2_2();
47
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update 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...
48
                // No break, intentionally, so all upgrades are ran during a single request...
49
            case '2.4':
50
                if ( get_option( 'autoptimize_version', 'none' ) == '2.4.2' ) {
51
                    $this->upgrade_from_2_4_2();
52
                }
53
                $major_update = false;
54
                // No break, intentionally, so all upgrades are ran during a single request...
55
        }
56
57
        if ( true === $major_update ) {
58
            $this->on_major_version_update();
59
        }
60
    }
61
62
    /**
63
     * Checks specified version against the one stored in the database under `autoptimize_version` and performs
64
     * any major upgrade routines if needed.
65
     * Updates the database version to the specified $target if it's different to the one currently stored there.
66
     *
67
     * @param string $target Target version to check against (ie., the currently running one).
68
     */
69
    public static function check_installed_and_update( $target )
70
    {
71
        $db_version = get_option( 'autoptimize_version', 'none' );
72
        if ( $db_version !== $target ) {
73
            if ( 'none' === $db_version ) {
74
                add_action( 'admin_notices', 'autoptimizeMain::notice_installed' );
75
            } else {
76
                $updater = new self( $db_version );
77
                $updater->run_needed_major_upgrades();
78
            }
79
80
            // Versions differed, upgrades happened if needed, store the new version.
81
            update_option( 'autoptimize_version', $target );
82
        }
83
    }
84
85
    /**
86
     * Called after any major version update (and it's accompanying upgrade procedure)
87
     * has happened. Clears cache and sets an admin notice.
88
     */
89
    protected function on_major_version_update()
90
    {
91
        // The transients guard here prevents stale object caches from busting the cache on every request.
92
        if ( false == get_transient( 'autoptimize_stale_option_buster' ) ) {
93
            set_transient( 'autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS );
94
            autoptimizeCache::clearall();
95
            add_action( 'admin_notices', 'autoptimizeMain::notice_updated' );
96
        }
97
    }
98
99
    /**
100
     * From back in the days when I did not yet consider multisite.
101
     */
102
    private function upgrade_from_1_6()
103
    {
104
        // If user was on version 1.6.x, force advanced options to be shown by default.
105
        update_option( 'autoptimize_show_adv', '1' );
106
107
        // And remove old options.
108
        $to_delete_options = array(
109
            'autoptimize_cdn_css',
110
            'autoptimize_cdn_css_url',
111
            'autoptimize_cdn_js',
112
            'autoptimize_cdn_js_url',
113
            'autoptimize_cdn_img',
114
            'autoptimize_cdn_img_url',
115
            'autoptimize_css_yui',
116
        );
117
        foreach ( $to_delete_options as $del_opt ) {
118
            delete_option( $del_opt );
119
        }
120
    }
121
122
    /**
123
     * Forces WP 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8
124
     *
125
     * @global $wpdb
126
     */
127
    private function upgrade_from_1_7()
128
    {
129
        if ( ! is_multisite() ) {
130
            $css_exclude = get_option( 'autoptimize_css_exclude' );
131 View Code Duplication
            if ( empty( $css_exclude ) ) {
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...
132
                $css_exclude = 'admin-bar.min.css, dashicons.min.css';
133
            } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
134
                $css_exclude .= ', dashicons.min.css';
135
            }
136
            update_option( 'autoptimize_css_exclude', $css_exclude );
137
        } else {
138
            global $wpdb;
139
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
140
            $original_blog_id = get_current_blog_id();
141
            foreach ( $blog_ids as $blog_id ) {
142
                switch_to_blog( $blog_id );
143
                $css_exclude = get_option( 'autoptimize_css_exclude' );
144 View Code Duplication
                if ( empty( $css_exclude ) ) {
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...
145
                    $css_exclude = 'admin-bar.min.css, dashicons.min.css';
146
                } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
147
                    $css_exclude .= ', dashicons.min.css';
148
                }
149
                update_option( 'autoptimize_css_exclude', $css_exclude );
150
            }
151
            switch_to_blog( $original_blog_id );
152
        }
153
    }
154
155
    /**
156
     * 2.0 will not aggregate inline CSS/JS by default, but we want users
157
     * upgrading from 1.9 to keep their inline code aggregated by default.
158
     *
159
     * @global $wpdb
160
     */
161
    private function upgrade_from_1_9()
162
    {
163
        if ( ! is_multisite() ) {
164
            update_option( 'autoptimize_css_include_inline', 'on' );
165
            update_option( 'autoptimize_js_include_inline', 'on' );
166
        } else {
167
            global $wpdb;
168
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
169
            $original_blog_id = get_current_blog_id();
170
            foreach ( $blog_ids as $blog_id ) {
171
                switch_to_blog( $blog_id );
172
                update_option( 'autoptimize_css_include_inline', 'on' );
173
                update_option( 'autoptimize_js_include_inline', 'on' );
174
            }
175
            switch_to_blog( $original_blog_id );
176
        }
177
    }
178
179
    /**
180
     * 2.3 has no "remove google fonts" in main screen, moved to "extra"
181
     *
182
     * @global $wpdb
183
     */
184
    private function upgrade_from_2_2()
185
    {
186
        if ( ! is_multisite() ) {
187
            $this->do_2_2_settings_update();
188
        } else {
189
            global $wpdb;
190
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
191
            $original_blog_id = get_current_blog_id();
192
            foreach ( $blog_ids as $blog_id ) {
193
                switch_to_blog( $blog_id );
194
                $this->do_2_2_settings_update();
195
            }
196
            switch_to_blog( $original_blog_id );
197
        }
198
    }
199
200
    /**
201
     * Helper for 2.2 autoptimize_extra_settings upgrade to avoid duplicate code
202
     */
203
    private function do_2_2_settings_update()
204
    {
205
        $nogooglefont    = get_option( 'autoptimize_css_nogooglefont', '' );
206
        $ao_extrasetting = get_option( 'autoptimize_extra_settings', '' );
207
        if ( ( $nogooglefont ) && ( empty( $ao_extrasetting ) ) ) {
208
            update_option( 'autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options() );
209
        }
210
        delete_option( 'autoptimize_css_nogooglefont' );
211
    }
212
213
    /**
214
     * 2.4.2 introduced too many cronned ao_cachecheckers, make this right
215
     */
216
    private function upgrade_from_2_4_2() {
217
        wp_clear_scheduled_hook( 'ao_cachechecker' );
218
    }
219
}
220