Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.86% covered (danger)
12.86%
18 / 140
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Color_Schemes
12.86% covered (danger)
12.86%
18 / 140
28.57% covered (danger)
28.57%
2 / 7
107.29
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 / 108
0.00% covered (danger)
0.00%
0 / 1
6
 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 ) {
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        global $_wp_admin_css_colors;
90
91        // Adds custom CSS overrides for Fresh
92        if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
93            $fresh = $_wp_admin_css_colors['fresh'];
94            wp_admin_css_color(
95                'fresh',
96                $fresh->name,
97                $this->get_admin_color_scheme_url( 'fresh' ),
98                $fresh->colors,
99                $fresh->icon_colors
100            );
101        }
102
103        wp_admin_css_color(
104            'aquatic',
105            __( 'Aquatic', 'jetpack-masterbar' ),
106            $this->get_admin_color_scheme_url( 'aquatic' ),
107            array( '#135e96', '#007e65', '#043959', '#c5d9ed' ),
108            array(
109                'base'    => '#c5d9ed',
110                'focus'   => '#fff',
111                'current' => '#01263a',
112            )
113        );
114
115        wp_admin_css_color(
116            'classic-blue',
117            __( 'Classic Blue', 'jetpack-masterbar' ),
118            $this->get_admin_color_scheme_url( 'classic-blue' ),
119            array( '#135e96', '#b26200', '#dcdcde', '#646970' ),
120            array(
121                'base'    => '#646970',
122                'focus'   => '#2271b1',
123                'current' => '#fff',
124            )
125        );
126
127        wp_admin_css_color(
128            'classic-bright',
129            __( 'Classic Bright', 'jetpack-masterbar' ),
130            $this->get_admin_color_scheme_url( 'classic-bright' ),
131            array( '#135e96', '#c9256e', '#ffffff', '#e9eff5' ),
132            array(
133                'base'    => '#646970',
134                'focus'   => '#1d2327',
135                'current' => '#0a4b78',
136            )
137        );
138
139        wp_admin_css_color(
140            'classic-dark',
141            __( 'Classic Dark', 'jetpack-masterbar' ),
142            $this->get_admin_color_scheme_url( 'classic-dark' ),
143            array( '#101517', '#c9356e', '#32373c', '#0073aa' ),
144            array(
145                'base'    => '#a2aab2',
146                'focus'   => '#00b9eb',
147                'current' => '#fff',
148            )
149        );
150
151        wp_admin_css_color(
152            'contrast',
153            __( 'Contrast', 'jetpack-masterbar' ),
154            $this->get_admin_color_scheme_url( 'contrast' ),
155            array( '#101517', '#ffffff', '#32373c', '#b4b9be' ),
156            array(
157                'base'    => '#1d2327',
158                'focus'   => '#fff',
159                'current' => '#fff',
160            )
161        );
162
163        wp_admin_css_color(
164            'nightfall',
165            __( 'Nightfall', 'jetpack-masterbar' ),
166            $this->get_admin_color_scheme_url( 'nightfall' ),
167            array( '#00131c', '#043959', '#2271b1', '#9ec2e6' ),
168            array(
169                'base'    => '#9ec2e6',
170                'focus'   => '#fff',
171                'current' => '#fff',
172            )
173        );
174
175        wp_admin_css_color(
176            'powder-snow',
177            __( 'Powder Snow', 'jetpack-masterbar' ),
178            $this->get_admin_color_scheme_url( 'powder-snow' ),
179            array( '#101517', '#2271b1', '#dcdcde', '#646970' ),
180            array(
181                'base'    => '#646970',
182                'focus'   => '#135e96',
183                'current' => '#fff',
184            )
185        );
186
187        wp_admin_css_color(
188            'sakura',
189            __( 'Sakura', 'jetpack-masterbar' ),
190            $this->get_admin_color_scheme_url( 'sakura' ),
191            array( '#005042', '#f2ceda', '#2271b1', '#8c1749' ),
192            array(
193                'base'    => '#8c1749',
194                'focus'   => '#4f092a',
195                'current' => '#fff',
196            )
197        );
198
199        wp_admin_css_color(
200            'sunset',
201            __( 'Sunset', 'jetpack-masterbar' ),
202            $this->get_admin_color_scheme_url( 'sunset' ),
203            array( '#691c1c', '#b26200', '#f0c930', '#facfd2' ),
204            array(
205                'base'    => '#facfd2',
206                'focus'   => '#fff',
207                'current' => '#4f3500',
208            )
209        );
210    }
211
212    /**
213     * Enqueues current color-scheme sidebar notice overrides for core color schemes
214     */
215    public function enqueue_color_scheme_for_sidebar_notice() {
216        $color_scheme = get_user_option( 'admin_color' );
217        if ( in_array( $color_scheme, static::CORE_COLOR_SCHEMES, true ) ) {
218            wp_enqueue_style(
219                'jetpack-core-color-schemes-overrides-sidebar-notice',
220                $this->get_admin_color_scheme_url( $color_scheme, 'sidebar-notice.css' ),
221                array(),
222                Main::PACKAGE_VERSION
223            );
224        }
225    }
226
227    /**
228     * Currently, the selected color scheme CSS (with id = "colors") is concatenated (by Jetpack Boost / Page Optimize),
229     * and is output before the default color scheme CSS, making it lose in specificity.
230     *
231     * To prevent this, we disable CSS concatenation for color schemes.
232
233     * @param boolean $do_concat  Whether to concat the CSS file.
234     * @param string  $handle     The file handle.
235     * @return boolean
236     */
237    public function disable_css_concat_for_color_schemes( $do_concat, $handle ) {
238        if ( $handle === 'colors' ) {
239            return false;
240        }
241        return $do_concat;
242    }
243}