Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.12% covered (danger)
12.12%
20 / 165
18.18% covered (danger)
18.18%
2 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Color_Schemes
12.12% covered (danger)
12.12%
20 / 165
18.18% covered (danger)
18.18%
2 / 11
189.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
3.14
 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
 get_admin_color_scheme_names
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 get_admin_color_scheme_name
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 / 99
0.00% covered (danger)
0.00%
0 / 1
2
 show_deprecated_admin_color_scheme_notice
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 move_admin_color_schemes_last
0.00% covered (danger)
0.00%
0 / 7
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     * A list of WP.com color schemes that are now deprecated.
28     *
29     * @var array
30     */
31    const WPCOM_COLOR_SCHEMES = array( 'aquatic', 'classic-blue', 'classic-bright', 'classic-dark', 'contrast', 'nightfall', 'powder-snow', 'sakura', 'sunset' );
32
33    /**
34     * Admin_Color_Schemes constructor.
35     */
36    public function __construct() {
37        // We want to register the admin color schemes across all environments.
38        add_action( 'admin_init', array( $this, 'register_admin_color_schemes' ) );
39        add_action( 'admin_notices', array( $this, 'show_deprecated_admin_color_scheme_notice' ) );
40        add_action( 'admin_color_scheme_picker', array( $this, 'move_admin_color_schemes_last' ), 11 );
41        // We don't want to make the admin_color available in users REST API endpoint for Simple sites.
42        if ( false === ( new Host() )->is_wpcom_simple() ) {
43            add_action( 'rest_api_init', array( $this, 'register_admin_color_meta' ) );
44        }
45
46        if ( ( new Host() )->is_wpcom_platform() ) { // Simple and Atomic sites.
47            add_filter( 'css_do_concat', array( $this, 'disable_css_concat_for_color_schemes' ), 10, 2 );
48            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_color_scheme_for_sidebar_notice' ) );
49        }
50    }
51
52    /**
53     * Makes admin_color available in users REST API endpoint.
54     */
55    public function register_admin_color_meta() {
56        register_meta(
57            'user',
58            'admin_color',
59            array(
60                'auth_callback' => array( $this, 'update_admin_color_permissions_check' ),
61                'description'   => __( 'Slug of the admin color scheme.', 'jetpack-masterbar' ),
62                'single'        => true,
63                'show_in_rest'  => array(
64                    'schema' => array( 'default' => 'fresh' ),
65                ),
66                'type'          => 'string',
67            )
68        );
69    }
70
71    /**
72     * Permission callback to edit the `admin_color` user meta.
73     *
74     * @param bool   $allowed   Whether the given user is allowed to edit this meta value.
75     * @param string $meta_key  Meta key. In this case `admin_color`.
76     * @param int    $object_id Queried user ID.
77     * @return bool
78     */
79    public function update_admin_color_permissions_check( $allowed, $meta_key, $object_id ) {
80        return current_user_can( 'edit_user', $object_id );
81    }
82
83    /**
84     * Get the admin color scheme URL based on the environment
85     *
86     * @param string $color_scheme  The color scheme to get the URL for.
87     * @param string $file          The file name (optional, default: colors.css).
88     * @return string
89     */
90    public function get_admin_color_scheme_url( $color_scheme, $file = 'colors.css' ) {
91        return plugins_url( '../../dist/admin-color-schemes/colors/' . $color_scheme . '/' . $file, __FILE__ );
92    }
93
94    /**
95     * Gets the translated names of the color schemes, keyed by slug.
96     *
97     * @return array
98     */
99    private function get_admin_color_scheme_names() {
100        return array(
101            'aquatic'        => __( 'Aquatic', 'jetpack-masterbar' ),
102            'classic-blue'   => __( 'Classic Blue', 'jetpack-masterbar' ),
103            'classic-bright' => __( 'Classic Bright', 'jetpack-masterbar' ),
104            'classic-dark'   => __( 'Classic Dark', 'jetpack-masterbar' ),
105            'contrast'       => __( 'Contrast', 'jetpack-masterbar' ),
106            'nightfall'      => __( 'Nightfall', 'jetpack-masterbar' ),
107            'powder-snow'    => __( 'Powder Snow', 'jetpack-masterbar' ),
108            'sakura'         => __( 'Sakura', 'jetpack-masterbar' ),
109            'sunset'         => __( 'Sunset', 'jetpack-masterbar' ),
110        );
111    }
112
113    /**
114     * Gets a color scheme's name, marked as deprecated.
115     *
116     * @param string $color_scheme The color scheme slug.
117     * @return string
118     */
119    private function get_admin_color_scheme_name( $color_scheme ) {
120        /* translators: %s: Admin color scheme name, e.g. "Aquatic". */
121        return sprintf( __( '%s (Retired)', 'jetpack-masterbar' ), $this->get_admin_color_scheme_names()[ $color_scheme ] );
122    }
123
124    /**
125     * Registers new admin color schemes
126     */
127    public function register_admin_color_schemes() {
128        wp_admin_css_color(
129            'aquatic',
130            $this->get_admin_color_scheme_name( 'aquatic' ),
131            $this->get_admin_color_scheme_url( 'aquatic' ),
132            array( '#135e96', '#007e65', '#043959', '#c5d9ed' ),
133            array(
134                'base'    => '#c5d9ed',
135                'focus'   => '#fff',
136                'current' => '#01263a',
137            )
138        );
139
140        wp_admin_css_color(
141            'classic-blue',
142            $this->get_admin_color_scheme_name( 'classic-blue' ),
143            $this->get_admin_color_scheme_url( 'classic-blue' ),
144            array( '#135e96', '#b26200', '#dcdcde', '#646970' ),
145            array(
146                'base'    => '#646970',
147                'focus'   => '#2271b1',
148                'current' => '#fff',
149            )
150        );
151
152        wp_admin_css_color(
153            'classic-bright',
154            $this->get_admin_color_scheme_name( 'classic-bright' ),
155            $this->get_admin_color_scheme_url( 'classic-bright' ),
156            array( '#135e96', '#c9256e', '#ffffff', '#e9eff5' ),
157            array(
158                'base'    => '#646970',
159                'focus'   => '#1d2327',
160                'current' => '#0a4b78',
161            )
162        );
163
164        wp_admin_css_color(
165            'classic-dark',
166            $this->get_admin_color_scheme_name( 'classic-dark' ),
167            $this->get_admin_color_scheme_url( 'classic-dark' ),
168            array( '#101517', '#c9356e', '#32373c', '#0073aa' ),
169            array(
170                'base'    => '#a2aab2',
171                'focus'   => '#00b9eb',
172                'current' => '#fff',
173            )
174        );
175
176        wp_admin_css_color(
177            'contrast',
178            $this->get_admin_color_scheme_name( 'contrast' ),
179            $this->get_admin_color_scheme_url( 'contrast' ),
180            array( '#101517', '#ffffff', '#32373c', '#b4b9be' ),
181            array(
182                'base'    => '#1d2327',
183                'focus'   => '#fff',
184                'current' => '#fff',
185            )
186        );
187
188        wp_admin_css_color(
189            'nightfall',
190            $this->get_admin_color_scheme_name( 'nightfall' ),
191            $this->get_admin_color_scheme_url( 'nightfall' ),
192            array( '#00131c', '#043959', '#2271b1', '#9ec2e6' ),
193            array(
194                'base'    => '#9ec2e6',
195                'focus'   => '#fff',
196                'current' => '#fff',
197            )
198        );
199
200        wp_admin_css_color(
201            'powder-snow',
202            $this->get_admin_color_scheme_name( 'powder-snow' ),
203            $this->get_admin_color_scheme_url( 'powder-snow' ),
204            array( '#101517', '#2271b1', '#dcdcde', '#646970' ),
205            array(
206                'base'    => '#646970',
207                'focus'   => '#135e96',
208                'current' => '#fff',
209            )
210        );
211
212        wp_admin_css_color(
213            'sakura',
214            $this->get_admin_color_scheme_name( 'sakura' ),
215            $this->get_admin_color_scheme_url( 'sakura' ),
216            array( '#005042', '#f2ceda', '#2271b1', '#8c1749' ),
217            array(
218                'base'    => '#8c1749',
219                'focus'   => '#4f092a',
220                'current' => '#fff',
221            )
222        );
223
224        wp_admin_css_color(
225            'sunset',
226            $this->get_admin_color_scheme_name( 'sunset' ),
227            $this->get_admin_color_scheme_url( 'sunset' ),
228            array( '#691c1c', '#b26200', '#f0c930', '#facfd2' ),
229            array(
230                'base'    => '#facfd2',
231                'focus'   => '#fff',
232                'current' => '#4f3500',
233            )
234        );
235    }
236
237    /**
238     * Asks users who still use a deprecated color scheme to switch to another one.
239     */
240    public function show_deprecated_admin_color_scheme_notice() {
241        $color_scheme = get_user_option( 'admin_color' );
242        if ( ! in_array( $color_scheme, static::WPCOM_COLOR_SCHEMES, true ) ) {
243            return;
244        }
245
246        wp_admin_notice(
247            sprintf(
248                /* translators: 1: Admin color scheme name, e.g. "Aquatic". 2: URL of the user profile page. */
249                __( 'Your current <strong>%1$s</strong> admin color scheme is no longer compatible with the editor. It is now retired and will be removed soon. <a href="%2$s">Switch to a different color scheme now</a>.', 'jetpack-masterbar' ),
250                esc_html( $this->get_admin_color_scheme_names()[ $color_scheme ] ),
251                esc_url( admin_url( 'profile.php#color-picker' ) )
252            ),
253            array(
254                'type' => 'warning',
255            )
256        );
257    }
258
259    /**
260     * Moves the admin color schemes to the end of the color scheme picker.
261     */
262    public function move_admin_color_schemes_last() {
263        wp_print_inline_script_tag(
264            sprintf(
265                'document.addEventListener( "DOMContentLoaded", function () {
266                    var picker = document.getElementById( "color-picker" );
267                    if ( ! picker ) {
268                        return;
269                    }
270                    %s.forEach( function ( slug ) {
271                        var input = document.getElementById( "admin_color_" + slug );
272                        if ( input ) {
273                            picker.appendChild( input.parentNode );
274                        }
275                    } );
276                } );',
277                wp_json_encode( static::WPCOM_COLOR_SCHEMES, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP )
278            )
279        );
280    }
281
282    /**
283     * Enqueues current color-scheme sidebar notice overrides for core color schemes
284     */
285    public function enqueue_color_scheme_for_sidebar_notice() {
286        $color_scheme = get_user_option( 'admin_color' );
287        if ( in_array( $color_scheme, static::CORE_COLOR_SCHEMES, true ) ) {
288            wp_enqueue_style(
289                'jetpack-core-color-schemes-overrides-sidebar-notice',
290                $this->get_admin_color_scheme_url( $color_scheme, 'sidebar-notice.css' ),
291                array(),
292                Main::PACKAGE_VERSION
293            );
294        }
295    }
296
297    /**
298     * Currently, the selected color scheme CSS (with id = "colors") is concatenated (by Jetpack Boost / Page Optimize),
299     * and is output before the default color scheme CSS, making it lose in specificity.
300     *
301     * To prevent this, we disable CSS concatenation for color schemes.
302
303     * @param boolean $do_concat  Whether to concat the CSS file.
304     * @param string  $handle     The file handle.
305     * @return boolean
306     */
307    public function disable_css_concat_for_color_schemes( $do_concat, $handle ) {
308        if ( $handle === 'colors' ) {
309            return false;
310        }
311        return $do_concat;
312    }
313}