← All changes
|
jetpack_vendor/automattic/jetpack-backup/src/class-initial-state.php
+69
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,69 @@ | ||
| 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). | |
| 12 | +namespace Automattic\Jetpack\Backup\V0005; | |
| 13 | + | |
| 14 | +use Automattic\Jetpack\Connection\Plugin_Storage as Connection_Plugin_Storage; | |
| 15 | +use Automattic\Jetpack\Status; | |
| 16 | +use Jetpack_Options; | |
| 17 | +use function add_action; | |
| 18 | +use function admin_url; | |
| 19 | +use function esc_url; | |
| 20 | +use function esc_url_raw; | |
| 21 | +use function get_bloginfo; | |
| 22 | +use function get_site_url; | |
| 23 | +use function plugins_url; | |
| 24 | +use function rest_url; | |
| 25 | +use function wp_create_nonce; | |
| 26 | +use function wp_json_encode; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * The React initial state. | |
| 30 | + */ | |
| 31 | +class 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 | +} | |