Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initial_State
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 get_data
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
6
 render
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
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\Status;
16use Jetpack_Options;
17use function add_action;
18use function admin_url;
19use function esc_url;
20use function esc_url_raw;
21use function get_bloginfo;
22use function get_option;
23use function get_site_url;
24use function get_user_locale;
25use function plugins_url;
26use function rest_url;
27use function wp_create_nonce;
28use function wp_json_encode;
29
30/**
31 * The React initial state.
32 */
33class Initial_State {
34    /**
35     * Get the initial state data.
36     *
37     * @return array
38     */
39    private function get_data() {
40        return array(
41            'API'              => array(
42                'WP_API_root'       => esc_url_raw( rest_url() ),
43                'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
44                'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
45            ),
46            'jetpackStatus'    => array(
47                'calypsoSlug' => ( new Status() )->get_site_suffix(),
48            ),
49            'connectedPlugins' => Connection_Plugin_Storage::get_all(),
50            'siteData'         => array(
51                'id'             => Jetpack_Options::get_option( 'id' ),
52                'title'          => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(),
53                'adminUrl'       => esc_url( admin_url() ),
54                'gmtOffset'      => (float) get_option( 'gmt_offset', 0 ),
55                'timezoneString' => (string) get_option( 'timezone_string', '' ),
56                'locale'         => get_user_locale(),
57            ),
58            'assets'           => array(
59                'buildUrl' => plugins_url( '../build/', __FILE__ ),
60            ),
61        );
62    }
63
64    /**
65     * Render the initial state into a JavaScript variable.
66     *
67     * @return string
68     */
69    public function render() {
70        add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' );
71
72        return 'var JPBACKUP_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
73    }
74}