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