← All changes
|
jetpack_vendor/automattic/jetpack-activity-log/src/class-initial-state.php
+75
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,75 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * The Activity Log React initial state. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-activity-log | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Activity_Log; | |
| 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 wp_create_nonce; | |
| 19 | +use function wp_json_encode; | |
| 20 | +use function wp_parse_url; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * The Activity Log React initial state. | |
| 24 | + */ | |
| 25 | +class Initial_State { | |
| 26 | + /** | |
| 27 | + * Get the initial state data. | |
| 28 | + * | |
| 29 | + * @return array | |
| 30 | + */ | |
| 31 | + private function get_data() { | |
| 32 | + $gmt_offset = get_option( 'gmt_offset' ); | |
| 33 | + $timezone_string = get_option( 'timezone_string' ); | |
| 34 | + $home_host = wp_parse_url( get_site_url(), PHP_URL_HOST ); | |
| 35 | + | |
| 36 | + return array( | |
| 37 | + 'jetpackStatus' => array( | |
| 38 | + 'calypsoSlug' => ( new Status() )->get_site_suffix(), | |
| 39 | + ), | |
| 40 | + 'siteData' => array( | |
| 41 | + 'id' => Jetpack_Options::get_option( 'id' ), | |
| 42 | + 'title' => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(), | |
| 43 | + 'adminUrl' => esc_url_raw( admin_url() ), | |
| 44 | + 'slug' => is_string( $home_host ) ? $home_host : '', | |
| 45 | + 'gmtOffset' => is_numeric( $gmt_offset ) ? (float) $gmt_offset : 0.0, | |
| 46 | + 'timezoneString' => is_string( $timezone_string ) ? $timezone_string : '', | |
| 47 | + 'locale' => str_replace( '_', '-', (string) get_locale() ), | |
| 48 | + // The paid-plan capability check. Drives the free-tier | |
| 49 | + // upsell callout and matches the server-side clamp in | |
| 50 | + // REST_Controller::get_activity_log(). The result is | |
| 51 | + // cached in a site transient by the REST controller | |
| 52 | + // (5-minute TTL); on a cache miss this call issues a | |
| 53 | + // synchronous WPCOM request (~200–800ms typical, up to | |
| 54 | + // the 2s internal timeout) that blocks page rendering | |
| 55 | + // until it returns. | |
| 56 | + 'hasActivityLogsAccess' => REST_Controller::has_activity_logs_access(), | |
| 57 | + ), | |
| 58 | + 'nonces' => array( | |
| 59 | + // Consumed by `UpsellCallout` to build a `redirect_to` | |
| 60 | + // URL that invalidates the access cache on return from | |
| 61 | + // checkout. See `Jetpack_Activity_Log::admin_init()`. | |
| 62 | + 'refreshAccess' => wp_create_nonce( Jetpack_Activity_Log::REFRESH_ACCESS_NONCE_ACTION ), | |
| 63 | + ), | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Render the initial state into a JavaScript variable. | |
| 69 | + * | |
| 70 | + * @return string | |
| 71 | + */ | |
| 72 | + public function render() { | |
| 73 | + return 'var JPACTIVITYLOG_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; | |
| 74 | + } | |
| 75 | +} | |