| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Prevent Browser Caching |
| 4 |
* Description: Prevents browser cache problems: visitors always get the current version of your CSS, JS, images and pages, while caching keeps working. |
| 5 |
* Version: 3.2.1 |
| 6 |
* Requires at least: 4.7 |
| 7 |
* Requires PHP: 7.2 |
| 8 |
* Author: Kostya Tereshchuk |
| 9 |
* Author URI: https://tutori.org/kostya/ |
| 10 |
* License: GPLv2 or later |
| 11 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 12 |
* Text Domain: prevent-browser-caching |
| 13 |
* Domain Path: /lang/ |
| 14 |
*/ |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
if ( ! defined( 'PREVENT_BROWSER_CACHING_VERSION' ) ) { |
| 22 |
define( 'PREVENT_BROWSER_CACHING_VERSION', '3.2.1' ); |
| 23 |
} |
| 24 |
|
| 25 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 26 |
include_once dirname( __FILE__ ) . '/includes/class-prevent-browser-caching-cli.php'; |
| 27 |
} |
| 28 |
|
| 29 |
add_action( 'wp_abilities_api_init', 'prevent_browser_caching_register_abilities' ); |
| 30 |
|
| 31 |
if ( ! function_exists( 'prevent_browser_caching_register_abilities' ) ) { |
| 32 |
/** |
| 33 |
* Register the plugin's abilities (no-op on WordPress without the Abilities API). |
| 34 |
*/ |
| 35 |
function prevent_browser_caching_register_abilities() |
| 36 |
{ |
| 37 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
include_once dirname( __FILE__ ) . '/includes/class-prevent-browser-caching-abilities.php'; |
| 42 |
|
| 43 |
Prevent_Browser_Caching_Abilities::register(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! function_exists( 'prevent_browser_caching_plugin_actions' ) ) { |
| 48 |
/** |
| 49 |
* Add settings to plugin links. |
| 50 |
* @param $actions |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
function prevent_browser_caching_plugin_actions( $actions ) |
| 54 |
{ |
| 55 |
array_unshift( $actions, "<a href=\"" . esc_url( menu_page_url( 'prevent-browser-caching', false ) ) . "\">" . esc_html__( "Settings", "prevent-browser-caching" ) . "</a>" ); |
| 56 |
return $actions; |
| 57 |
} |
| 58 |
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'prevent_browser_caching_plugin_actions', 10, 1 ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! function_exists( 'prevent_browser_caching_load_textdomain' ) ) { |
| 62 |
/** |
| 63 |
* Set languages directory. |
| 64 |
*/ |
| 65 |
function prevent_browser_caching_load_textdomain() |
| 66 |
{ |
| 67 |
load_plugin_textdomain( 'prevent-browser-caching', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); |
| 68 |
} |
| 69 |
add_action( 'plugins_loaded', 'prevent_browser_caching_load_textdomain' ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! function_exists( 'prevent_browser_caching' ) ) { |
| 73 |
/** |
| 74 |
* Changes the version of CSS and JS files. |
| 75 |
* Disables loading Prevent_Browser_Caching class if this function is used before setup theme. |
| 76 |
* |
| 77 |
* @param array $args |
| 78 |
*/ |
| 79 |
function prevent_browser_caching( $args = array() ) { |
| 80 |
if ( ! class_exists( 'Prevent_Browser_Caching_Function' ) ) { |
| 81 |
include_once 'includes/class-prevent-browser-caching-function.php'; |
| 82 |
} |
| 83 |
|
| 84 |
Prevent_Browser_Caching_Function::instance( $args ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! function_exists( 'maybe_load_class_prevent_browser_caching' ) ) { |
| 89 |
/** |
| 90 |
* Load Prevent_Browser_Caching class if the function prevent_browser_caching is not used before setup theme. |
| 91 |
*/ |
| 92 |
function maybe_load_class_prevent_browser_caching() |
| 93 |
{ |
| 94 |
if ( ! class_exists( 'Prevent_Browser_Caching' ) && ! class_exists( 'Prevent_Browser_Caching_Function' ) ) { |
| 95 |
include_once 'includes/class-prevent-browser-caching.php'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
add_action( 'after_setup_theme', 'maybe_load_class_prevent_browser_caching' ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! function_exists( 'prevent_browser_caching_activate' ) ) { |
| 103 |
/** |
| 104 |
* On activation: store the recommended defaults for fresh installs (an existing |
| 105 |
* option is never overwritten) and remember to open the settings page. |
| 106 |
*/ |
| 107 |
function prevent_browser_caching_activate( $network_wide = false ) |
| 108 |
{ |
| 109 |
if ( ! class_exists( 'Prevent_Browser_Caching' ) ) { |
| 110 |
include_once dirname( __FILE__ ) . '/includes/class-prevent-browser-caching.php'; |
| 111 |
} |
| 112 |
|
| 113 |
add_option( 'prevent_browser_caching_options', Prevent_Browser_Caching::get_fresh_defaults() ); |
| 114 |
|
| 115 |
// Fresh installs never see the "what's new" notice; upgraded sites |
| 116 |
// (option row already exists, no seen-version yet) see it once. |
| 117 |
add_option( 'prevent_browser_caching_seen_version', PREVENT_BROWSER_CACHING_VERSION, '', false ); |
| 118 |
|
| 119 |
// A re-activation with the cache-policy option on must restore the |
| 120 |
// .htaccess block that deactivation removed. |
| 121 |
$pbc_existing = get_option( 'prevent_browser_caching_options' ); |
| 122 |
|
| 123 |
if ( is_array( $pbc_existing ) && ! empty( $pbc_existing['cache_policy'] ) ) { |
| 124 |
Prevent_Browser_Caching::require_cache_policy_class(); |
| 125 |
Prevent_Browser_Caching_Cache_Policy::sync( array(), Prevent_Browser_Caching::instance()->filter_options( $pbc_existing ) ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! $network_wide ) { |
| 129 |
set_transient( 'pbc_activation_redirect', 1, MINUTE_IN_SECONDS ); |
| 130 |
} |
| 131 |
} |
| 132 |
register_activation_hook( __FILE__, 'prevent_browser_caching_activate' ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! function_exists( 'prevent_browser_caching_deactivate' ) ) { |
| 136 |
/** |
| 137 |
* On deactivation: a disabled plugin must leave no server-config footprint, |
| 138 |
* so the .htaccess cache-policy block is removed (options are kept). |
| 139 |
*/ |
| 140 |
function prevent_browser_caching_deactivate() |
| 141 |
{ |
| 142 |
if ( ! class_exists( 'Prevent_Browser_Caching' ) ) { |
| 143 |
include_once dirname( __FILE__ ) . '/includes/class-prevent-browser-caching.php'; |
| 144 |
} |
| 145 |
|
| 146 |
Prevent_Browser_Caching::require_cache_policy_class(); |
| 147 |
|
| 148 |
Prevent_Browser_Caching_Cache_Policy::remove_rules(); |
| 149 |
delete_option( Prevent_Browser_Caching_Cache_Policy::PROBE_OPTION ); |
| 150 |
} |
| 151 |
register_deactivation_hook( __FILE__, 'prevent_browser_caching_deactivate' ); |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! function_exists( 'prevent_browser_caching_activation_redirect' ) ) { |
| 155 |
/** |
| 156 |
* Open the settings page right after a single-plugin activation. |
| 157 |
*/ |
| 158 |
function prevent_browser_caching_activation_redirect() |
| 159 |
{ |
| 160 |
if ( ! get_transient( 'pbc_activation_redirect' ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
delete_transient( 'pbc_activation_redirect' ); |
| 165 |
|
| 166 |
if ( isset( $_GET['activate-multi'] ) || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
wp_safe_redirect( admin_url( 'options-general.php?page=prevent-browser-caching' ) ); |
| 171 |
exit; |
| 172 |
} |
| 173 |
add_action( 'admin_init', 'prevent_browser_caching_activation_redirect' ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( is_admin() ) { |
| 177 |
include_once 'includes/admin/class-prevent-browser-caching-admin-settings.php'; |
| 178 |
} |
| 179 |
|