Completed
Push — master ( 8e7dca...6ec0d0 )
by frank
01:41
created

autoptimizeMain::should_buffer()   F

Complexity

Conditions 22
Paths 513

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
nc 513
nop 1
dl 0
loc 68
rs 0.6763
c 0
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
 * Wraps base plugin logic/hooks and handles activation/deactivation/uninstall.
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
class autoptimizeMain
11
{
12
    const INIT_EARLIER_PRIORITY = -1;
13
    const DEFAULT_HOOK_PRIORITY = 2;
14
15
    /**
16
     * Version string.
17
     *
18
     * @var string
19
     */
20
    protected $version = null;
21
22
    /**
23
     * Main plugin filepath.
24
     * Used for activation/deactivation/uninstall hooks.
25
     *
26
     * @var string
27
     */
28
    protected $filepath = null;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string $version Version.
34
     * @param string $filepath Filepath. Needed for activation/deactivation/uninstall hooks.
35
     */
36
    public function __construct( $version, $filepath )
37
    {
38
        $this->version  = $version;
39
        $this->filepath = $filepath;
40
    }
41
42
    public function run()
43
    {
44
        $this->add_hooks();
45
46
        // Runs cache size checker.
47
        $checker = new autoptimizeCacheChecker();
48
        $checker->run();
49
    }
50
51
    protected function add_hooks()
52
    {
53
        add_action( 'plugins_loaded', array( $this, 'setup' ) );
54
55
        add_action( 'autoptimize_setup_done', array( $this, 'version_upgrades_check' ) );
56
        add_action( 'autoptimize_setup_done', array( $this, 'check_cache_and_run' ) );
57
        add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_ao_extra' ) );
58
        add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_partners_tab' ) );
59
60
        add_action( 'init', array( $this, 'load_textdomain' ) );
61
        add_action( 'plugins_loaded', array( $this, 'hook_page_cache_purge' ) );
62
        add_action( 'admin_init', array( 'PAnD', 'init' ) );
63
64
        register_activation_hook( $this->filepath, array( $this, 'on_activate' ) );
65
    }
66
67
    public function on_activate()
68
    {
69
        register_uninstall_hook( $this->filepath, 'autoptimizeMain::on_uninstall' );
70
    }
71
72
    public function load_textdomain()
73
    {
74
        load_plugin_textdomain( 'autoptimize' );
75
    }
76
77
    public function setup()
78
    {
79
        // Do we gzip in php when caching or is the webserver doing it?
80
        define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) get_option( 'autoptimize_cache_nogzip' ) );
81
82
        // These can be overridden by specifying them in wp-config.php or such.
83
        if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_NAME' ) ) {
84
            define( 'AUTOPTIMIZE_WP_CONTENT_NAME', '/' . wp_basename( WP_CONTENT_DIR ) );
85
        }
86
        if ( ! defined( 'AUTOPTIMIZE_CACHE_CHILD_DIR' ) ) {
87
            define( 'AUTOPTIMIZE_CACHE_CHILD_DIR', '/cache/autoptimize/' );
88
        }
89
        if ( ! defined( 'AUTOPTIMIZE_CACHEFILE_PREFIX' ) ) {
90
            define( 'AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_' );
91
        }
92
        // Note: trailing slash is not optional!
93
        if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) {
94
            define( 'AUTOPTIMIZE_CACHE_DIR', autoptimizeCache::get_pathname() );
95
        }
96
97
        define( 'WP_ROOT_DIR', substr( WP_CONTENT_DIR, 0, strlen( WP_CONTENT_DIR ) - strlen( AUTOPTIMIZE_WP_CONTENT_NAME ) ) );
98
99
        if ( ! defined( 'AUTOPTIMIZE_WP_SITE_URL' ) ) {
100
            if ( function_exists( 'domain_mapping_siteurl' ) ) {
101
                define( 'AUTOPTIMIZE_WP_SITE_URL', domain_mapping_siteurl( get_current_blog_id() ) );
102
            } else {
103
                define( 'AUTOPTIMIZE_WP_SITE_URL', site_url() );
104
            }
105
        }
106
        if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_URL' ) ) {
