Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.02% covered (success)
95.02%
191 / 201
85.00% covered (warning)
85.00%
17 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Connector
95.02% covered (success)
95.02%
191 / 201
85.00% covered (warning)
85.00%
17 / 20
88
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 register_connector
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_script_module
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
5
 get_module_asset
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 get_connector_data
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
10
 add_identity_crisis_data
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
10.02
 is_tracking_allowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_protected_owner_card_state
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
 get_connection_owner_edit_url
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 get_card_asset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 hide_safe_mode_notice
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_connectors_screen
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get_connectors_page_path
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 store_auth_error
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 consume_auth_error
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 get_connected_plugin_logos
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 get_connected_plugin_families
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 get_connector_logo_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 get_inline_connector_logo_url
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 get_plugin_logo_url
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
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
12namespace Automattic\Jetpack\Connection;
13
14use Automattic\Jetpack\Assets;
15use Automattic\Jetpack\Identity_Crisis;
16use Automattic\Jetpack\IdentityCrisis\UI;
17use Automattic\Jetpack\Modules;
18use Automattic\Jetpack\Status;
19use Automattic\Jetpack\Status\Host;
20use Automattic\Jetpack\Terms_Of_Service;
21use Automattic\Jetpack\Tracking;
22
23/**
24 * Jetpack connector card handler.
25 *
26 * @since 8.2.0
27 */
28class Jetpack_Connector {
29
30    /**
31     * Whether the connector has been initialized.
32     *
33     * @var bool
34     */
35    private static $initialized = false;
36
37    /**
38     * Script module identifier.
39     *
40     * @var string
41     */
42    const MODULE_ID = '@automattic/jetpack-connection-connectors';
43
44    /**
45     * Path of the card's built script module relative to this file, without the extension.
46     *
47     * @since 9.11.0
48     *
49     * @var string
50     */
51    const MODULE_FILE = '../../dist/connectors/connectors-card';
52
53    /**
54     * Screen ID assigned by WordPress to the Gutenberg plugin's connectors submenu page.
55     *
56     * @var string
57     */
58    const GUTENBERG_CONNECTORS_SCREEN_ID = 'settings_page_options-connectors-wp-admin';
59
60    /**
61     * Page slug registered by the Gutenberg plugin for the connectors submenu page.
62     *
63     * @var string
64     */
65    const GUTENBERG_CONNECTORS_PAGE_SLUG = 'options-connectors-wp-admin';
66
67    /**
68     * Initialize the connector.
69     */
70    public static function init() {
71        if ( static::$initialized ) {
72            return;
73        }
74        static::$initialized = true;
75
76        add_action( 'wp_connectors_init', array( static::class, 'register_connector' ), 20 );
77        add_action( 'admin_enqueue_scripts', array( static::class, 'enqueue_script_module' ) );
78        add_action( 'jetpack_client_authorize_error', array( static::class, 'store_auth_error' ) );
79        add_filter( 'jetpack_identity_crisis_show_notice', array( static::class, 'hide_safe_mode_notice' ) );
80    }
81
82    /**
83     * Register Jetpack as a connector in the WP core Connectors screen.
84     *
85     * The wp_connectors_init action is available in WordPress 7.0+.
86     * On older versions this action never fires, so the hook is safely a no-op.
87     *
88     * @since 8.2.0
89     *
90     * @param \WP_Connector_Registry $registry Connector registry instance.
91     */
92    public static function register_connector( $registry ) {
93        $registry->register(
94            'wordpress_com',
95            array(
96                'name'           => 'Jetpack Connection',
97                'description'    => __( 'Enhanced functionality for Jetpack and WooCommerce with WordPress.com cloud services.', 'jetpack-connection' ),
98                'type'           => 'cloud_service',
99                'logo_url'       => static::get_connector_logo_url(),
100                'authentication' => array(
101                    'method' => 'none',
102                ),
103            )
104        );
105    }
106
107    /**
108     * Enqueue the connectors card script module on the Settings > Connectors page.
109     *
110     * Skips the card when its built files are missing.
111     *
112     * @since 8.2.0
113     * @since 9.11.0 Loads the webpack build from dist/, with its dependencies and version from the asset file.
114     * @since 9.14.0 Loads the Tracks script when tracking is allowed.
115     */
116    public static function enqueue_script_module() {
117        $asset = static::get_card_asset();
118        if ( null === $asset ) {
119            return;
120        }
121
122        $css_path = __DIR__ . '/css/connectors-card.css';
123        wp_enqueue_style(
124            'jetpack-connector-card',
125            plugins_url( 'css/connectors-card.css', __FILE__ ),
126            array(),
127            (string) @filemtime( $css_path ) // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- fallback to empty string if file is missing.
128        );
129
130        wp_register_script_module(
131            static::MODULE_ID,
132            // Resolve the '..' so core finds translations under the file's real path, as Assets::register_script() does.
133            Assets::normalize_path( plugins_url( static::MODULE_FILE . '.js', __FILE__ ) ),
134            $asset['module_dependencies'] ?? array(),
135            $asset['version'] ?? false
136        );
137        // WP 7.0+; the Gutenberg plugin's Connectors screen can run on older cores.
138        if ( function_exists( 'wp_set_script_module_translations' ) ) {
139            wp_set_script_module_translations( static::MODULE_ID, 'jetpack-connection' );
140        }
141        wp_enqueue_script_module( static::MODULE_ID );
142
143        // A script module can't depend on classic scripts, so the ones the card imports are enqueued beside it.
144        // Assets::enqueue_script also loads the stylesheet registered with the handle.
145        foreach ( $asset['dependencies'] ?? array() as $handle ) {
146            Assets::enqueue_script( $handle );
147        }
148
149        // The analytics package adds blog_id to every event from this global. Ungated, as an identity crisis loads Tracks without consent; same line as Initial_State::render().
150        wp_add_inline_script(
151            'jetpack-connection',
152            sprintf( 'window.jpTracksContext = window.jpTracksContext || {}; window.jpTracksContext.blog_id = %d;', \Jetpack_Options::get_option( 'id', 0 ) ),
153            'before'
154        );
155
156        // The shared components the card renders push Tracks events to _tkq; only w.js is needed to send them. Same gate as My Jetpack.
157        if ( static::is_tracking_allowed() ) {
158            Tracking::register_tracks_functions_scripts();
159            wp_enqueue_script( 'jp-tracks' );
160        }
161
162        add_filter(
163            'script_module_data_' . static::MODULE_ID,
164            array( static::class, 'get_connector_data' )
165        );
166    }
167
168    /**
169     * Read the asset file the build emits beside the card's script module.
170     *
171     * @since 9.11.0
172     *
173     * @param string $module_path Path of the built module, without the extension.
174     * @return array|null Asset data, or null if the module or its asset file is missing, or the asset is invalid.
175     */
176    private static function get_module_asset( $module_path ) {
177        $asset_path = $module_path . '.asset.php';
178        if ( ! file_exists( $module_path . '.js' ) || ! file_exists( $asset_path ) ) {
179            return null;
180        }
181
182        $asset = require $asset_path;
183
184        return is_array( $asset ) ? $asset : null;
185    }
186
187    /**
188     * Build the data passed to the script module via the script_module_data_ filter.
189     *
190     * Card-only data. Connection state that the package's components also read (registration,
191     * owner, viewer, connected plugins, offline mode) comes from the jetpack-connection store,
192     * which Initial_State fills, so it is not repeated here.
193     *
194     * @since 8.2.0
195     * @since 9.13.0 Shared connection state moved to the jetpack-connection store.
196     * @since 9.14.0 Adds `connectorsUrl`; sends `connectedUsersUrl` whenever there are account rows, including while the owner's token is broken.
197     * @since $$next-version$$ Leaves out `pluginLogos` in Safe Mode.
198     *
199     * @param array $data Existing script module data.
200     * @return array Filtered script module data.
201     */
202    public static function get_connector_data( $data ) {
203        $manager       = new Manager();
204        $is_registered = $manager->is_connected();
205
206        $data['apiRoot']              = esc_url_raw( rest_url() );
207        $data['apiNonce']             = wp_create_nonce( 'wp_rest' );
208        $data['redirectUri']          = static::get_connectors_page_path();
209        $data['connectorsUrl']        = admin_url( $data['redirectUri'] );
210        $data['connectorName']        = 'Jetpack Connection';
211        $data['connectorDescription'] = __( 'Enhanced functionality for Jetpack and WooCommerce with WordPress.com cloud services.', 'jetpack-connection' );
212        $data['connectorLogoUrl']     = static::get_connector_logo_url();
213
214        $connected_plugins = $manager->get_connected_plugins();
215        $connected_plugins = is_array( $connected_plugins ) ? $connected_plugins : array();
216
217        // In Safe Mode the card lists no plugins, and the Safe Mode screen gets the logos from Initial_State.
218        if ( ! ( new Status() )->in_safe_mode() ) {
219            $data['pluginLogos'] = static::get_connected_plugin_logos();
220        }
221
222        if ( $is_registered ) {
223            $data['siteDetails'] = array(
224                'siteUrl' => site_url(),
225                'homeUrl' => home_url(),
226            );
227
228            if ( array_key_exists( 'jetpack', $connected_plugins ) ) {
229                $data['ssoStatus'] = ( new Modules() )->is_active( 'sso', false );
230            }
231
232            static::add_identity_crisis_data( $data );
233        }
234
235        // Not gated on has_connected_owner(): it is false when the owner's token breaks,
236        // and the card still lists the owner of record and a healthy viewer's account then.
237        if ( $is_registered && ( (int) \Jetpack_Options::get_option( 'master_user' ) > 0 || $manager->is_user_connected() ) ) {
238            $data['connectedUsersUrl'] = Users_Connection_Admin::get_connected_view_url();
239        }
240
241        $data['connectionOwnerEditUrl'] = static::get_connection_owner_edit_url( ( new Status() )->is_offline_mode() );
242
243        $protected_owner = static::get_protected_owner_card_state( $manager );
244        if ( null !== $protected_owner ) {
245            $data['protectedOwner'] = $protected_owner;
246        }
247
248        $host              = new Host();
249        $data['isWoaSite'] = $host->is_woa_site();
250        $data['isVipSite'] = $host->is_vip_site();
251
252        $auth_error = static::consume_auth_error();
253        if ( $auth_error ) {
254            $data['authError'] = $auth_error;
255        }
256
257        return $data;
258    }
259
260    /**
261     * Add Jetpack Identity Crisis (Safe Mode) data to the script module data.
262     *
263     * Sent only in Safe Mode, which the card reads from the store's `connectionStatus.isInSafeMode`.
264     * The card uses it to render IDC resolution options (migrate / start fresh / stay in safe
265     * mode) in the expanded details. Mirrors the data assembled by
266     * \Automattic\Jetpack\IdentityCrisis\UI::get_initial_state_data().
267     *
268     * @since 8.7.0
269     * @since 9.13.0 No longer sends `isInSafeMode`; the card reads it from the store.
270     * @since 9.18.3 Not sent with the `connection-idc-screen` flag on.
271     *
272     * @param array $data Script module data passed by reference.
273     */
274    private static function add_identity_crisis_data( &$data ) {
275        if ( ! class_exists( Identity_Crisis::class ) ) {
276            return;
277        }
278
279        if ( ! ( new Status() )->in_safe_mode() ) {
280            return;
281        }
282
283        // The new screen reads the shared Safe Mode state. Guarded: the abandoned jetpack-identity-crisis package defines the same class.
284        if ( method_exists( Identity_Crisis::class, 'is_new_screen_enabled' ) && Identity_Crisis::is_new_screen_enabled() ) {
285            return;
286        }
287
288        $idc_urls = Identity_Crisis::get_mismatched_urls();
289
290        $data['isSafeModeConfirmed'] = (bool) Identity_Crisis::$is_safe_mode_confirmed;
291        $data['idc']                 = array(
292            'currentUrl'                     => ( is_array( $idc_urls ) && array_key_exists( 'current_url', $idc_urls ) ) ? $idc_urls['current_url'] : home_url(),
293            'wpcomHomeUrl'                   => ( is_array( $idc_urls ) && array_key_exists( 'wpcom_url', $idc_urls ) ) ? $idc_urls['wpcom_url'] : '',
294            'isDevelopmentSite'              => (bool) Status::is_development_site(),
295            'possibleDynamicSiteUrlDetected' => (bool) Identity_Crisis::detect_possible_dynamic_site_url(),
296            // Guarded: an older UI class (mid-update, or the abandoned jetpack-identity-crisis package another plugin bundles) has it private.
297            'platform'                       => is_callable( array( UI::class, 'get_platform' ) ) ? UI::get_platform() : null,
298            'bumpStats'                      => static::is_tracking_allowed(),
299        );
300    }
301
302    /**
303     * Whether the site allows tracking, the same gate My Jetpack uses for Tracks.
304     *
305     * @since 9.15.0
306     *
307     * @return bool
308     */
309    private static function is_tracking_allowed() {
310        return ( new Tracking() )->should_enable_tracking( new Terms_Of_Service(), new Status() );
311    }
312
313    /**
314     * Protected-owner state for the connector card.
315     *
316     * Omitted when nothing requests a protected owner and no anchor is stored,
317     * so the card keeps its current account sections. The status is
318     * Manager::resolve_protected_owner_state(), which the card switches on.
319     *
320     * `viewerIsConfirmedOwner` is that method's `is_current_user_the_po`. It tells the recovery
321     * copy apart: an owner whose own token broke is asked to reconnect, not to connect.
322     *
323     * The card always uses the package dialogs. `jetpack_connection_protected_owner_default_ui`
324     * is for a consumer's own surface, not this one.
325     *
326     * @since 9.9.0
327     *
328     * @param Manager $manager Connection manager instance.
329     * @return array{status: string, viewerIsConfirmedOwner: bool}|null
330     */
331    private static function get_protected_owner_card_state( $manager ) {
332        $requires = $manager->requires_protected_owner();
333        $anchor   = Protected_Owner::get_locked();
334
335        if ( ! $requires && ! $anchor ) {
336            return null;
337        }
338
339        $state = $manager->resolve_protected_owner_state();
340
341        // No anchor and this viewer cannot confirm: leave the card as it is.
342        if ( ! $anchor && Manager::PO_STATE_NOT_ELIGIBLE === $state['status'] ) {
343            return null;
344        }
345
346        return array(
347            'status'                 => $state['status'],
348            'viewerIsConfirmedOwner' => $state['is_current_user_the_po'],
349        );
350    }
351
352    /**
353     * The connection owner's profile URL, for a viewer who may open it.
354     *
355     * Needs `jetpack_connect` outside offline mode, so a restricted site admin sees the name as plain
356     * text. get_edit_user_link() adds the `edit_user` check, since multisite site admins cannot edit other users.
357     *
358     * @since 9.13.0
359     *
360     * @param bool $is_offline_mode Whether the site is in offline mode.
361     * @return string|null
362     */
363    private static function get_connection_owner_edit_url( $is_offline_mode ) {
364        if ( ! $is_offline_mode && ! current_user_can( 'jetpack_connect' ) ) {
365            return null;
366        }
367
368        $owner_id = (int) \Jetpack_Options::get_option( 'master_user' );
369        $url      = $owner_id > 0 ? get_edit_user_link( $owner_id ) : '';
370
371        return '' !== $url ? $url : null;
372    }
373
374    /**
375     * The card's asset data, when the current admin page shows the card.
376     *
377     * @return array|null Null off the Connectors page, on a WordPress without connector cards, or without the build.
378     */
379    private static function get_card_asset() {
380        $screen = get_current_screen();
381        if ( ! $screen || ! static::is_connectors_screen( $screen ) || ! class_exists( 'WP_Connector_Registry' ) ) {
382            return null;
383        }
384
385        // The built files are missing in a checkout that hasn't run the build, or mid-deploy.
386        return static::get_module_asset( __DIR__ . '/' . static::MODULE_FILE );
387    }
388
389    /**
390     * Hide the Safe Mode notice where the card shows, since the card shows the same screen.
391     *
392     * @since $$next-version$$
393     *
394     * @param bool $show Whether to show the notice.
395     * @return bool
396     */
397    public static function hide_safe_mode_notice( $show ) {
398        return null === static::get_card_asset() ? $show : false;
399    }
400
401    /**
402     * Check whether the given screen is the Connectors settings page.
403     *
404     * Handles both WP 7.0 core (`options-connectors`) and the Gutenberg
405     * plugin (`settings_page_options-connectors-wp-admin`).
406     *
407     * @param \WP_Screen $screen Current admin screen.
408     * @return bool
409     */
410    private static function is_connectors_screen( $screen ) {
411        return 'options-connectors' === $screen->id
412            || static::GUTENBERG_CONNECTORS_SCREEN_ID === $screen->id;
413    }
414
415    /**
416     * Return the admin-relative path for the Connectors page.
417     *
418     * WP 7.0 core uses the standalone `options-connectors.php` file while
419     * the Gutenberg plugin registers a submenu page under options-general.php
420     * with slug `options-connectors-wp-admin`. Both set parent_file to
421     * `options-general.php` for menu highlighting, so we distinguish them by
422     * checking the actual script filename being served.
423     *
424     * Note: for the Gutenberg case we use the registered page slug directly,
425     * not `$screen->id`. WordPress auto-prefixes screen IDs for submenu pages
426     * (e.g. `settings_page_options-connectors-wp-admin`), so using `$screen->id`
427     * as the `page=` parameter produces an invalid URL.
428     *
429     * The result is suitable for the `redirect_uri` parameter accepted by the
430     * `jetpack/v4/connection/register` REST endpoint (which wraps it in `admin_url()`).
431     *
432     * @return string Admin-relative path, e.g. 'options-connectors.php'.
433     */
434    private static function get_connectors_page_path() {
435        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only compared against a hardcoded string.
436        $script = isset( $_SERVER['SCRIPT_NAME'] ) ? wp_basename( wp_unslash( $_SERVER['SCRIPT_NAME'] ) ) : '';
437
438        if ( 'options-connectors.php' === $script ) {
439            return 'options-connectors.php';
440        }
441
442        // Gutenberg plugin registers the page under options-general.php.
443        $screen = get_current_screen();
444        if ( $screen && static::GUTENBERG_CONNECTORS_SCREEN_ID === $screen->id ) {
445            return 'options-general.php?page=' . static::GUTENBERG_CONNECTORS_PAGE_SLUG;
446        }
447
448        return 'options-connectors.php';
449    }
450
451    /**
452     * Store an authorization error in a short-lived transient.
453     *
454     * Hooked to `jetpack_client_authorize_error` which fires when
455     * the auth webhook fails. The transient is read on the next
456     * Connectors page load so the JS card can display the error.
457     *
458     * @since 8.2.0
459     *
460     * @param \WP_Error $error Authorization error.
461     */
462    public static function store_auth_error( $error ) {
463        if ( is_wp_error( $error ) ) {
464            $user_id = get_current_user_id();
465            if ( $user_id ) {
466                set_transient(
467                    'jetpack_connector_auth_error_' . $user_id,
468                    $error->get_error_message(),
469                    60
470                );
471            }
472        }
473    }
474
475    /**
476     * Read and delete a stored authorization error for the current user.
477     *
478     * @return string|false Error message or false if none.
479     */
480    private static function consume_auth_error() {
481        $user_id = get_current_user_id();
482        if ( ! $user_id ) {
483            return false;
484        }
485
486        $key   = 'jetpack_connector_auth_error_' . $user_id;
487        $error = get_transient( $key );
488        if ( false !== $error ) {
489            delete_transient( $key );
490        }
491
492        return $error;
493    }
494
495    /**
496     * Get the connected plugins' logo URLs.
497     *
498     * @since $$next-version$$
499     *
500     * @return array<string,string> Logo URL by slug, for plugins that have one.
501     */
502    public static function get_connected_plugin_logos() {
503        $plugins = ( new Manager() )->get_connected_plugins();
504        $plugins = is_array( $plugins ) ? $plugins : array();
505        $logos   = array();
506
507        foreach ( array_keys( $plugins ) as $slug ) {
508            $logo_url = static::get_plugin_logo_url( (string) $slug );
509            if ( $logo_url ) {
510                $logos[ $slug ] = $logo_url;
511            }
512        }
513
514        return $logos;
515    }
516
517    /**
518     * Detect which plugin families are using the connection.
519     *
520     * @since 8.5.0
521     *
522     * @return array{has_woo: bool, has_a4a: bool}
523     */
524    public static function get_connected_plugin_families() {
525        $plugins = Plugin_Storage::get_all();
526
527        $has_woo = false;
528        $has_a4a = false;
529
530        if ( is_array( $plugins ) ) {
531            foreach ( array_keys( $plugins ) as $slug ) {
532                if ( str_starts_with( $slug, 'woocommerce' ) ) {
533                    $has_woo = true;
534                }
535                if ( str_starts_with( $slug, 'automattic' ) ) {
536                    $has_a4a = true;
537                }
538            }
539        }
540
541        return array(
542            'has_woo' => $has_woo,
543            'has_a4a' => $has_a4a,
544        );
545    }
546
547    /**
548     * Determine the connector card logo based on which plugin families are connected.
549     *
550     * Priority:
551     * 1. Both Woo-family and A4A plugins → jetpack-connect-all.svg
552     * 2. Woo-family only                 → jetpack-connect-woo.svg
553     * 3. A4A only                        → jetpack-connect-a8c.svg
554     * 4. Default (Jetpack only or other) → jetpack-connect.svg
555     *
556     * @since 8.3.2
557     *
558     * @return string Logo URL.
559     */
560    public static function get_connector_logo_url() {
561        $families = self::get_connected_plugin_families();
562
563        if ( $families['has_woo'] && $families['has_a4a'] ) {
564            return plugins_url( 'images/jetpack-connect-all.svg', __FILE__ );
565        }
566
567        if ( $families['has_woo'] ) {
568            return plugins_url( 'images/jetpack-connect-woo.svg', __FILE__ );
569        }
570
571        if ( $families['has_a4a'] ) {
572            return plugins_url( 'images/jetpack-connect-a8c.svg', __FILE__ );
573        }
574
575        return plugins_url( 'images/jetpack-connect.svg', __FILE__ );
576    }
577
578    /**
579     * Get the inline (single-row) connector logo for use in compact contexts like table cells.
580     *
581     * All circles are arranged horizontally in a single row, unlike the card
582     * logos which stack circles vertically for 3+ plugins.
583     *
584     * @since 8.5.0
585     *
586     * @return string Logo URL.
587     */
588    public static function get_inline_connector_logo_url() {
589        $families = self::get_connected_plugin_families();
590
591        if ( $families['has_woo'] && $families['has_a4a'] ) {
592            return plugins_url( 'images/jetpack-connect-all-inline.svg', __FILE__ );
593        }
594
595        if ( $families['has_woo'] ) {
596            return plugins_url( 'images/jetpack-connect-woo-inline.svg', __FILE__ );
597        }
598
599        if ( $families['has_a4a'] ) {
600            return plugins_url( 'images/jetpack-connect-a8c-inline.svg', __FILE__ );
601        }
602
603        return plugins_url( 'images/jetpack-connect.svg', __FILE__ );
604    }
605
606    /**
607     * Map a plugin slug to a brand logo URL.
608     *
609     * Jetpack-family plugins get the Jetpack mark, WooCommerce-family
610     * plugins get the Woo mark, and Automattic for Agencies gets the
611     * Automattic mark. Unknown slugs fall through to the
612     * `jetpack_connection_plugin_logo_url` filter so that third-party
613     * plugins can register their own logo. Only SVG URLs are accepted
614     * to keep the icons sharp at every display density.
615     *
616     * @since 8.3.2
617     *
618     * @param string $slug Plugin slug.
619     * @return string|null Logo URL or null.
620     */
621    private static function get_plugin_logo_url( $slug ) {
622        if ( str_starts_with( $slug, 'jetpack' ) ) {
623            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+.
624        }
625
626        if ( str_starts_with( $slug, 'woocommerce' ) ) {
627            return plugins_url( 'images/woo-icon.svg', __FILE__ );
628        }
629
630        if ( str_starts_with( $slug, 'automattic' ) ) {
631            return plugins_url( 'images/automattic-icon.svg', __FILE__ );
632        }
633
634        /**
635         * Filters a map of plugin slugs to custom logo URLs for the
636         * Settings → Connectors card and the Safe Mode screen.
637         *
638         * Add entries as `$slug => $url` pairs. URLs must point to an
639         * SVG file (`.svg` extension required); non-SVG values are
640         * silently ignored: the card shows its generic fallback icon,
641         * and the Safe Mode screen shows the plugin's name only.
642         *
643         * Example:
644         *
645         *     add_filter( 'jetpack_connection_plugin_logos', function ( $logos ) {
646         *         $logos['my-plugin'] = plugins_url( 'assets/logo.svg', __FILE__ );
647         *         return $logos;
648         *     } );
649         *
650         * @since 8.3.2
651         *
652         * @param array<string,string> $logos Map of plugin slug to SVG URL.
653         */
654        $logos = apply_filters( 'jetpack_connection_plugin_logos', array() );
655
656        if ( isset( $logos[ $slug ] ) && is_string( $logos[ $slug ] ) && str_ends_with( strtolower( $logos[ $slug ] ), '.svg' ) ) {
657            return esc_url( $logos[ $slug ] );
658        }
659
660        return null;
661    }
662}