1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of mundschenk-at/wp-data-storage. |
4
|
|
|
* |
5
|
|
|
* Copyright 2018 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or ( at your option ) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* @package mundschenk-at/wp-data-storage/tests |
22
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Mundschenk\Data_Storage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Implements an interface to the WordPress Site Transients API. |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* |
32
|
|
|
* @author Peter Putzer <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Site_Transients extends Transients { |
35
|
|
|
const TRANSIENT_SQL_PREFIX = '_site_transient_'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retrieves a list of transients set by the plugin from the options table. |
39
|
|
|
* |
40
|
|
|
* @return string[] |
41
|
|
|
*/ |
42
|
2 |
|
public function get_keys_from_database() { |
43
|
|
|
// If we are not running on multisite, fall back to the parent implementation. |
44
|
2 |
|
if ( ! is_multisite() ) { |
45
|
1 |
|
return parent::get_keys_from_database(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* WordPress database handler. |
50
|
|
|
* |
51
|
|
|
* @var \wpdb |
52
|
|
|
*/ |
53
|
1 |
|
global $wpdb; |
54
|
|
|
|
55
|
1 |
|
$results = $wpdb->get_results( |
56
|
1 |
|
$wpdb->prepare( |
57
|
1 |
|
"SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key like %s and site_id = %d", |
58
|
1 |
|
self::TRANSIENT_SQL_PREFIX . "{$this->get_prefix()}%", |
59
|
1 |
|
\get_current_network_id() |
60
|
|
|
), |
61
|
1 |
|
ARRAY_A |
62
|
|
|
); // WPCS: db call ok, cache ok. |
63
|
|
|
|
64
|
1 |
|
return \str_replace( self::TRANSIENT_SQL_PREFIX, '', \wp_list_pluck( $results, 'meta_key' ) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieves a cached value. |
69
|
|
|
* |
70
|
|
|
* @param string $key The cache key. |
71
|
|
|
* @param bool $raw Optional. Use the raw key name (i.e. don't call get_key). Default false. |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
1 |
|
public function get( $key, $raw = false ) { |
76
|
1 |
|
return \get_site_transient( $raw ? $key : $this->get_key( $key ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets an entry in the cache and stores the key. |
81
|
|
|
* |
82
|
|
|
* @param string $key The cache key. |
83
|
|
|
* @param mixed $value The value to store. |
84
|
|
|
* @param int $duration Optional. The duration in seconds. Default 0 (no expiration). |
85
|
|
|
* @param bool $raw Optional. Use the raw key name (i.e. don't call get_key). Default false. |
86
|
|
|
* |
87
|
|
|
* @return bool True if the cache could be set successfully. |
88
|
|
|
*/ |
89
|
1 |
|
public function set( $key, $value, $duration = 0, $raw = false ) { |
90
|
1 |
|
return \set_site_transient( $raw ? $key : $this->get_key( $key ), $value, $duration ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Deletes an entry from the cache. |
95
|
|
|
* |
96
|
|
|
* @param string $key The cache key root. |
97
|
|
|
* @param bool $raw Optional. Use the raw key name (i.e. don't call get_key). Default false. |
98
|
|
|
* |
99
|
|
|
* @return bool True on successful removal, false on failure. |
100
|
|
|
*/ |
101
|
1 |
|
public function delete( $key, $raw = false ) { |
102
|
1 |
|
return \delete_site_transient( $raw ? $key : $this->get_key( $key ) ); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|