Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/google-site-kit/includes/Core/Storage/Cache.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Class Google\Site_Kit\Core\Storage\Cache
4 + *
5 + * @package Google\Site_Kit
6 + * @copyright 2021 Google LLC
7 + * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8 + * @link https://sitekit.withgoogle.com
9 + */
10 +
11 + namespace Google\Site_Kit\Core\Storage;
12 +
13 + use Google\Site_Kit_Dependencies\Google\Service\Exception as Google_Service_Exception;
14 +
15 + /**
16 + * Class providing a server side caching framework.
17 + *
18 + * @since 1.0.0
19 + * @access private
20 + * @ignore
21 + */
22 + final class Cache {
23 +
24 + /**
25 + * The key for saving the global cache keys.
26 + *
27 + * @var string $global_cache_keys_key The key.
28 + */
29 + private static $global_cache_keys_key = 'googlesitekit_global_cache_keys';
30 +
31 + /**
32 + * The global record of cache keys used on the site.
33 + *
34 + * @var array
35 + */
36 + private $global_cache_keys;
37 +
38 + /**
39 + * Construct the Cache class.
40 + */
41 + public function __construct() {
42 + $this->global_cache_keys = get_option( self::$global_cache_keys_key ) ?: array();
43 + }
44 +
45 + /**
46 + * Helper function to get the cache data.
47 + */
48 + public function get_current_cache_data() {
49 +
50 + $cache_data = array();
51 +
52 + // Add the global cache data.
53 + $keys = $this->get_global_cache_keys();
54 + foreach ( $keys as $key ) {
55 +
56 + // This only retrieves fresh data because transients expire.
57 + $cache = get_transient( 'googlesitekit_' . $key );
58 +
59 + if ( $cache ) {
60 + $cache_data[ $key ] = $cache;
61 + } else {
62 +
63 + // Remove the expired key from the global cache.
64 + $this->remove_global_cache_key( $key );
65 + }
66 + }
67 + return $cache_data;
68 + }
69 +
70 + /**
71 + * Remove a cache key to the global record of cache keys.
72 + *
73 + * @param string $key The key to add.
74 + */
75 + private function remove_global_cache_key( $key ) {
76 + $key_index = array_search( $key, $this->global_cache_keys, true );
77 +
78 + if ( $key_index ) {
79 + unset( $this->global_cache_keys[ $key_index ] );
80 + update_option( self::$global_cache_keys_key, $this->global_cache_keys, false );
81 + }
82 + }
83 +
84 + /**
85 + * Add a cache key to the global record of cache keys.
86 + *
87 + * @param string $key The key to add.
88 + */
89 + private function add_global_cache_key( $key ) {
90 + // Only add the key if it isn't already present.
91 + if ( ! in_array( $key, $this->global_cache_keys, true ) ) {
92 + $this->global_cache_keys[] = $key;
93 + update_option( self::$global_cache_keys_key, $this->global_cache_keys, false );
94 + }
95 + }
96 +
97 + /**
98 + * Retrieve the global record of cache keys.
99 + *
100 + * @return array The array of cache keys used on the site.
101 + */
102 + private function get_global_cache_keys() {
103 + return $this->global_cache_keys;
104 + }
105 +
106 + /**
107 + * Cache some data.
108 + *
109 + * @param Object $key The original data key.
110 + * @param Object $data The data to cache.
111 + */
112 + public function set_cache_data( $key, $data ) {
113 + set_transient( 'googlesitekit_' . $key, $data, HOUR_IN_SECONDS );
114 + $this->add_global_cache_key( $key );
115 + }
116 +
117 + /**
118 + * Cache the results of a batch operation.
119 + *
120 + * @param array $batch_requests The original requests.
121 + * @param array $results The results to cache.
122 + */
123 + public function cache_batch_results( $batch_requests, $results ) {
124 + $request_keys = wp_list_pluck( $batch_requests, 'key' );
125 +
126 + foreach ( $results as $key => $result ) {
127 + if ( $result instanceof \Exception || $result instanceof Google_Service_Exception ) {
128 + continue;
129 + }
130 + $key = str_replace( 'response-', '', $key );
131 + if ( in_array( $key, $request_keys, true ) ) {
132 + $this->set_cache_data( $key, $result );
133 + }
134 + }
135 + }
136 + }
137 +