| 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 |
|