PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Options/Repository.php +79 -5 1.4.32.3.2 View file →
@@ -8,8 +8,10 @@
8 8 declare( strict_types=1 );
9 9
10 10 namespace Packetery\Module\Options;
11 11
12 +use Packetery\Module\WpdbAdapter;
13 +
12 14 /**
13 15 * Class Repository.
14 16 *
15 17 * @package Packetery\Module\Options
@@ -16,15 +18,87 @@
16 18 */
17 19 class Repository {
18 20
19 21 /**
20 - * Get all packetery related options.
22 + * WpdbAdapter.
21 23 *
22 - * @return array|object|null
24 + * @var WpdbAdapter
23 25 */
24 - public function getPluginOptions() {
25 - global $wpdb;
26 + private $wpdbAdapter;
26 27
27 - return $wpdb->get_results( "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE 'packetery%'" );
28 + /**
29 + * Constructor.
30 + *
31 + * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
32 + */
33 + public function __construct( WpdbAdapter $wpdbAdapter ) {
34 + $this->wpdbAdapter = $wpdbAdapter;
28 35 }
29 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 + }
30 104 }