| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Schema_Aggregator\Application\Cache; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Config; |
| 9 |
|
| 10 |
/** |
| 11 |
* Cache manager for the XML sitemap. |
| 12 |
* |
| 13 |
* Manages cache storage/retrieval/invalidation using WordPress Transients API. |
| 14 |
*/ |
| 15 |
class Xml_Manager { |
| 16 |
/** |
| 17 |
* Cache key prefix |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private const CACHE_PREFIX = 'yoast_schema_aggregator'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Cache version for invalidation |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
private const CACHE_VERSION = 1; |
| 29 |
|
| 30 |
/** |
| 31 |
* Configuration provider |
| 32 |
* |
| 33 |
* @var Config |
| 34 |
*/ |
| 35 |
private $config; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
* |
| 40 |
* @param Config $config Configuration provider. |
| 41 |
*/ |
| 42 |
public function __construct( Config $config ) { |
| 43 |
$this->config = $config; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get cached data for a page. |
| 48 |
* |
| 49 |
* @return string|null Cached data or null. |
| 50 |
*/ |
| 51 |
public function get(): ?string { |
| 52 |
try { |
| 53 |
if ( ! $this->config->cache_enabled() ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
$key = $this->get_cache_key(); |
| 58 |
|
| 59 |
$data = \get_transient( $key ); |
| 60 |
|
| 61 |
if ( $data === false ) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
if ( ! \is_string( $data ) ) { |
| 65 |
\delete_transient( $key ); |
| 66 |
|
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
return $data; |
| 71 |
|
| 72 |
} catch ( Exception $e ) { |
| 73 |
return null; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set cache data for a page. |
| 79 |
* |
| 80 |
* @param string $data Data to cache. |
| 81 |
* |
| 82 |
* @return bool Success. |
| 83 |
*/ |
| 84 |
public function set( string $data ): bool { |
| 85 |
try { |
| 86 |
$key = $this->get_cache_key(); |
| 87 |
$expiration = $this->config->get_expiration( [ $data ] ); |
| 88 |
|
| 89 |
return \set_transient( $key, $data, $expiration ); |
| 90 |
|
| 91 |
} catch ( Exception $e ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Invalidate cache for the xml sitemap. |
| 98 |
* |
| 99 |
* @return bool Success. |
| 100 |
*/ |
| 101 |
public function invalidate(): bool { |
| 102 |
return \delete_transient( $this->get_cache_key() ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Generate cache key for page. |
| 107 |
* |
| 108 |
* @return string Cache key. |
| 109 |
*/ |
| 110 |
private function get_cache_key(): string { |
| 111 |
return \sprintf( |
| 112 |
'%s_xml_sitemap_v%d', |
| 113 |
self::CACHE_PREFIX, |
| 114 |
self::CACHE_VERSION, |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|