| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 1.1 |
| 9 |
* Class Powered_Cache_Hooks |
| 10 |
*/ |
| 11 |
class Powered_Cache_Hooks { |
| 12 |
|
| 13 |
/** |
| 14 |
* Setup actions and filters |
| 15 |
* |
| 16 |
* @since 1.1 |
| 17 |
*/ |
| 18 |
public function setup() { |
| 19 |
if ( powered_cache_get_option( 'remove_query_string' ) ) { |
| 20 |
add_filter( 'script_loader_src', array( $this, 'remove_script_version' ), 15, 1 ); |
| 21 |
add_filter( 'style_loader_src', array( $this, 'remove_script_version' ), 15, 1 ); |
| 22 |
} |
| 23 |
|
| 24 |
add_action( 'admin_bar_menu', array( $this, 'purge_all_admin_bar_menu' ) ); |
| 25 |
add_action( 'admin_post_powered_cache_purge_all_cache', array( $this, 'purge_all_cache' ) ); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Removes query string from the url |
| 31 |
* |
| 32 |
* @param string $src resource url |
| 33 |
* |
| 34 |
* @since 1.1 |
| 35 |
* @return mixed |
| 36 |
*/ |
| 37 |
public function remove_script_version( $src ) { |
| 38 |
$parts = explode( '?', $src ); |
| 39 |
|
| 40 |
return $parts[0]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Adds `Purge All Cache` menu bar item |
| 45 |
* |
| 46 |
* @param $wp_admin_bar |
| 47 |
* |
| 48 |
* @since 1.1 |
| 49 |
*/ |
| 50 |
public function purge_all_admin_bar_menu( $wp_admin_bar ) { |
| 51 |
// Only available for the network admins on multisite. |
| 52 |
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$wp_admin_bar->add_menu( array( |
| 57 |
'id' => 'all-cache-purge', |
| 58 |
'title' => __( 'Purge All Cache', 'powered-cache' ), |
| 59 |
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ), |
| 60 |
'parent' => 'powered-cache', |
| 61 |
) ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Purges all cache related things |
| 67 |
* |
| 68 |
* @since 1.1 |
| 69 |
*/ |
| 70 |
public function purge_all_cache() { |
| 71 |
|
| 72 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_all_cache' ) ) { |
| 73 |
wp_nonce_ays( '' ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) { |
| 77 |
Powered_Cache_Admin_Helper::set_flash_message( __( "You don't have permission to perform this action!", 'powered-cache' ), 'error' ); |
| 78 |
wp_safe_redirect( wp_get_referer() ); |
| 79 |
die(); |
| 80 |
} |
| 81 |
|
| 82 |
powered_cache_flush();// cleans object cache + page cache dir |
| 83 |
|
| 84 |
// extensions should use this action |
| 85 |
do_action( 'powered_cache_purge_all_cache' ); |
| 86 |
|
| 87 |
$msg = __( 'Cache flushed successfully', 'powered-cache' ); |
| 88 |
Powered_Cache_Admin_Helper::set_flash_message( $msg ); |
| 89 |
|
| 90 |
wp_safe_redirect( wp_get_referer() ); |
| 91 |
die(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @since 1.1 |
| 96 |
* @return Powered_Cache_Hooks |
| 97 |
*/ |
| 98 |
public static function factory() { |
| 99 |
|
| 100 |
static $instance; |
| 101 |
|
| 102 |
if ( ! $instance ) { |
| 103 |
$instance = new self(); |
| 104 |
$instance->setup(); |
| 105 |
} |
| 106 |
|
| 107 |
return $instance; |
| 108 |
} |
| 109 |
} |