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