PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
4.7.0 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 All 111 releases
wp-optimize / optimizations / transient.php

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

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