| @@ 18-67 (lines=50) @@ | ||
| 15 | /** |
|
| 16 | * The React initial state. |
|
| 17 | */ |
|
| 18 | class Initial_State { |
|
| 19 | ||
| 20 | /** |
|
| 21 | * The connection manager object. |
|
| 22 | * |
|
| 23 | * @var Manager |
|
| 24 | */ |
|
| 25 | private $manager; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * The constructor. |
|
| 29 | */ |
|
| 30 | public function __construct() { |
|
| 31 | $this->manager = new Manager(); |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Get the initial state data. |
|
| 36 | * |
|
| 37 | * @return array |
|
| 38 | */ |
|
| 39 | private function get_data() { |
|
| 40 | global $is_safari; |
|
| 41 | ||
| 42 | return array( |
|
| 43 | 'connectionStatus' => REST_Connector::connection_status( false ), |
|
| 44 | 'API' => array( |
|
| 45 | 'WP_API_root' => esc_url_raw( rest_url() ), |
|
| 46 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 47 | 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), |
|
| 48 | ), |
|
| 49 | 'connectionData' => array( |
|
| 50 | 'doNotUseConnectionIframe' => $is_safari || User_Agent_Info::is_opera_desktop() || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ), |
|
| 51 | 'authorizationUrl' => ( $this->manager->is_connected() && ! $this->manager->is_user_connected() ) |
|
| 52 | ? $this->manager->get_authorization_url( null, admin_url( 'tools.php?page=wpcom-connection-manager' ) ) |
|
| 53 | : null, |
|
| 54 | ), |
|
| 55 | ); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * Render the initial state into a JavaScript variable. |
|
| 60 | * |
|
| 61 | * @return string |
|
| 62 | */ |
|
| 63 | public function render() { |
|
| 64 | return 'var CUI_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));'; |
|
| 65 | } |
|
| 66 | ||
| 67 | } |
|
| 68 | ||
| @@ 16-65 (lines=50) @@ | ||
| 13 | /** |
|
| 14 | * The React initial state. |
|
| 15 | */ |
|
| 16 | class Initial_State { |
|
| 17 | ||
| 18 | /** |
|
| 19 | * The connection manager object. |
|
| 20 | * |
|
| 21 | * @var Manager |
|
| 22 | */ |
|
| 23 | private $manager; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * The constructor. |
|
| 27 | */ |
|
| 28 | public function __construct() { |
|
| 29 | $this->manager = new Manager(); |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Get the initial state data. |
|
| 34 | * |
|
| 35 | * @return array |
|
| 36 | */ |
|
| 37 | private function get_data() { |
|
| 38 | global $is_safari; |
|
| 39 | ||
| 40 | return array( |
|
| 41 | 'connectionStatus' => REST_Connector::connection_status( false ), |
|
| 42 | 'API' => array( |
|
| 43 | 'WP_API_root' => esc_url_raw( rest_url() ), |
|
| 44 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 45 | 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), |
|
| 46 | ), |
|
| 47 | 'connectionData' => array( |
|
| 48 | 'doNotUseConnectionIframe' => $is_safari || User_Agent_Info::is_opera_desktop() || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ), |
|
| 49 | 'authorizationUrl' => ( $this->manager->is_connected() && ! $this->manager->is_user_connected() ) |
|
| 50 | ? $this->manager->get_authorization_url( null, admin_url( 'tools.php?page=wpcom-connection-manager' ) ) |
|
| 51 | : null, |
|
| 52 | ), |
|
| 53 | ); |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * Render the initial state into a JavaScript variable. |
|
| 58 | * |
|
| 59 | * @return string |
|
| 60 | */ |
|
| 61 | public function render() { |
|
| 62 | return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));'; |
|
| 63 | } |
|
| 64 | ||
| 65 | } |
|
| 66 | ||