| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class TransientPurger. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Options; |
| 11 |
|
| 12 |
use Packetery\Module\Framework\WpAdapter; |
| 13 |
use Packetery\Module\Plugin; |
| 14 |
use Packetery\Module\Transients; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class TransientPurger. |
| 18 |
* |
| 19 |
* @package Packetery |
| 20 |
*/ |
| 21 |
class TransientPurger { |
| 22 |
/** |
| 23 |
* @var Repository |
| 24 |
*/ |
| 25 |
private $optionsRepository; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var WpAdapter |
| 29 |
*/ |
| 30 |
private $wpAdapter; |
| 31 |
|
| 32 |
public function __construct( Repository $optionsRepository, WpAdapter $wpAdapter ) { |
| 33 |
$this->optionsRepository = $optionsRepository; |
| 34 |
$this->wpAdapter = $wpAdapter; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Deletes expired transients. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function purge(): void { |
| 43 |
if ( $this->wpAdapter->isMultisite() ) { |
| 44 |
$sites = Plugin::getSites(); |
| 45 |
foreach ( $sites as $site ) { |
| 46 |
$this->wpAdapter->switchToBlog( $site ); |
| 47 |
$this->purgeForSite(); |
| 48 |
$this->wpAdapter->restoreCurrentBlog(); |
| 49 |
} |
| 50 |
} else { |
| 51 |
$this->purgeForSite(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
private function purgeForSite(): void { |
| 56 |
$transients = $this->optionsRepository->getExpiredTransientsByPrefix( Transients::CHECKOUT_DATA_PREFIX ); |
| 57 |
foreach ( $transients as $transient ) { |
| 58 |
$this->wpAdapter->deleteTransient( $transient ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|