Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.62% covered (danger)
8.62%
5 / 58
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin
8.62% covered (danger)
8.62%
5 / 58
16.67% covered (danger)
16.67%
1 / 6
44.39
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 handle_admin_menu
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 admin_init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 plugin_page_settings_link
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 render_settings
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * The admin-specific functionality of the plugin.
4 *
5 * @since      1.0.0
6 * @package    automattic/jetpack-boost
7 */
8
9namespace Automattic\Jetpack_Boost\Admin;
10
11use Automattic\Jetpack\Admin_UI\Admin_Menu;
12use Automattic\Jetpack\Assets;
13use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
14use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
15use Automattic\Jetpack_Boost\Lib\Analytics;
16use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector;
17use Automattic\Jetpack_Boost\Lib\Premium_Features;
18use Automattic\Jetpack_Boost\Modules\Modules_Setup;
19
20class Admin {
21    /**
22     * Menu slug.
23     */
24    const MENU_SLUG = 'jetpack-boost';
25
26    public function init( Modules_Setup $modules ) {
27        Environment_Change_Detector::init();
28
29        // Initiate speed scores.
30        new Speed_Score( $modules->get_ready_active_optimization_modules(), 'boost-plugin' );
31
32        add_action( 'init', array( new Analytics(), 'init' ) );
33        add_filter( 'plugin_action_links_' . JETPACK_BOOST_PLUGIN_BASE, array( $this, 'plugin_page_settings_link' ) );
34        add_action( 'admin_menu', array( $this, 'handle_admin_menu' ), 1 ); // Akismet uses 4, so we use 1 to ensure both menus are added when only they exist.
35    }
36
37    public function handle_admin_menu() {
38        /**
39         * Filters the number of problems shown in the Boost sidebar menu
40         *
41         * @param int $count the number of problems shown.
42         *
43         * @since   1.0.0
44         */
45        $total_problems = apply_filters( 'jetpack_boost_total_problem_count', 0 );
46        $menu_label     = _x( 'Boost', 'The Jetpack Boost product name, without the Jetpack prefix', 'jetpack-boost' );
47        if ( $total_problems ) {
48            $menu_label .= sprintf( ' <span class="menu-counter count-%d"><span class="count">%d</span></span>', $total_problems, $total_problems );
49        }
50
51        $page_suffix = Admin_Menu::add_menu(
52            __( 'Jetpack Boost - Settings', 'jetpack-boost' ),
53            $menu_label,
54            'manage_options',
55            JETPACK_BOOST_SLUG,
56            array( $this, 'render_settings' ),
57            2
58        );
59        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
60    }
61
62    /**
63     * Enqueue scripts and styles for the admin page.
64     */
65    public function admin_init() {
66        // Clear premium features cache when the plugin settings page is loaded.
67        Premium_Features::clear_cache();
68
69        add_action( 'admin_enqueue_scripts', array( My_Jetpack_Initializer::class, 'enqueue_scripts' ) );
70        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
71    }
72
73    /**
74     * Register the JavaScript for the admin area.
75     *
76     * @since    1.0.0
77     */
78    public function enqueue_scripts() {
79        /**
80         * Filters the internal path to the distributed assets used by the plugin
81         *
82         * @param string $path the path to the assets
83         */
84        $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
85
86        $admin_js_handle = 'jetpack-boost-admin';
87
88        $admin_js_dependencies = array(
89            'wp-i18n',
90            'wp-components',
91            'my_jetpack_main_app',
92        );
93
94        Assets::register_script(
95            $admin_js_handle,
96            $internal_path . 'jetpack-boost.js',
97            JETPACK_BOOST_PATH,
98            array(
99                'dependencies' => $admin_js_dependencies,
100                'in_footer'    => true,
101                'textdomain'   => 'jetpack-boost',
102                'css_path'     => $internal_path . 'jetpack-boost.css',
103            )
104        );
105
106        wp_localize_script(
107            $admin_js_handle,
108            'Jetpack_Boost',
109            ( new Config() )->constants()
110        );
111
112        Assets::enqueue_script( $admin_js_handle );
113    }
114
115    /**
116     * Get settings link.
117     *
118     * @param array $links the array of links.
119     */
120    public function plugin_page_settings_link( $links ) {
121        $settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-boost' ) . '">' . esc_html__( 'Settings', 'jetpack-boost' ) . '</a>';
122        array_unshift( $links, $settings_link );
123
124        return $links;
125    }
126
127    /**
128     * Generate the settings page.
129     */
130    public function render_settings() {
131        wp_localize_script(
132            'jetpack-boost-admin',
133            'wpApiSettings',
134            array(
135                'root'  => esc_url_raw( rest_url() ),
136                'nonce' => wp_create_nonce( 'wp_rest' ),
137            )
138        );
139        ?>
140        <div id="jb-admin-settings"></div>
141        <?php
142    }
143}