PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
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 trunk, at includes/classes/ObjectCache.php

143 lines 3.3 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 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
94 if ( ! wp_verify_nonce( $nonce, 'powered_cache_purge_object_cache' ) ) {
95 wp_nonce_ays( '' );
96 }
97
98 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
99 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
100 wp_safe_redirect( esc_url_raw( $redirect_url ) );
101 exit;
102 }
103
104 if ( ! current_user_can( 'manage_options' ) ) {
105 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
106 wp_safe_redirect( esc_url_raw( $redirect_url ) );
107 exit;
108 }
109
110 if ( function_exists( 'wp_cache_flush' ) ) {
111 wp_cache_flush();
112 }
113
114 $redirect_url = add_query_arg( 'pc_action', 'flush_object_cache', wp_get_referer() );
115 wp_safe_redirect( esc_url_raw( $redirect_url ) );
116 exit;
117 }
118
119
120 /**
121 * Fix a race condition in alloptions caching
122 *
123 * @param string $option option name
124 *
125 * @see https://github.com/skopco/powered-cache/issues/47
126 * @see https://core.trac.wordpress.org/ticket/31245
127 *
128 *
129 * Ported from https://core.trac.wordpress.org/ticket/31245#comment:57
130 * @since 1.2.4
131 */
132 public function maybe_clear_alloptions_cache( $option ) {
133 if ( ! wp_installing() ) {
134 $alloptions = wp_load_alloptions(); // alloptions should be cached at this point
135
136 if ( isset( $alloptions[ $option ] ) ) { // only if option is among alloptions
137 wp_cache_delete( 'alloptions', 'options' );
138 }
139 }
140 }
141
142 }
143