| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-cache.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Helper_E2pdf_Cache { |
| 15 |
|
| 16 |
private $helper; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 20 |
} |
| 21 |
|
| 22 |
public function purge_cache() { |
| 23 |
$this->purge_objects_cache(); |
| 24 |
$this->purge_fonts_cache(); |
| 25 |
$this->purge_pdfs_cache(); |
| 26 |
} |
| 27 |
|
| 28 |
public function pre_objects_cache() { |
| 29 |
if (function_exists('w3tc_dbcache_flush')) { |
| 30 |
w3tc_dbcache_flush(); |
| 31 |
} |
| 32 |
if ( |
| 33 |
class_exists('SiteGround_Optimizer\Supercacher\Supercacher') && |
| 34 |
class_exists('SitePress') && |
| 35 |
get_option('siteground_optimizer_enable_memcached') && |
| 36 |
function_exists('wp_cache_flush') |
| 37 |
) { |
| 38 |
wp_cache_flush(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function purge_objects_cache() { |
| 43 |
if (function_exists('w3tc_dbcache_flush')) { |
| 44 |
w3tc_dbcache_flush(); |
| 45 |
} |
| 46 |
wp_cache_flush(); |
| 47 |
} |
| 48 |
|
| 49 |
public function purge_fonts_cache() { |
| 50 |
update_option('e2pdf_cached_fonts', []); |
| 51 |
} |
| 52 |
|
| 53 |
public function cache_pdf($cached_pdf = '', $request = []) { |
| 54 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 55 |
if ($cached_pdf && get_option('e2pdf_cache_pdfs', '0') && !@file_exists($this->helper->get('cache_dir') . $cached_pdf)) { |
| 56 |
if (!isset($request['error']) && !empty($request['file'])) { |
| 57 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 58 |
file_put_contents($this->helper->get('cache_dir') . $cached_pdf, $request['file'], LOCK_EX); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function get_cached_pdf($cached_pdf = '') { |
| 64 |
$request = []; |
| 65 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 66 |
if ($cached_pdf && get_option('e2pdf_cache_pdfs', '0') && @file_exists($this->helper->get('cache_dir') . $cached_pdf)) { |
| 67 |
$request = [ |
| 68 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 69 |
'file' => @file_get_contents($this->helper->get('cache_dir') . $cached_pdf), |
| 70 |
]; |
| 71 |
$this->purge_pdfs_cache_ttl(); |
| 72 |
} |
| 73 |
return $request; |
| 74 |
} |
| 75 |
|
| 76 |
public function purge_pdfs_cache_ttl() { |
| 77 |
$files = glob($this->helper->get('cache_dir') . '*', GLOB_MARK); |
| 78 |
$ttl = max(10, (int) get_option('e2pdf_cache_pdfs_ttl', '180')); |
| 79 |
foreach ($files as $file) { |
| 80 |
if (false === strpos($file, 'index.php') && false === strpos($file, '.htaccess') && (time() - filectime($file)) > $ttl) { |
| 81 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 82 |
@unlink($file); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function purge_pdfs_cache() { |
| 88 |
$files = glob($this->helper->get('cache_dir') . '*', GLOB_MARK); |
| 89 |
foreach ($files as $file) { |
| 90 |
if (false === strpos($file, 'index.php') && false === strpos($file, '.htaccess')) { |
| 91 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 92 |
@unlink($file); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function purge_tmp_cache() { |
| 98 |
$files = glob($this->helper->get('tmp_dir') . '*', GLOB_MARK); |
| 99 |
$ttl = (int) get_option('e2pdf_cache_tmp_ttl', '7200'); |
| 100 |
foreach ($files as $file) { |
| 101 |
if (0 === strpos(basename($file), 'e2pdf') && (time() - filectime($file)) > $ttl) { |
| 102 |
$this->helper->delete_dir($file); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|