object-cache.php
345 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | /* |
| 6 | * This file is the object-cache.php drop-in, not ordinary plugin code: it gets copied to |
| 7 | * wp-content/ and has to define the wp_cache_* API under exactly those names, because that |
| 8 | * is the contract WordPress core calls. The prefixing rule therefore cannot apply, and the |
| 9 | * blanket ignoreFile that used to sit here has been narrowed to just that rule so |
| 10 | * everything else in the file is still checked. |
| 11 | */ |
| 12 | // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- drop-in implementing the core object cache API. |
| 13 | |
| 14 | if ( function_exists( 'apcu_fetch' ) ) : |
| 15 | |
| 16 | if ( version_compare( phpversion(), '7.4', '<' ) ) { |
| 17 | wp_die( 'The APC object cache backend requires PHP 7.4 or higher. You are running ' . esc_attr(phpversion()) . '. Please remove the <code>object-cache.php</code> file from your content directory.' ); |
| 18 | } |
| 19 | |
| 20 | if ( function_exists( 'wp_cache_add' ) ) { |
| 21 | // esc_attr() was being used as if it were a sanitizer on the raw $_SERVER value; |
| 22 | // the path is now sanitized once and escaped where it is printed. |
| 23 | $ahsc_document_root = isset( $_SERVER['DOCUMENT_ROOT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) : ''; |
| 24 | |
| 25 | // Regular die, not wp_die(), because it gets sandboxed and shown in a small iframe |
| 26 | die( |
| 27 | '<strong>ERROR:</strong> This is <em>not</em> a plugin, and it should not be activated as one.<br /><br />Instead, <code>' |
| 28 | . esc_html( str_replace( $ahsc_document_root, '', __FILE__ ) ) |
| 29 | . '</code> must be moved to <code>' |
| 30 | . esc_html( str_replace( $ahsc_document_root, '', trailingslashit( WP_CONTENT_DIR ) ) ) |
| 31 | . 'object-cache.php</code>' |
| 32 | ); |
| 33 | } else { |
| 34 | |
| 35 | if ( !defined( 'WP_APC_KEY_SALT' ) ) |
| 36 | define( 'WP_APC_KEY_SALT', 'wp' ); |
| 37 | |
| 38 | function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { |
| 39 | global $wp_object_cache; |
| 40 | |
| 41 | return $wp_object_cache->add( $key, $data, $group, $expire ); |
| 42 | } |
| 43 | |
| 44 | function wp_cache_incr( $key, $n = 1, $group = '' ) { |
| 45 | global $wp_object_cache; |
| 46 | |
| 47 | return $wp_object_cache->incr2( $key, $n, $group ); |
| 48 | } |
| 49 | |
| 50 | function wp_cache_decr( $key, $n = 1, $group = '' ) { |
| 51 | global $wp_object_cache; |
| 52 | |
| 53 | return $wp_object_cache->decr( $key, $n, $group ); |
| 54 | } |
| 55 | |
| 56 | function wp_cache_close() { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | function wp_cache_delete( $key, $group = '' ) { |
| 61 | global $wp_object_cache; |
| 62 | |
| 63 | return $wp_object_cache->delete( $key, $group ); |
| 64 | } |
| 65 | |
| 66 | function wp_cache_flush() { |
| 67 | global $wp_object_cache; |
| 68 | |
| 69 | return $wp_object_cache->flush(); |
| 70 | } |
| 71 | |
| 72 | function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
| 73 | global $wp_object_cache; |
| 74 | |
| 75 | return $wp_object_cache->get( $key, $group, $force, $found ); |
| 76 | } |
| 77 | |
| 78 | function wp_cache_init() { |
| 79 | global $wp_object_cache; |
| 80 | |
| 81 | $wp_object_cache = new APC_Object_Cache(); |
| 82 | } |
| 83 | |
| 84 | function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { |
| 85 | global $wp_object_cache; |
| 86 | |
| 87 | return $wp_object_cache->replace( $key, $data, $group, $expire ); |
| 88 | } |
| 89 | |
| 90 | function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { |
| 91 | global $wp_object_cache; |
| 92 | |
| 93 | if ( defined('WP_INSTALLING') == false ) |
| 94 | return $wp_object_cache->set( $key, $data, $group, $expire ); |
| 95 | else |
| 96 | return $wp_object_cache->delete( $key, $group ); |
| 97 | } |
| 98 | |
| 99 | function wp_cache_switch_to_blog( $blog_id ) { |
| 100 | global $wp_object_cache; |
| 101 | |
| 102 | return $wp_object_cache->switch_to_blog( $blog_id ); |
| 103 | } |
| 104 | |
| 105 | function wp_cache_add_global_groups( $groups ) { |
| 106 | global $wp_object_cache; |
| 107 | |
| 108 | $wp_object_cache->add_global_groups( $groups ); |
| 109 | } |
| 110 | |
| 111 | function wp_cache_add_non_persistent_groups( $groups ) { |
| 112 | global $wp_object_cache; |
| 113 | |
| 114 | $wp_object_cache->add_non_persistent_groups( $groups ); |
| 115 | } |
| 116 | |
| 117 | class WP_Object_Cache { |
| 118 | public $global_groups = array(); |
| 119 | |
| 120 | public $no_mc_groups = array(); |
| 121 | |
| 122 | public $cache = array(); |
| 123 | public $stats = array( 'get' => 0, 'delete' => 0, 'add' => 0 ); |
| 124 | public $group_ops = array(); |
| 125 | |
| 126 | public $cache_enabled = true; |
| 127 | public $default_expiration = 0; |
| 128 | public $abspath = ''; |
| 129 | public $debug = false; |
| 130 | public $global_prefix = ''; |
| 131 | public $blog_prefix = ''; |
| 132 | public $cache_hits = 0; |
| 133 | public $cache_misses = 0; |
| 134 | |
| 135 | function add( $id, $data, $group = 'default', $expire = 0 ) { |
| 136 | $key = $this->key( $id, $group ); |
| 137 | |
| 138 | if ( is_object( $data ) ) |
| 139 | $data = clone $data; |
| 140 | |
| 141 | $store_data = $data; |
| 142 | |
| 143 | if ( is_array( $data ) ) |
| 144 | $store_data = new ArrayObject( $data ); |
| 145 | |
| 146 | if ( in_array( $group, $this->no_mc_groups ) ) { |
| 147 | $this->cache[$key] = $data; |
| 148 | return true; |
| 149 | } elseif ( isset( $this->cache[$key] ) && $this->cache[$key] !== false ) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | $expire = ( $expire == 0 ) ? $this->default_expiration : $expire; |
| 154 | |
| 155 | $result = apcu_add( $key, $store_data, $expire ); |
| 156 | if ( false !== $result ) { |
| 157 | @ ++$this->stats['add']; |
| 158 | $this->group_ops[$group][] = "add $id"; |
| 159 | $this->cache[$key] = $data; |
| 160 | } |
| 161 | |
| 162 | return $result; |
| 163 | } |
| 164 | |
| 165 | function add_global_groups( $groups ) { |
| 166 | if ( !is_array( $groups ) ) |
| 167 | $groups = (array) $groups; |
| 168 | |
| 169 | $this->global_groups = array_merge( $this->global_groups, $groups ); |
| 170 | $this->global_groups = array_unique( $this->global_groups ); |
| 171 | } |
| 172 | |
| 173 | function add_non_persistent_groups( $groups ) { |
| 174 | if ( !is_array( $groups ) ) |
| 175 | $groups = (array) $groups; |
| 176 | |
| 177 | $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups ); |
| 178 | $this->no_mc_groups = array_unique( $this->no_mc_groups ); |
| 179 | } |
| 180 | |
| 181 | // This is named incr2 because Batcache looks for incr |
| 182 | // We will define that in a class extension if it is available (APC 3.1.1 or higher) |
| 183 | function incr2( $id, $n = 1, $group = 'default' ) { |
| 184 | $key = $this->key( $id, $group ); |
| 185 | if ( function_exists( 'apcu_inc' ) ) |
| 186 | return apcu_inc( $key, $n ); |
| 187 | else |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | function decr( $id, $n = 1, $group = 'default' ) { |
| 192 | $key = $this->key( $id, $group ); |
| 193 | if ( function_exists( 'apcu_dec' ) ) |
| 194 | return apcu_dec( $key, $n ); |
| 195 | else |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | function close() { |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | function delete( $id, $group = 'default' ) { |
| 204 | $key = $this->key( $id, $group ); |
| 205 | |
| 206 | if ( in_array( $group, $this->no_mc_groups ) ) { |
| 207 | unset( $this->cache[$key] ); |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | $result = apcu_delete( $key ); |
| 212 | |
| 213 | @ ++$this->stats['delete']; |
| 214 | $this->group_ops[$group][] = "delete $id"; |
| 215 | |
| 216 | if ( false !== $result ) |
| 217 | unset( $this->cache[$key] ); |
| 218 | |
| 219 | return $result; |
| 220 | } |
| 221 | |
| 222 | function flush() { |
| 223 | // Don't flush if multi-blog. |
| 224 | if ( function_exists( 'is_site_admin' ) || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) |
| 225 | return true; |
| 226 | |
| 227 | $this->cache = array(); |
| 228 | return apcu_clear_cache(); |
| 229 | } |
| 230 | |
| 231 | function get($id, $group = 'default', $force = false, &$found = null) { |
| 232 | $key = $this->key($id, $group); |
| 233 | |
| 234 | if ( isset($this->cache[$key]) && ( !$force || in_array($group, $this->no_mc_groups) ) ) { |
| 235 | $found = true; |
| 236 | if ( is_object( $this->cache[$key] ) ) |
| 237 | $value = clone $this->cache[$key]; |
| 238 | else |
| 239 | $value = $this->cache[$key]; |
| 240 | } else if ( in_array($group, $this->no_mc_groups) ) { |
| 241 | $found = false; |
| 242 | $this->cache[$key] = $value = false; |
| 243 | } else { |
| 244 | $value = apcu_fetch( $key, $success ); |
| 245 | $found = $success; |
| 246 | if ( is_object( $value ) && 'ArrayObject' == get_class( $value ) ) |
| 247 | $value = $value->getArrayCopy(); |
| 248 | if ( NULL === $value ) |
| 249 | $value = false; |
| 250 | $this->cache[$key] = ( is_object( $value ) ) ? clone $value : $value; |
| 251 | } |
| 252 | |
| 253 | @ ++$this->stats['get']; |
| 254 | $this->group_ops[$group][] = "get $id"; |
| 255 | |
| 256 | if ( 'checkthedatabaseplease' === $value ) { |
| 257 | unset( $this->cache[$key] ); |
| 258 | $value = false; |
| 259 | $found = false; |
| 260 | } |
| 261 | |
| 262 | return $value; |
| 263 | } |
| 264 | |
| 265 | function key( $key, $group ) { |
| 266 | global $blog_id, $table_prefix; |
| 267 | if ( empty( $group ) ) |
| 268 | $group = 'default'; |
| 269 | |
| 270 | if ( false !== array_search( $group, $this->global_groups ) ) |
| 271 | $prefix = $this->global_prefix; |
| 272 | else |
| 273 | $prefix =( is_multisite() ? $blog_id : $table_prefix ); |
| 274 | |
| 275 | return WP_APC_KEY_SALT . ':' . $this->abspath . ":$prefix$group:$key"; |
| 276 | } |
| 277 | |
| 278 | function replace( $id, $data, $group = 'default', $expire = 0 ) { |
| 279 | return $this->set( $id, $data, $group, $expire ); |
| 280 | } |
| 281 | |
| 282 | function set( $id, $data, $group = 'default', $expire = 0 ) { |
| 283 | $key = $this->key( $id, $group ); |
| 284 | if ( isset( $this->cache[$key] ) && ('checkthedatabaseplease' === $this->cache[$key] ) ) |
| 285 | return false; |
| 286 | |
| 287 | if ( is_object( $data ) ) |
| 288 | $data = clone $data; |
| 289 | |
| 290 | $store_data = $data; |
| 291 | |
| 292 | if ( is_array( $data ) ) |
| 293 | $store_data = new ArrayObject( $data ); |
| 294 | |
| 295 | $this->cache[$key] = $data; |
| 296 | |
| 297 | if ( in_array( $group, $this->no_mc_groups ) ) |
| 298 | return true; |
| 299 | |
| 300 | $expire = ( $expire == 0 ) ? $this->default_expiration : $expire; |
| 301 | $result = apcu_store( $key, $store_data, $expire ); |
| 302 | |
| 303 | return $result; |
| 304 | } |
| 305 | |
| 306 | function switch_to_blog( $blog_id ) { |
| 307 | global $table_prefix; |
| 308 | |
| 309 | $blog_id = (int) $blog_id; |
| 310 | $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; |
| 311 | } |
| 312 | function __construct() { |
| 313 | $this->abspath = md5( ABSPATH ); |
| 314 | |
| 315 | global $blog_id, $table_prefix; |
| 316 | $this->global_prefix = ''; |
| 317 | $this->blog_prefix = ''; |
| 318 | if ( function_exists( 'is_multisite' ) ) { |
| 319 | $this->global_prefix = ( is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ) ? '' : $table_prefix; |
| 320 | $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; |
| 321 | } |
| 322 | |
| 323 | $this->cache_hits =& $this->stats['get']; |
| 324 | $this->cache_misses =& $this->stats['add']; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if ( function_exists( 'apcu_inc' ) ) { |
| 329 | class APC_Object_Cache extends WP_Object_Cache { |
| 330 | function incr( $id, $n = 1, $group = 'default' ) { |
| 331 | return parent::incr2( $id, $n, $group ); |
| 332 | } |
| 333 | } |
| 334 | } else { |
| 335 | class APC_Object_Cache extends WP_Object_Cache { |
| 336 | // Blank |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } // !function_exists( 'wp_cache_add' ) |
| 341 | |
| 342 | else : // No APC |
| 343 | $GLOBALS['_wp_using_ext_object_cache'] = false; // This will get overridden as of WP 3.5, so we have to hook in to 'all': |
| 344 | require_once ( ABSPATH . WPINC . '/cache.php' ); |
| 345 | endif; |