Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.43% |
85 / 94 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| UI | |
90.43% |
85 / 94 |
|
55.56% |
5 / 9 |
47.86 | |
0.00% |
0 / 1 |
| init | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| use_new_screen | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| enqueue_scripts | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
10 | |||
| render_container | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_initial_state | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_initial_state_data | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 | |||
| get_container_id | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_consumer_data | |
88.46% |
23 / 26 |
|
0.00% |
0 / 1 |
16.39 | |||
| get_platform | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
4.84 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Identity_Crisis UI class of the Connection package. |
| 4 | * |
| 5 | * @package automattic/jetpack-connection |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\IdentityCrisis; |
| 9 | |
| 10 | use Automattic\Jetpack\Assets; |
| 11 | use Automattic\Jetpack\Identity_Crisis; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | use Automattic\Jetpack\Terms_Of_Service; |
| 15 | use Automattic\Jetpack\Tracking; |
| 16 | use Jetpack_Options; |
| 17 | use Jetpack_Tracks_Client; |
| 18 | |
| 19 | /** |
| 20 | * The Identity Crisis UI handling. |
| 21 | */ |
| 22 | class UI { |
| 23 | |
| 24 | /** |
| 25 | * Temporary storage for consumer data. |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | private static $consumers; |
| 30 | |
| 31 | /** |
| 32 | * Initialization. |
| 33 | */ |
| 34 | public static function init() { |
| 35 | if ( did_action( 'jetpack_identity_crisis_ui_init' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Action called after initializing Identity Crisis UI. |
| 41 | * |
| 42 | * @since 0.6.0 |
| 43 | */ |
| 44 | do_action( 'jetpack_identity_crisis_ui_init' ); |
| 45 | |
| 46 | $idc_data = Identity_Crisis::check_identity_crisis(); |
| 47 | |
| 48 | if ( false === $idc_data ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | add_action( 'admin_enqueue_scripts', array( static::class, 'enqueue_scripts' ) ); |
| 53 | |
| 54 | // The new screen loads Tracks from enqueue_scripts(), so only in wp-admin. |
| 55 | if ( ! static::use_new_screen() ) { |
| 56 | Tracking::register_tracks_functions_scripts( true ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Whether the `connection-idc-screen` flag is on. |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | private static function use_new_screen() { |
| 66 | // Guarded for the same reason as in Initial_State::get_identity_crisis_data(). |
| 67 | return method_exists( Identity_Crisis::class, 'is_new_screen_enabled' ) && Identity_Crisis::is_new_screen_enabled(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Enqueue scripts! |
| 72 | */ |
| 73 | public static function enqueue_scripts() { |
| 74 | if ( is_admin() ) { |
| 75 | /** |
| 76 | * Whether to show the Safe Mode notice when the `connection-idc-screen` flag is on. |
| 77 | * |
| 78 | * A page that shows the Safe Mode screen itself returns false, so the screen doesn't show twice. |
| 79 | * |
| 80 | * @since $$next-version$$ |
| 81 | * |
| 82 | * @param bool $show Whether to show the notice. Default true. |
| 83 | */ |
| 84 | if ( static::use_new_screen() && ! apply_filters( 'jetpack_identity_crisis_show_notice', true ) ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if ( static::use_new_screen() && ( new Tracking() )->should_enable_tracking( new Terms_Of_Service(), new Status() ) ) { |
| 89 | Tracking::register_tracks_functions_scripts( true ); |
| 90 | } |
| 91 | |
| 92 | // Only the notice's own container shows the new screen, and a confirmed Safe Mode shows no screen. |
| 93 | $shows_new_screen = static::use_new_screen() && ! Identity_Crisis::$is_safe_mode_confirmed && ! static::get_container_id(); |
| 94 | |
| 95 | Assets::register_script( |
| 96 | 'jp_identity_crisis_banner', |
| 97 | '../../dist/identity-crisis.js', |
| 98 | __FILE__, |
| 99 | array( |
| 100 | 'in_footer' => true, |
| 101 | 'textdomain' => 'jetpack-connection', |
| 102 | // The new screen comes from the shared connection script, which the old screen doesn't need. |
| 103 | 'dependencies' => $shows_new_screen ? array( 'jetpack-connection' ) : array(), |
| 104 | ) |
| 105 | ); |
| 106 | Assets::enqueue_script( 'jp_identity_crisis_banner' ); |
| 107 | if ( $shows_new_screen ) { |
| 108 | // Also loads the screen's stylesheet, which a script dependency doesn't. |
| 109 | Assets::enqueue_script( 'jetpack-connection' ); |
| 110 | } |
| 111 | wp_add_inline_script( 'jp_identity_crisis_banner', static::get_initial_state(), 'before' ); |
| 112 | |
| 113 | add_action( 'admin_notices', array( static::class, 'render_container' ) ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Create the container element for the IDC banner. |
| 119 | */ |
| 120 | public static function render_container() { |
| 121 | ?> |
| 122 | <div id="jp-identity-crisis-container" class="notice"></div> |
| 123 | <?php |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Return the rendered initial state JavaScript code. |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | private static function get_initial_state() { |
| 132 | return 'var JP_IDENTITY_CRISIS__INITIAL_STATE=' . wp_json_encode( static::get_initial_state_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the initial state data. |
| 137 | * |
| 138 | * @return array |
| 139 | */ |
| 140 | private static function get_initial_state_data() { |
| 141 | $idc_urls = Identity_Crisis::get_mismatched_urls(); |
| 142 | $current_screen = get_current_screen(); |
| 143 | $is_admin = current_user_can( 'jetpack_disconnect' ); |
| 144 | $possible_dynamic_site_url_detected = (bool) Identity_Crisis::detect_possible_dynamic_site_url(); |
| 145 | $is_development_site = (bool) Status::is_development_site(); |
| 146 | |
| 147 | return array( |
| 148 | 'WP_API_root' => esc_url_raw( rest_url() ), |
| 149 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
| 150 | 'wpcomHomeUrl' => ( is_array( $idc_urls ) && array_key_exists( 'wpcom_url', $idc_urls ) ) ? $idc_urls['wpcom_url'] : null, |
| 151 | 'currentUrl' => ( is_array( $idc_urls ) && array_key_exists( 'current_url', $idc_urls ) ) ? $idc_urls['current_url'] : null, |
| 152 | 'redirectUri' => isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/wp-admin/', '/', filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '', |
| 153 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
| 154 | 'tracksEventData' => array( |
| 155 | 'isAdmin' => $is_admin, |
| 156 | 'currentScreen' => $current_screen ? $current_screen->id : false, |
| 157 | 'blogID' => Jetpack_Options::get_option( 'id' ), |
| 158 | 'platform' => static::get_platform(), |
| 159 | ), |
| 160 | 'isSafeModeConfirmed' => Identity_Crisis::$is_safe_mode_confirmed, |
| 161 | 'consumerData' => static::get_consumer_data(), |
| 162 | 'isAdmin' => $is_admin, |
| 163 | 'possibleDynamicSiteUrlDetected' => $possible_dynamic_site_url_detected, |
| 164 | 'isDevelopmentSite' => $is_development_site, |
| 165 | 'containerID' => static::get_container_id(), |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the ID of a custom container that the screen renders into instead of the notice. |
| 171 | * |
| 172 | * @return string|null Null for the notice's own container. |
| 173 | */ |
| 174 | private static function get_container_id() { |
| 175 | /** |
| 176 | * Use the filter to provide custom HTML container element ID. |
| 177 | * |
| 178 | * The element may be added after the page loads, within 60 seconds of it; |
| 179 | * the screen is rendered into it once. |
| 180 | * |
| 181 | * @since 0.10.0 |
| 182 | * |
| 183 | * @param string|null $containerID The container ID. |
| 184 | */ |
| 185 | return apply_filters( 'identity_crisis_container_id', null ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get the package consumer data. |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | public static function get_consumer_data() { |
| 194 | if ( null !== static::$consumers ) { |
| 195 | return static::$consumers; |
| 196 | } |
| 197 | |
| 198 | $consumers = apply_filters( 'jetpack_idc_consumers', array() ); |
| 199 | |
| 200 | if ( ! $consumers ) { |
| 201 | return array(); |
| 202 | } |
| 203 | |
| 204 | usort( |
| 205 | $consumers, |
| 206 | function ( $c1, $c2 ) { |
| 207 | $priority1 = ( array_key_exists( 'priority', $c1 ) && (int) $c1['priority'] ) ? (int) $c1['priority'] : 10; |
| 208 | $priority2 = ( array_key_exists( 'priority', $c2 ) && (int) $c2['priority'] ) ? (int) $c2['priority'] : 10; |
| 209 | |
| 210 | return $priority1 <=> $priority2; |
| 211 | } |
| 212 | ); |
| 213 | |
| 214 | $consumer_chosen = null; |
| 215 | $consumer_url_length = 0; |
| 216 | foreach ( $consumers as &$consumer ) { |
| 217 | if ( empty( $consumer['admin_page'] ) || ! is_string( $consumer['admin_page'] ) ) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if ( isset( $consumer['customContent'] ) && is_callable( $consumer['customContent'] ) ) { |
| 222 | $consumer['customContent'] = call_user_func( $consumer['customContent'] ); |
| 223 | } |
| 224 | |
| 225 | if ( isset( $_SERVER['REQUEST_URI'] ) && str_starts_with( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $consumer['admin_page'] ) && strlen( $consumer['admin_page'] ) > $consumer_url_length ) { |
| 226 | $consumer_chosen = $consumer; |
| 227 | $consumer_url_length = strlen( $consumer['admin_page'] ); |
| 228 | } |
| 229 | } |
| 230 | unset( $consumer ); |
| 231 | |
| 232 | static::$consumers = $consumer_chosen ? $consumer_chosen : array_shift( $consumers ); |
| 233 | |
| 234 | return static::$consumers; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the site platform. |
| 239 | * |
| 240 | * @since 9.15.0 Public, so the Connectors card can send the same value to Tracks. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public static function get_platform() { |
| 245 | $host = new Host(); |
| 246 | |
| 247 | if ( $host->is_woa_site() ) { |
| 248 | return 'woa'; |
| 249 | } |
| 250 | |
| 251 | if ( $host->is_vip_site() ) { |
| 252 | return 'vip'; |
| 253 | } |
| 254 | |
| 255 | if ( $host->is_newspack_site() ) { |
| 256 | return 'newspack'; |
| 257 | } |
| 258 | |
| 259 | return 'self-hosted'; |
| 260 | } |
| 261 | } |