| @@ -34,13 +34,71 @@ | ||
| 34 | 34 | $this->wpdbAdapter = $wpdbAdapter; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | - * Get all packetery related options. | |
| 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 | |
| 39 | 62 | * |
| 40 | - * @return object[]|null | |
| 63 | + * @return string[] | |
| 41 | 64 | */ |
| 42 | - public function getPluginOptions(): ?array { | |
| 43 | - return $this->wpdbAdapter->get_results( 'SELECT `option_name` FROM `' . $this->wpdbAdapter->options . "` WHERE `option_name` LIKE 'packetery%'" ); | |
| 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; | |
| 44 | 83 | } |
| 45 | 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 | + } | |
| 46 | 104 | } |