107
            if ( function_exists( 'domain_mapping_siteurl' ) ) {
108
                define( 'AUTOPTIMIZE_WP_CONTENT_URL', str_replace( get_original_url( AUTOPTIMIZE_WP_SITE_URL ), AUTOPTIMIZE_WP_SITE_URL, content_url() ) );
109
            } else {
110
                define( 'AUTOPTIMIZE_WP_CONTENT_URL', content_url() );
111
            }
112
        }
113
        if ( ! defined( 'AUTOPTIMIZE_CACHE_URL' ) ) {
114
            if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) {
115
                $blog_id = get_current_blog_id();
116
                define( 'AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL . AUTOPTIMIZE_CACHE_CHILD_DIR . $blog_id . '/' );
117
            } else {
118
                define( 'AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL . AUTOPTIMIZE_CACHE_CHILD_DIR );
119
            }
120
        }
121
        if ( ! defined( 'AUTOPTIMIZE_WP_ROOT_URL' ) ) {
122
            define( 'AUTOPTIMIZE_WP_ROOT_URL', str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', AUTOPTIMIZE_WP_CONTENT_URL ) );
123
        }
124
        if ( ! defined( 'AUTOPTIMIZE_HASH' ) ) {
125
            define( 'AUTOPTIMIZE_HASH', wp_hash( AUTOPTIMIZE_CACHE_URL ) );
126
        }
127
        if ( ! defined( 'AUTOPTIMIZE_SITE_DOMAIN' ) ) {
128
            define( 'AUTOPTIMIZE_SITE_DOMAIN', parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) );
129
        }
130
        do_action( 'autoptimize_setup_done' );
131
    }
132
133
    /**
134
     * Checks if there's a need to upgrade/update options and whatnot,
135
     * in which case we might need to do stuff and flush the cache
136
     * to avoid old versions of aggregated files lingering around.
137
     */
138
    public function version_upgrades_check()
139
    {
140
        autoptimizeVersionUpdatesHandler::check_installed_and_update( $this->version );
141
    }
142
143
    public function check_cache_and_run()
144
    {
145
        if ( autoptimizeCache::cacheavail() ) {
146
            $conf = autoptimizeConfig::instance();
147
            if ( $conf->get( 'autoptimize_html' ) || $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) ) {
148
                // Hook into WordPress frontend.
149
                if ( defined( 'AUTOPTIMIZE_INIT_EARLIER' ) ) {
150
                    add_action(
151
                        'init',
152
                        array( $this, 'start_buffering' ),
153
                        self::INIT_EARLIER_PRIORITY
154
                    );
155
                } else {
156
                    if ( ! defined( 'AUTOPTIMIZE_HOOK_INTO' ) ) {
157
                        define( 'AUTOPTIMIZE_HOOK_INTO', 'template_redirect' );
158
                    }
159
                    add_action(
160
                        constant( 'AUTOPTIMIZE_HOOK_INTO' ),
161
                        array( $this, 'start_buffering' ),
162
                        self::DEFAULT_HOOK_PRIORITY
163
                    );
164
                }
165
            }
166
        } else {
167
            add_action( 'admin_notices', 'autoptimizeMain::notice_cache_unavailable' );
168
        }
169
    }
170
171
    public function maybe_run_ao_extra()
172
    {
173
        if ( apply_filters( 'autoptimize_filter_extra_activate', true ) ) {
174
            $ao_extra = new autoptimizeExtra();
175
            $ao_extra->run();
176
        }
177
    }
178
179
    public function maybe_run_partners_tab()
180
    {
181
        // Loads partners tab code if in admin (and not in admin-ajax.php)!
182
        if ( autoptimizeConfig::is_admin_and_not_ajax() ) {
183
            new autoptimizePartners();
184
        }
185
    }
186
187
    public function hook_page_cache_purge()
188
    {
189
        // hook into a collection of page cache purge actions if filter allows.
190
        if ( apply_filters( 'autoptimize_filter_main_hookpagecachepurge', true ) ) {
191
            $page_cache_purge_actions = array(
192
                'after_rocket_clean_domain', // exists.
193
                'hyper_cache_purged', // Stefano confirmed this will be added.
194
                'w3tc_flush_posts', // exits.
195
                'w3tc_flush_all', // exists.
196
                'ce_action_cache_cleared', // Sven confirmed this will be added.
197
                'comet_cache_wipe_cache', // still to be confirmed by Raam.
198
                'wp_cache_cleared', // cfr. https://github.com/Automattic/wp-super-cache/pull/537.
199
                'wpfc_delete_cache', // Emre confirmed this will be added this.
200
                'swift_performance_after_clear_all_cache', // swift perf. yeah!
201
            );
202
            $page_cache_purge_actions = apply_filters( 'autoptimize_filter_main_pagecachepurgeactions', $page_cache_purge_actions );
203
            foreach ( $page_cache_purge_actions as $purge_action ) {
204
                add_action( $purge_action, 'autoptimizeCache::clearall_actionless' );
205
            }
206
        }
207
    }
