Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.77% covered (danger)
30.77%
16 / 52
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Starter_Plugin
32.00% covered (danger)
32.00%
16 / 50
0.00% covered (danger)
0.00%
0 / 7
22.41
0.00% covered (danger)
0.00%
0 / 1
 __construct
59.26% covered (warning)
59.26%
16 / 27
0.00% covered (danger)
0.00%
0 / 1
1.07
 admin_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_admin_scripts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 render_initial_state
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initial_state
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 plugin_settings_page
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 plugin_deactivation
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Primary class file for the Jetpack Starter Plugin plugin.
4 *
5 * @package automattic/jetpack-starter-plugin
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12use Automattic\Jetpack\Admin_UI\Admin_Menu;
13use Automattic\Jetpack\Assets;
14use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
15use Automattic\Jetpack\Connection\Manager as Connection_Manager;
16use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
17use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
18use Automattic\Jetpack\Sync\Data_Settings;
19
20/**
21 * Class Jetpack_Starter_Plugin
22 *
23 * @phan-constructor-used-for-side-effects
24 */
25class Jetpack_Starter_Plugin {
26
27    /**
28     * Constructor.
29     */
30    public function __construct() {
31        // Set up the REST authentication hooks.
32        Connection_Rest_Authentication::init();
33
34        $page_suffix = Admin_Menu::add_menu(
35            __( 'Jetpack Starter Plugin', 'jetpack-starter-plugin' ),
36            _x( 'Starter Plugin', 'The Jetpack Starter Plugin product name, without the Jetpack prefix', 'jetpack-starter-plugin' ),
37            'manage_options',
38            'jetpack-starter-plugin',
39            array( $this, 'plugin_settings_page' )
40        );
41        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
42
43        // Init Jetpack packages
44        add_action(
45            'plugins_loaded',
46            function () {
47                $config = new Automattic\Jetpack\Config();
48                // Connection package.
49                $config->ensure(
50                    'connection',
51                    array(
52                        'slug'     => JETPACK_STARTER_PLUGIN_SLUG,
53                        'name'     => JETPACK_STARTER_PLUGIN_NAME,
54                        'url_info' => JETPACK_STARTER_PLUGIN_URI,
55                    )
56                );
57                // Sync package.
58                $config->ensure( 'sync', Data_Settings::MUST_SYNC_DATA_SETTINGS );
59
60                // Identity crisis package.
61                $config->ensure( 'identity_crisis' );
62            },
63            1
64        );
65
66        My_Jetpack_Initializer::init();
67    }
68
69    /**
70     * Initialize the admin resources.
71     */
72    public function admin_init() {
73        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
74    }
75
76    /**
77     * Enqueue plugin admin scripts and styles.
78     */
79    public function enqueue_admin_scripts() {
80
81        Assets::register_script(
82            'jetpack-starter-plugin',
83            'build/index.js',
84            JETPACK_STARTER_PLUGIN_ROOT_FILE,
85            array(
86                'in_footer'  => true,
87                'textdomain' => 'jetpack-starter-plugin',
88            )
89        );
90        Assets::enqueue_script( 'jetpack-starter-plugin' );
91        // Initial JS state including JP Connection data.
92        Connection_Initial_State::render_script( 'jetpack-starter-plugin' );
93        wp_add_inline_script( 'jetpack-starter-plugin', $this->render_initial_state(), 'before' );
94    }
95
96    /**
97     * Render the initial state into a JavaScript variable.
98     *
99     * @return string
100     */
101    public function render_initial_state() {
102        return 'var jetpackStarterPluginInitialState=' . wp_json_encode( $this->initial_state(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
103    }
104
105    /**
106     * Get the initial state data for hydrating the React UI.
107     *
108     * @return array
109     */
110    public function initial_state() {
111        return array(
112            'apiRoot'           => esc_url_raw( rest_url() ),
113            'apiNonce'          => wp_create_nonce( 'wp_rest' ),
114            'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
115        );
116    }
117
118    /**
119     * Main plugin settings page.
120     */
121    public function plugin_settings_page() {
122        ?>
123            <div id="jetpack-starter-plugin-root"></div>
124        <?php
125    }
126
127    /**
128     * Removes plugin from the connection manager
129     * If it's the last plugin using the connection, the site will be disconnected.
130     *
131     * @access public
132     * @static
133     */
134    public static function plugin_deactivation() {
135        $manager = new Connection_Manager( 'jetpack-starter-plugin' );
136        $manager->remove_connection();
137    }
138}