| 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' ) ) { |
| 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( $redirect_url ); |
| 100 |
exit; |
| 101 |
} |
| 102 |
|
| 103 |
if ( function_exists( 'wp_cache_flush' ) ) { |
| 104 |
wp_cache_flush(); |
| 105 |
} |
| 106 |
|
| 107 |
$redirect_url = add_query_arg( 'pc_action', 'flush_object_cache', wp_get_referer() ); |
| 108 |
wp_safe_redirect( $redirect_url ); |
| 109 |
exit; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/** |
| 114 |
* Fix a race condition in alloptions caching |
| 115 |
* |
| 116 |
* @param string $option option name |
| 117 |
* |
| 118 |
* @see https://github.com/skopco/powered-cache/issues/47 |
| 119 |
* @see https://core.trac.wordpress.org/ticket/31245 |
| 120 |
* |
| 121 |
* |
| 122 |
* Ported from https://core.trac.wordpress.org/ticket/31245#comment:57 |
| 123 |
* @since 1.2.4 |
| 124 |
*/ |
| 125 |
public function maybe_clear_alloptions_cache( $option ) { |
| 126 |
if ( ! wp_installing() ) { |
| 127 |
$alloptions = wp_load_alloptions(); // alloptions should be cached at this point |
| 128 |
|
| 129 |
if ( isset( $alloptions[ $option ] ) ) { // only if option is among alloptions |
| 130 |
wp_cache_delete( 'alloptions', 'options' ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|