208
209
    /**
210
     * Setup output buffering if needed.
211
     *
212
     * @return void
213
     */
214
    public function start_buffering()
215
    {
216
        if ( $this->should_buffer() ) {
217
218
            // Load speedupper conditionally (true by default).
219
            if ( apply_filters( 'autoptimize_filter_speedupper', true ) ) {
220
                $ao_speedupper = new autoptimizeSpeedupper();
0 ignored issues
show
Unused Code introduced by
$ao_speedupper 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...
221
            }
222
223
            $conf = autoptimizeConfig::instance();
224
225
            if ( $conf->get( 'autoptimize_js' ) ) {
226
                if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) {
227
                    define( 'CONCATENATE_SCRIPTS', false );
228
                }
229
                if ( ! defined( 'COMPRESS_SCRIPTS' ) ) {
230
                    define( 'COMPRESS_SCRIPTS', false );
231
                }
232
            }
233
234
            if ( $conf->get( 'autoptimize_css' ) ) {
235
                if ( ! defined( 'COMPRESS_CSS' ) ) {
236
                    define( 'COMPRESS_CSS', false );
237
                }
238
            }
239
240
            if ( apply_filters( 'autoptimize_filter_obkiller', false ) ) {
241
                while ( ob_get_level() > 0 ) {
242
                    ob_end_clean();
243
                }
244
            }
245
246
            // Now, start the real thing!
247
            ob_start( array( $this, 'end_buffering' ) );
248
        }
249
    }
250
251
    /**
252
     * Returns true if all the conditions to start output buffering are satisfied.
253
     *
254
     * @param bool $doing_tests Allows overriding the optimization of only
255
     *                          deciding once per request (for use in tests).
256
     * @return bool
257
     */
258
    public function should_buffer( $doing_tests = false )
259
    {
260
        static $do_buffering = null;
261
262
        // Only check once in case we're called multiple times by others but
263
        // still allows multiple calls when doing tests.
264
        if ( null === $do_buffering || $doing_tests ) {
265
266
            $ao_noptimize = false;
267
268
            // Checking for DONOTMINIFY constant as used by e.g. WooCommerce POS.
269
            if ( defined( 'DONOTMINIFY' ) && ( constant( 'DONOTMINIFY' ) === true || constant( 'DONOTMINIFY' ) === 'true' ) ) {
270
                $ao_noptimize = true;
271
            }
272
273
            // Skip checking query strings if they're disabled.
274
            if ( apply_filters( 'autoptimize_filter_honor_qs_noptimize', true ) ) {
275
                // Check for `ao_noptimize` (and other) keys in the query string
276
                // to get non-optimized page for debugging.
277
                $keys = array(
278
                    'ao_noptimize',
279
                    'ao_noptirocket',
280
                );
281
                foreach ( $keys as $key ) {
282
                    if ( array_key_exists( $key, $_GET ) && '1' === $_GET[ $key ] ) {
283
                        $ao_noptimize = true;
284
                        break;
285
                    }
286
                }
287
            }
288
289
            // If setting says not to optimize logged in user and user is logged in...
290
            if ( 'on' !== get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) {
291
                $ao_noptimize = true;
292
            }
293
294
            // If setting says not to optimize cart/checkout.
295
            if ( 'on' !== get_option( 'autoptimize_optimize_checkout', 'on' ) ) {
296
                // Checking for woocommerce, easy digital downloads and wp ecommerce...
297
                foreach ( array( 'is_checkout', 'is_cart', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout' ) as $func ) {
298
                    if ( function_exists( $func ) && $func() ) {
299
                        $ao_noptimize = true;
300
                        break;
301
                    }
302
                }
303
            }
304
305
            // Allows blocking of autoptimization on your own terms regardless of above decisions.
306
            $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize );
307
308
            // Check for site being previewed in the Customizer (available since WP 4.0).
309
            $is_customize_preview = false;
310
            if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
311
                $is_customize_preview = is_customize_preview();
312
            }
313
314
            /**
315
             * We only buffer the frontend requests (and then only if not a feed
316
             * and not turned off explicitly and not when being previewed in Customizer)!
317
             * NOTE: Tests throw a notice here due to is_feed() being called
318
             * while the main query hasn't been ran yet. Thats why we use
319
             * AUTOPTIMIZE_INIT_EARLIER in tests.
320
             */
321
            $do_buffering = ( ! is_admin() && ! is_feed() && ! $ao_noptimize && ! $is_customize_preview );
322
        }
