← All changes
|
jetpack_vendor/automattic/jetpack-scan-page/src/class-initial-state.php
+69
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,69 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * The Scan React initial state. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-scan-page | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Scan_Page; | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Status; | |
| 11 | +use Jetpack_Options; | |
| 12 | +use function admin_url; | |
| 13 | +use function esc_url_raw; | |
| 14 | +use function get_bloginfo; | |
| 15 | +use function get_locale; | |
| 16 | +use function get_option; | |
| 17 | +use function get_site_url; | |
| 18 | +use function plugins_url; | |
| 19 | +use function rest_url; | |
| 20 | +use function wp_create_nonce; | |
| 21 | +use function wp_json_encode; | |
| 22 | +use function wp_parse_url; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * The Scan React initial state. | |
| 26 | + */ | |
| 27 | +class Initial_State { | |
| 28 | + /** | |
| 29 | + * Get the initial state data. | |
| 30 | + * | |
| 31 | + * @return array | |
| 32 | + */ | |
| 33 | + private function get_data() { | |
| 34 | + $gmt_offset = get_option( 'gmt_offset' ); | |
| 35 | + $timezone_string = get_option( 'timezone_string' ); | |
| 36 | + $home_host = wp_parse_url( get_site_url(), PHP_URL_HOST ); | |
| 37 | + | |
| 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 | + ), | |
| 43 | + 'jetpackStatus' => array( | |
| 44 | + 'calypsoSlug' => ( new Status() )->get_site_suffix(), | |
| 45 | + ), | |
| 46 | + 'siteData' => array( | |
| 47 | + 'id' => Jetpack_Options::get_option( 'id' ), | |
| 48 | + 'title' => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(), | |
| 49 | + 'adminUrl' => esc_url_raw( admin_url() ), | |
| 50 | + 'slug' => is_string( $home_host ) ? $home_host : '', | |
| 51 | + 'gmtOffset' => is_numeric( $gmt_offset ) ? (float) $gmt_offset : 0.0, | |
| 52 | + 'timezoneString' => is_string( $timezone_string ) ? $timezone_string : '', | |
| 53 | + 'locale' => str_replace( '_', '-', (string) get_locale() ), | |
| 54 | + ), | |
| 55 | + 'assets' => array( | |
| 56 | + 'buildUrl' => plugins_url( '../build/', __FILE__ ), | |
| 57 | + ), | |
| 58 | + ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Render the initial state into a JavaScript variable. | |
| 63 | + * | |
| 64 | + * @return string | |
| 65 | + */ | |
| 66 | + public function render() { | |
| 67 | + return 'var JPSCAN_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; | |
| 68 | + } | |
| 69 | +} | |