Completed
Push — master ( e85ac8...7dbd8d )
by Der Mundschenk
03:18
created

Site_Transients::delete()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
	public function get_keys_from_database() {
43
		/**
44
		 * WordPress database handler.
45
		 *
46
		 * @var \wpdb
47
		 */
48
		global $wpdb;
49
50
		$results = $wpdb->get_results(
51
			$wpdb->prepare(
52
				"SELECT meta_name FROM {$wpdb->sitemeta} WHERE meta_name like %s and site_id = %d",
53
				self::TRANSIENT_SQL_PREFIX . "{$this->get_prefix()}%", \get_current_network_id()
54
			),
55
			ARRAY_A
56
		); // WPCS: db call ok, cache ok.
57
58
		return \str_replace( self::TRANSIENT_SQL_PREFIX, '', \wp_list_pluck( $results, 'meta_name' ) );
59
	}
60
61
	/**
62
	 * Retrieves a cached value.
63
	 *
64
	 * @param string $key The cache key.
65
	 * @param bool   $raw Optional. Use the raw key name (i.e. don't call get_key). Default false.
66
	 *
67
	 * @return mixed
68
	 */
69
	public function get( $key, $raw = false ) {
70
		return \get_site_transient( $raw ? $key : $this->get_key( $key ) );
71
	}
72
73
	/**
74
	 * Sets an entry in the cache and stores the key.
75
	 *
76
	 * @param string $key       The cache key.
77
	 * @param mixed  $value     The value to store.
78
	 * @param int    $duration  Optional. The duration in seconds. Default 0 (no expiration).
79
	 * @param bool   $raw       Optional. Use the raw key name (i.e. don't call get_key). Default false.
80
	 *
81
	 * @return bool True if the cache could be set successfully.
82
	 */
83
	public function set( $key, $value, $duration = 0, $raw = false ) {
84
		return \set_site_transient( $raw ? $key : $this->get_key( $key ), $value, $duration );
85
	}
86
87
	/**
88
	 * Deletes an entry from the cache.
89
	 *
90
	 * @param string $key The cache key root.
91
	 * @param bool   $raw Optional. Use the raw key name (i.e. don't call get_key). Default false.
92
	 *
93
	 * @return bool True on successful removal, false on failure.
94
	 */
95
	public function delete( $key, $raw = false ) {
96
		return \delete_site_transient( $raw ? $key : $this->get_key( $key ) );
97
	}
98
}
99