Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Bar_Compatibility
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 enable_asynchronous_admin_bar
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 dequeue_admin_bar
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 force_admin_bar_stylesheet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Automattic\Jetpack_Boost\Lib\Critical_CSS;
3
4class Admin_Bar_Compatibility {
5
6    /**
7     * Enforces the admin bar stylesheet to load late and synchronously
8     * when the admin bar is present on the page.
9     */
10    public static function init() {
11
12        // Force the Admin Bar to render in the footer.
13        remove_action( 'wp_body_open', 'wp_admin_bar_render', '0' );
14
15        add_filter( 'jetpack_boost_async_style', array( __CLASS__, 'enable_asynchronous_admin_bar' ), 10, 2 );
16        add_action( 'wp_before_admin_bar_render', array( __CLASS__, 'force_admin_bar_stylesheet' ) );
17        add_action( 'wp_head', array( __CLASS__, 'dequeue_admin_bar' ), 0 );
18    }
19
20    /**
21     * Load the admin bar CSS synchronously.
22     *
23     * @param bool   $is_async Whether admin bar is async.
24     * @param string $handle   Asset handle.
25     *
26     * @return bool
27     */
28    public static function enable_asynchronous_admin_bar( $is_async, $handle ) {
29
30        if ( 'admin-bar' === $handle ) {
31            $is_async = false;
32        }
33
34        return $is_async;
35    }
36
37    /**
38     * Dequeue the admin bar stylesheet, so that it's not printed early.
39     *
40     * @see     wp_head
41     */
42    public static function dequeue_admin_bar() {
43        wp_dequeue_style( 'admin-bar' );
44    }
45
46    /**
47     * Force the admin bar stylesheet to print right before the admin bar markup.
48     *
49     * @see     wp_before_admin_bar_render
50     */
51    public static function force_admin_bar_stylesheet() {
52        wp_print_styles( 'admin-bar' );
53    }
54}