Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.48% covered (danger)
43.48%
20 / 46
30.77% covered (danger)
30.77%
4 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tus_Abstract_Cache
43.18% covered (danger)
43.18%
19 / 44
30.77% covered (danger)
30.77%
4 / 13
120.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cache_get
n/a
0 / 0
n/a
0 / 0
0
 cache_set
n/a
0 / 0
n/a
0 / 0
0
 cache_delete
n/a
0 / 0
n/a
0 / 0
0
 cache_keys
n/a
0 / 0
n/a
0 / 0
0
 build_key
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
4.84
 delete
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 set
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 keys
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 delete_all
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 get_ttl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_ttl
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 set_prefix
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_prefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 log
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_content_expired
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Main
4 *
5 * @package VideoPressUploader
6 **/
7
8namespace VideoPressUploader;
9
10// Avoid direct calls to this file.
11if ( ! defined( 'ABSPATH' ) ) {
12    die( 0 );
13}
14
15/**
16 * Abstract cache class.
17 **/
18abstract class Tus_Abstract_Cache {
19
20    /**
21     * Date format standard.
22     *
23     * @see https://tools.ietf.org/html/rfc7231#section-7.1.1.1
24     *
25     * @var int
26     **/
27    const RFC_7231 = 'D, d M Y H:i:s \G\M\T';
28
29    /**
30     * TTL in secs (default 1 day).
31     *
32     * @var int
33     **/
34    protected $ttl = 86400;
35
36    /** Prefix for cache keys.
37     *
38     * @var string
39     **/
40    protected $prefix = 'videopress_uploader_1_';
41
42    /**
43     * Various blog_id.
44     *
45     * @var string
46     */
47    protected $blog_id = 'simple';
48
49    /**
50     * Cache constructor.
51     *
52     * @param int|string $blog_id The blog_id.
53     */
54    public function __construct( $blog_id ) {
55        $this->blog_id = (string) $blog_id;
56    }
57
58    /**
59     * Cache Get key.
60     *
61     * @param string $key The blog_id.
62     *
63     * @return mixed|null
64     */
65    abstract protected function cache_get( $key );
66
67    /**
68     * Set data to the given key.
69     *
70     * @param string $key The key.
71     * @param mixed  $value The value.
72     * @param bool   $is_update Is update.
73     *
74     * @return mixed
75     */
76    abstract public function cache_set( $key, $value, $is_update );
77
78    /**
79     * Delete data associated with the key.
80     *
81     * @param string $key The key.
82     *
83     * @return bool
84     */
85    abstract public function cache_delete( $key );
86
87    /**
88     * Get cache keys.
89     *
90     * @param string $keys_prefix The key prefix.
91     *
92     * @return mixed
93     */
94    abstract public function cache_keys( $keys_prefix );
95
96    /**
97     * Build a cache key.
98     *
99     * @param string $key Key.
100     *
101     * @return string
102     */
103    public function build_key( $key ) {
104        $prefix = $this->get_prefix();
105
106        if ( ! str_starts_with( $key, $prefix ) ) {
107            $key = $prefix . $key;
108        }
109
110        return $key;
111    }
112
113    /**
114     * Get key.
115     *
116     * @param string $key The blog_id.
117     * @param bool   $with_expired Even get the expired keys.
118     *
119     * @return mixed|null
120     */
121    public function get( $key, $with_expired = false ) {
122        $key = $this->build_key( $key );
123
124        $contents_str = $this->cache_get( $key );
125        $contents     = json_decode( $contents_str, true );
126
127        if ( ! $contents ) {
128            return null;
129        }
130
131        if ( $with_expired ) {
132            return $contents;
133        }
134
135        return $this->is_content_expired( $contents ) ? null : $contents;
136    }
137
138    /**
139     * Deletes a key.
140     *
141     * @param string $key The key.
142     *
143     * @return mixed
144     */
145    public function delete( $key ) {
146        $key = $this->build_key( $key );
147        return $this->cache_delete( $key ) > 0;
148    }
149
150    /**
151     * Set cache key.
152     *
153     * @param string      $key The key.
154     * @param array|mixed $value Even get the expired key.
155     *
156     * @return bool
157     */
158    public function set( $key, $value ) {
159        $key        = $this->build_key( $key );
160        $cache_data = $this->get( $key );
161        $contents   = $cache_data ? $cache_data : array();
162
163        if ( \is_array( $value ) ) {
164            $contents = array_merge( $contents, $value );
165        } else {
166            $contents[] = $value;
167        }
168
169        $status = $this->cache_set( $key, \wp_json_encode( $contents, JSON_UNESCAPED_SLASHES ), ! empty( $cache_data ) );
170        return false !== $status;
171    }
172
173    /**
174     * Get cache keys.
175     *
176     * @return mixed
177     */
178    public function keys() {
179        // TODO: This needs more thought.
180        $keys_entry = $this->cache_keys( 'videopress_cache_keys_blog_' . $this->blog_id );
181        if ( is_string( $keys_entry ) ) {
182            return json_decode( $keys_entry );
183        }
184        return $keys_entry;
185    }
186
187    /**
188     * Delete all data associated with the keys.
189     *
190     * @param array $keys The keys to delete.
191     *
192     * @return bool
193     */
194    public function delete_all( $keys ) {
195        $status = true;
196
197        foreach ( $keys as $key ) {
198            $r      = $this->delete( $this->build_key( $key ) );
199            $status = $status && $r;
200        }
201
202        return $status;
203    }
204
205    /**
206     * Get time to live.
207     *
208     * @return int
209     */
210    public function get_ttl() {
211        return $this->ttl;
212    }
213
214    /**
215     * Set time to live.
216     *
217     * @param int $secs The ttl.
218     *
219     * @return self
220     */
221    public function set_ttl( $secs ) {
222        $this->ttl = $secs;
223
224        return $this;
225    }
226
227    /**
228     * Set cache prefix.
229     *
230     * @param string $prefix The prefix.
231     *
232     * @return self
233     */
234    public function set_prefix( $prefix ) {
235        $this->prefix = $prefix;
236        return $this;
237    }
238
239    /**
240     * Get cache prefix.
241     *
242     * @return string
243     */
244    public function get_prefix() {
245        /**
246         * Filters the cache prefix that will be used for VideoPress Uploader.
247         *
248         * @param string $cache_prefix The cache prefix.
249         *
250         * @return string
251         */
252        return apply_filters( 'videopress_uploader_cache_prefix', $this->prefix );
253    }
254
255    /**
256     * Log stuff
257     *
258     * @param string $what Stuff to log.
259     */
260    protected function log( $what ) {
261        // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
262        do_action( 'videopress_uploader_log', 'info', get_class( $this ) . ' Cache Store: \n\n' . print_r( $what, true ) );
263    }
264
265    /**
266     * Checks if content has an expires_at entry and compares to time().
267     *
268     * @param array $contents The contents.
269     *
270     * @return bool
271     **/
272    protected function is_content_expired( $contents ) {
273        if ( ! isset( $contents['expires_at'] ) ) {
274            return false;
275        }
276
277        $expires_at = $contents['expires_at'];
278        $date       = Tus_Date_Utils::date_utc( $expires_at );
279
280        return absint( $date->getTimestamp() ) < time();
281    }
282}