Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.13% |
142 / 189 |
|
71.43% |
10 / 14 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Connector | |
75.13% |
142 / 189 |
|
71.43% |
10 / 14 |
124.04 | |
0.00% |
0 / 1 |
| init | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| register_connector | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_script_module | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
20 | |||
| get_connector_data | |
93.33% |
28 / 30 |
|
0.00% |
0 / 1 |
6.01 | |||
| get_current_user_data | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| get_connection_owner_data | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| resolve_user_fields | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
10 | |||
| is_connectors_screen | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_connectors_page_path | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| store_auth_error | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| consume_auth_error | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| get_connected_plugins_data | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| get_connector_logo_url | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
9 | |||
| get_plugin_logo_url | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
7.05 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack connector card for the WP core Connectors screen. |
| 4 | * |
| 5 | * Registers a connector in the WP 7.0+ Connectors registry and enqueues |
| 6 | * a script module that provides a custom render function with connection |
| 7 | * details (owner, connected plugins, disconnect). |
| 8 | * |
| 9 | * @package automattic/jetpack-connection |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\Jetpack\Connection; |
| 13 | |
| 14 | use Automattic\Jetpack\Modules; |
| 15 | use Automattic\Jetpack\Status\Host; |
| 16 | |
| 17 | /** |
| 18 | * Jetpack connector card handler. |
| 19 | * |
| 20 | * @since 8.2.0 |
| 21 | */ |
| 22 | class Jetpack_Connector { |
| 23 | |
| 24 | /** |
| 25 | * Whether the connector has been initialized. |
| 26 | * |
| 27 | * @var bool |
| 28 | */ |
| 29 | private static $initialized = false; |
| 30 | |
| 31 | /** |
| 32 | * Script module identifier. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | const MODULE_ID = '@automattic/jetpack-connection-connectors'; |
| 37 | |
| 38 | /** |
| 39 | * Screen ID assigned by WordPress to the Gutenberg plugin's connectors submenu page. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | const GUTENBERG_CONNECTORS_SCREEN_ID = 'settings_page_options-connectors-wp-admin'; |
| 44 | |
| 45 | /** |
| 46 | * Page slug registered by the Gutenberg plugin for the connectors submenu page. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | const GUTENBERG_CONNECTORS_PAGE_SLUG = 'options-connectors-wp-admin'; |
| 51 | |
| 52 | /** |
| 53 | * Initialize the connector. |
| 54 | */ |
| 55 | public static function init() { |
| 56 | if ( static::$initialized ) { |
| 57 | return; |
| 58 | } |
| 59 | static::$initialized = true; |
| 60 | |
| 61 | add_action( 'wp_connectors_init', array( static::class, 'register_connector' ), 20 ); |
| 62 | add_action( 'admin_enqueue_scripts', array( static::class, 'enqueue_script_module' ) ); |
| 63 | add_action( 'jetpack_client_authorize_error', array( static::class, 'store_auth_error' ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Register Jetpack as a connector in the WP core Connectors screen. |
| 68 | * |
| 69 | * The wp_connectors_init action is available in WordPress 7.0+. |
| 70 | * On older versions this action never fires, so the hook is safely a no-op. |
| 71 | * |
| 72 | * @since 8.2.0 |
| 73 | * |
| 74 | * @param \WP_Connector_Registry $registry Connector registry instance. |
| 75 | */ |
| 76 | public static function register_connector( $registry ) { |
| 77 | // @phan-suppress-previous-line PhanUndeclaredTypeParameter -- WP 7.0+ class. |
| 78 | $registry->register( // @phan-suppress-current-line PhanUndeclaredClassMethod -- WP 7.0+ class. |
| 79 | 'wordpress_com', |
| 80 | array( |
| 81 | 'name' => 'Jetpack Connection', |
| 82 | 'description' => __( 'Enhanced functionality for Jetpack and WooCommerce with WordPress.com.', 'jetpack-connection' ), |
| 83 | 'type' => 'cloud_service', |
| 84 | 'logo_url' => static::get_connector_logo_url(), |
| 85 | 'authentication' => array( |
| 86 | 'method' => 'none', |
| 87 | ), |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Enqueue the connectors card script module on the Settings > Connectors page. |
| 94 | * |
| 95 | * @since 8.2.0 |
| 96 | */ |
| 97 | public static function enqueue_script_module() { |
| 98 | $screen = get_current_screen(); |
| 99 | |
| 100 | if ( ! $screen || ! static::is_connectors_screen( $screen ) ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if ( ! class_exists( 'WP_Connector_Registry' ) ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | $css_path = __DIR__ . '/css/connectors-card.css'; |
| 109 | wp_enqueue_style( |
| 110 | 'jetpack-connector-card', |
| 111 | plugins_url( 'css/connectors-card.css', __FILE__ ), |
| 112 | array(), |
| 113 | (string) @filemtime( $css_path ) // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- fallback to empty string if file is missing. |
| 114 | ); |
| 115 | |
| 116 | wp_register_script_module( |
| 117 | static::MODULE_ID, |
| 118 | plugins_url( 'js/connectors-card.js', __FILE__ ), |
| 119 | array( |
| 120 | array( |
| 121 | 'id' => '@wordpress/connectors', |
| 122 | 'import' => 'static', |
| 123 | ), |
| 124 | ) |
| 125 | ); |
| 126 | wp_enqueue_script_module( static::MODULE_ID ); |
| 127 | |
| 128 | add_filter( |
| 129 | 'script_module_data_' . static::MODULE_ID, |
| 130 | array( static::class, 'get_connector_data' ) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Build the data passed to the script module via the script_module_data_ filter. |
| 136 | * |
| 137 | * @since 8.2.0 |
| 138 | * |
| 139 | * @param array $data Existing script module data. |
| 140 | * @return array Filtered script module data. |
| 141 | */ |
| 142 | public static function get_connector_data( $data ) { |
| 143 | $manager = new Manager(); |
| 144 | $is_registered = $manager->is_connected(); |
| 145 | $is_connected = $is_registered && $manager->has_connected_owner(); |
| 146 | |
| 147 | $data['isConnected'] = $is_connected; |
| 148 | $data['isRegistered'] = $is_registered; |
| 149 | $data['apiRoot'] = esc_url_raw( rest_url() ); |
| 150 | $data['apiNonce'] = wp_create_nonce( 'wp_rest' ); |
| 151 | $data['redirectUri'] = static::get_connectors_page_path(); |
| 152 | $data['connectorName'] = 'Jetpack Connection'; |
| 153 | $data['connectorDescription'] = __( 'Enhanced functionality for Jetpack and WooCommerce with WordPress.com.', 'jetpack-connection' ); |
| 154 | $data['connectorLogoUrl'] = static::get_connector_logo_url(); |
| 155 | |
| 156 | $data['connectedPlugins'] = static::get_connected_plugins_data( $manager ); |
| 157 | |
| 158 | if ( $is_registered ) { |
| 159 | $data['siteDetails'] = array( |
| 160 | 'blogId' => (int) \Jetpack_Options::get_option( 'id' ), |
| 161 | 'siteUrl' => site_url(), |
| 162 | 'homeUrl' => home_url(), |
| 163 | ); |
| 164 | |
| 165 | if ( in_array( 'jetpack', array_column( $data['connectedPlugins'], 'slug' ), true ) ) { |
| 166 | $data['ssoStatus'] = ( new Modules() )->is_active( 'sso', false ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if ( $is_connected ) { |
| 171 | $data['currentUser'] = static::get_current_user_data( $manager ); |
| 172 | $data['connectionOwner'] = static::get_connection_owner_data( $manager ); |
| 173 | } |
| 174 | |
| 175 | $host = new Host(); |
| 176 | $data['isWoaSite'] = $host->is_woa_site(); |
| 177 | $data['isVipSite'] = $host->is_vip_site(); |
| 178 | |
| 179 | $auth_error = static::consume_auth_error(); |
| 180 | if ( $auth_error ) { |
| 181 | $data['authError'] = $auth_error; |
| 182 | } |
| 183 | |
| 184 | return $data; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the current (logged-in) user's connection details. |
| 189 | * |
| 190 | * @param Manager $manager Connection manager instance. |
| 191 | * @return array|null Current user data or null if not connected. |
| 192 | */ |
| 193 | private static function get_current_user_data( $manager ) { |
| 194 | $user_id = get_current_user_id(); |
| 195 | |
| 196 | if ( ! $user_id || ! $manager->is_user_connected( $user_id ) ) { |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | $user = get_userdata( $user_id ); |
| 201 | $user_info = static::resolve_user_fields( $user, $manager->get_connected_user_data( $user_id ) ); |
| 202 | $is_owner = $manager->is_connection_owner( $user_id ); |
| 203 | |
| 204 | $has_other_connected_users = false; |
| 205 | if ( $is_owner ) { |
| 206 | $connected_users = $manager->get_connected_users( 'any', 2 ); |
| 207 | $has_other_connected_users = count( $connected_users ) > 1; |
| 208 | } |
| 209 | |
| 210 | return array_merge( |
| 211 | $user_info, |
| 212 | array( |
| 213 | 'isOwner' => $is_owner, |
| 214 | 'hasOtherConnectedUsers' => $has_other_connected_users, |
| 215 | ) |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get the connection owner details for the script module. |
| 221 | * |
| 222 | * @param Manager $manager Connection manager instance. |
| 223 | * @return array|null Owner data or null if unavailable. |
| 224 | */ |
| 225 | private static function get_connection_owner_data( $manager ) { |
| 226 | $owner = $manager->get_connection_owner(); |
| 227 | |
| 228 | if ( false === $owner ) { |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | $fields = static::resolve_user_fields( $owner, $manager->get_connected_user_data( $owner->ID ) ); |
| 233 | |
| 234 | $fields['localLogin'] = $owner->user_login; |
| 235 | |
| 236 | return $fields; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Merge local WP user fields with WordPress.com user data. |
| 241 | * |
| 242 | * WPCOM values take precedence when available. Returns the common |
| 243 | * user shape used by both currentUser and connectionOwner. |
| 244 | * |
| 245 | * @param \WP_User|false $wp_user Local WordPress user object (false if unavailable). |
| 246 | * @param array|false $wpcom_user_data WPCOM user data from the connection manager. |
| 247 | * @return array User data with displayName, login, email, and avatar. |
| 248 | */ |
| 249 | private static function resolve_user_fields( $wp_user, $wpcom_user_data ) { |
| 250 | $display_name = $wp_user ? $wp_user->display_name : ''; |
| 251 | $login = $wp_user ? $wp_user->user_login : ''; |
| 252 | $email = $wp_user ? $wp_user->user_email : ''; |
| 253 | |
| 254 | if ( is_array( $wpcom_user_data ) ) { |
| 255 | if ( ! empty( $wpcom_user_data['display_name'] ) ) { |
| 256 | $display_name = $wpcom_user_data['display_name']; |
| 257 | } |
| 258 | if ( ! empty( $wpcom_user_data['login'] ) ) { |
| 259 | $login = $wpcom_user_data['login']; |
| 260 | } |
| 261 | if ( ! empty( $wpcom_user_data['email'] ) ) { |
| 262 | $email = $wpcom_user_data['email']; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | $user_id = $wp_user ? $wp_user->ID : 0; |
| 267 | |
| 268 | return array( |
| 269 | 'displayName' => $display_name, |
| 270 | 'login' => $login, |
| 271 | 'email' => $email, |
| 272 | 'avatar' => $user_id |
| 273 | ? get_avatar_url( |
| 274 | $user_id, |
| 275 | array( |
| 276 | 'size' => 48, |
| 277 | 'default' => 'mysteryman', |
| 278 | ) |
| 279 | ) |
| 280 | : '', |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Check whether the given screen is the Connectors settings page. |
| 286 | * |
| 287 | * Handles both WP 7.0 core (`options-connectors`) and the Gutenberg |
| 288 | * plugin (`settings_page_options-connectors-wp-admin`). |
| 289 | * |
| 290 | * @param \WP_Screen $screen Current admin screen. |
| 291 | * @return bool |
| 292 | */ |
| 293 | private static function is_connectors_screen( $screen ) { |
| 294 | return 'options-connectors' === $screen->id |
| 295 | || static::GUTENBERG_CONNECTORS_SCREEN_ID === $screen->id; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Return the admin-relative path for the Connectors page. |
| 300 | * |
| 301 | * WP 7.0 core uses the standalone `options-connectors.php` file while |
| 302 | * the Gutenberg plugin registers a submenu page under options-general.php |
| 303 | * with slug `options-connectors-wp-admin`. Both set parent_file to |
| 304 | * `options-general.php` for menu highlighting, so we distinguish them by |
| 305 | * checking the actual script filename being served. |
| 306 | * |
| 307 | * Note: for the Gutenberg case we use the registered page slug directly, |
| 308 | * not `$screen->id`. WordPress auto-prefixes screen IDs for submenu pages |
| 309 | * (e.g. `settings_page_options-connectors-wp-admin`), so using `$screen->id` |
| 310 | * as the `page=` parameter produces an invalid URL. |
| 311 | * |
| 312 | * The result is suitable for the `redirect_uri` parameter accepted by the |
| 313 | * `jetpack/v4/connection/register` REST endpoint (which wraps it in `admin_url()`). |
| 314 | * |
| 315 | * @return string Admin-relative path, e.g. 'options-connectors.php'. |
| 316 | */ |
| 317 | private static function get_connectors_page_path() { |
| 318 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only compared against a hardcoded string. |
| 319 | $script = isset( $_SERVER['SCRIPT_NAME'] ) ? wp_basename( wp_unslash( $_SERVER['SCRIPT_NAME'] ) ) : ''; |
| 320 | |
| 321 | if ( 'options-connectors.php' === $script ) { |
| 322 | return 'options-connectors.php'; |
| 323 | } |
| 324 | |
| 325 | // Gutenberg plugin registers the page under options-general.php. |
| 326 | $screen = get_current_screen(); |
| 327 | if ( $screen && static::GUTENBERG_CONNECTORS_SCREEN_ID === $screen->id ) { |
| 328 | return 'options-general.php?page=' . static::GUTENBERG_CONNECTORS_PAGE_SLUG; |
| 329 | } |
| 330 | |
| 331 | return 'options-connectors.php'; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Store an authorization error in a short-lived transient. |
| 336 | * |
| 337 | * Hooked to `jetpack_client_authorize_error` which fires when |
| 338 | * the auth webhook fails. The transient is read on the next |
| 339 | * Connectors page load so the JS card can display the error. |
| 340 | * |
| 341 | * @since 8.2.0 |
| 342 | * |
| 343 | * @param \WP_Error $error Authorization error. |
| 344 | */ |
| 345 | public static function store_auth_error( $error ) { |
| 346 | if ( is_wp_error( $error ) ) { |
| 347 | $user_id = get_current_user_id(); |
| 348 | if ( $user_id ) { |
| 349 | set_transient( |
| 350 | 'jetpack_connector_auth_error_' . $user_id, |
| 351 | $error->get_error_message(), |
| 352 | 60 |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Read and delete a stored authorization error for the current user. |
| 360 | * |
| 361 | * @return string|false Error message or false if none. |
| 362 | */ |
| 363 | private static function consume_auth_error() { |
| 364 | $user_id = get_current_user_id(); |
| 365 | if ( ! $user_id ) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | $key = 'jetpack_connector_auth_error_' . $user_id; |
| 370 | $error = get_transient( $key ); |
| 371 | if ( false !== $error ) { |
| 372 | delete_transient( $key ); |
| 373 | } |
| 374 | |
| 375 | return $error; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get connected plugins data for the script module. |
| 380 | * |
| 381 | * @param Manager $manager Connection manager instance. |
| 382 | * @return array List of connected plugin data. |
| 383 | */ |
| 384 | private static function get_connected_plugins_data( $manager ) { |
| 385 | $plugins = $manager->get_connected_plugins(); |
| 386 | |
| 387 | if ( is_wp_error( $plugins ) || ! is_array( $plugins ) ) { |
| 388 | return array(); |
| 389 | } |
| 390 | |
| 391 | $result = array(); |
| 392 | |
| 393 | foreach ( $plugins as $slug => $plugin_data ) { |
| 394 | $name = $plugin_data['name'] ?? $slug; |
| 395 | |
| 396 | $entry = array( |
| 397 | 'name' => $name, |
| 398 | 'slug' => $slug, |
| 399 | ); |
| 400 | |
| 401 | $logo_url = static::get_plugin_logo_url( $slug ); |
| 402 | if ( $logo_url ) { |
| 403 | $entry['logoUrl'] = $logo_url; |
| 404 | } |
| 405 | |
| 406 | $result[] = $entry; |
| 407 | } |
| 408 | |
| 409 | return $result; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Determine the connector card logo based on which plugin families are connected. |
| 414 | * |
| 415 | * Priority: |
| 416 | * 1. Both Woo-family and A4A plugins → jetpack-connect-all.svg |
| 417 | * 2. Woo-family only → jetpack-connect-woo.svg |
| 418 | * 3. A4A only → jetpack-connect-a8c.svg |
| 419 | * 4. Default (Jetpack only or other) → jetpack-connect.svg |
| 420 | * |
| 421 | * @since 8.3.2 |
| 422 | * |
| 423 | * @return string Logo URL. |
| 424 | */ |
| 425 | private static function get_connector_logo_url() { |
| 426 | $plugins = Plugin_Storage::get_all(); |
| 427 | |
| 428 | $has_woo = false; |
| 429 | $has_a4a = false; |
| 430 | |
| 431 | if ( is_array( $plugins ) ) { |
| 432 | foreach ( array_keys( $plugins ) as $slug ) { |
| 433 | if ( str_starts_with( $slug, 'woocommerce' ) ) { |
| 434 | $has_woo = true; |
| 435 | } |
| 436 | if ( str_starts_with( $slug, 'automattic' ) ) { |
| 437 | $has_a4a = true; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if ( $has_woo && $has_a4a ) { |
| 443 | return plugins_url( 'images/jetpack-connect-all.svg', __FILE__ ); |
| 444 | } |
| 445 | |
| 446 | if ( $has_woo ) { |
| 447 | return plugins_url( 'images/jetpack-connect-woo.svg', __FILE__ ); |
| 448 | } |
| 449 | |
| 450 | if ( $has_a4a ) { |
| 451 | return plugins_url( 'images/jetpack-connect-a8c.svg', __FILE__ ); |
| 452 | } |
| 453 | |
| 454 | return plugins_url( 'images/jetpack-connect.svg', __FILE__ ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Map a plugin slug to a brand logo URL. |
| 459 | * |
| 460 | * Jetpack-family plugins get the Jetpack mark, WooCommerce-family |
| 461 | * plugins get the Woo mark, and Automattic for Agencies gets the |
| 462 | * Automattic mark. Unknown slugs fall through to the |
| 463 | * `jetpack_connection_plugin_logo_url` filter so that third-party |
| 464 | * plugins can register their own logo. Only SVG URLs are accepted |
| 465 | * to keep the icons sharp at every display density. |
| 466 | * |
| 467 | * @since 8.3.2 |
| 468 | * |
| 469 | * @param string $slug Plugin slug. |
| 470 | * @return string|null Logo URL or null. |
| 471 | */ |
| 472 | private static function get_plugin_logo_url( $slug ) { |
| 473 | if ( str_starts_with( $slug, 'jetpack' ) ) { |
| 474 | return plugins_url( 'images/jetpack-icon.svg', __FILE__ ); // str_starts_with() is polyfilled by WP since 5.9; this code only runs on WP 7.0+. |
| 475 | } |
| 476 | |
| 477 | if ( str_starts_with( $slug, 'woocommerce' ) ) { |
| 478 | return plugins_url( 'images/woo-icon.svg', __FILE__ ); |
| 479 | } |
| 480 | |
| 481 | if ( str_starts_with( $slug, 'automattic' ) ) { |
| 482 | return plugins_url( 'images/automattic-icon.svg', __FILE__ ); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Filters a map of plugin slugs to custom logo URLs for the |
| 487 | * Settings → Connectors card. |
| 488 | * |
| 489 | * Add entries as `$slug => $url` pairs. URLs must point to an |
| 490 | * SVG file (`.svg` extension required); non-SVG values are |
| 491 | * silently ignored and the generic fallback icon is shown. |
| 492 | * |
| 493 | * Example: |
| 494 | * |
| 495 | * add_filter( 'jetpack_connection_plugin_logos', function ( $logos ) { |
| 496 | * $logos['my-plugin'] = plugins_url( 'assets/logo.svg', __FILE__ ); |
| 497 | * return $logos; |
| 498 | * } ); |
| 499 | * |
| 500 | * @since 8.3.2 |
| 501 | * |
| 502 | * @param array<string,string> $logos Map of plugin slug to SVG URL. |
| 503 | */ |
| 504 | $logos = apply_filters( 'jetpack_connection_plugin_logos', array() ); |
| 505 | |
| 506 | if ( isset( $logos[ $slug ] ) && is_string( $logos[ $slug ] ) && str_ends_with( strtolower( $logos[ $slug ] ), '.svg' ) ) { |
| 507 | return esc_url( $logos[ $slug ] ); |
| 508 | } |
| 509 | |
| 510 | return null; |
| 511 | } |
| 512 | } |