Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| File_Paths | |
0.00% |
0 / 15 |
|
0.00% |
0 / 6 |
42 | |
0.00% |
0 / 1 |
| set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| get_paths | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonUnserialize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| generate_cache_id | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| cache_prefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib\Minify; |
| 4 | |
| 5 | use Automattic\Jetpack\Boost_Core\Lib\Cacheable; |
| 6 | |
| 7 | /** |
| 8 | * Store the hash of the path string so that the concatenated file can be constructed |
| 9 | * on-demand. |
| 10 | * |
| 11 | * We are setting the transient for a long time because, if the content of the hash is not accessible, the concatenated file |
| 12 | * will not be able to construct itself. Because of page caching, there may be situations where this function will not |
| 13 | * get called for a long time, and we still want to serve the concatenated file in those cases. Having the value in |
| 14 | * a Boost Transient ensures that the value is cleaned up when Boost is uninstalled. |
| 15 | */ |
| 16 | final class File_Paths extends Cacheable { |
| 17 | private $paths; |
| 18 | private $mtime; |
| 19 | private $cache_buster; |
| 20 | |
| 21 | protected const DEFAULT_EXPIRY = MONTH_IN_SECONDS; |
| 22 | |
| 23 | public function set( $paths, $mtime, $cache_buster ) { |
| 24 | $this->paths = $paths; |
| 25 | $this->mtime = $mtime; |
| 26 | $this->cache_buster = $cache_buster; |
| 27 | } |
| 28 | |
| 29 | public function jsonSerialize(): array { |
| 30 | return array( |
| 31 | 'paths' => $this->paths, |
| 32 | 'mtime' => $this->mtime, |
| 33 | 'cache_buster' => $this->cache_buster, |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get an array of file paths that belong to the same hash. |
| 39 | * |
| 40 | * @return array Array of file paths relative to the webroot. |
| 41 | */ |
| 42 | public function get_paths() { |
| 43 | return $this->paths; |
| 44 | } |
| 45 | |
| 46 | public static function jsonUnserialize( $data ) { |
| 47 | $instance = new self(); |
| 48 | $instance->set( $data['paths'], $data['mtime'], $data['cache_buster'] ); |
| 49 | return $instance; |
| 50 | } |
| 51 | |
| 52 | protected function generate_cache_id() { |
| 53 | $hash = md5( implode( ',', $this->paths ) . '_' . $this->mtime . '_' . $this->cache_buster ); |
| 54 | |
| 55 | // Keep cache id small as option keys have a character limit. |
| 56 | return substr( $hash, 0, 10 ); |
| 57 | } |
| 58 | |
| 59 | protected static function cache_prefix() { |
| 60 | return 'concat_paths_'; |
| 61 | } |
| 62 | } |