autoptimizeCacheChecker::setup()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 8
nop 0
dl 0
loc 17
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * CacheChecker - new in AO 2.0
4
 *
5
 * Daily cronned job (filter to change freq. + filter to disable).
6
 * Checks if cachesize is > 0.5GB (size is filterable), if so, an option is set which controls showing an admin notice.
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
    exit;
11
}
12
13
class autoptimizeCacheChecker
14
{
15
    const SCHEDULE_HOOK = 'ao_cachechecker';
16
17
    public function __construct()
18
    {
19
    }
20
21
    public function run()
22
    {
23
        $this->add_hooks();
24
    }
25
26
    public function add_hooks()
27
    {
28
        if ( is_admin() ) {
29
            add_action( 'plugins_loaded', array( $this, 'setup' ) );
30
        }
31
        add_action( self::SCHEDULE_HOOK, array( $this, 'cronjob' ) );
32
        add_action( 'admin_notices', array( $this, 'show_admin_notice' ) );
33
    }
34
35
    public function setup()
36
    {
37
        $do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true );
38
        $schedule       = wp_get_schedule( self::SCHEDULE_HOOK );
39
        $frequency      = apply_filters( 'autoptimize_filter_cachecheck_frequency', 'twicedaily' );
40
        if ( ! in_array( $frequency, array( 'hourly', 'twicedaily', 'daily', 'weekly', 'monthly' ) ) ) {
41
            $frequency = 'twicedaily';
42
        }
43
        if ( $do_cache_check && ( ! $schedule || $schedule !== $frequency ) ) {
44
            if ( $schedule ) {
45
                wp_clear_scheduled_hook( self::SCHEDULE_HOOK );
46
            }
47
            wp_schedule_event( time(), $frequency, self::SCHEDULE_HOOK );
48
        } elseif ( $schedule && ! $do_cache_check ) {
49
            wp_clear_scheduled_hook( self::SCHEDULE_HOOK );
50
        }
51
    }
52
53
    public function cronjob()
54
    {
55
        // Check cachesize and act accordingly.
56
        $max_size       = (int) apply_filters( 'autoptimize_filter_cachecheck_maxsize', 536870912 );
57
        $do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true );
58
        $stat_array     = autoptimizeCache::stats();
59
        $cache_size     = round( $stat_array[1] );
60
        if ( ( $cache_size > $max_size ) && ( $do_cache_check ) ) {
61
            autoptimizeOptionWrapper::update_option( 'autoptimize_cachesize_notice', true );
62
            if ( apply_filters( 'autoptimize_filter_cachecheck_sendmail', true ) ) {
63
                $home_url  = esc_url( home_url() );
64
                $ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', autoptimizeOptionWrapper::get_option( 'admin_email', '' ) );
65
66
                $ao_mailsubject = __( 'Autoptimize cache size warning', 'autoptimize' ) . ' (' . $home_url . ')';
67
                $ao_mailbody    = __( 'Autoptimize\'s cache size is getting big, consider purging the cache. Have a look at https://wordpress.org/plugins/autoptimize/faq/ to see how you can keep the cache size under control.', 'autoptimize' ) . ' (site: ' . $home_url . ')';
68
69
                if ( ! empty( $ao_mailto ) ) {
70
                    $ao_mailresult = wp_mail( $ao_mailto, $ao_mailsubject, $ao_mailbody );
71
                    if ( ! $ao_mailresult ) {
72
                        error_log( 'Autoptimize could not send cache size warning mail.' );
73
                    }
74
                }
75
            }
76
        }
77
78
        // Check if 3rd party services (e.g. image proxy) are up.
79
        autoptimizeUtils::check_service_availability();
80
81
        // Nukes advanced cache clearing artifacts if they exists...
82
        autoptimizeCache::delete_advanced_cache_clear_artifacts();
83
84
        // Check image optimization stats.
85
        autoptimizeImages::instance()->query_img_provider_stats();
86
    }
87
88
    public function show_admin_notice()
89
    {
90
        if ( (bool) autoptimizeOptionWrapper::get_option( 'autoptimize_cachesize_notice', false ) && current_user_can( 'manage_options' ) ) {
91
            echo '<div class="notice notice-warning"><p>';
92
            _e( '<strong>Autoptimize\'s cache size is getting big</strong>, consider purging the cache. Have a look at <a href="https://wordpress.org/plugins/autoptimize/faq/" target="_blank" rel="noopener noreferrer">the Autoptimize FAQ</a> to see how you can keep the cache size under control.', 'autoptimize' );
93
            echo '</p></div>';
94
            autoptimizeOptionWrapper::update_option( 'autoptimize_cachesize_notice', false );
95
        }
96
97
        // Notice for image proxy usage.
98
        $_imgopt_notice = autoptimizeImages::instance()->get_imgopt_status_notice_wrapper();
99
        if ( current_user_can( 'manage_options' ) && is_array( $_imgopt_notice ) && array_key_exists( 'status', $_imgopt_notice ) && in_array( $_imgopt_notice['status'], array( 1, -1, -2, -3 ) ) ) {
100
            $_dismissible = 'ao-img-opt-notice-';
101
            $_hide_notice = '7';
102
103
            if ( -1 == $_imgopt_notice['status'] || -2 == $_imgopt_notice['status'] || -3 == $_imgopt_notice['status'] ) {
104
                $_hide_notice = '1';
105
            }
106
107
            $_imgopt_notice_dismissible = apply_filters( 'autoptimize_filter_imgopt_notice_dismissable', $_dismissible . $_hide_notice );
108
109
            if ( $_imgopt_notice && PAnD::is_admin_notice_active( $_imgopt_notice_dismissible ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $_imgopt_notice of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110
                echo '<div class="notice notice-warning is-dismissible" data-dismissible="' . $_imgopt_notice_dismissible . '"><p><strong>' . __( 'Autoptimize', 'autoptimize' ) . '</strong>: ' . $_imgopt_notice['notice'] . '</p></div>';
111
            }
112
        }
113
    }
114
}
115