Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Initial_State
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 get_data
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
 is_add_license_screen_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 render
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * The React initial state.
4 *
5 * @package automattic/jetpack-backup-plugin
6 */
7
8// After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in
9// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
10// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
11// are installed, or in some other cases).
12namespace Automattic\Jetpack\Backup\V0005;
13
14use Automattic\Jetpack\Connection\Plugin_Storage as Connection_Plugin_Storage;
15use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
16use Automattic\Jetpack\Status;
17use Jetpack_Options;
18use function add_action;
19use function admin_url;
20use function esc_url;
21use function esc_url_raw;
22use function get_bloginfo;
23use function get_site_url;
24use function plugins_url;
25use function rest_url;
26use function wp_create_nonce;
27use function wp_json_encode;
28
29/**
30 * The React initial state.
31 */
32class Initial_State {
33    /**
34     * Get the initial state data.
35     *
36     * @return array
37     */
38    private function get_data() {
39        return array(
40            'API'              => array(
41                'WP_API_root'       => esc_url_raw( rest_url() ),
42                'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
43                'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
44            ),
45            'jetpackStatus'    => array(
46                'calypsoSlug' => ( new Status() )->get_site_suffix(),
47            ),
48            'connectedPlugins' => Connection_Plugin_Storage::get_all(),
49            'siteData'         => array(
50                'id'         => Jetpack_Options::get_option( 'id' ),
51                'title'      => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(),
52                'adminUrl'   => esc_url( admin_url() ),
53                'addLicense' => self::is_add_license_screen_enabled(),
54            ),
55            'assets'           => array(
56                'buildUrl' => plugins_url( '../build/', __FILE__ ),
57            ),
58        );
59    }
60
61    /**
62     * Whether My Jetpack's add-license screen is available, which offline mode turns off.
63     *
64     * @since 5.3.0
65     *
66     * @return bool
67     */
68    public static function is_add_license_screen_enabled() {
69        return class_exists( My_Jetpack_Initializer::class ) && My_Jetpack_Initializer::is_licensing_ui_enabled();
70    }
71
72    /**
73     * Render the initial state into a JavaScript variable.
74     *
75     * @return string
76     */
77    public function render() {
78        add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' );
79
80        return 'var JPBACKUP_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
81    }
82}