Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initial_State
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 get_data
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
 render
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * The Activity Log React initial state.
4 *
5 * @package automattic/jetpack-activity-log
6 */
7
8namespace Automattic\Jetpack\Activity_Log;
9
10use Automattic\Jetpack\Status;
11use Jetpack_Options;
12use function admin_url;
13use function esc_url_raw;
14use function get_bloginfo;
15use function get_locale;
16use function get_option;
17use function get_site_url;
18use function plugins_url;
19use function rest_url;
20use function wp_create_nonce;
21use function wp_json_encode;
22use function wp_parse_url;
23
24/**
25 * The Activity Log React initial state.
26 */
27class 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                // The paid-plan capability check. Drives the free-tier
55                // upsell callout and matches the server-side clamp in
56                // REST_Controller::get_activity_log(). The result is
57                // cached in a site transient by the REST controller
58                // (5-minute TTL); on a cache miss this call issues a
59                // synchronous WPCOM request (~200–800ms typical, up to
60                // the 2s internal timeout) that blocks page rendering
61                // until it returns.
62                'hasActivityLogsAccess' => REST_Controller::has_activity_logs_access(),
63            ),
64            'nonces'        => array(
65                // Consumed by `UpsellCallout` to build a `redirect_to`
66                // URL that invalidates the access cache on return from
67                // checkout. See `Jetpack_Activity_Log::admin_init()`.
68                'refreshAccess' => wp_create_nonce( Jetpack_Activity_Log::REFRESH_ACCESS_NONCE_ACTION ),
69            ),
70            'assets'        => array(
71                'buildUrl' => plugins_url( '../build/', __FILE__ ),
72            ),
73        );
74    }
75
76    /**
77     * Render the initial state into a JavaScript variable.
78     *
79     * @return string
80     */
81    public function render() {
82        return 'var JPACTIVITYLOG_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
83    }
84}