Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sync_Jetpack_Module_Status
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 10
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 alter_jetpack_available_modules
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 get_jetpack_module_status
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sync_to_jetpack
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 sync_from_jetpack
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 add_sync_from_jetpack_action
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 remove_sync_from_jetpack_action
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 add_sync_to_jetpack_action
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 remove_sync_to_jetpack_action
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Automattic\Jetpack_Boost\Compatibility\Jetpack;
3
4use Automattic\Jetpack_Boost\Modules\Features_Index;
5use Automattic\Jetpack_Boost\Modules\Module;
6
7/**
8 * Class that handles the sync of Jetpack module status to Boost module status.
9 */
10class Sync_Jetpack_Module_Status {
11
12    /**
13     * Slug of the Jetpack module
14     *
15     * @var string
16     */
17    public $jetpack_module_slug;
18
19    /**
20     * Slug of the Boost module
21     *
22     * @var string
23     */
24    public $boost_module_slug;
25
26    public function __construct( $boost_module_slug, $jetpack_module_slug ) {
27        $this->boost_module_slug   = str_replace( '_', '-', $boost_module_slug );
28        $this->jetpack_module_slug = $jetpack_module_slug;
29    }
30
31    public function init() {
32        // Use Jetpack as the source of truth for the module status
33        add_filter( "default_option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'get_jetpack_module_status' ) );
34        add_filter( "option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'get_jetpack_module_status' ) );
35        add_filter( 'jetpack_get_available_modules', array( $this, 'alter_jetpack_available_modules' ) );
36
37        $this->add_sync_to_jetpack_action();
38        $this->add_sync_from_jetpack_action();
39
40        /**
41         * Update the Jetpack Boost option to match the Jetpack option,
42         * in case the options are out of sync when the page is loaded.
43         */
44        add_action( 'load-jetpack_page_jetpack-boost', array( $this, 'sync_from_jetpack' ) );
45    }
46
47    /**
48     * If a Boost module is not available, remove it from the Jetpack available modules as well.
49     *
50     * This is useful in situations like disabling a Boost module with URL parameter.
51     */
52    public function alter_jetpack_available_modules( $jetpack_modules ) {
53        foreach ( Features_Index::FEATURES as $feature_class ) {
54            if ( str_replace( '_', '-', $feature_class::get_slug() ) !== $this->boost_module_slug ) {
55                continue;
56            }
57
58            $boost_module = new Module( new $feature_class() );
59            if ( ! $boost_module->is_available() ) {
60                unset( $jetpack_modules[ $this->jetpack_module_slug ] );
61                break;
62            }
63        }
64
65        return $jetpack_modules;
66    }
67
68    /**
69     * Get the status of the Jetpack module
70     *
71     * @return string
72     */
73    public function get_jetpack_module_status() {
74        return (string) \Jetpack::is_module_active( $this->jetpack_module_slug );
75    }
76
77    /**
78     * Forward all module status changes to Jetpack
79     * when interacting with Jetpack Boost dashboard.
80     */
81    public function sync_to_jetpack( $_unused, $new_value ) {
82        $this->remove_sync_from_jetpack_action();
83
84        if ( $new_value ) {
85            \Jetpack::activate_module( $this->jetpack_module_slug, false, false );
86        } else {
87            \Jetpack::deactivate_module( $this->jetpack_module_slug );
88        }
89
90        $this->add_sync_from_jetpack_action();
91
92        return $new_value;
93    }
94
95    /**
96     * The compatibility layer uses Jetpack as the single source of truth for shared modules.
97     * As a fallback, Boost still keeps track of the value in the database,
98     * This ensures that the value is still present when Jetpack is deactivated.
99     *
100     * This filter is going to track changes to the modules shared between Jetpack and Boost
101     * and make sure that both plugins are in in sync.
102     * Example: image_cdn
103     */
104    public function sync_from_jetpack() {
105        $this->remove_sync_to_jetpack_action();
106        update_option( "jetpack_boost_status_{$this->boost_module_slug}", \Jetpack::is_module_active( $this->jetpack_module_slug ) );
107        $this->add_sync_to_jetpack_action();
108    }
109
110    /**
111     * Sync the status to Boost when interacting with the Jetpack dashboard.
112     */
113    public function add_sync_from_jetpack_action() {
114        add_action( "jetpack_deactivate_module_{$this->jetpack_module_slug}", array( $this, 'sync_from_jetpack' ), 10, 2 );
115        add_action( "jetpack_activate_module_{$this->jetpack_module_slug}", array( $this, 'sync_from_jetpack' ), 10, 2 );
116    }
117
118    public function remove_sync_from_jetpack_action() {
119        remove_action( "jetpack_deactivate_module_{$this->jetpack_module_slug}", array( $this, 'sync_from_jetpack' ), 10 );
120        remove_action( "jetpack_activate_module_{$this->jetpack_module_slug}", array( $this, 'sync_from_jetpack' ), 10 );
121    }
122
123    /**
124     * Sync the status to Jetpack when interacting with the Boost dashboard
125     */
126    public function add_sync_to_jetpack_action() {
127        add_action( "add_option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'sync_to_jetpack' ), 10, 2 );
128        add_action( "update_option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'sync_to_jetpack' ), 10, 2 );
129    }
130
131    public function remove_sync_to_jetpack_action() {
132        remove_action( "add_option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'sync_to_jetpack' ), 10 );
133        remove_action( "update_option_jetpack_boost_status_{$this->boost_module_slug}", array( $this, 'sync_to_jetpack' ), 10 );
134    }
135}