Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Wpcom_Admin_Shell_Rollout
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 default_on_for_a11ns
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 apply_kill_switch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_killed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Admin shell rollout on WordPress.com: on for Automatticians, behind a kill switch.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Turns the `jetpack-admin-shell` flag on for Automatticians, and lets WordPress.com force it off.
12 *
13 * Precedence, highest first: kill switch, the site's Tools → Feature Flags override, the Automattician default.
14 * Temporary: remove the default once the flag defaults on, and the class once the kill switch is no longer needed.
15 */
16class Wpcom_Admin_Shell_Rollout {
17
18    /**
19     * The flag Admin_Shell registers in the admin-ui package, which this package does not require.
20     */
21    const FLAG = 'jetpack-admin-shell';
22
23    /**
24     * Network option that, when truthy, turns the shell off for every site on the network.
25     */
26    const KILL_SWITCH_OPTION = 'wpcom_admin_shell_kill_switch';
27
28    /**
29     * Register the hooks.
30     *
31     * @return void
32     */
33    public static function init() {
34        // Before Wpcom_Feature_Flags::filter_enabled() at 10, so a per-site override beats this default.
35        add_filter( 'jetpack_feature_flag_enabled', array( self::class, 'default_on_for_a11ns' ), 5, 2 );
36        // Both: the flag hook keeps direct flag reads honest, the shell hook beats any later shell filter.
37        add_filter( 'jetpack_feature_flag_enabled_' . self::FLAG, array( self::class, 'apply_kill_switch' ), PHP_INT_MAX );
38        add_filter( 'jetpack_admin_shell_enabled', array( self::class, 'apply_kill_switch' ), PHP_INT_MAX );
39    }
40
41    /**
42     * Turn the shell's flag on when the visitor is an Automattician.
43     *
44     * @param bool   $enabled Whether the flag is enabled.
45     * @param string $name    Flag name.
46     * @return bool Whether the flag is enabled.
47     */
48    public static function default_on_for_a11ns( $enabled, $name ) {
49        if ( self::FLAG !== $name || $enabled ) {
50            return $enabled;
51        }
52
53        return Wpcom_Feature_Flags::is_a11n();
54    }
55
56    /**
57     * Force the shell off while the kill switch is on.
58     *
59     * @param bool $enabled Whether the shell is on.
60     * @return bool Whether the shell is on.
61     */
62    public static function apply_kill_switch( $enabled ) {
63        return self::is_killed() ? false : (bool) $enabled;
64    }
65
66    /**
67     * Whether the kill switch is on.
68     *
69     * @return bool
70     */
71    public static function is_killed() {
72        /**
73         * Filters whether the admin shell is forced off on WordPress.com, ahead of every other setting.
74         *
75         * @since $$next-version$$
76         *
77         * @param bool $killed Whether the shell is forced off. Defaults to the `wpcom_admin_shell_kill_switch` network option.
78         */
79        return (bool) apply_filters( 'wpcom_admin_shell_kill_switch', wp_validate_boolean( get_site_option( self::KILL_SWITCH_OPTION, false ) ) );
80    }
81}