323
324
        return $do_buffering;
325
    }
326
327
    /**
328
     * Returns true if given markup is considered valid/processable/optimizable.
329
     *
330
     * @param string $content Markup.
331
     *
332
     * @return bool
333
     */
334
    public function is_valid_buffer( $content )
335
    {
336
        // Defaults to true.
337
        $valid = true;
338
339
        $has_no_html_tag    = ( false === stripos( $content, '<html' ) );
340
        $has_xsl_stylesheet = ( false !== stripos( $content, '<xsl:stylesheet' ) );
341
        $has_html5_doctype  = ( preg_match( '/^<!DOCTYPE.+html>/i', $content ) > 0 );
342
343
        if ( $has_no_html_tag ) {
344
            // Can't be valid amp markup without an html tag preceding it.
345
            $is_amp_markup = false;
346
        } else {
347
            $is_amp_markup = self::is_amp_markup( $content );
348
        }
349
350
        // If it's not html, or if it's amp or contains xsl stylesheets we don't touch it.
351
        if ( $has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet ) {
352
            $valid = false;
353
        }
354
355
        return $valid;
356
    }
357
358
    /**
359
     * Returns true if given $content is considered to be AMP markup.
360
     * This is far from actual validation against AMP spec, but it'll do for now.
361
     *
362
     * @param string $content Markup to check.
363
     *
364
     * @return bool
365
     */
366
    public static function is_amp_markup( $content )
367
    {
368
        $is_amp_markup = preg_match( '/<html[^>]*(?:amp|⚡)/i', $content );
369
370
        return (bool) $is_amp_markup;
371
    }
372
373
    /**
374
     * Processes/optimizes the output-buffered content and returns it.
375
     * If the content is not processable, it is returned unmodified.
376
     *
377
     * @param string $content Buffered content.
378
     *
379
     * @return string
380
     */
381
    public function end_buffering( $content )
382
    {
383
        // Bail early without modifying anything if we can't handle the content.
384
        if ( ! $this->is_valid_buffer( $content ) ) {
385
            return $content;
386
        }
387
388
        $conf = autoptimizeConfig::instance();
389
390
        // Determine what needs to be ran.
391
        $classes = array();
392
        if ( $conf->get( 'autoptimize_js' ) ) {
393
            $classes[] = 'autoptimizeScripts';
394
        }
395
        if ( $conf->get( 'autoptimize_css' ) ) {
396
            $classes[] = 'autoptimizeStyles';
397
        }
398
        if ( $conf->get( 'autoptimize_html' ) ) {
399
            $classes[] = 'autoptimizeHTML';
400
        }
401
402
        $classoptions = array(
403
            'autoptimizeScripts' => array(
404
                'aggregate'      => $conf->get( 'autoptimize_js_aggregate' ),
405
                'justhead'       => $conf->get( 'autoptimize_js_justhead' ),
406
                'forcehead'      => $conf->get( 'autoptimize_js_forcehead' ),
407
                'trycatch'       => $conf->get( 'autoptimize_js_trycatch' ),
408
                'js_exclude'     => $conf->get( 'autoptimize_js_exclude' ),
409
                'cdn_url'        => $conf->get( 'autoptimize_cdn_url' ),
410
                'include_inline' => $conf->get( 'autoptimize_js_include_inline' ),
411
            ),
412
            'autoptimizeStyles'  => array(
413
                'aggregate'      => $conf->get( 'autoptimize_css_aggregate' ),
414
                'justhead'       => $conf->get( 'autoptimize_css_justhead' ),
415
                'datauris'       => $conf->get( 'autoptimize_css_datauris' ),
416
                'defer'          => $conf->get( 'autoptimize_css_defer' ),
417
                'defer_inline'   => $conf->get( 'autoptimize_css_defer_inline' ),
418
                'inline'         => $conf->get( 'autoptimize_css_inline' ),
419
                'css_exclude'    => $conf->get( 'autoptimize_css_exclude' ),
420
                'cdn_url'        => $conf->get( 'autoptimize_cdn_url' ),
421
                'include_inline' => $conf->get( 'autoptimize_css_include_inline' ),
422
                'nogooglefont'   => $conf->get( 'autoptimize_css_nogooglefont' ),
423
            ),
424
            'autoptimizeHTML'    => array(
425
                'keepcomments' => $conf->get( 'autoptimize_html_keepcomments' ),
426
            ),
427
        );
428
429
        $content = apply_filters( 'autoptimize_filter_html_before_minify', $content );
430
431
        // Run the classes!
432
        foreach ( $classes as $name ) {
433
            $instance = new $name( $content );
434
            if ( $instance->read( $classoptions[ $name ] ) ) {
435
                $instance->minify();
436
                $instance->cache();
437
                $content = $instance->getcontent();
438
            }
439
            unset( $instance );
440
        }
441
442
        $content = apply_filters( 'autoptimize_html_after_minify', $content );
443
444
        return $content;
445
    }
