Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.78% covered (success)
97.78%
44 / 45
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initial_State
97.78% covered (success)
97.78%
44 / 45
75.00% covered (warning)
75.00%
3 / 4
12
0.00% covered (danger)
0.00%
0 / 1
 get_data
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
8
 get_wpcom_upgrade_url
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 get_wpcom_tracks_user_data
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\Status;
12use function add_query_arg;
13use function admin_url;
14use function esc_url_raw;
15use function get_bloginfo;
16use function get_locale;
17use function get_option;
18use function get_site_url;
19use function rawurlencode;
20use function wp_create_nonce;
21use function wp_get_current_user;
22use function wp_json_encode;
23use function wp_parse_url;
24
25/**
26 * The Activity Log React initial state.
27 */
28class Initial_State {
29    /**
30     * Get the initial state data.
31     *
32     * @return array
33     */
34    private function get_data() {
35        $gmt_offset      = get_option( 'gmt_offset' );
36        $timezone_string = get_option( 'timezone_string' );
37        $home_host       = wp_parse_url( get_site_url(), PHP_URL_HOST );
38        $site_suffix     = ( new Status() )->get_site_suffix();
39        $is_wpcom_simple = Jetpack_Activity_Log::is_wpcom_simple();
40
41        return array(
42            'jetpackStatus' => array(
43                'calypsoSlug' => $site_suffix,
44            ),
45            'config'        => array(
46                // Simple sites have no Jetpack REST API to proxy through, so the app calls WordPress.com directly.
47                'apiSource'       => $is_wpcom_simple ? 'wpcom' : 'jetpack',
48                'wpcomUpgradeUrl' => $is_wpcom_simple ? self::get_wpcom_upgrade_url( $site_suffix ) : '',
49                'tracksUserData'  => $is_wpcom_simple ? self::get_wpcom_tracks_user_data() : null,
50            ),
51            'siteData'      => array(
52                'id'                    => Connection_Manager::get_site_id( true ),
53                'title'                 => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(),
54                'adminUrl'              => esc_url_raw( admin_url() ),
55                'slug'                  => is_string( $home_host ) ? $home_host : '',
56                'gmtOffset'             => is_numeric( $gmt_offset ) ? (float) $gmt_offset : 0.0,
57                'timezoneString'        => is_string( $timezone_string ) ? $timezone_string : '',
58                'locale'                => str_replace( '_', '-', (string) get_locale() ),
59                // The paid-plan capability check. Drives the free-tier
60                // upsell callout and matches the server-side clamp in
61                // REST_Controller::get_activity_log(). The result is
62                // cached in a site transient by the REST controller
63                // (5-minute TTL); on a cache miss this call issues a
64                // synchronous WPCOM request (~200–800ms typical, up to
65                // the 2s internal timeout) that blocks page rendering
66                // until it returns.
67                'hasActivityLogsAccess' => REST_Controller::has_activity_logs_access(),
68            ),
69            'nonces'        => array(
70                // Consumed by `UpsellCallout` to build a `redirect_to`
71                // URL that invalidates the access cache on return from
72                // checkout. See `Jetpack_Activity_Log::admin_init()`.
73                'refreshAccess' => wp_create_nonce( Jetpack_Activity_Log::REFRESH_ACCESS_NONCE_ACTION ),
74            ),
75        );
76    }
77
78    /**
79     * WordPress.com plan selection, returning here after checkout or cancel.
80     *
81     * @param string $site_suffix The site's Calypso slug.
82     * @return string
83     */
84    private static function get_wpcom_upgrade_url( $site_suffix ) {
85        $page_url = rawurlencode( admin_url( 'admin.php?page=' . Jetpack_Activity_Log::PAGE_SLUG ) );
86
87        return add_query_arg(
88            array(
89                'siteSlug'    => rawurlencode( $site_suffix ),
90                'redirect_to' => $page_url,
91                'cancel_to'   => $page_url,
92            ),
93            'https://wordpress.com/setup/plan-upgrade/'
94        );
95    }
96
97    /**
98     * The Tracks identity of the current user, who on Simple is the WordPress.com user.
99     *
100     * @return array{userid: int, username: string}|null
101     */
102    private static function get_wpcom_tracks_user_data() {
103        $user = wp_get_current_user();
104        if ( ! $user->exists() ) {
105            return null;
106        }
107
108        return array(
109            'userid'   => $user->ID,
110            'username' => $user->user_login,
111        );
112    }
113
114    /**
115     * Render the initial state into a JavaScript variable.
116     *
117     * @return string
118     */
119    public function render() {
120        return 'var JPACTIVITYLOG_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
121    }
122}