| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Browser_Cache; |
| 5 |
|
| 6 |
/** |
| 7 |
* Responsible for the browser cache configuration. |
| 8 |
*/ |
| 9 |
class Browser_Cache_Configuration { |
| 10 |
|
| 11 |
/** |
| 12 |
* Gets the Time To Live for each widget's cache. |
| 13 |
* |
| 14 |
* @return array<string, array<string, int>> The cache TTL for each widget. |
| 15 |
*/ |
| 16 |
private function get_widgets_cache_ttl() { |
| 17 |
return [ |
| 18 |
'topPages' => [ |
| 19 |
'ttl' => ( 1 * \MINUTE_IN_SECONDS ), |
| 20 |
], |
| 21 |
'topQueries' => [ |
| 22 |
'ttl' => ( 1 * \HOUR_IN_SECONDS ), |
| 23 |
], |
| 24 |
'searchRankingCompare' => [ |
| 25 |
'ttl' => ( 1 * \HOUR_IN_SECONDS ), |
| 26 |
], |
| 27 |
'organicSessions' => [ |
| 28 |
'ttl' => ( 1 * \HOUR_IN_SECONDS ), |
| 29 |
], |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Gets the prefix for the client side cache key. |
| 35 |
* |
| 36 |
* Cache key is scoped to user session and blog_id to isolate the |
| 37 |
* cache between users and sites (in multisite). |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
private function get_storage_prefix() { |
| 42 |
$current_user = \wp_get_current_user(); |
| 43 |
$auth_cookie = \wp_parse_auth_cookie(); |
| 44 |
$blog_id = \get_current_blog_id(); |
| 45 |
$session_token = ( $auth_cookie['token'] ?? '' ); |
| 46 |
|
| 47 |
return \wp_hash( $current_user->user_login . '|' . $session_token . '|' . $blog_id ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the browser cache configuration. |
| 52 |
* |
| 53 |
* @return array<string, string|array<string, array<string, int>>> |
| 54 |
*/ |
| 55 |
public function get_configuration(): array { |
| 56 |
return [ |
| 57 |
'storagePrefix' => $this->get_storage_prefix(), |
| 58 |
'yoastVersion' => \WPSEO_VERSION, |
| 59 |
'widgetsCacheTtl' => $this->get_widgets_cache_ttl(), |
| 60 |
]; |
| 61 |
} |
| 62 |
} |
| 63 |
|