PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.11
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.11
4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 3.8.0 All 110 releases
wp-optimize / optimizations / transient.php

transient.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 2.2.11, at optimizations/transient.php

485 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $available_for_saving = true;
10
11 public $auto_default = false;
12
13 public $ui_sort_order = 5000;
14
15 public $run_multisite = true;
16
17 public $support_preview = true;
18
19 private $found_count_all = 0;
20
21 /**
22 * Prepare data for preview widget.
23 *
24 * @param array $params
25 *
26 * @return array
27 */
28 public function preview($params) {
29
30 // get type of data for return single site or multisite.
31 $type = isset($params['type']) && 'multisite' == $params['type'] ? 'multisite' : 'single';
32
33 // set remove_all_transients for correctly handling "all" checkbox for preview transients.
34 if (isset($params['remove_all_transients'])) {
35 $this->data['remove_all_transients'] = $params['remove_all_transients'];
36 }
37
38 // get data requested for preview.
39 if ('single' == $type) {
40 $sql = $this->wpdb->prepare("
41 SELECT
42 a.option_id,
43 a.option_name,
44 SUBSTR(a.option_value, 1, 128) as option_value
45 FROM
46 " . $this->wpdb->options . " a
47 LEFT JOIN
48 " . $this->wpdb->options . " b
49 ON
50 b.option_name = CONCAT(
51 '_transient_timeout_',
52 SUBSTRING(
53 a.option_name,
54 CHAR_LENGTH('_transient_') + 1
55 )
56 )
57 WHERE
58 a.option_name LIKE '_transient_%' AND
59 a.option_name NOT LIKE '_transient_timeout_%'
60 ". ($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "").
61 " ORDER BY a.option_id LIMIT %d, %d;",
62 array(
63 $params['offset'],
64 $params['limit'],
65 )
66 );
67
68 $sql_count = "
69 SELECT
70 COUNT(*)
71 FROM
72 " . $this->wpdb->options . " a
73 LEFT JOIN
74 " . $this->wpdb->options . " b
75 ON
76 b.option_name = CONCAT(
77 '_transient_timeout_',
78 SUBSTRING(
79 a.option_name,
80 CHAR_LENGTH('_transient_') + 1
81 )
82 )
83 WHERE
84 a.option_name LIKE '_transient_%'
85 ". ($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
86 } else {
87 $sql = $this->wpdb->prepare("
88 SELECT
89 a.meta_id,
90 a.meta_key,
91 SUBSTR(a.meta_value, 1, 128) as meta_value
92 FROM
93 ".$this->wpdb->sitemeta." a
94 LEFT JOIN
95 ".$this->wpdb->sitemeta." b
96 ON
97 b.meta_key = CONCAT(
98 '_site_transient_timeout_',
99 SUBSTRING(
100 a.meta_key,
101 CHAR_LENGTH('_site_transient_') + 1
102 )
103 )
104 WHERE
105 a.meta_key LIKE '_site_transient_%' AND
106 a.meta_key NOT LIKE '_site_transient_timeout_%'
107 ".($this->remove_only_expired() ? " AND b.meta_value < UNIX_TIMESTAMP()" : "").
108 " ORDER BY a.meta_id LIMIT %d, %d;",
109 array(
110 $params['offset'],
111 $params['limit'],
112 )
113 );
114
115 $sql_count = "
116 SELECT
117 COUNT(*)
118 FROM
119 ".$this->wpdb->sitemeta." a
120 LEFT JOIN ".$this->wpdb->sitemeta." b
121 ON
122 b.meta_key = CONCAT(
123 '_site_transient_timeout_',
124 SUBSTRING(
125 a.meta_key,
126 CHAR_LENGTH('_site_transient_') + 1
127 )
128 )
129 WHERE
130 a.meta_key LIKE '_site_transient_%' AND
131 a.meta_key NOT LIKE '_site_transient_timeout_%'
132 ".($this->remove_only_expired() ? " AND b.meta_value < UNIX_TIMESTAMP()" : "");
133 }
134
135 $posts = $this->wpdb->get_results($sql, ARRAY_A);
136
137 $total = $this->wpdb->get_var($sql_count);
138
139 // define columns array depends on source type of request.
140 if ('single' == $type) {
141 $columns = array(
142 'option_id' => __('ID', 'wp-optimize'),
143 'option_name' => __('Name', 'wp-optimize'),
144 'option_value' => __('Value', 'wp-optimize'),
145 );
146 } else {
147 $columns = array(
148 'meta_id' => __('ID', 'wp-optimize'),
149 'meta_key' => __('Name', 'wp-optimize'),
150 'meta_value' => __('Value', 'wp-optimize'),
151 );
152 }
153
154 return array(
155 'id_key' => ('single' == $type) ? 'option_id' : 'meta_id',
156 'columns' => $columns,
157 'offset' => $params['offset'],
158 'limit' => $params['limit'],
159 'total' => $total,
160 'data' => $this->htmlentities_array($posts, array('option_id', 'meta_id')),
161 'message' => $total > 0 ? '' : __('No transient options found', 'wp-optimize'),
162 );
163 }
164
165 /**
166 * Do actions before optimize() function.
167 */
168 public function before_optimize() {
169 $this->processed_count = 0;
170 }
171
172 /**
173 * Do actions after optimize() function.
174 */
175 public function after_optimize() {
176
177 $message = sprintf(_n('%s transient option deleted', '%s transient options deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
178
179 if ($this->is_multisite_mode()) {
180 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
181 }
182
183 $this->logger->info($message);
184 $this->register_output($message);
185
186 // Delete transients from multisite, if configured as such.
187 if (is_multisite() && is_main_network()) {
188 if (isset($this->data['ids'])) {
189 // clean timeouts rows by transient option ids, before deleting transients.
190 // this is done for correct counting deleted transient options.
191 $clean2_timeouts = "
192 DELETE
193 b
194 FROM
195 " . $this->wpdb->sitemeta . " a
196 LEFT JOIN " . $this->wpdb->sitemeta . " b
197 ON
198 b.meta_key = CONCAT(
199 '_site_transient_timeout_',
200 SUBSTRING(
201 a.meta_key,
202 CHAR_LENGTH('_site_transient_') + 1
203 )
204 )
205 WHERE
206 a.meta_id IN (".join(',', $this->data['ids']).")";
207
208 // run clean timeouts query.
209 $this->query($clean2_timeouts);
210
211 // reset clean timeouts query to avoid future run.
212 $clean2_timeouts = '';
213
214 // clean transients rows by id.
215 $clean2 = "
216 DELETE
217 a
218 FROM
219 " . $this->wpdb->sitemeta . " a
220 WHERE
221 a.meta_id IN (".join(',', $this->data['ids']).")
222 ". ($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
223 } else {
224 $clean2 = "
225 DELETE
226 a
227 FROM
228 ".$this->wpdb->sitemeta." a, ".$this->wpdb->sitemeta." b
229 WHERE
230 a.meta_key LIKE '_site_transient_%' AND
231 a.meta_key NOT LIKE '_site_transient_timeout_%' AND
232 b.meta_key = CONCAT(
233 '_site_transient_timeout_',
234 SUBSTRING(
235 a.meta_key,
236 CHAR_LENGTH('_site_transient_') + 1
237 )
238 )
239 ".($this->remove_only_expired() ? " AND b.meta_value < UNIX_TIMESTAMP()" : "");
240
241 $clean2_timeouts = "
242 DELETE
243 b
244 FROM
245 " . $this->wpdb->options . " b
246 WHERE
247 b.option_name LIKE '_site_transient_timeout_%'
248 ".($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
249 }
250
251 $sitemeta_table_transients_deleted = $this->query($clean2);
252 if ('' != $clean2_timeouts) $this->query($clean2_timeouts);
253
254 $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));
255
256 $this->logger->info($message2);
257 $this->register_output($message2);
258 }
259 }
260
261 /**
262 * Optimize transients options
263 */
264 public function optimize() {
265
266 // if posted ids then build sql queries for deleting selected data.
267 if (isset($this->data['ids'])) {
268 // clean timeouts rows by transient option ids, before deleting transients.
269 // this is done for correct counting deleted transient options.
270 $clean_timeouts = "
271 DELETE
272 b
273 FROM
274 " . $this->wpdb->options . " a
275 LEFT JOIN " . $this->wpdb->options . " b
276 ON
277 b.option_name = CONCAT(
278 '_transient_timeout_',
279 SUBSTRING(
280 a.option_name,
281 CHAR_LENGTH('_transient_') + 1
282 )
283 )
284 WHERE
285 a.option_id IN (".join(',', $this->data['ids']).")";
286
287 // run clean timeouts query.
288 $this->query($clean_timeouts);
289
290 // reset clean timeouts query to avoid future run.
291 $clean_timeouts = '';
292
293 // clean transients rows by id.
294 $clean = "
295 DELETE
296 a
297 FROM
298 " . $this->wpdb->options . " a
299 WHERE
300 a.option_id IN (".join(',', $this->data['ids']).")
301 ". ($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
302 } else {
303 // clean transients rows.
304 $clean = "
305 DELETE
306 a
307 FROM
308 " . $this->wpdb->options . " a
309 LEFT JOIN " . $this->wpdb->options . " b
310 ON
311 b.option_name = CONCAT(
312 '_transient_timeout_',
313 SUBSTRING(
314 a.option_name,
315 CHAR_LENGTH('_transient_') + 1
316 )
317 )
318 WHERE
319 a.option_name LIKE '_transient_%' AND
320 a.option_name NOT LIKE '_transient_timeout_%'
321 ". ($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
322
323 // clean transient timeouts rows.
324 $clean_timeouts = "
325 DELETE
326 b
327 FROM
328 " . $this->wpdb->options . " b
329 WHERE
330 b.option_name LIKE '_transient_timeout_%'
331 ".($this->remove_only_expired() ? " AND b.option_value < UNIX_TIMESTAMP()" : "");
332 }
333
334 // run clean transients query and get count of deleted rows.
335 $options_table_transients_deleted = $this->query($clean);
336 $this->processed_count += $options_table_transients_deleted;
337
338 if ('' != $clean_timeouts) $this->query($clean_timeouts);
339 }
340
341 /**
342 * Do actions before get_info() function.
343 */
344 public function before_get_info() {
345 $this->found_count = 0;
346 $this->found_count_all = 0;
347 }
348
349 /**
350 * Do actions after get_info() function.
351 */
352 public function after_get_info() {
353
354 if (is_multisite() && is_main_network()) {
355 $sitemeta_table_sql = "
356 SELECT
357 COUNT(*)
358 FROM
359 ".$this->wpdb->sitemeta." a
360 LEFT JOIN
361 ".$this->wpdb->sitemeta." b
362 ON
363 b.meta_key = CONCAT(
364 '_site_transient_timeout_',
365 SUBSTRING(
366 a.meta_key,
367 CHAR_LENGTH('_site_transient_') + 1
368 )
369 )
370 WHERE
371 a.meta_key LIKE '_site_transient_%' AND
372 a.meta_key NOT LIKE '_site_transient_timeout_%'";
373
374 $expired_suffix_sql = " AND b.meta_value < UNIX_TIMESTAMP()";
375
376 // get count of expired transients.
377 $sitemeta_table_transients = $this->wpdb->get_var($sitemeta_table_sql . $expired_suffix_sql);
378 // get count of all transients.
379 $sitemeta_table_transients_all = $this->wpdb->get_var($sitemeta_table_sql);
380 } else {
381 $sitemeta_table_transients = 0;
382 $sitemeta_table_transients_all = 0;
383 }
384
385 if ($this->found_count_all > 0) {
386 $message = sprintf(_n('%1$d of %2$d transient option expired', '%1$d of %2$d transient options expired', $this->found_count_all, 'wp-optimize'), number_format_i18n($this->found_count), number_format_i18n($this->found_count_all));
387 } else {
388 $message = __('No transient options found', 'wp-optimize');
389 }
390
391 if ($this->is_multisite_mode()) {
392 $message .= ' ' . sprintf(_n('across %d site', 'across %d sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
393 }
394
395 // add preview link to $message.
396 if ($this->found_count_all > 0) {
397 $message = $this->get_preview_link($message, array('data-type' => 'single'));
398 }
399
400 $this->register_output($message);
401
402 if ($this->is_multisite_mode()) {
403 if ($sitemeta_table_transients_all > 0) {
404 $message2 = sprintf(_n('%1$d of %2$d network-wide transient option found', '%1$d of %2$d network-wide transient options found', $sitemeta_table_transients_all, 'wp-optimize'), number_format_i18n($sitemeta_table_transients), number_format_i18n($sitemeta_table_transients_all));
405 $message2 = $this->get_preview_link($message2, array('data-type' => 'multisite'));
406 } else {
407 $message2 = __('No site-wide transient options found', 'wp-optimize');
408 }
409
410 $this->register_output($message2);
411 }
412
413 // If any kind of transients exists then
414 if ($this->found_count_all > 0 || ($sitemeta_table_transients + $sitemeta_table_transients_all > 0)) {
415 $remove_all_transients = $this->options->get_option('remove_all_transients', 'false');
416 $this->register_output('<input id="remove_all_transients" name="remove_all_transients" type="checkbox" value="true" '.checked($remove_all_transients, 'true').'><label for="remove_all_transients" style="color: inherit;">'.__('Remove all transient options (not only expired)', 'wp-optimize').'</label>');
417 }
418 }
419
420 /**
421 * Returns info about possibility to optimize transient options.
422 */
423 public function get_info() {
424
425 $blogs = $this->get_optimization_blogs();
426
427 foreach ($blogs as $blog_id) {
428 $this->switch_to_blog($blog_id);
429
430 $options_table_sql = "
431 SELECT
432 COUNT(*)
433 FROM
434 " . $this->wpdb->options . " a
435 LEFT JOIN
436 " . $this->wpdb->options . " b
437 ON
438 b.option_name = CONCAT(
439 '_transient_timeout_',
440 SUBSTRING(
441 a.option_name,
442 CHAR_LENGTH('_transient_') + 1
443 )
444 )
445 WHERE
446 a.option_name LIKE '_transient_%' AND
447 a.option_name NOT LIKE '_transient_timeout_%'
448 ";
449
450 $expired_suffix_sql = " AND b.option_value < UNIX_TIMESTAMP()";
451
452 // get count of expired transients.
453 $options_table_transients = $this->wpdb->get_var($options_table_sql . $expired_suffix_sql);
454 $this->found_count += $options_table_transients;
455
456 // get count of all transients.
457 $options_table_transients = $this->wpdb->get_var($options_table_sql);
458 $this->found_count_all += $options_table_transients;
459 $this->restore_current_blog();
460 }
461
462 }
463
464 public function settings_label() {
465 return __('Remove expired transient options', 'wp-optimize');
466 }
467
468 public function get_auto_option_description() {
469 return __('Remove expired transient options', 'wp-optimize');
470 }
471
472 /**
473 * Check optimization param and return true if we should remove only expired transients.
474 *
475 * @return bool
476 */
477 private function remove_only_expired() {
478 if (isset($this->data['remove_all_transients']) && 'true' == $this->data['remove_all_transients']) {
479 return false;
480 }
481
482 return true;
483 }
484 }
485