PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.2.5
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.2.5
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 / class-powered-cache-object-cache.php

class-powered-cache-object-cache.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.2.5, at includes/class-powered-cache-object-cache.php

112 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class Powered_Cache_Object_Cache {
8
9 /**
10 * Return an instance of the current class
11 *
12 * @since 1.0
13 * @return Powered_Cache_Object_Cache
14 */
15 public static function factory() {
16
17 static $instance;
18
19 if ( ! $instance ) {
20 $instance = new self();
21 $instance->setup();
22 }
23
24 return $instance;
25 }
26
27
28 /**
29 * Setup hooks
30 *
31 * @since 1.0
32 */
33 public function setup() {
34 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
35 add_action( 'admin_post_powered_cache_purge_object_cache', array( $this, 'purge_object_cache' ) );
36
37 add_action( 'added_option', array( $this, 'maybe_clear_alloptions_cache' ) );
38 add_action( 'updated_option', array( $this, 'maybe_clear_alloptions_cache' ) );
39 add_action( 'deleted_option', array( $this, 'maybe_clear_alloptions_cache' ) );
40 }
41
42
43 /**
44 * Add purge button on admin bar
45 *
46 * @since 1.0
47 *
48 * @param $wp_admin_bar
49 */
50 public function admin_bar_menu( $wp_admin_bar ) {
51
52 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
53 return;
54 }
55
56 $wp_admin_bar->add_menu( array(
57 'id' => 'object-cache-purge',
58 'title' => __( 'Purge Object Cache', 'powered-cache' ),
59 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_object_cache' ), 'powered_cache_purge_object_cache' ),
60 'parent' => 'powered-cache',
61 ) );
62 }
63
64
65 /**
66 * Purge object cache
67 *
68 * @since 1.0
69 */
70 public function purge_object_cache() {
71 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_object_cache' ) ) {
72 wp_nonce_ays( '' );
73 }
74
75 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
76 Powered_Cache_Admin_Helper::set_flash_message( __( "You don't have permission to perform this action!", 'powered-cache' ) ,'error' );
77 wp_safe_redirect( wp_get_referer() );
78 die();
79 }
80
81 if ( function_exists( 'wp_cache_flush' ) ) {
82 wp_cache_flush();
83 }
84
85 Powered_Cache_Admin_Helper::set_flash_message( __( 'Object cache deleted successfully!', 'powered-cache' ) );
86
87 wp_safe_redirect( wp_get_referer() );
88 die();
89 }
90
91
92 /**
93 * Fix a race condition in alloptions caching
94 *
95 * @see https://github.com/skopco/powered-cache/issues/47
96 * @see https://core.trac.wordpress.org/ticket/31245
97 *
98 *
99 * Ported from https://core.trac.wordpress.org/ticket/31245#comment:57
100 * @since 1.2.4
101 */
102 public function maybe_clear_alloptions_cache( $option ) {
103 if ( ! wp_installing() ) {
104 $alloptions = wp_load_alloptions(); //alloptions should be cached at this point
105
106 if ( isset( $alloptions[ $option ] ) ) { //only if option is among alloptions
107 wp_cache_delete( 'alloptions', 'options' );
108 }
109 }
110 }
111
112 }