Object
8 years ago
Subject
8 years ago
API.php
8 years ago
Cache.php
8 years ago
Compatibility.php
8 years ago
Config.php
8 years ago
ConfigPress.php
8 years ago
Console.php
8 years ago
Exporter.php
8 years ago
Importer.php
8 years ago
Media.php
8 years ago
Object.php
8 years ago
Request.php
8 years ago
Server.php
8 years ago
Subject.php
8 years ago
Cache.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Core Cache |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Core_Cache { |
| 17 | |
| 18 | /** |
| 19 | * DB Cache option |
| 20 | */ |
| 21 | const CACHE_OPTION = 'cache'; |
| 22 | |
| 23 | /** |
| 24 | * Core config |
| 25 | * |
| 26 | * @var array |
| 27 | * |
| 28 | * @access protected |
| 29 | */ |
| 30 | protected static $cache = false; |
| 31 | |
| 32 | /** |
| 33 | * Update cache flag |
| 34 | * |
| 35 | * @var boolean |
| 36 | * |
| 37 | * @access protected |
| 38 | */ |
| 39 | protected static $updated = false; |
| 40 | |
| 41 | /** |
| 42 | * Get cached option |
| 43 | * |
| 44 | * @param string $option |
| 45 | * |
| 46 | * @return mixed |
| 47 | * |
| 48 | * @access public |
| 49 | */ |
| 50 | public static function get($option, $default = null) { |
| 51 | return (isset(self::$cache[$option]) ? self::$cache[$option] : $default); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set cache option |
| 56 | * |
| 57 | * @param string $subject |
| 58 | * @param string $option |
| 59 | * @param mixed $data |
| 60 | * |
| 61 | * @return void |
| 62 | * |
| 63 | * @access public |
| 64 | */ |
| 65 | public static function set($subject, $option, $data) { |
| 66 | if (!isset(self::$cache[$option]) || (self::$cache[$option] != $data)) { |
| 67 | self::$cache[$option] = $data; |
| 68 | self::$updated = true; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * |
| 74 | * @param type $option |
| 75 | * @return type |
| 76 | */ |
| 77 | public static function has($option) { |
| 78 | return (isset(self::$cache[$option])); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Clear cache |
| 83 | * |
| 84 | * @return void |
| 85 | * |
| 86 | * @access public |
| 87 | * @global WPDB $wpdb |
| 88 | */ |
| 89 | public static function clear($user = null) { |
| 90 | global $wpdb; |
| 91 | |
| 92 | if (is_null($user)) { |
| 93 | //clear visitor cache |
| 94 | $oquery = "DELETE FROM {$wpdb->options} WHERE `option_name` = %s"; |
| 95 | $wpdb->query($wpdb->prepare($oquery, 'aam_visitor_cache' )); |
| 96 | |
| 97 | $mquery = "DELETE FROM {$wpdb->usermeta} WHERE `meta_key` = %s"; |
| 98 | $wpdb->query($wpdb->prepare($mquery, $wpdb->prefix . 'aam_cache' )); |
| 99 | } else { |
| 100 | $query = "DELETE FROM {$wpdb->usermeta} WHERE (`user_id` = %d) AND "; |
| 101 | $query .= "`meta_key` = %s"; |
| 102 | $wpdb->query($wpdb->prepare($query, $user, $wpdb->prefix . 'aam_cache')); |
| 103 | } |
| 104 | |
| 105 | self::$cache = false; |
| 106 | |
| 107 | //clear updated flag |
| 108 | self::$updated = false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Save cache |
| 113 | * |
| 114 | * Save aam cache but only if changes deleted |
| 115 | * |
| 116 | * @return void |
| 117 | * |
| 118 | * @access public |
| 119 | */ |
| 120 | public static function save() { |
| 121 | if (self::$updated) { |
| 122 | AAM::getUser()->updateOption(self::$cache, self::CACHE_OPTION); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Bootstrap cache |
| 128 | * |
| 129 | * Do not load cache if user is on AAM page |
| 130 | * |
| 131 | * @return void |
| 132 | * |
| 133 | * @access public |
| 134 | */ |
| 135 | public static function bootstrap() { |
| 136 | if (!AAM::isAAM()) { |
| 137 | self::$cache = AAM::getUser()->readOption(self::CACHE_OPTION); |
| 138 | add_action('shutdown', 'AAM_Core_Cache::save'); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | } |