Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.68% covered (danger)
12.68%
18 / 142
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Color_Schemes
12.68% covered (danger)
12.68%
18 / 142
28.57% covered (danger)
28.57%
2 / 7
91.57
0.00% covered (danger)
0.00%
0 / 1
 __construct
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 register_admin_color_meta
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 update_admin_color_permissions_check
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_admin_color_scheme_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_admin_color_schemes
0.00% covered (danger)
0.00%
0 / 110
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_color_scheme_for_sidebar_notice
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 disable_css_concat_for_color_schemes
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Unifies admin color scheme selection across WP.com sites.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Status\Host;
11
12/**
13 * Unifies admin color scheme selection across WP.com sites.
14 *
15 * @phan-constructor-used-for-side-effects
16 */
17class Admin_Color_Schemes {
18
19    /**
20     * A list of core color schemes to override.
21     *
22     * @var array
23     */
24    const CORE_COLOR_SCHEMES = array( 'blue', 'coffee', 'ectoplasm', 'fresh', 'light', 'midnight', 'modern', 'ocean', 'sunrise' );
25
26    /**
27     * Admin_Color_Schemes constructor.
28     */
29    public function __construct() {
30        // We want to register the admin color schemes across all environments.
31        add_action( 'admin_init', array( $this, 'register_admin_color_schemes' ) );
32        // We don't want to make the admin_color available in users REST API endpoint for Simple sites.
33        if ( false === ( new Host() )->is_wpcom_simple() ) {
34            add_action( 'rest_api_init', array( $this, 'register_admin_color_meta' ) );
35        }
36
37        if ( ( new Host() )->is_wpcom_platform() ) { // Simple and Atomic sites.
38            add_filter( 'css_do_concat', array( $this, 'disable_css_concat_for_color_schemes' ), 10, 2 );
39            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_color_scheme_for_sidebar_notice' ) );
40        }
41    }
42
43    /**
44     * Makes admin_color available in users REST API endpoint.
45     */
46    public function register_admin_color_meta() {
47        register_meta(
48            'user',
49            'admin_color',
50            array(
51                'auth_callback' => array( $this, 'update_admin_color_permissions_check' ),
52                'description'   => __( 'Slug of the admin color scheme.', 'jetpack-masterbar' ),
53                'single'        => true,
54                'show_in_rest'  => array(
55                    'schema' => array( 'default' => 'fresh' ),
56                ),
57                'type'          => 'string',
58            )
59        );
60    }
61
62    /**
63     * Permission callback to edit the `admin_color` user meta.
64     *
65     * @param bool   $allowed   Whether the given user is allowed to edit this meta value.
66     * @param string $meta_key  Meta key. In this case `admin_color`.
67     * @param int    $object_id Queried user ID.
68     * @return bool
69     */
70    public function update_admin_color_permissions_check( $allowed, $meta_key, $object_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
71        return current_user_can( 'edit_user', $object_id );
72    }
73
74    /**
75     * Get the admin color scheme URL based on the environment
76     *
77     * @param string $color_scheme  The color scheme to get the URL for.
78     * @param string $file          The file name (optional, default: colors.css).
79     * @return string
80     */
81    public function get_admin_color_scheme_url( $color_scheme, $file = 'colors.css' ) {
82        return plugins_url( '../../dist/admin-color-schemes/colors/' . $color_scheme . '/' . $file, __FILE__ );
83    }
84
85    /**
86     * Registers new admin color schemes
87     */
88    public function register_admin_color_schemes() {
89
90        wp_admin_css_color(
91            'aquatic',
92            __( 'Aquatic', 'jetpack-masterbar' ),
93            $this->get_admin_color_scheme_url( 'aquatic' ),
94            array( '#135e96', '#007e65', '#043959', '#c5d9ed' ),
95            array(
96                'base'    => '#c5d9ed',
97                'focus'   => '#fff',
98                'current' => '#01263a',
99            )
100        );
101
102        wp_admin_css_color(
103            'classic-blue',
104            __( 'Classic Blue', 'jetpack-masterbar' ),
105            $this->get_admin_color_scheme_url( 'classic-blue' ),
106            array( '#135e96', '#b26200', '#dcdcde', '#646970' ),
107            array(
108                'base'    => '#646970',
109                'focus'   => '#2271b1',
110                'current' => '#fff',
111            )
112        );
113
114        wp_admin_css_color(
115            'classic-bright',
116            __( 'Classic Bright', 'jetpack-masterbar' ),
117            $this->get_admin_color_scheme_url( 'classic-bright' ),
118            array( '#135e96', '#c9256e', '#ffffff', '#e9eff5' ),
119            array(
120                'base'    => '#646970',
121                'focus'   => '#1d2327',
122                'current' => '#0a4b78',
123            )
124        );
125
126        wp_admin_css_color(
127            'classic-dark',
128            __( 'Classic Dark', 'jetpack-masterbar' ),
129            $this->get_admin_color_scheme_url( 'classic-dark' ),
130            array( '#101517', '#c9356e', '#32373c', '#0073aa' ),
131            array(
132                'base'    => '#a2aab2',
133                'focus'   => '#00b9eb',
134                'current' => '#fff',
135            )
136        );
137
138        wp_admin_css_color(
139            'contrast',
140            __( 'Contrast', 'jetpack-masterbar' ),
141            $this->get_admin_color_scheme_url( 'contrast' ),
142            array( '#101517', '#ffffff', '#32373c', '#b4b9be' ),
143            array(
144                'base'    => '#1d2327',
145                'focus'   => '#fff',
146                'current' => '#fff',
147            )
148        );
149
150        wp_admin_css_color(
151            'fresh',
152            __( 'Default', 'jetpack-masterbar' ),
153            $this->get_admin_color_scheme_url( 'fresh' ),
154            array( '#1d2327', '#2c3338', '#2271b1', '#72aee6' ),
155            array(
156                'base'    => '#a7aaad',
157                'focus'   => '#72aee6',
158                'current' => '#fff',
159            )
160        );
161
162        wp_admin_css_color(
163            'nightfall',
164            __( 'Nightfall', 'jetpack-masterbar' ),
165            $this->get_admin_color_scheme_url( 'nightfall' ),
166            array( '#00131c', '#043959', '#2271b1', '#9ec2e6' ),
167            array(
168                'base'    => '#9ec2e6',
169                'focus'   => '#fff',
170                'current' => '#fff',
171            )
172        );
173
174        wp_admin_css_color(
175            'powder-snow',
176            __( 'Powder Snow', 'jetpack-masterbar' ),
177            $this->get_admin_color_scheme_url( 'powder-snow' ),
178            array( '#101517', '#2271b1', '#dcdcde', '#646970' ),
179            array(
180                'base'    => '#646970',
181                'focus'   => '#135e96',
182                'current' => '#fff',
183            )
184        );
185
186        wp_admin_css_color(
187            'sakura',
188            __( 'Sakura', 'jetpack-masterbar' ),
189            $this->get_admin_color_scheme_url( 'sakura' ),
190            array( '#005042', '#f2ceda', '#2271b1', '#8c1749' ),
191            array(
192                'base'    => '#8c1749',
193                'focus'   => '#4f092a',
194                'current' => '#fff',
195            )
196        );
197
198        wp_admin_css_color(
199            'sunset',
200            __( 'Sunset', 'jetpack-masterbar' ),
201            $this->get_admin_color_scheme_url( 'sunset' ),
202            array( '#691c1c', '#b26200', '#f0c930', '#facfd2' ),
203            array(
204                'base'    => '#facfd2',
205                'focus'   => '#fff',
206                'current' => '#4f3500',
207            )
208        );
209    }
210
211    /**
212     * Enqueues current color-scheme sidebar notice overrides for core color schemes
213     */
214    public function enqueue_color_scheme_for_sidebar_notice() {
215        $color_scheme = get_user_option( 'admin_color' );
216        if ( in_array( $color_scheme, static::CORE_COLOR_SCHEMES, true ) ) {
217            wp_enqueue_style(
218                'jetpack-core-color-schemes-overrides-sidebar-notice',
219                $this->get_admin_color_scheme_url( $color_scheme, 'sidebar-notice.css' ),
220                array(),
221                Main::PACKAGE_VERSION
222            );
223        }
224    }
225
226    /**
227     * Currently, the selected color scheme CSS (with id = "colors") is concatenated (by Jetpack Boost / Page Optimize),
228     * and is output before the default color scheme CSS, making it lose in specificity.
229     *
230     * To prevent this, we disable CSS concatenation for color schemes.
231
232     * @param boolean $do_concat  Whether to concat the CSS file.
233     * @param string  $handle     The file handle.
234     * @return boolean
235     */
236    public function disable_css_concat_for_color_schemes( $do_concat, $handle ) {
237        if ( $handle === 'colors' ) {
238            return false;
239        }
240        return $do_concat;
241    }
242}