PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 2.2
Vimeography: Vimeo Video Gallery WordPress Plugin v2.2
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / lib / cache.php

cache.php in Vimeography: Vimeo Video Gallery WordPress Plugin 2.2, at lib/cache.php

151 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Vimeography;
4
5 class Cache {
6
7 /**
8 * The user's cache expiration setting for the current gallery.
9 *
10 * @var int | NULL
11 */
12 private $_expiration;
13
14 /**
15 * The path to the cache file, built in the constructor based on the
16 * provided gallery id.
17 *
18 * @var string
19 */
20 private $_cache_file;
21
22
23 /**
24 * [__construct description]
25 * @param [type] $settings [description]
26 */
27 public function __construct( $engine ) {
28
29 wp_mkdir_p( VIMEOGRAPHY_CACHE_PATH );
30 $scope = is_multisite() ? get_current_blog_id() . '-' . $engine->gallery_id : $engine->gallery_id;
31 $this->_cache_file = VIMEOGRAPHY_CACHE_PATH . 'vimeography-gallery-' . $scope . '.cache';
32
33 if ( isset( $engine->gallery_settings['cache'] ) ) {
34 $this->_expiration = intval( $engine->gallery_settings['cache'] );
35 }
36
37 }
38
39 /**
40 * Checks if a cache file exists.
41 *
42 * @param string $file Path to the cache file.
43 * @return bool true if exists, false if not.
44 */
45 public function exists() {
46 return file_exists( $this->_cache_file );
47 }
48
49
50 /**
51 * Check if the cache is expired
52 *
53 * @return bool
54 */
55 public function expired() {
56
57 if ( ! isset( $this->_expiration ) ) {
58 return false;
59 }
60
61 $last_modified = @filemtime( $this->_cache_file );
62 $expires = $last_modified + $this->_expiration;
63
64 if ( $expires < time() ) {
65 // The cache is expired, but we don't want to remove it here,
66 // We only want to remove it if we do not receive a 304 response or the user forces a cache refresh.
67
68 // Set $last_modified to make the request along with the modified time in the header.
69 $this->last_modified = date( DATE_ISO8601, $last_modified );
70
71 return true;
72 }
73
74 return false;
75 }
76
77 /**
78 * Get the serialized data stored in the Vimeography cache
79 *
80 * @access public
81 * @static
82 * @param string A cache filename
83 * @return object/bool
84 */
85 public function get() {
86 if ( file_exists( $this->_cache_file ) ) {
87 return unserialize( file_get_contents( $this->_cache_file ) );
88 } else {
89 return false;
90 }
91 }
92
93 /**
94 * Writes the video set to a cache file.
95 *
96 * @access public
97 * @static
98 * @param string $file A cache filename, equal to tokenized shortcode settings.
99 * @param object $video_set Vimeo collection data
100 */
101 public function set($data) {
102 return @file_put_contents( $this->_cache_file, serialize( $data ) );
103 }
104
105 /**
106 * If there is a 304 Not Changed, update the modified time to reset the expiration
107 * Changes the file modified time to current
108 *
109 * @param string $file A cache filename, equal to tokenized shortcode settings.
110 * @return bool Whether the file was updated or not.
111 */
112 public function renew() {
113 if ( touch( $this->_cache_file ) ) {
114 return $this;
115 } else {
116 return false;
117 }
118 }
119
120 /**
121 * Delete a cache file if it is old or the user forces a refresh.
122 *
123 * @param string $token A cache filename, equal to tokenized shortcode settings.
124 * @return bool Success or failure deleting file.
125 */
126 public function delete() {
127 if ( substr( $this->_cache_file, -6 ) === '.cache' ) {
128 return @unlink( $this->_cache_file );
129 }
130 }
131
132 /**
133 * Remove any expired cache files from the cache directory.
134 * We can safely do this after the request was made, but we really shouldn't
135 * get rid of valid, expired files if the 304 is working properly.
136 *
137 * @return void
138 */
139 protected function cleanup() {
140 $files = scandir( VIMEOGRAPHY_CACHE_PATH );
141
142 foreach ($files as $file) {
143 $last_modified = @filemtime( VIMEOGRAPHY_CACHE_PATH . $file );
144
145 if ( substr($file, -6) === '.cache' && ( $last_modified + $this->_expiration ) < time() ) {
146 unlink( VIMEOGRAPHY_CACHE_PATH . $file );
147 }
148 }
149 }
150 }
151