| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Repository. |
| 4 |
* |
| 5 |
* @package Packetery\Module\Options |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Options; |
| 11 |
|
| 12 |
use Packetery\Module\WpdbAdapter; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Repository. |
| 16 |
* |
| 17 |
* @package Packetery\Module\Options |
| 18 |
*/ |
| 19 |
class Repository { |
| 20 |
|
| 21 |
/** |
| 22 |
* WpdbAdapter. |
| 23 |
* |
| 24 |
* @var WpdbAdapter |
| 25 |
*/ |
| 26 |
private $wpdbAdapter; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
* |
| 31 |
* @param WpdbAdapter $wpdbAdapter WpdbAdapter. |
| 32 |
*/ |
| 33 |
public function __construct( WpdbAdapter $wpdbAdapter ) { |
| 34 |
$this->wpdbAdapter = $wpdbAdapter; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @return string[] |
| 39 |
*/ |
| 40 |
public function getExpiredTransientsByPrefix( string $prefix ): array { |
| 41 |
$transientPrefix = sprintf( '_transient_%s', $prefix ); |
| 42 |
$transientTimeoutPrefix = sprintf( '_transient_timeout_%s', $prefix ); |
| 43 |
|
| 44 |
return $this->wpdbAdapter->get_col( |
| 45 |
$this->wpdbAdapter->prepare( |
| 46 |
"SELECT SUBSTRING(`a`.`option_name`, 12) |
| 47 |
FROM `{$this->wpdbAdapter->options}` `a` |
| 48 |
JOIN `{$this->wpdbAdapter->options}` `b` |
| 49 |
ON `b`.`option_name` = CONCAT('_transient_timeout_', SUBSTRING(`a`.`option_name`, 12)) |
| 50 |
WHERE `a`.`option_name` LIKE %s |
| 51 |
AND `a`.`option_name` NOT LIKE %s |
| 52 |
AND `b`.`option_value` < %d", |
| 53 |
$this->wpdbAdapter->escLike( $transientPrefix ) . '%', |
| 54 |
$this->wpdbAdapter->escLike( $transientTimeoutPrefix ) . '%', |
| 55 |
time() |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param string[] $prefixes |
| 62 |
* |
| 63 |
* @return string[] |
| 64 |
*/ |
| 65 |
public function getAllTransientsByPrefixes( array $prefixes ): array { |
| 66 |
if ( $prefixes === [] ) { |
| 67 |
return []; |
| 68 |
} |
| 69 |
|
| 70 |
$optionPrefixes = []; |
| 71 |
foreach ( $prefixes as $prefix ) { |
| 72 |
$optionPrefixes[] = '_transient_' . $prefix; |
| 73 |
} |
| 74 |
|
| 75 |
$transientNames = []; |
| 76 |
$result = $this->getAllOptionNamesByPrefixes( $optionPrefixes ); |
| 77 |
|
| 78 |
foreach ( $result as $optionName ) { |
| 79 |
$transientNames[] = preg_replace( '~^_transient_~', '', $optionName ); |
| 80 |
} |
| 81 |
|
| 82 |
return $transientNames; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param string[] $prefixes |
| 87 |
* |
| 88 |
* @return string[] |
| 89 |
*/ |
| 90 |
public function getAllOptionNamesByPrefixes( array $prefixes ): array { |
| 91 |
if ( $prefixes === [] ) { |
| 92 |
return []; |
| 93 |
} |
| 94 |
|
| 95 |
$prefixWhere = []; |
| 96 |
foreach ( $prefixes as $prefix ) { |
| 97 |
$prefixWhere[] = sprintf( '`option_name` LIKE "%s"', $this->wpdbAdapter->escLike( $prefix ) . '%' ); |
| 98 |
} |
| 99 |
|
| 100 |
$prefixWhereDisjunction = implode( ' OR ', $prefixWhere ); |
| 101 |
|
| 102 |
return $this->wpdbAdapter->get_col( "SELECT `option_name` FROM `{$this->wpdbAdapter->options}` WHERE {$prefixWhereDisjunction}" ); |
| 103 |
} |
| 104 |
} |
| 105 |
|