PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Action / ClearTransient.php

ClearTransient.php in ManageWP Worker 4.9.25, at src/MWP/Action/ClearTransient.php

81 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_Action_ClearTransient extends MWP_Action_Abstract
12 {
13 public function execute(array $params = array())
14 {
15 $total = array(
16 'deletedTransients' => 0,
17 'deletedTransientTimeouts' => 0,
18 );
19
20 if (is_array($params['transient'])) {
21 foreach ($params['transient'] as $transient) {
22 $cleared = $this->clearTransients($params['prefix'], $transient['name'], $transient['suffix'], $transient['timeout'], $transient['mask'], $transient['limit']);
23 $total['deletedTransients'] += $cleared['deletedTransients'];
24 $total['deletedTransientTimeouts'] += $cleared['deletedTransientTimeouts'];
25 }
26 }
27
28 return $total;
29 }
30
31 private function clearTransients($prefix, $transientType, $suffix, $timeout, $mask, $limit)
32 {
33 $wpdb = $this->container->getWordPressContext()->getDb();
34
35 $timeoutName = $transientType.$suffix;
36 $subStrLength = strlen($timeoutName) + 1;
37
38 $escapedTimeoutName = str_replace('_', '\_', $timeoutName);
39
40 $selectTimeOutedTransients = <<<SQL
41 SELECT SUBSTR(option_name, {$subStrLength}) AS transient_name FROM {$prefix}options WHERE option_name LIKE '{$escapedTimeoutName}{$mask}' AND option_value < {$timeout} LIMIT {$limit}
42 SQL;
43
44 $transientsToDelete = $wpdb->get_col($selectTimeOutedTransients);
45 $timeoutsToDelete = array();
46
47 if (count($transientsToDelete) === 0) {
48 return array(
49 'deletedTransients' => 0,
50 'deletedTransientTimeouts' => 0,
51 );
52 }
53
54 foreach ($transientsToDelete as &$transient) {
55 $timeoutsToDelete[] = "'".$timeoutName.$transient."'";
56 $transient = "'".$transientType.$transient."'";
57 }
58
59 $deleteQuery = implode(',', $transientsToDelete);
60
61 $deleteTransients = <<<SQL
62 DELETE FROM {$prefix}options WHERE option_name IN ({$deleteQuery})
63 SQL;
64
65 $deletedTransients = $wpdb->query($deleteTransients);
66
67 $deleteQuery = implode(',', $timeoutsToDelete);
68
69 $deleteTransients = <<<SQL
70 DELETE FROM {$prefix}options WHERE option_name IN ({$deleteQuery})
71 SQL;
72
73 $deletedTransientTimeouts = $wpdb->query($deleteTransients);
74
75 return array(
76 'deletedTransients' => $deletedTransients,
77 'deletedTransientTimeouts' => $deletedTransientTimeouts,
78 );
79 }
80 }
81