Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.07% |
34 / 43 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Page_Cache | |
79.07% |
34 / 43 |
|
50.00% |
4 / 8 |
19.65 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_data_sync | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
| handle_page_output_change | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| deactivate | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
| is_ready | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_available | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
9.66 | |||
| get_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache; |
| 4 | |
| 5 | use Automattic\Jetpack\Schema\Schema; |
| 6 | use Automattic\Jetpack\Status\Host; |
| 7 | use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync; |
| 8 | use Automattic\Jetpack_Boost\Contracts\Feature; |
| 9 | use Automattic\Jetpack_Boost\Contracts\Has_Data_Sync; |
| 10 | use Automattic\Jetpack_Boost\Contracts\Has_Deactivate; |
| 11 | use Automattic\Jetpack_Boost\Contracts\Needs_To_Be_Ready; |
| 12 | use Automattic\Jetpack_Boost\Contracts\Optimization; |
| 13 | use Automattic\Jetpack_Boost\Lib\Cache_Compatibility; |
| 14 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync\Page_Cache_Entry; |
| 15 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Clear_Page_Cache; |
| 16 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Deactivate_WPSC; |
| 17 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Run_Setup; |
| 18 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache; |
| 19 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache_Error; |
| 20 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache_Settings; |
| 21 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Logger; |
| 22 | |
| 23 | class Page_Cache implements Feature, Has_Deactivate, Has_Data_Sync, Optimization, Needs_To_Be_Ready { |
| 24 | /** |
| 25 | * @var array - The errors that occurred when removing the cache. |
| 26 | */ |
| 27 | private $removal_errors = array(); |
| 28 | |
| 29 | /** |
| 30 | * The signature used to identify the advanced-cache.php file owned by Jetpack Boost. |
| 31 | */ |
| 32 | const ADVANCED_CACHE_SIGNATURE = 'Boost Cache Plugin'; |
| 33 | |
| 34 | /** |
| 35 | * The full signature including the current version, to verify the Advanced-cache file is current. |
| 36 | */ |
| 37 | const ADVANCED_CACHE_VERSION = 'v0.0.3'; |
| 38 | |
| 39 | /** |
| 40 | * @var Boost_Cache_Settings - The settings for the page cache. |
| 41 | */ |
| 42 | private $settings; |
| 43 | |
| 44 | public function __construct() { |
| 45 | $this->settings = Boost_Cache_Settings::get_instance(); |
| 46 | } |
| 47 | |
| 48 | public function setup() { |
| 49 | Garbage_Collection::setup(); |
| 50 | |
| 51 | add_action( 'jetpack_boost_page_output_changed', array( $this, 'handle_page_output_change' ) ); |
| 52 | } |
| 53 | |
| 54 | public function register_data_sync( Data_Sync $instance ) { |
| 55 | $page_cache_schema = Schema::as_assoc_array( |
| 56 | array( |
| 57 | 'bypass_patterns' => Schema::as_array( Schema::as_string() ), |
| 58 | 'logging' => Schema::as_boolean(), |
| 59 | ) |
| 60 | ); |
| 61 | $page_cache_error_schema = Schema::as_assoc_array( |
| 62 | array( |
| 63 | 'code' => Schema::as_string(), |
| 64 | 'message' => Schema::as_string(), |
| 65 | 'dismissed' => Schema::as_boolean()->fallback( false ), |
| 66 | ) |
| 67 | )->nullable(); |
| 68 | |
| 69 | $instance->register_readonly( 'cache_debug_log', Schema::as_unsafe_any(), array( Logger::class, 'read' ) ); |
| 70 | $instance->register_readonly( 'cache_engine_loading', Schema::as_unsafe_any(), array( Boost_Cache::class, 'is_loaded' ) ); |
| 71 | |
| 72 | $instance->register( 'page_cache', $page_cache_schema, new Page_Cache_Entry() ); |
| 73 | // Page Cache error |
| 74 | $instance->register( 'page_cache_error', $page_cache_error_schema ); |
| 75 | |
| 76 | $instance->register_action( 'page_cache', 'run-setup', Schema::as_void(), new Run_Setup() ); |
| 77 | |
| 78 | $instance->register_action( 'page_cache', 'clear-page-cache', Schema::as_void(), new Clear_Page_Cache() ); |
| 79 | $instance->register_action( 'page_cache', 'deactivate-wpsc', Schema::as_void(), new Deactivate_WPSC() ); |
| 80 | } |
| 81 | |
| 82 | public function handle_page_output_change() { |
| 83 | Garbage_Collection::schedule_single_garbage_collection(); |
| 84 | |
| 85 | // Remove the action so it doesn't run again during the same request. |
| 86 | remove_action( 'jetpack_boost_page_output_changed', array( $this, 'handle_page_output_change' ) ); |
| 87 | } |
| 88 | /** |
| 89 | * Runs cleanup when the feature is deactivated. |
| 90 | * |
| 91 | * @return true|Boost_Cache_Error True if cleanup succeeded, or an error otherwise. |
| 92 | */ |
| 93 | public static function deactivate() { |
| 94 | Garbage_Collection::deactivate(); |
| 95 | $settings = Boost_Cache_Settings::get_instance(); |
| 96 | // Avoid failing on read-only settings when caching is already disabled. |
| 97 | $settings_result = $settings->get_enabled() ? $settings->set( array( 'enabled' => false ) ) : true; |
| 98 | $drop_in_deleted = Page_Cache_Setup::delete_advanced_cache(); |
| 99 | |
| 100 | if ( $settings_result instanceof Boost_Cache_Error ) { |
| 101 | return $settings_result; |
| 102 | } |
| 103 | |
| 104 | if ( ! $drop_in_deleted && file_exists( Page_Cache_Setup::get_advanced_cache_path() ) && Page_Cache_Setup::can_run_cache() ) { |
| 105 | return new Boost_Cache_Error( 'unable-to-delete-advanced-cache', __( 'Could not remove the Boost Page Cache drop-in.', 'jetpack-boost' ) ); |
| 106 | } |
| 107 | |
| 108 | return $settings_result; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * The module is active if cache engine is loaded. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | public function is_ready() { |
| 117 | return Boost_Cache::is_loaded(); |
| 118 | } |
| 119 | |
| 120 | public static function is_available() { |
| 121 | // Disable Page Cache on WoA and WP Cloud clients. |
| 122 | // They already have caching enabled. |
| 123 | if ( ( new Host() )->is_woa_site() || ( new Host() )->is_atomic_platform() ) { |
| 124 | if ( Page_Cache_Setup::can_run_cache() ) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // Disable Page Cache on sites that have their own caching service. |
| 132 | if ( Cache_Compatibility::has_cache() ) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | public static function get_slug() { |
| 140 | return 'page_cache'; |
| 141 | } |
| 142 | } |