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