Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Automattic_For_Agencies_Client
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 9
110
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 init_packages
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 add_submenu
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 load_scripts_styles
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 Automattic For Agencies Client plugin.
4 *
5 * @package automattic/automattic-for-agencies-client-plugin
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12use Automattic\Jetpack\Assets;
13use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
14use Automattic\Jetpack\Connection\Manager as Connection_Manager;
15use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
16use Automattic\Jetpack\Plugin_Deactivation\Deactivation_Handler;
17use Automattic\Jetpack\Sync\Data_Settings;
18
19/**
20 * Class Automattic_For_Agencies_Client
21 */
22class Automattic_For_Agencies_Client {
23    /**
24     * Initialize the plugin.
25     */
26    public static function init() {
27        // Set up the REST authentication hooks.
28        Connection_Rest_Authentication::init();
29
30        // Init Jetpack packages
31        add_action( 'plugins_loaded', array( static::class, 'init_packages' ), 1 );
32
33        // Add submenu.
34        add_action( 'admin_menu', array( static::class, 'add_submenu' ) );
35
36        // Add scripts and styles to our admin page.
37        add_action( 'load-settings_page_' . AUTOMATTIC_FOR_AGENCIES_CLIENT_SLUG, array( static::class, 'load_scripts_styles' ) );
38
39        // Display a modal when trying to deactivate the plugin.
40        $manager = new Connection_Manager( 'automattic-for-agencies-client' );
41        if ( $manager->is_connected() ) {
42            Deactivation_Handler::init( AUTOMATTIC_FOR_AGENCIES_CLIENT_SLUG, __DIR__ . '/admin/deactivation-dialog.php' );
43        }
44    }
45
46    /**
47     * Configure what Jetpack packages should get automatically initialized.
48     *
49     * @since 0.1.0
50     *
51     * @return void
52     */
53    public static function init_packages() {
54        $config = new Automattic\Jetpack\Config();
55
56        // Connection package.
57        $config->ensure(
58            'connection',
59            array(
60                'slug'     => AUTOMATTIC_FOR_AGENCIES_CLIENT_SLUG,
61                'name'     => AUTOMATTIC_FOR_AGENCIES_CLIENT_NAME,
62                'url_info' => AUTOMATTIC_FOR_AGENCIES_CLIENT_URI,
63            )
64        );
65        // Sync package.
66        $must_sync_data = Data_Settings::MUST_SYNC_DATA_SETTINGS;
67        // Add additional modules.
68        $must_sync_data['jetpack_sync_modules'][] = 'Automattic\\Jetpack\\Sync\\Modules\\Plugins';
69        $must_sync_data['jetpack_sync_modules'][] = 'Automattic\\Jetpack\\Sync\\Modules\\Users';
70        $must_sync_data['jetpack_sync_modules'][] = 'Automattic\\Jetpack\\Sync\\Modules\\Meta';
71        $must_sync_data['jetpack_sync_modules'][] = 'Automattic\\Jetpack\\Sync\\Modules\\Stats';
72        $config->ensure( 'sync', $must_sync_data );
73
74        // Identity crisis package.
75        $config->ensure( 'identity_crisis' );
76    }
77
78    /**
79     * Add submenu.
80     *
81     * @since 0.1.0
82     *
83     * @return void
84     */
85    public static function add_submenu() {
86        add_submenu_page(
87            '',
88            AUTOMATTIC_FOR_AGENCIES_CLIENT_NAME,
89            __( 'Automattic for Agencies', 'automattic-for-agencies-client' ),
90            'manage_options',
91            AUTOMATTIC_FOR_AGENCIES_CLIENT_SLUG,
92            array( static::class, 'plugin_settings_page' )
93        );
94    }
95
96    /**
97     * Set up Admin Settings screen.
98     */
99    public static function load_scripts_styles() {
100        add_action( 'admin_enqueue_scripts', array( static::class, 'enqueue_admin_scripts' ) );
101    }
102
103    /**
104     * Enqueue plugin admin scripts and styles.
105     */
106    public static function enqueue_admin_scripts() {
107        Assets::register_script(
108            'automattic-for-agencies-client',
109            'build/index.js',
110            AUTOMATTIC_FOR_AGENCIES_CLIENT_ROOT_FILE,
111            array(
112                'in_footer'  => true,
113                'textdomain' => 'automattic-for-agencies-client',
114            )
115        );
116        Assets::enqueue_script( 'automattic-for-agencies-client' );
117        // Initial JS state including JP Connection data.
118        Connection_Initial_State::render_script( 'automattic-for-agencies-client' );
119        wp_add_inline_script( 'automattic-for-agencies-client', static::render_initial_state(), 'before' );
120    }
121
122    /**
123     * Render the initial state into a JavaScript variable.
124     *
125     * @return string
126     */
127    private static function render_initial_state() {
128        return 'var automatticForAgenciesClientInitialState=' . wp_json_encode( static::initial_state(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
129    }
130
131    /**
132     * Get the initial state data for hydrating the React UI.
133     *
134     * @return array
135     */
136    private static function initial_state() {
137        return array(
138            'apiRoot'           => esc_url_raw( rest_url() ),
139            'apiNonce'          => wp_create_nonce( 'wp_rest' ),
140            'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
141        );
142    }
143
144    /**
145     * Main plugin settings page.
146     */
147    public static function plugin_settings_page() {
148        ?>
149            <div id="automattic-for-agencies-client-root"></div>
150        <?php
151    }
152
153    /**
154     * Removes plugin from the connection manager
155     * If it's the last plugin using the connection, the site will be disconnected.
156     *
157     * @access public
158     * @static
159     */
160    public static function plugin_deactivation() {
161        $manager = new Connection_Manager( 'automattic-for-agencies-client' );
162        $manager->remove_connection();
163    }
164}