| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class Vimeography_Cache extends Vimeography { |
| 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 |
* [__construct description] |
| 24 |
* @param [type] $settings [description] |
| 25 |
*/ |
| 26 |
public function __construct($gallery_id, $expiration = NULL) { |
| 27 |
wp_mkdir_p( VIMEOGRAPHY_CACHE_PATH ); |
| 28 |
$scope = is_multisite() ? get_current_blog_id() . '-' . $gallery_id : $gallery_id; |
| 29 |
$this->_cache_file = VIMEOGRAPHY_CACHE_PATH . 'vimeography-gallery-' . $scope . '.cache'; |
| 30 |
|
| 31 |
if ( isset($expiration) ) { |
| 32 |
$this->_expiration = intval($expiration); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Checks if a cache file exists. |
| 38 |
* |
| 39 |
* @param string $file Path to the cache file. |
| 40 |
* @return bool TRUE if exists, FALSE if not. |
| 41 |
*/ |
| 42 |
public function exists() { |
| 43 |
return file_exists($this->_cache_file); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* [expired description] |
| 48 |
* @param [type] $file [description] |
| 49 |
* @return [type] [description] |
| 50 |
*/ |
| 51 |
public function expired() { |
| 52 |
if (! isset($this->_expiration)) |
| 53 |
return FALSE; |
| 54 |
|
| 55 |
// Check if the cache is expired |
| 56 |
$last_modified = @filemtime($this->_cache_file); |
| 57 |
|
| 58 |
if (substr($this->_cache_file, -6) == '.cache' && ($last_modified + $this->_expiration) < time()) { |
| 59 |
// The cache is expired, but we don't want to kill it here, |
| 60 |
// We only want to remove it if we do not receive a 304 response or the user forces a cache refresh. |
| 61 |
// Return $last_modified to make the request along with the modified time in the header. |
| 62 |
|
| 63 |
// $est = new DateTimeZone("America/New_York"); |
| 64 |
// $date = new DateTime(); |
| 65 |
// $date->setTimestamp($last_modified); |
| 66 |
// $date->setTimezone($est); |
| 67 |
// return $date->format(DateTime::ISO8601); |
| 68 |
|
| 69 |
return date(DATE_ISO8601, $last_modified); |
| 70 |
} |
| 71 |
|
| 72 |
return FALSE; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the serialized data stored in the Vimeography cache |
| 77 |
* |
| 78 |
* @access public |
| 79 |
* @static |
| 80 |
* @param string A cache filename |
| 81 |
* @return object/bool |
| 82 |
*/ |
| 83 |
public function get() { |
| 84 |
if (file_exists($this->_cache_file)) { |
| 85 |
return unserialize(file_get_contents($this->_cache_file)); |
| 86 |
} else { |
| 87 |
return FALSE; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Writes the video set to a cache file. |
| 93 |
* |
| 94 |
* @access public |
| 95 |
* @static |
| 96 |
* @param string $file A cache filename, equal to tokenized shortcode settings. |
| 97 |
* @param object $video_set Vimeo collection data |
| 98 |
*/ |
| 99 |
public function set($data) { |
| 100 |
return @file_put_contents($this->_cache_file, serialize($data)); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* If there is a 304 Not Changed, update the modified time to reset the expiration |
| 105 |
* Changes the file modified time to current |
| 106 |
* |
| 107 |
* @param string $file A cache filename, equal to tokenized shortcode settings. |
| 108 |
* @return bool Whether the file was updated or not. |
| 109 |
*/ |
| 110 |
public function renew() { |
| 111 |
if ( touch($this->_cache_file) ) { |
| 112 |
return $this; |
| 113 |
} else { |
| 114 |
return FALSE; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Delete a cache file if it is old or the user forces a refresh. |
| 120 |
* |
| 121 |
* @param string $token A cache filename, equal to tokenized shortcode settings. |
| 122 |
* @return bool Success or failure deleting file. |
| 123 |
*/ |
| 124 |
public function delete() { |
| 125 |
if ( substr($this->_cache_file, -6) == '.cache' ) { |
| 126 |
return @unlink($this->_cache_file); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Remove any expired cache files from the cache directory. |
| 132 |
* We can safely do this after the request was made, but we really shouldn't |
| 133 |
* get rid of valid, expired files if the 304 is working properly. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
protected function cleanup() { |
| 138 |
$files = scandir(VIMEOGRAPHY_CACHE_PATH); |
| 139 |
|
| 140 |
foreach ($files as $file) { |
| 141 |
$last_modified = @filemtime(VIMEOGRAPHY_CACHE_PATH . $file); |
| 142 |
|
| 143 |
if (substr($file, -6) == '.cache' && ($last_modified + $this->_expiration) < time()) |
| 144 |
unlink(VIMEOGRAPHY_CACHE_PATH . $file); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|