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