446
447
    public static function on_uninstall()
448
    {
449
        autoptimizeCache::clearall();
450
451
        $delete_options = array(
452
            'autoptimize_cache_clean',
453
            'autoptimize_cache_nogzip',
454
            'autoptimize_css',
455
            'autoptimize_css_aggregate',
456
            'autoptimize_css_datauris',
457
            'autoptimize_css_justhead',
458
            'autoptimize_css_defer',
459
            'autoptimize_css_defer_inline',
460
            'autoptimize_css_inline',
461
            'autoptimize_css_exclude',
462
            'autoptimize_html',
463
            'autoptimize_html_keepcomments',
464
            'autoptimize_js',
465
            'autoptimize_js_aggregate',
466
            'autoptimize_js_exclude',
467
            'autoptimize_js_forcehead',
468
            'autoptimize_js_justhead',
469
            'autoptimize_js_trycatch',
470
            'autoptimize_version',
471
            'autoptimize_show_adv',
472
            'autoptimize_cdn_url',
473
            'autoptimize_cachesize_notice',
474
            'autoptimize_css_include_inline',
475
            'autoptimize_js_include_inline',
476
            'autoptimize_optimize_logged',
477
            'autoptimize_optimize_checkout',
478
            'autoptimize_extra_settings',
479
            'autoptimize_service_availablity',
480
            'autoptimize_imgopt_provider_stat',
481
            'autoptimize_imgopt_launched',
482
        );
483
484
        if ( ! is_multisite() ) {
485
            foreach ( $delete_options as $del_opt ) {
486
                delete_option( $del_opt );
487
            }
488
        } else {
489
            global $wpdb;
490
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
491
            $original_blog_id = get_current_blog_id();
492
            foreach ( $blog_ids as $blog_id ) {
493
                switch_to_blog( $blog_id );
494
                foreach ( $delete_options as $del_opt ) {
495
                    delete_option( $del_opt );
496
                }
497
            }
498
            switch_to_blog( $original_blog_id );
499
        }
500
501
        if ( wp_get_schedule( 'ao_cachechecker' ) ) {
502
            wp_clear_scheduled_hook( 'ao_cachechecker' );
503
        }
504
    }
505
506
    public static function notice_cache_unavailable()
507
    {
508
        echo '<div class="error"><p>';
509
        // Translators: %s is the cache directory location.
510
        printf( __( 'Autoptimize cannot write to the cache directory (%s), please fix to enable CSS/ JS optimization!', 'autoptimize' ), AUTOPTIMIZE_CACHE_DIR );
511
        echo '</p></div>';
512
    }
513
514
    public static function notice_installed()
515
    {
516
        echo '<div class="updated"><p>';
517
        _e( 'Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' );
518
        echo '</p></div>';
519
    }
520
521
    public static function notice_updated()
522
    {
523
        echo '<div class="updated"><p>';
524
        _e( 'Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' );
525
        echo '</p></div>';
526
    }
527
528
}
529