Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Regenerate_Admin_Notice
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 8
380
0.00% covered (danger)
0.00%
0 / 1
 enable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 dismiss
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_enabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_dismiss_url
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_handle_dismissal
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 maybe_render
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
42
 render
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Admin notice base class. Override this to implement each admin notice Jetpack Boost may show.
4 *
5 * @package automattic/jetpack-boost
6 */
7
8namespace Automattic\Jetpack_Boost\Admin;
9
10use Automattic\Jetpack\Boost_Core\Lib\Transient;
11
12/**
13 * Admin notice for letting users know they need to regenerate their Critical CSS.
14 */
15class Regenerate_Admin_Notice {
16
17    private static $dismissal_key = 'dismiss-critical-css-notice';
18
19    public static function enable() {
20        Transient::set( 'regenerate_admin_notice', true );
21    }
22
23    public static function dismiss() {
24        Transient::delete( 'regenerate_admin_notice' );
25    }
26
27    public static function is_enabled() {
28        return Transient::get( 'regenerate_admin_notice', false );
29    }
30
31    /**
32     * Helper method to generate a dismissal link for this message.
33     */
34    private static function get_dismiss_url() {
35        return add_query_arg(
36            array(
37                self::$dismissal_key => '',
38                'nonce'              => wp_create_nonce( 'jb_dismiss_notice' ),
39            )
40        );
41    }
42
43    public static function maybe_handle_dismissal() {
44        if ( ! is_admin()
45            || ! current_user_can( 'manage_options' )
46            || ! isset( $_GET[ self::$dismissal_key ] ) || ! isset( $_GET['nonce'] )
47            || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'jb_dismiss_notice' )
48        ) {
49            return;
50        }
51
52        // Dismiss the notice that shows up for major changes.
53        static::dismiss();
54
55        wp_safe_redirect( remove_query_arg( array( self::$dismissal_key, 'nonce' ) ) );
56    }
57
58    public static function init() {
59        add_action( 'admin_notices', array( static::class, 'maybe_render' ) );
60        if ( static::is_enabled() ) {
61            static::maybe_handle_dismissal();
62        }
63    }
64
65    public static function maybe_render() {
66        // We're not actually using the GET parameter here, it's only used to find out what page we're on.
67        // phpcs:disable WordPress.Security.NonceVerification.Recommended
68        $on_settings_page = is_admin() && isset( $_GET['page'] ) && Admin::MENU_SLUG === $_GET['page'];
69        if ( $on_settings_page || ! current_user_can( 'manage_options' ) ) {
70            return;
71        }
72
73        if ( static::is_enabled() ) {
74            static::render();
75        }
76    }
77
78    public static function render() {
79        ?>
80        <div id="jetpack-boost-notice-critical-css-regenerate" class="notice notice-warning is-dismissible">
81            <h3>
82                <?php esc_html_e( 'Jetpack Boost - Action Required', 'jetpack-boost' ); ?>
83            </h3>
84            <p>
85                <?php esc_html_e( 'The Critical CSS generated by Jetpack Boost was cleared due to a change in the site theme.', 'jetpack-boost' ); ?>
86            </p>
87            <p>
88                <?php esc_html_e( 'Please head to the Jetpack Boost dashboard to regenerate your Critical CSS.', 'jetpack-boost' ); ?>
89            </p>
90
91            <p>
92                <a class='button button-primary' href="<?php echo esc_url( admin_url( 'admin.php?page=' . Admin::MENU_SLUG ) ); ?>">
93                    <strong>
94                        <?php esc_html_e( 'Go to Jetpack Boost', 'jetpack-boost' ); ?>
95                    </strong>
96                </a>
97                <a class="jb-dismiss-notice" href="<?php echo esc_url( static::get_dismiss_url() ); ?>">
98                    <strong>
99                        <?php esc_html_e( 'Dismiss notice', 'jetpack-boost' ); ?>
100                    </strong>
101                </a>
102            </p>
103        </div>
104        <?php
105    }
106}