Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.65% covered (danger)
17.65%
3 / 17
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Modules_State_Entry
17.65% covered (danger)
17.65%
3 / 17
25.00% covered (danger)
25.00%
1 / 4
65.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 set
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 merge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack_Boost\Data_Sync;
4
5use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
6use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Merge;
7use Automattic\Jetpack_Boost\Modules\Module;
8
9class Modules_State_Entry implements Entry_Can_Get, Entry_Can_Merge {
10
11    protected $modules = array();
12
13    public function __construct( $features ) {
14        foreach ( $features as $feature ) {
15            $instance                               = new Module( new $feature() );
16            $this->modules[ $instance->get_slug() ] = $instance;
17        }
18    }
19
20    public function get( $_fallback = false ) {
21        $state = array();
22
23        /*
24         * Module states are stored in their individual wp_options records.
25         * We're combining the states of all modules into a single record and attaching the availability of the module.
26         */
27        foreach ( $this->modules as $module ) {
28            $state[ $module->get_slug() ] = array(
29                'active'    => $module->is_available() && $module->is_enabled(),
30                'available' => $module->is_available(),
31            );
32        }
33
34        return $state;
35    }
36
37    public function set( $value ) {
38        foreach ( $value as $module_slug => $module_state ) {
39            if ( ! isset( $this->modules[ $module_slug ] ) ) {
40                continue;
41            }
42
43            $updated = $this->modules[ $module_slug ]->update( $module_state['active'] );
44            if ( $updated ) {
45                /**
46                 * Fires when a module is enabled or disabled.
47                 *
48                 * @param string $module The module slug.
49                 * @param bool   $status The new status.
50                 * @since 1.5.2
51                 */
52                do_action( 'jetpack_boost_module_status_updated', $module_slug, $module_state['active'] );
53            }
54        }
55    }
56
57    public function merge( $value, $partial_value ) {
58        return array_merge( $value, $partial_value );
59    }
60}