PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-connection/src/class-initial-state.php +74 -21 12.1.3 → 16.3-a.1 View file →
@@ -14,15 +14,8 @@
14 14 */
15 15 class Initial_State {
16 16
17 17 /**
18 - * Whether the initial state was already rendered
19 - *
20 - * @var boolean
21 - */
22 - private static $rendered = false;
23 -
24 - /**
25 18 * Get the initial state data.
26 19 *
27 20 * @return array
28 21 */
@@ -28,31 +21,91 @@
28 21 */
29 22 private static function get_data() {
30 23 global $wp_version;
31 24
25 + $status = new Status();
26 +
27 + // Only expose the owner's identity to users who can act on connection issues:
28 + // this state is printed for any logged-in user loading connection scripts
29 + // (e.g. contributors in the editor). The owner is derived from the master_user
30 + // option rather than the token-dependent Manager::get_connection_owner(): that
31 + // method returns false exactly when the owner's token is broken, which is the
32 + // scenario connection-error UIs need this data for (and it re-reports
33 + // invalid_connection_owner as a side effect). Unlike
34 + // userConnectionData.connectionOwner (the *connected* owner), this field
35 + // identifies who holds ownership regardless of token health.
36 + $connection_owner = null;
37 + if ( current_user_can( 'jetpack_connect' ) ) {
38 + $owner_id = (int) \Jetpack_Options::get_option( 'master_user' );
39 + $owner = $owner_id > 0 ? get_userdata( $owner_id ) : false;
40 +
41 + if ( $owner instanceof \WP_User ) {
42 + $connection_owner = array(
43 + 'id' => $owner_id,
44 + 'displayName' => $owner->display_name,
45 + );
46 + }
47 + }
48 +
32 49 return array(
33 - 'apiRoot' => esc_url_raw( rest_url() ),
34 - 'apiNonce' => wp_create_nonce( 'wp_rest' ),
35 - 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
36 - 'connectionStatus' => REST_Connector::connection_status( false ),
37 - 'userConnectionData' => REST_Connector::get_user_connection_data( false ),
38 - 'connectedPlugins' => REST_Connector::get_connection_plugins( false ),
39 - 'wpVersion' => $wp_version,
40 - 'siteSuffix' => ( new Status() )->get_site_suffix(),
41 - 'connectionErrors' => Error_Handler::get_instance()->get_verified_errors(),
50 + 'apiRoot' => esc_url_raw( rest_url() ),
51 + 'apiNonce' => wp_create_nonce( 'wp_rest' ),
52 + 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
53 + 'connectionStatus' => REST_Connector::connection_status( false ),
54 + 'userConnectionData' => REST_Connector::get_user_connection_data( false ),
55 + 'connectedPlugins' => REST_Connector::get_connection_plugins( false ),
56 + 'wpVersion' => $wp_version,
57 + 'siteSuffix' => $status->get_site_suffix(),
58 + 'connectionErrors' => Error_Handler::get_instance()->get_displayable_errors(),
59 + 'isOfflineMode' => $status->is_offline_mode(),
60 + 'calypsoEnv' => ( new Status\Host() )->get_calypso_env(),
61 + 'isOwnershipTransferable' => ( new Manager() )->is_ownership_transferable(),
62 + 'connectionOwner' => $connection_owner,
42 63 );
43 64 }
44 65
45 66 /**
67 + * Set the connection script data.
68 + *
69 + * @param array $data The script data.
70 + */
71 + public static function set_connection_script_data( $data ) {
72 +
73 + $data['connection'] = self::get_data();
74 +
75 + if ( empty( $data['site']['wpcom']['blog_id'] ) ) {
76 + $data['site']['wpcom']['blog_id'] = absint( \Jetpack_Options::get_option( 'id', 0 ) );
77 + }
78 +
79 + return $data;
80 + }
81 +
82 + /**
46 83 * Render the initial state into a JavaScript variable.
47 84 *
48 85 * @return string
49 86 */
50 87 public static function render() {
51 - if ( self::$rendered ) {
52 - return null;
53 - }
54 - self::$rendered = true;
55 - return 'var JP_CONNECTION_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( self::get_data() ) ) . '"));';
88 + /*
89 + * `window.jpTracksContext` is an intentionally minimal, Tracks-specific global used by the
90 + * @automattic/jetpack-analytics package to read `blog_id` at event-fire time. It exists
91 + * separately from `window.JetpackScriptData` because the analytics package is consumed in
92 + * contexts (e.g. Boost frontend) where `JetpackScriptData` is not reliably available, and
93 + * coupling the analytics package to that schema would widen its surface unnecessarily.
94 + * When both are present, `JetpackScriptData.site.wpcom.blog_id` is populated via
95 + * `set_connection_script_data()` above for other consumers.
96 + */
97 + return 'var JP_CONNECTION_INITIAL_STATE; typeof JP_CONNECTION_INITIAL_STATE === "object" || (JP_CONNECTION_INITIAL_STATE = ' . wp_json_encode( self::get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');'
98 + . sprintf( 'window.jpTracksContext = window.jpTracksContext || {}; window.jpTracksContext.blog_id = %s;', absint( \Jetpack_Options::get_option( 'id', 0 ) ) );
56 99 }
57 100
101 + /**
102 + * Render the initial state using an inline script.
103 + *
104 + * @param string $handle The JS script handle.
105 + *
106 + * @return void
107 + */
108 + public static function render_script( $handle ) {
109 + wp_add_inline_script( $handle, static::render(), 'before' );
110 + }
58 111 }