class-ga-cache.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Request caching tool. |
| 4 | * |
| 5 | * @package GoogleAnalytics |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Cache class. |
| 10 | */ |
| 11 | class Ga_Cache { |
| 12 | |
| 13 | /** |
| 14 | * Time until expiration in seconds |
| 15 | */ |
| 16 | const GA_DATA_EXPIRATION_TIME = 86400;// 60 * 60 * 24 = 86400; // 24h |
| 17 | |
| 18 | const GA_TRANSIENT_PREFIX = 'googleanalytics_cache_'; |
| 19 | |
| 20 | const GA_LAST_CACHE_OPTION_NAME = 'googleanalytics_cache_last_cache_time'; |
| 21 | |
| 22 | const GA_LAST_TIME_ATTEMPT_OPTION_NAME = 'googleanalytics_cache_last_time_attempt'; |
| 23 | |
| 24 | const GA_BUFFER_CACHE_OPTION_NAME = 'googleanalytics_cache_buffer'; |
| 25 | |
| 26 | const GA_WAIT_AFTER_ERROR_TIME = 300; // 60 * 5 = 5 min |
| 27 | |
| 28 | /** |
| 29 | * Add cache options. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public static function add_cache_options() { |
| 34 | add_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 35 | add_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 36 | add_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Delete cache options. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public static function delete_cache_options() { |
| 45 | delete_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 46 | delete_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 47 | delete_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Generates transient name. |
| 52 | * |
| 53 | * @param string $rest_url Rest URL string. |
| 54 | * @param array $query_params Query params. |
| 55 | * @param string $appendix String to append. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function get_transient_name( $rest_url, $query_params, $appendix = '' ) { |
| 60 | |
| 61 | if ( is_array( $query_params ) ) { |
| 62 | $query_params = wp_json_encode( $query_params ); |
| 63 | } |
| 64 | |
| 65 | $name = md5( $rest_url . $query_params ); |
| 66 | |
| 67 | return self::GA_TRANSIENT_PREFIX . $name . '_' . $appendix; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets cached data. |
| 72 | * |
| 73 | * @param string $name Cache key string. |
| 74 | * |
| 75 | * @return bool|mixed |
| 76 | */ |
| 77 | public static function get_cached_result( $name ) { |
| 78 | $data = get_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 79 | |
| 80 | // Check if cache exists. |
| 81 | if ( false === empty( $data[ $name ] ) ) { |
| 82 | return $data[ $name ]; |
| 83 | } else { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Sets cache value. |
| 90 | * |
| 91 | * @param string $name Cache key name. |
| 92 | * @param mixed $result Cache key value. |
| 93 | */ |
| 94 | public static function set_cache( $name, $result ) { |
| 95 | if ( false === empty( $result ) ) { |
| 96 | self::set_last_cache_time( $name ); |
| 97 | self::set_cache_buffer( $name, $result ); |
| 98 | self::delete_last_time_attempt(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Updates the time the response was cached. |
| 104 | * |
| 105 | * @param string $name Cache key name. |
| 106 | */ |
| 107 | public static function set_last_cache_time( $name ) { |
| 108 | $data = get_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 109 | |
| 110 | if ( empty( $data ) ) { |
| 111 | $data = array(); |
| 112 | $data[ $name ] = time(); |
| 113 | } else { |
| 114 | $data = array_merge( $data, array( $name => time() ) ); |
| 115 | } |
| 116 | |
| 117 | update_option( self::GA_LAST_CACHE_OPTION_NAME, $data ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets or update data cache. |
| 122 | * |
| 123 | * @param string $name Cache key name. |
| 124 | * @param string $result Cache key value. |
| 125 | */ |
| 126 | public static function set_cache_buffer( $name, $result ) { |
| 127 | $data = get_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 128 | |
| 129 | if ( empty( $data ) ) { |
| 130 | $data = array(); |
| 131 | $data[ $name ] = $result; |
| 132 | } else { |
| 133 | $data = array_merge( $data, array( $name => $result ) ); |
| 134 | } |
| 135 | |
| 136 | update_option( self::GA_BUFFER_CACHE_OPTION_NAME, $data ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets last time attempt option |
| 141 | */ |
| 142 | public static function set_last_time_attempt() { |
| 143 | update_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME, time() ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Deletes last time attepmt option |
| 148 | */ |
| 149 | public static function delete_last_time_attempt() { |
| 150 | delete_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Checks if the next rest API request is allowed. |
| 155 | * |
| 156 | * Next api request is allowed when there was an unsuccessful attempt and data_cache is empty. |
| 157 | * |
| 158 | * @param string $name Name string. |
| 159 | * |
| 160 | * @return bool |
| 161 | */ |
| 162 | public static function is_next_request_allowed( $name ) { |
| 163 | $last_time_attempt = get_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 164 | |
| 165 | if ( true === empty( $last_time_attempt ) ) { |
| 166 | // If there is no last_time_attempt then return true. |
| 167 | return true; |
| 168 | } else { |
| 169 | $outdated_last_attempt_time = ( $last_time_attempt + self::GA_WAIT_AFTER_ERROR_TIME ) < time(); |
| 170 | } |
| 171 | |
| 172 | return ( false === empty( self::get_cached_result( $name ) ) && true === $outdated_last_attempt_time ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Checks whether data cache is outdated. |
| 177 | * |
| 178 | * @param string $name Cache key name string. |
| 179 | * @param string $appendix Cache key string to append. |
| 180 | * |
| 181 | * @return bool |
| 182 | */ |
| 183 | public static function is_data_cache_outdated( $name = '', $appendix = '' ) { |
| 184 | $last_time = get_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 185 | $outdated = 0; |
| 186 | if ( false === empty( $last_time ) ) { |
| 187 | |
| 188 | // Validate cache for given rest name. |
| 189 | if ( false === empty( $name ) ) { |
| 190 | |
| 191 | // If appendix is set then check only that cache which concerns given appendix. |
| 192 | if ( false === empty( $appendix ) ) { |
| 193 | return ( |
| 194 | preg_match( '/' . $appendix . '/', $name ) |
| 195 | && ( $last_time[ $name ] + self::GA_DATA_EXPIRATION_TIME ) < time() |
| 196 | ); |
| 197 | } else { |
| 198 | return ! empty( $last_time[ $name ] ) && ( $last_time[ $name ] + self::GA_DATA_EXPIRATION_TIME ) < time(); |
| 199 | } |
| 200 | } else { // Validate cache for all requests. |
| 201 | |
| 202 | // If any of existing caches is outdated. |
| 203 | foreach ( $last_time as $item => $time ) { |
| 204 | // If appendix is set then check only entries concerns given appendix. |
| 205 | if ( false === empty( $appendix ) ) { |
| 206 | if ( |
| 207 | preg_match( '/' . $appendix . '/', $item ) |
| 208 | && ( $time + self::GA_DATA_EXPIRATION_TIME ) < time() ) { |
| 209 | $outdated ++; |
| 210 | } |
| 211 | } else { |
| 212 | if ( ( $time + self::GA_DATA_EXPIRATION_TIME ) < time() ) { |
| 213 | $outdated ++; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return $outdated > 0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 |