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