| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_transient extends WP_Optimization { |
| 6 |
|
| 7 |
public $available_for_auto = true; |
| 8 |
|
| 9 |
public $auto_default = false; |
| 10 |
|
| 11 |
public $ui_sort_order = 5000; |
| 12 |
|
| 13 |
public $run_multisite = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Do actions before optimize() function. |
| 17 |
*/ |
| 18 |
public function before_optimize() { |
| 19 |
$this->processed_count = 0; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Do actions after optimize() function. |
| 24 |
*/ |
| 25 |
public function after_optimize() { |
| 26 |
|
| 27 |
$message = sprintf(_n('%s transient option deleted', '%s transient options deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 28 |
|
| 29 |
if ($this->is_multisite_mode()) { |
| 30 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 31 |
} |
| 32 |
|
| 33 |
$this->logger->info($message); |
| 34 |
$this->register_output($message); |
| 35 |
|
| 36 |
// Delete transients from multisite, if configured as such. |
| 37 |
if (is_multisite() && is_main_network()) { |
| 38 |
$clean2 = " |
| 39 |
DELETE |
| 40 |
a |
| 41 |
FROM |
| 42 |
".$this->wpdb->sitemeta." a, ".$this->wpdb->sitemeta." b |
| 43 |
WHERE |
| 44 |
a.meta_key LIKE '_site_transient_%' AND |
| 45 |
a.meta_key NOT LIKE '_site_transient_timeout_%' AND |
| 46 |
b.meta_key = CONCAT( |
| 47 |
'_site_transient_timeout_', |
| 48 |
SUBSTRING( |
| 49 |
a.meta_key, |
| 50 |
CHAR_LENGTH('_site_transient_') + 1 |
| 51 |
) |
| 52 |
) |
| 53 |
AND b.meta_value < UNIX_TIMESTAMP() |
| 54 |
"; |
| 55 |
|
| 56 |
$sitemeta_table_transients_deleted = $this->query($clean2); |
| 57 |
|
| 58 |
$clean2_timeouts = " |
| 59 |
DELETE |
| 60 |
b |
| 61 |
FROM |
| 62 |
" . $this->wpdb->options . " b |
| 63 |
WHERE |
| 64 |
b.option_name LIKE '_site_transient_timeout_%' AND |
| 65 |
b.option_value < UNIX_TIMESTAMP() |
| 66 |
"; |
| 67 |
|
| 68 |
$this->query($clean2_timeouts); |
| 69 |
|
| 70 |
$message2 = sprintf(_n('%s network-wide transient option deleted', '%s network-wide transient options deleted', $sitemeta_table_transients_deleted, 'wp-optimize'), number_format_i18n($sitemeta_table_transients_deleted)); |
| 71 |
|
| 72 |
$this->logger->info($message2); |
| 73 |
$this->register_output($message2); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Optimize transients options |
| 79 |
*/ |
| 80 |
public function optimize() { |
| 81 |
|
| 82 |
// clean transients rows. |
| 83 |
$clean = " |
| 84 |
DELETE |
| 85 |
a |
| 86 |
FROM |
| 87 |
" . $this->wpdb->options . " a, " . $this->wpdb->options . " b |
| 88 |
WHERE |
| 89 |
a.option_name LIKE '_transient_%' AND |
| 90 |
b.option_name = CONCAT( |
| 91 |
'_transient_timeout_', |
| 92 |
SUBSTRING( |
| 93 |
a.option_name, |
| 94 |
CHAR_LENGTH('_transient_') + 1 |
| 95 |
) |
| 96 |
) |
| 97 |
AND b.option_value < UNIX_TIMESTAMP() |
| 98 |
"; |
| 99 |
|
| 100 |
$options_table_transients_deleted = $this->query($clean); |
| 101 |
$this->processed_count += $options_table_transients_deleted; |
| 102 |
|
| 103 |
// clean transient timeouts rows. |
| 104 |
$clean_timeouts = " |
| 105 |
DELETE |
| 106 |
b |
| 107 |
FROM |
| 108 |
" . $this->wpdb->options . " b |
| 109 |
WHERE |
| 110 |
b.option_name LIKE '_transient_timeout_%' AND |
| 111 |
b.option_value < UNIX_TIMESTAMP() |
| 112 |
"; |
| 113 |
|
| 114 |
$this->query($clean_timeouts); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Do actions before get_info() function. |
| 119 |
*/ |
| 120 |
public function before_get_info() { |
| 121 |
$this->found_count = 0; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Do actions after get_info() function. |
| 126 |
*/ |
| 127 |
public function after_get_info() { |
| 128 |
|
| 129 |
if (is_multisite() && is_main_network()) { |
| 130 |
$sitemeta_table_sql = " |
| 131 |
SELECT |
| 132 |
COUNT(*) |
| 133 |
FROM |
| 134 |
".$this->wpdb->sitemeta." a, ".$this->wpdb->sitemeta." b |
| 135 |
WHERE |
| 136 |
a.meta_key LIKE '_site_transient_%' AND |
| 137 |
a.meta_key NOT LIKE '_site_transient_timeout_%' AND |
| 138 |
b.meta_key = CONCAT( |
| 139 |
'_site_transient_timeout_', |
| 140 |
SUBSTRING( |
| 141 |
a.meta_key, |
| 142 |
CHAR_LENGTH('_site_transient_') + 1 |
| 143 |
) |
| 144 |
) |
| 145 |
AND b.meta_value < UNIX_TIMESTAMP() |
| 146 |
"; |
| 147 |
|
| 148 |
$sitemeta_table_transients = $this->wpdb->get_var($sitemeta_table_sql); |
| 149 |
} else { |
| 150 |
$sitemeta_table_transients = 0; |
| 151 |
} |
| 152 |
|
| 153 |
if ($this->found_count > 0) { |
| 154 |
$message = sprintf(_n('%s transient option found', '%s transient options found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 155 |
} else { |
| 156 |
$message = __('No transient options found', 'wp-optimize'); |
| 157 |
} |
| 158 |
|
| 159 |
if ($this->is_multisite_mode()) { |
| 160 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 161 |
} |
| 162 |
|
| 163 |
$this->register_output($message); |
| 164 |
|
| 165 |
if ($this->is_multisite_mode()) { |
| 166 |
if ($sitemeta_table_transients > 0) { |
| 167 |
$message2 = sprintf(_n('%d network-wide transient option found', '%d network-wide transient options found', $sitemeta_table_transients, 'wp-optimize'), number_format_i18n($sitemeta_table_transients)); |
| 168 |
} else { |
| 169 |
$message2 = __('No site-wide transient options found', 'wp-optimize'); |
| 170 |
} |
| 171 |
|
| 172 |
$this->register_output($message2); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns info about possibility to optimize transient options. |
| 178 |
*/ |
| 179 |
public function get_info() { |
| 180 |
|
| 181 |
$blogs = $this->get_optimization_blogs(); |
| 182 |
|
| 183 |
foreach ($blogs as $blog_id) { |
| 184 |
$this->switch_to_blog($blog_id); |
| 185 |
|
| 186 |
$options_table_sql = " |
| 187 |
SELECT |
| 188 |
COUNT(*) |
| 189 |
FROM |
| 190 |
" . $this->wpdb->options . " a, " . $this->wpdb->options . " b |
| 191 |
WHERE |
| 192 |
a.option_name LIKE '_transient_%' AND |
| 193 |
a.option_name NOT LIKE '_transient_timeout_%' AND |
| 194 |
b.option_name = CONCAT( |
| 195 |
'_transient_timeout_', |
| 196 |
SUBSTRING( |
| 197 |
a.option_name, |
| 198 |
CHAR_LENGTH('_transient_') + 1 |
| 199 |
) |
| 200 |
) |
| 201 |
AND b.option_value < UNIX_TIMESTAMP()"; |
| 202 |
|
| 203 |
$options_table_transients = $this->wpdb->get_var($options_table_sql); |
| 204 |
$this->found_count += $options_table_transients; |
| 205 |
$this->restore_current_blog(); |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
public function settings_label() { |
| 211 |
return __('Remove expired transient options', 'wp-optimize'); |
| 212 |
} |
| 213 |
|
| 214 |
public function get_auto_option_description() { |
| 215 |
return __('Remove expired transient options', 'wp-optimize'); |
| 216 |
} |
| 217 |
} |
| 218 |
|