Activation
2 years ago
Admin
2 years ago
Block
2 years ago
Container
2 years ago
Front
2 years ago
Rest
2 years ago
Traits
2 years ago
Widget
2 years ago
Bootstrap.php
2 years ago
Cache.php
2 years ago
Helper.php
2 years ago
I18N.php
2 years ago
Image.php
2 years ago
Output.php
2 years ago
Query.php
2 years ago
Settings.php
2 years ago
Themer.php
2 years ago
Translate.php
2 years ago
WordPressPopularPosts.php
2 years ago
deprecated.php
2 years ago
template-tags.php
2 years ago
Cache.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Helper class to store data in cache for a fixed amount of time. |
| 5 | * |
| 6 | * @link https://cabrerahector.com |
| 7 | * @since 4.1.2 |
| 8 | * |
| 9 | * @package WordPressPopularPosts |
| 10 | * @subpackage WordPressPopularPosts/includes |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Helper class to store data in cache for a fixed amount of time. |
| 15 | * |
| 16 | * Stores data in cache via WordPress Transients (or any other available |
| 17 | * method in the future) for a fixed amount of time to reduce the number |
| 18 | * of database calls. |
| 19 | * |
| 20 | * @package WordPressPopularPosts |
| 21 | * @subpackage WordPressPopularPosts/includes |
| 22 | * @author Hector Cabrera <me@cabrerahector.com> |
| 23 | */ |
| 24 | |
| 25 | namespace WordPressPopularPosts; |
| 26 | |
| 27 | class Cache { |
| 28 | |
| 29 | /** |
| 30 | * Retrieves cached data. |
| 31 | * |
| 32 | * @since 4.1.2 |
| 33 | * @access public |
| 34 | * @param string $key The name of the cached data. |
| 35 | * @return mixed |
| 36 | */ |
| 37 | public static function get(string $key) |
| 38 | { |
| 39 | return get_transient($key); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves cached data. |
| 44 | * |
| 45 | * @since 4.1.2 |
| 46 | * @access public |
| 47 | * @param string $key The name of the cached data. |
| 48 | * @param mixed $data The data being stored. |
| 49 | */ |
| 50 | public static function set(string $key = null, $data = [], int $time_value = 1, string $time_unit = 'minute') /** @TODO: starting PHP 8.0 $data can be declared as mixed $data */ |
| 51 | { |
| 52 | if ( ! $key ) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if ( |
| 57 | false === filter_var($time_value, FILTER_VALIDATE_INT) |
| 58 | || $time_value <= 0 |
| 59 | ) { |
| 60 | $time_value = 1; |
| 61 | } |
| 62 | |
| 63 | switch( $time_unit ){ |
| 64 | case 'minute': |
| 65 | $time = 60; |
| 66 | break; |
| 67 | case 'hour': |
| 68 | $time = 60 * 60; |
| 69 | break; |
| 70 | case 'day': |
| 71 | $time = 60 * 60 * 24; |
| 72 | break; |
| 73 | case 'week': |
| 74 | $time = 60 * 60 * 24 * 7; |
| 75 | break; |
| 76 | case 'month': |
| 77 | $time = 60 * 60 * 24 * 30; |
| 78 | break; |
| 79 | case 'year': |
| 80 | $time = 60 * 60 * 24 * 365; |
| 81 | break; |
| 82 | default: |
| 83 | $time = 60; |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | $expiration = $time * $time_value; |
| 88 | |
| 89 | // Store transient |
| 90 | set_transient($key, $data, $expiration); |
| 91 | |
| 92 | // Store transient keys in WPP's transients table for garbage collection |
| 93 | global $wpdb; |
| 94 | |
| 95 | $wpdb->insert( |
| 96 | $wpdb->prefix . 'popularpoststransients', |
| 97 | [ |
| 98 | 'tkey' => $key, |
| 99 | 'tkey_date' => Helper::now() |
| 100 | ], |
| 101 | ['%s', '%s'] |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 |