| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Surge |
| 4 |
* Plugin URI: https://github.com/kovshenin/surge |
| 5 |
* Description: A fast and simple page caching plugin for WordPress |
| 6 |
* Author: Konstantin Kovshenin |
| 7 |
* Author URI: https://konstantin.blog |
| 8 |
* Text Domain: surge |
| 9 |
* Domain Path: /languages |
| 10 |
* Version: 1.2.0 |
| 11 |
* |
| 12 |
* @package Surge |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Surge; |
| 16 |
|
| 17 |
// Attempt to cache this request if cache is on. |
| 18 |
if ( defined( 'WP_CACHE' ) && WP_CACHE ) { |
| 19 |
include_once( __DIR__ . '/include/cache.php' ); |
| 20 |
} |
| 21 |
|
| 22 |
// Load more files later when necessary. |
| 23 |
add_action( 'plugins_loaded', function() { |
| 24 |
if ( false === get_option( 'surge_installed', false ) ) { |
| 25 |
if ( add_option( 'surge_installed', 0 ) ) { |
| 26 |
require_once( __DIR__ . '/include/install.php' ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if ( wp_doing_cron() ) { |
| 31 |
include_once( __DIR__ . '/include/cron.php' ); |
| 32 |
} |
| 33 |
|
| 34 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 35 |
include_once( __DIR__ . '/include/cli.php' ); |
| 36 |
} |
| 37 |
|
| 38 |
include_once( __DIR__ . '/include/invalidate.php' ); |
| 39 |
} ); |
| 40 |
|
| 41 |
// Site Health events |
| 42 |
add_filter( 'site_status_tests', function( $tests ) { |
| 43 |
include_once( __DIR__ . '/include/health.php' ); |
| 44 |
|
| 45 |
$tests['direct']['surge'] = [ |
| 46 |
'label' => 'Caching Test', |
| 47 |
'test' => '\Surge\health_test', |
| 48 |
]; |
| 49 |
|
| 50 |
return $tests; |
| 51 |
} ); |
| 52 |
|
| 53 |
// Support for 6.1+ cache headers check. |
| 54 |
add_filter( 'site_status_page_cache_supported_cache_headers', function( $headers ) { |
| 55 |
$headers['x-cache'] = static function( $value ) { |
| 56 |
return false !== strpos( strtolower( $value ), 'hit' ); |
| 57 |
}; |
| 58 |
return $headers; |
| 59 |
} ); |
| 60 |
|
| 61 |
// Schedule cron events. |
| 62 |
add_action( 'shutdown', function() { |
| 63 |
if ( ! wp_next_scheduled( 'surge_delete_expired' ) ) { |
| 64 |
wp_schedule_event( time(), 'hourly', 'surge_delete_expired' ); |
| 65 |
} |
| 66 |
} ); |
| 67 |
|
| 68 |
// Re-install on activation |
| 69 |
register_activation_hook( __FILE__, function() { |
| 70 |
delete_option( 'surge_installed' ); |
| 71 |
} ); |
| 72 |
|
| 73 |
// Remove advanced-cache.php on deactivation |
| 74 |
register_deactivation_hook( __FILE__, function() { |
| 75 |
delete_option( 'surge_installed' ); |
| 76 |
|
| 77 |
// Remove advanced-cache.php only if its ours. |
| 78 |
if ( file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) ) { |
| 79 |
$contents = file_get_contents( WP_CONTENT_DIR . '/advanced-cache.php' ); |
| 80 |
if ( strpos( $contents, 'namespace Surge;' ) !== false ) { |
| 81 |
unlink( WP_CONTENT_DIR . '/advanced-cache.php' ); |
| 82 |
} |
| 83 |
} |
| 84 |
} ); |
| 85 |
|