Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.85% covered (warning)
51.85%
14 / 27
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Protect_Dashboard_Feature_Flags
58.33% covered (warning)
58.33%
14 / 24
75.00% covered (warning)
75.00%
3 / 4
19.75
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 is_protect_plugin_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filter_available_modules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 filter_default_modules
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Protect dashboard feature flags.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Constants;
9use Automattic\Jetpack\Feature_Flags\Feature_Flags;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * Registers the flag that gates the `protect-dashboard` module, and hides the module while the Jetpack Protect plugin is active.
17 *
18 * @since 16.4
19 */
20class Jetpack_Protect_Dashboard_Feature_Flags {
21
22    const DASHBOARD = 'jetpack-protect-dashboard';
23
24    const MODULE = 'protect-dashboard';
25
26    /**
27     * Set while computing defaults as if the Jetpack Protect plugin were not active.
28     *
29     * @var bool
30     */
31    private static $ignore_protect_plugin = false;
32
33    /**
34     * Register the flag and the module filters.
35     *
36     * @return void
37     */
38    public static function register() {
39        Feature_Flags::register(
40            self::DASHBOARD,
41            array(
42                'default'     => true,
43                'description' => 'Make the Protect dashboard module available, which replaces the Protect page, including the Jetpack Protect plugin\'s. Not for sites using that plugin until the new page matches it.',
44                'owner'       => 'jetpack',
45            )
46        );
47
48        add_filter( 'jetpack_get_available_modules', array( __CLASS__, 'filter_available_modules' ) );
49        add_filter( 'jetpack_get_default_modules', array( __CLASS__, 'filter_default_modules' ), 10, 5 );
50    }
51
52    /**
53     * Whether the Jetpack Protect plugin is active, whose own page the module would take over until JETPACK-3062.
54     *
55     * @return bool
56     */
57    public static function is_protect_plugin_active() {
58        return Constants::is_defined( 'JETPACK_PROTECT_VERSION' );
59    }
60
61    /**
62     * Drop the module from the available list while the flag is off, which also keeps it from loading.
63     *
64     * It stays off while the Jetpack Protect plugin is active, too. Jetpack keeps an unavailable module in its stored active list.
65     *
66     * @param array $modules Available modules, keyed by slug.
67     * @return array
68     */
69    public static function filter_available_modules( $modules ) {
70        if (
71            ! Feature_Flags::is_enabled( self::DASHBOARD )
72            || ( ! self::$ignore_protect_plugin && self::is_protect_plugin_active() )
73        ) {
74            unset( $modules[ self::MODULE ] );
75        }
76        return $modules;
77    }
78
79    /**
80     * Keep the module a default while the Jetpack Protect plugin hides it, so the page appears once that plugin is gone.
81     *
82     * @param array       $modules                  Default module slugs.
83     * @param bool|string $min_version              Only modules introduced after this version.
84     * @param bool|string $max_version              Only modules introduced in or before this version.
85     * @param bool|null   $requires_connection      Only modules that do, or don't, require a connection.
86     * @param bool|null   $requires_user_connection Only modules that do, or don't, require a user connection.
87     * @return array
88     */
89    public static function filter_default_modules( $modules, $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) {
90        if ( self::$ignore_protect_plugin || ! self::is_protect_plugin_active() || in_array( self::MODULE, $modules, true ) ) {
91            return $modules;
92        }
93
94        $defaults                    = array();
95        self::$ignore_protect_plugin = true;
96        try {
97            $defaults = Jetpack::get_default_modules( $min_version, $max_version, $requires_connection, $requires_user_connection );
98        } finally {
99            self::$ignore_protect_plugin = false;
100        }
101
102        if ( in_array( self::MODULE, $defaults, true ) ) {
103            $modules[] = self::MODULE;
104        }
105        return $modules;
106    }
107}
108
109Jetpack_Protect_Dashboard_Feature_Flags::register();