Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
15 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Initial_State | |
83.33% |
15 / 18 |
|
50.00% |
2 / 4 |
4.07 | |
0.00% |
0 / 1 |
| get_data | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| set_connection_script_data | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render_script | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The React initial state. |
| 4 | * |
| 5 | * @package automattic/jetpack-connection |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Connection; |
| 9 | |
| 10 | use Automattic\Jetpack\Status; |
| 11 | |
| 12 | /** |
| 13 | * The React initial state. |
| 14 | */ |
| 15 | class Initial_State { |
| 16 | |
| 17 | /** |
| 18 | * Get the initial state data. |
| 19 | * |
| 20 | * @return array |
| 21 | */ |
| 22 | private static function get_data() { |
| 23 | global $wp_version; |
| 24 | |
| 25 | $status = new Status(); |
| 26 | |
| 27 | return array( |
| 28 | 'apiRoot' => esc_url_raw( rest_url() ), |
| 29 | 'apiNonce' => wp_create_nonce( 'wp_rest' ), |
| 30 | 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), |
| 31 | 'connectionStatus' => REST_Connector::connection_status( false ), |
| 32 | 'userConnectionData' => REST_Connector::get_user_connection_data( false ), |
| 33 | 'connectedPlugins' => REST_Connector::get_connection_plugins( false ), |
| 34 | 'wpVersion' => $wp_version, |
| 35 | 'siteSuffix' => $status->get_site_suffix(), |
| 36 | 'connectionErrors' => Error_Handler::get_instance()->get_displayable_errors(), |
| 37 | 'isOfflineMode' => $status->is_offline_mode(), |
| 38 | 'calypsoEnv' => ( new Status\Host() )->get_calypso_env(), |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set the connection script data. |
| 44 | * |
| 45 | * @param array $data The script data. |
| 46 | */ |
| 47 | public static function set_connection_script_data( $data ) { |
| 48 | |
| 49 | $data['connection'] = self::get_data(); |
| 50 | |
| 51 | return $data; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Render the initial state into a JavaScript variable. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function render() { |
| 60 | return 'var JP_CONNECTION_INITIAL_STATE; typeof JP_CONNECTION_INITIAL_STATE === "object" || (JP_CONNECTION_INITIAL_STATE = ' . wp_json_encode( self::get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Render the initial state using an inline script. |
| 65 | * |
| 66 | * @param string $handle The JS script handle. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public static function render_script( $handle ) { |
| 71 | wp_add_inline_script( $handle, static::render(), 'before' ); |
| 72 | } |
| 73 | } |