Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.11% covered (warning)
51.11%
23 / 45
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Image_Guide
51.11% covered (warning)
51.11%
23 / 45
60.00% covered (warning)
60.00%
3 / 5
28.83
0.00% covered (danger)
0.00%
0 / 1
 setup
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 get_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_assets
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
2
 add_to_adminbar
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Image_Guide;
4
5use Automattic\Jetpack_Boost\Admin\Admin;
6use Automattic\Jetpack_Boost\Contracts\Feature;
7use Automattic\Jetpack_Boost\Lib\Analytics;
8
9class Image_Guide implements Feature {
10    /** @var string Asset manifest path. */
11    protected $asset_file = __DIR__ . '/dist/guide.min.asset.php';
12
13    public function setup() {
14        if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {
15            Image_Guide_Proxy::init();
16        }
17
18        // Show the UI only when the user is logged in, with sufficient permissions and isn't looking at the dashboard.
19        if ( is_admin() || ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
20            return;
21        }
22
23        // Enqueue the tracks library.
24        add_action( 'wp_enqueue_scripts', array( Analytics::class, 'init_tracks_scripts' ) );
25
26        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
27
28        /**
29         * The priority determines where the admin bar menu item is placed.
30         */
31        add_action( 'admin_bar_menu', array( $this, 'add_to_adminbar' ), 500 );
32    }
33
34    public static function get_slug() {
35        return 'image_guide';
36    }
37
38    public function enqueue_assets() {
39        $asset = file_exists( $this->asset_file ) ? require $this->asset_file : array(
40            'dependencies' => array( 'react', 'react-dom', 'react-jsx-runtime', 'wp-data', 'wp-i18n', 'wp-polyfill' ),
41            'version'      => JETPACK_BOOST_VERSION,
42        );
43        wp_enqueue_script( 'jetpack-boost-guide', plugins_url( 'dist/guide.min.js', __FILE__ ), $asset['dependencies'], $asset['version'], true );
44        wp_enqueue_style( 'jetpack-boost-guide', plugins_url( 'dist/guide.min.css', __FILE__ ), array(), JETPACK_BOOST_VERSION, 'screen' );
45
46        wp_localize_script(
47            'jetpack-boost-guide',
48            'jetpackBoostAnalytics',
49            array(
50                'tracksData' => Analytics::get_tracking_data(),
51            )
52        );
53        wp_localize_script(
54            'jetpack-boost-guide',
55            'jbImageGuide',
56            array(
57                'proxyNonce' => wp_create_nonce( Image_Guide_Proxy::NONCE_ACTION ),
58                'ajax_url'   => admin_url( 'admin-ajax.php' ),
59            )
60        );
61    }
62
63    /**
64     * @param \WP_Admin_Bar $bar
65     */
66    public function add_to_adminbar( $bar ) {
67        // Disable in Admin Dashboard
68        if ( is_admin() ) {
69            return;
70        }
71
72        $bar->add_menu(
73            array(
74                'id'     => 'jetpack-boost-guide',
75                'parent' => null,
76                'group'  => null,
77                'title'  => 'Jetpack Boost', // "Jetpack Boost" is a product name, do not translate.
78                'href'   => admin_url( 'admin.php?page=' . Admin::MENU_SLUG ),
79                'meta'   => array(
80                    'target' => '_self',
81                    'class'  => 'jetpack-boost-guide',
82                ),
83            )
84        );
85    }
86
87    public static function is_available() {
88        return true;
89    }
90}