PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.7.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.7.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / ObjectCache.php

ObjectCache.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 3.7.2, at includes/classes/ObjectCache.php

142 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Object Cache related functionalities
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache;
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class ObjectCache
17 */
18 class ObjectCache {
19
20 /**
21 * Return an instance of the current class
22 *
23 * @return ObjectCache
24 * @since 1.0
25 */
26 public static function factory() {
27
28 static $instance;
29
30 if ( ! $instance ) {
31 $instance = new self();
32 $instance->setup();
33 }
34
35 return $instance;
36 }
37
38
39 /**
40 * Setup hooks
41 *
42 * @since 1.0
43 */
44 public function setup() {
45 $settings = \PoweredCache\Utils\get_settings();
46
47 if ( 'off' === $settings['object_cache'] ) {
48 return;
49 }
50
51 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
52 add_action( 'admin_post_powered_cache_purge_object_cache', array( $this, 'purge_object_cache' ) );
53
54 add_action( 'added_option', array( $this, 'maybe_clear_alloptions_cache' ) );
55 add_action( 'updated_option', array( $this, 'maybe_clear_alloptions_cache' ) );
56 add_action( 'deleted_option', array( $this, 'maybe_clear_alloptions_cache' ) );
57 }
58
59
60 /**
61 * Add purge button on admin bar
62 *
63 * @param object $wp_admin_bar Admin bar object
64 *
65 * @since 1.0
66 */
67 public function admin_bar_menu( $wp_admin_bar ) {
68 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
69 return;
70 }
71
72 if ( ! current_user_can( 'manage_options' ) ) {
73 return;
74 }
75
76 $wp_admin_bar->add_menu(
77 array(
78 'id' => 'object-cache-purge',
79 'title' => __( 'Purge Object Cache', 'powered-cache' ),
80 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_object_cache' ), 'powered_cache_purge_object_cache' ),
81 'parent' => 'powered-cache',
82 )
83 );
84 }
85
86
87 /**
88 * Purge object cache
89 *
90 * @since 1.0
91 */
92 public function purge_object_cache() {
93 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_object_cache' ) ) { // phpcs:ignore
94 wp_nonce_ays( '' );
95 }
96
97 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
98 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
99 wp_safe_redirect( esc_url_raw( $redirect_url ) );
100 exit;
101 }
102
103 if ( ! current_user_can( 'manage_options' ) ) {
104 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
105 wp_safe_redirect( esc_url_raw( $redirect_url ) );
106 exit;
107 }
108
109 if ( function_exists( 'wp_cache_flush' ) ) {
110 wp_cache_flush();
111 }
112
113 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache', wp_get_referer() );
114 wp_safe_redirect( esc_url_raw( $redirect_url ) );
115 exit;
116 }
117
118
119 /**
120 * Fix a race condition in alloptions caching
121 *
122 * @param string $option option name
123 *
124 * @see https://github.com/skopco/powered-cache/issues/47
125 * @see https://core.trac.wordpress.org/ticket/31245
126 *
127 *
128 * Ported from https://core.trac.wordpress.org/ticket/31245#comment:57
129 * @since 1.2.4
130 */
131 public function maybe_clear_alloptions_cache( $option ) {
132 if ( ! wp_installing() ) {
133 $alloptions = wp_load_alloptions(); // alloptions should be cached at this point
134
135 if ( isset( $alloptions[ $option ] ) ) { // only if option is among alloptions
136 wp_cache_delete( 'alloptions', 'options' );
137 }
138 }
139 }
140
141 }
142