Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.46% covered (danger)
38.46%
25 / 65
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin
38.46% covered (danger)
38.46%
25 / 65
33.33% covered (danger)
33.33%
2 / 6
18.42
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
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
 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        // Only report the count to users who can actually reach the Boost menu
46        // (added below with the 'manage_options' cap). Otherwise the central
47        // menu-badges total would include problems the current user can't see.
48        if ( current_user_can( 'manage_options' ) ) {
49            $total_problems = apply_filters( 'jetpack_boost_total_problem_count', 0 );
50            \Automattic\Jetpack\Menu_Badges\Menu_Badges::init(); // idempotent; wires the renderer.
51            \Automattic\Jetpack\Menu_Badges\Notification_Counts::register(
52                'jetpack-boost',
53                array(
54                    'menu_slug' => JETPACK_BOOST_SLUG,
55                    'count'     => (int) $total_problems,
56                    'type'      => 'count',
57                )
58            );
59        }
60
61        $page_suffix = Admin_Menu::add_menu(
62            __( 'Jetpack Boost - Settings', 'jetpack-boost' ),
63            'Boost', // "Boost" is a product name, do not translate.
64            'manage_options',
65            JETPACK_BOOST_SLUG,
66            array( $this, 'render_settings' ),
67            2
68        );
69        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
70    }
71
72    /**
73     * Enqueue scripts and styles for the admin page.
74     */
75    public function admin_init() {
76        // Clear premium features cache when the plugin settings page is loaded.
77        Premium_Features::clear_cache();
78
79        add_action( 'admin_enqueue_scripts', array( My_Jetpack_Initializer::class, 'enqueue_scripts' ) );
80        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
81    }
82
83    /**
84     * Register the JavaScript for the admin area.
85     *
86     * @since    1.0.0
87     */
88    public function enqueue_scripts() {
89        /**
90         * Filters the internal path to the distributed assets used by the plugin
91         *
92         * @param string $path the path to the assets
93         */
94        $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
95
96        $admin_js_handle = 'jetpack-boost-admin';
97
98        $admin_js_dependencies = array(
99            'wp-i18n',
100            'wp-components',
101            'my_jetpack_main_app',
102        );
103
104        Assets::register_script(
105            $admin_js_handle,
106            $internal_path . 'jetpack-boost.js',
107            JETPACK_BOOST_PATH,
108            array(
109                'dependencies' => $admin_js_dependencies,
110                'in_footer'    => true,
111                'textdomain'   => 'jetpack-boost',
112                'css_path'     => $internal_path . 'jetpack-boost.css',
113            )
114        );
115
116        wp_localize_script(
117            $admin_js_handle,
118            'Jetpack_Boost',
119            ( new Config() )->constants()
120        );
121
122        Assets::enqueue_script( $admin_js_handle );
123    }
124
125    /**
126     * Get settings link.
127     *
128     * @param array $links the array of links.
129     */
130    public function plugin_page_settings_link( $links ) {
131        $settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-boost' ) . '">' . esc_html__( 'Settings', 'jetpack-boost' ) . '</a>';
132        array_unshift( $links, $settings_link );
133
134        return $links;
135    }
136
137    /**
138     * Generate the settings page.
139     */
140    public function render_settings() {
141        wp_localize_script(
142            'jetpack-boost-admin',
143            'wpApiSettings',
144            array(
145                'root'  => esc_url_raw( rest_url() ),
146                'nonce' => wp_create_nonce( 'wp_rest' ),
147            )
148        );
149        ?>
150        <div id="jb-admin-settings"></div>
151        <?php
152    }
153}