|
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', 'daily' ); |
|
40
|
|
|
if ( ! in_array( $frequency, array( 'hourly', 'daily', 'weekly', 'monthly' ) ) ) { |
|
41
|
|
|
$frequency = 'daily'; |
|
42
|
|
|
} |
|
43
|
|
|
if ( $do_cache_check && ( ! $schedule || $schedule !== $frequency ) ) { |
|
44
|
|
|
wp_schedule_event( time(), $frequency, self::SCHEDULE_HOOK ); |
|
45
|
|
|
} elseif ( $schedule && ! $do_cache_check ) { |
|
46
|
|
|
wp_clear_scheduled_hook( self::SCHEDULE_HOOK ); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function cronjob() |
|
51
|
|
|
{ |
|
52
|
|
|
// Check cachesize and act accordingly. |
|
53
|
|
|
$max_size = (int) apply_filters( 'autoptimize_filter_cachecheck_maxsize', 536870912 ); |
|
54
|
|
|
$do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true ); |
|
55
|
|
|
$stat_array = autoptimizeCache::stats(); |
|
56
|
|
|
$cache_size = round( $stat_array[1] ); |
|
57
|
|
|
if ( ( $cache_size > $max_size ) && ( $do_cache_check ) ) { |
|
58
|
|
|
update_option( 'autoptimize_cachesize_notice', true ); |
|
59
|
|
|
if ( apply_filters( 'autoptimize_filter_cachecheck_sendmail', true ) ) { |
|
60
|
|
|
$site_url = esc_url( site_url() ); |
|
61
|
|
|
$ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', get_option( 'admin_email', '' ) ); |
|
62
|
|
|
|
|
63
|
|
|
$ao_mailsubject = __( 'Autoptimize cache size warning', 'autoptimize' ) . ' (' . $site_url . ')'; |
|
64
|
|
|
$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: ' . $site_url . ')'; |
|
65
|
|
|
|
|
66
|
|
|
if ( ! empty( $ao_mailto ) ) { |
|
67
|
|
|
$ao_mailresult = wp_mail( $ao_mailto, $ao_mailsubject, $ao_mailbody ); |
|
68
|
|
|
if ( ! $ao_mailresult ) { |
|
69
|
|
|
error_log( 'Autoptimize could not send cache size warning mail.' ); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Check if 3rd party services (e.g. image proxy) are up. |
|
76
|
|
|
autoptimizeUtils::check_service_availability(); |
|
77
|
|
|
|
|
78
|
|
|
// Check image optimization stats. |
|
79
|
|
|
autoptimizeExtra::get_img_provider_stats(); |
|
80
|
|
|
|
|
81
|
|
|
// Nukes advanced cache clearing artifacts if they exists... |
|
82
|
|
|
autoptimizeCache::delete_advanced_cache_clear_artifacts(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function show_admin_notice() |
|
86
|
|
|
{ |
|
87
|
|
|
// fixme: make notices dismissable. |
|
88
|
|
|
if ( (bool) get_option( 'autoptimize_cachesize_notice', false ) ) { |
|
89
|
|
|
echo '<div class="notice notice-warning"><p>'; |
|
90
|
|
|
_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' ); |
|
91
|
|
|
echo '</p></div>'; |
|
92
|
|
|
update_option( 'autoptimize_cachesize_notice', false ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Notice for image proxy usage. |
|
96
|
|
|
$_imgopt_notice = autoptimizeExtra::get_imgopt_status_notice_wrapper(); |
|
97
|
|
|
if ( is_array( $_imgopt_notice ) && array_key_exists( 'status', $_imgopt_notice ) && in_array( $_imgopt_notice['status'], array( 1, -1 ) ) ) { |
|
98
|
|
|
$_dismissible = 'ao-img-opt-notice-'; |
|
99
|
|
|
$_hide_notice = '7'; |
|
100
|
|
|
|
|
101
|
|
|
if ( -1 == $_imgopt_notice['status'] ) { |
|
102
|
|
|
$_hide_notice = '1'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$_imgopt_notice_dismissible = apply_filters( 'autoptimize_filter_imgopt_notice_dismissable', $_dismissible . $_hide_notice ); |
|
106
|
|
|
|
|
107
|
|
|
if ( $_imgopt_notice && PAnD::is_admin_notice_active( $_imgopt_notice_dismissible ) ) { |
|
|
|
|
|
|
108
|
|
|
echo '<div class="notice notice-warning is-dismissible" data-dismissible="' . $_imgopt_notice_dismissible . '"><p>' . $_imgopt_notice['notice'] . '</p></div>'; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.