Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.51% covered (warning)
76.51%
127 / 166
50.00% covered (danger)
50.00%
4 / 8
CRAP
n/a
0 / 0
wpcom_should_show_ai_assistant_banner
46.15% covered (danger)
46.15%
6 / 13
0.00% covered (danger)
0.00%
0 / 1
17.99
wpcom_ai_assistant_banner_mcp_enabled
44.12% covered (danger)
44.12%
15 / 34
0.00% covered (danger)
0.00%
0 / 1
74.54
wpcom_ai_assistant_banner_script_data
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
wpcom_ai_assistant_banner_tracks_props
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
12
wpcom_render_ai_assistant_banner
100.00% covered (success)
100.00%
79 / 79
100.00% covered (success)
100.00%
1 / 1
2
wpcom_maybe_add_ai_assistant_banner
41.67% covered (danger)
41.67%
5 / 12
0.00% covered (danger)
0.00%
0 / 1
7.18
wpcom_ai_assistant_banner_record_dismissal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
wpcom_dismiss_ai_assistant_banner
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * AI Assistant Banner for the WordPress.com dashboard.
4 *
5 * Displays a banner prompting Business/Commerce admins to set up AI tools until
6 * they dismiss it or both the WordPress Agent and MCP access have been on once.
7 * Works on both Simple and Atomic sites via cross-platform utilities.
8 *
9 * @package automattic/jetpack-mu-wpcom
10 */
11
12use Automattic\Jetpack\Connection\Client;
13use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Data;
14use Automattic\Jetpack\Status\Host;
15
16// The audience helpers live in the AI Launchpad feature, loaded by a different
17// loader method; require them directly so the props can never silently degrade.
18require_once __DIR__ . '/../ai-launchpad/helpers.php';
19
20/**
21 * Determines whether the current user is eligible for the AI assistant banner.
22 *
23 * Eligible users see it while either tool is off, until they dismiss it or
24 * both tools have been on once for that user.
25 *
26 * @return bool
27 */
28function wpcom_should_show_ai_assistant_banner() {
29    if ( ! current_user_can( 'manage_options' ) ) {
30        return false;
31    }
32
33    // Check dismissal early — most users will have dismissed over time.
34    if ( get_user_meta( get_current_user_id(), 'wpcom_ai_assistant_banner_dismissed', true ) ) {
35        return false;
36    }
37
38    if ( ! function_exists( 'wpcom_site_has_feature' ) || ! wpcom_site_has_feature( WPCOM_Features::BIG_SKY ) ) {
39        return false;
40    }
41
42    if ( function_exists( 'get_user_attribute' ) ) {
43        $user_id = get_current_user_id();
44
45        // Don't show if user has opted out of AI features.
46        if ( '1' === get_user_attribute( $user_id, 'ai_features_opted_out' ) ) {
47            return false;
48        }
49
50        // Don't show if user has unsubscribed from AI Tips emails.
51        if ( '1' === get_user_attribute( $user_id, 'notifications_wpcom_email_ai_tips' ) ) {
52            return false;
53        }
54    }
55
56    return true;
57}
58
59/**
60 * Whether MCP access is enabled for the current user on this site.
61 *
62 * A short cache avoids repeated WordPress.com requests on Atomic Dashboards.
63 *
64 * @return bool|null True when on, false when off, or null when unavailable.
65 */
66function wpcom_ai_assistant_banner_mcp_enabled() {
67    $blog_id = get_wpcom_blog_id();
68    if ( ! $blog_id ) {
69        return null;
70    }
71
72    $is_simple = ( new Host() )->is_wpcom_simple();
73    $cache_key = 'wpcom_ai_banner_mcp_' . $blog_id . '_' . get_current_user_id();
74    if ( ! $is_simple ) {
75        $cached = get_transient( $cache_key );
76        if ( in_array( $cached, array( 'on', 'off', 'unavailable' ), true ) ) {
77            return 'unavailable' === $cached ? null : 'on' === $cached;
78        }
79    }
80
81    $path     = sprintf( '/sites/%d/mcp-abilities', $blog_id );
82    $response = $is_simple
83        ? Client::wpcom_json_api_request_as_blog( $path, '2', array( 'method' => 'GET' ), null, 'wpcom' )
84        : Client::wpcom_json_api_request_as_user( $path, '2', array( 'method' => 'GET' ), null, 'wpcom' );
85
86    if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
87        if ( ! $is_simple ) {
88            set_transient( $cache_key, 'unavailable', MINUTE_IN_SECONDS );
89        }
90        return null;
91    }
92
93    $data = json_decode( wp_remote_retrieve_body( $response ), true );
94    if ( ! is_array( $data ) || ! array_key_exists( 'has_mcp_plan', $data ) ) {
95        if ( ! $is_simple ) {
96            set_transient( $cache_key, 'unavailable', MINUTE_IN_SECONDS );
97        }
98        return null;
99    }
100
101    if ( ! $data['has_mcp_plan'] ) {
102        if ( ! $is_simple ) {
103            set_transient( $cache_key, 'unavailable', 5 * MINUTE_IN_SECONDS );
104        }
105        return null;
106    }
107
108    if ( ! isset( $data['site_level_enabled'] ) ) {
109        if ( ! $is_simple ) {
110            set_transient( $cache_key, 'unavailable', MINUTE_IN_SECONDS );
111        }
112        return null;
113    }
114
115    $enabled = (bool) $data['site_level_enabled'];
116    if ( ! $is_simple ) {
117        set_transient( $cache_key, $enabled ? 'on' : 'off', 5 * MINUTE_IN_SECONDS );
118    }
119    return $enabled;
120}
121
122/**
123 * Data for the banner's script: Tracks props and banner visibility.
124 *
125 * @param string $screen_id The current admin screen id.
126 * @return array{trackProps: array<string,string>, shouldShowBanner: string}
127 */
128function wpcom_ai_assistant_banner_script_data( $screen_id ) {
129    $agent_enabled         = wpcom_is_wordpress_agent_enabled();
130    $agent_and_mcp_enabled = $agent_enabled ? wpcom_ai_assistant_banner_mcp_enabled() : null;
131
132    // Dismiss for this user once both tools are on, so later loads skip the MCP check.
133    if ( true === $agent_and_mcp_enabled ) {
134        wpcom_ai_assistant_banner_record_dismissal( true );
135    }
136
137    return array(
138        'trackProps'       => wpcom_ai_assistant_banner_tracks_props( $screen_id ),
139        'shouldShowBanner' => ( ! $agent_enabled || false === $agent_and_mcp_enabled ) ? '1' : '0',
140    );
141}
142
143/**
144 * Tracks props shared by the banner's three events, per the AI events standard.
145 *
146 * @param string $screen_id The current admin screen id.
147 * @return array<string,string>
148 */
149function wpcom_ai_assistant_banner_tracks_props( $screen_id ) {
150    $purchases = function_exists( 'wpcom_expiry_get_purchases' ) ? wpcom_expiry_get_purchases() : array();
151    // Real plan bundles only: the expiry picker's slug fallback would also match
152    // add-ons such as Professional Email Premium ("premium" substring).
153    $bundles  = wp_list_filter( (array) $purchases, array( 'product_type' => 'bundle' ) );
154    $plan     = class_exists( Expiry_Data::class ) ? Expiry_Data::pick_primary_plan_purchase( $bundles ) : null;
155    $has_slug = $plan && isset( $plan->product_slug ) && is_string( $plan->product_slug ) && '' !== $plan->product_slug;
156
157    return array(
158        'channel'      => 'web',
159        'surface'      => 'dashboard',
160        // Deliberately the WP screen id, not $pagenow — AI Launchpad's hardcoded
161        // 'admin.php' uses the other vocabulary for the same standard prop.
162        'screen'       => (string) $screen_id,
163        // Two buckets, mirroring AI Launchpad: this package ships only on Simple and
164        // Atomic (wpcomsh, mu-wpcom-plugin). Add a self-hosted branch if the banner
165        // ever moves into the Jetpack plugin.
166        'site_type'    => ( new Host() )->is_wpcom_simple() ? 'simple' : 'atomic',
167        // The AI events standard wants the string "none" over an empty value.
168        'product_slug' => $has_slug ? $plan->product_slug : 'none',
169        'is_test'      => function_exists( 'wpcom_ai_launchpad_is_test' ) && wpcom_ai_launchpad_is_test() ? 'true' : 'false',
170        'is_a11n'      => function_exists( 'wpcom_ai_launchpad_is_a11n' ) && wpcom_ai_launchpad_is_a11n() ? 'true' : 'false',
171    );
172}
173
174/**
175 * Renders the AI assistant banner markup.
176 */
177function wpcom_render_ai_assistant_banner() {
178    $site_slug = wpcom_get_site_slug();
179    $cta_url   = wpcom_get_calypso_origin() . '/sites/' . $site_slug . '/settings/ai-tools';
180    $nonce     = wp_create_nonce( 'dismiss_ai_assistant_banner' );
181    // With the WordPress Agent already on, the banner starts hidden and the script
182    // reveals it only if MCP access is still off.
183    ?>
184    <div id="wpcom-ai-assistant-banner" class="notice is-dismissible" style="border-left-color: #3858E9;" data-nonce="<?php echo esc_attr( $nonce ); ?>"<?php echo wpcom_is_wordpress_agent_enabled() ? ' hidden' : ''; ?>>
185        <div class="wpcom-ai-assistant-banner__layout">
186            <svg width="24" height="24" viewBox="0 0 400 400" fill="none" xmlns="http://www.w3.org/2000/svg">
187                <path d="M391.528 188.061L309.455 159.75C276.997 148.597 251.403 123.003 240.25 90.5451L211.939 8.47185C208.079 -2.82395 191.921 -2.82395 188.061 8.47185L159.75 90.5451C148.597 123.003 123.003 148.597 90.5451 159.75L8.47185 188.061C-2.82395 191.921 -2.82395 208.079 8.47185 211.939L90.5451 240.25C123.003 251.403 148.597 276.997 159.75 309.455L188.061 391.528C191.921 402.824 208.079 402.824 211.939 391.528L240.25 309.455C251.403 276.997 276.997 251.403 309.455 240.25L391.528 211.939C402.824 208.079 402.824 191.921 391.528 188.061ZM295.728 206.077L254.692 220.232C238.391 225.809 225.666 238.677 220.089 254.835L205.934 295.871C203.932 301.591 195.925 301.591 193.923 295.871L179.768 254.835C174.191 238.534 161.323 225.809 145.165 220.232L104.129 206.077C98.4093 204.075 98.4093 196.068 104.129 194.066L145.165 179.911C161.466 174.334 174.191 161.466 179.768 145.308L193.923 104.272C195.925 98.5523 203.932 98.5523 205.934 104.272L220.089 145.308C225.666 161.609 238.534 174.334 254.692 179.911L295.728 194.066C301.448 196.068 301.448 204.075 295.728 206.077Z" fill="#3858E9"/>
188            </svg>
189            <div class="wpcom-ai-assistant-banner__body">
190                <div class="wpcom-ai-assistant-banner__text">
191                    <strong><?php esc_html_e( 'Build and manage your site with AI.', 'jetpack-mu-wpcom' ); ?></strong><br>
192                    <?php esc_html_e( 'Use ChatGPT, Claude, or the WordPress Agent to edit, design, and handle everyday site tasks.', 'jetpack-mu-wpcom' ); ?>
193                </div>
194                <a href="<?php echo esc_url( $cta_url ); ?>" class="button-secondary">
195                    <?php esc_html_e( 'Set up AI tools', 'jetpack-mu-wpcom' ); ?>
196                </a>
197            </div>
198        </div>
199    </div>
200    <style>
201        #wpcom-ai-assistant-banner[hidden] {
202            display: none;
203        }
204        #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__layout {
205            display: flex;
206            align-items: center;
207            gap: 16px;
208            padding: 4px 0;
209        }
210        #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__layout > svg {
211            flex-shrink: 0;
212        }
213        #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__body {
214            display: flex;
215            align-items: center;
216            gap: 16px;
217            flex: 1;
218        }
219        #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__text {
220            flex: 1;
221        }
222        #wpcom-ai-assistant-banner .button-secondary {
223            white-space: nowrap;
224        }
225        #wpcom-ai-assistant-banner .notice-dismiss {
226            top: 50%;
227            transform: translateY(-50%);
228        }
229        #wpcom-ai-assistant-banner .notice-dismiss:before {
230            content: "\2715";
231            font-family: inherit;
232            color: #787c82;
233            background: none;
234        }
235        @media ( max-width: 782px ) {
236            #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__layout {
237                align-items: flex-start;
238                gap: 8px;
239            }
240            #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__layout > svg {
241                width: 18px;
242                height: 18px;
243                margin-top: 4px;
244            }
245            /* Stack the copy and button, keeping both aligned to the text column rather than under the icon. */
246            #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__body {
247                flex-direction: column;
248                align-items: flex-start;
249                gap: 12px;
250            }
251            #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__text {
252                font-size: 13px;
253                line-height: 1.5;
254            }
255            #wpcom-ai-assistant-banner .wpcom-ai-assistant-banner__text strong {
256                font-size: 14px;
257            }
258            /* Keep the dismiss control top-right like every other notice, lined up with the title. */
259            #wpcom-ai-assistant-banner .notice-dismiss {
260                top: 8px;
261                transform: none;
262            }
263        }
264    </style>
265    <?php
266}
267
268/**
269 * Conditionally adds the AI assistant banner on the dashboard screen.
270 */
271function wpcom_maybe_add_ai_assistant_banner() {
272    $screen = get_current_screen();
273    if ( ! $screen || 'dashboard' !== $screen->id ) {
274        return;
275    }
276
277    if ( ! wpcom_should_show_ai_assistant_banner() ) {
278        return;
279    }
280
281    add_action( 'admin_notices', 'wpcom_render_ai_assistant_banner', 1 );
282
283    $asset_handle = jetpack_mu_wpcom_enqueue_assets( 'ai-assistant-banner', array( 'js' ) );
284    wp_localize_script(
285        $asset_handle,
286        'wpcomAiAssistantBanner',
287        wpcom_ai_assistant_banner_script_data( $screen->id )
288    );
289}
290add_action( 'current_screen', 'wpcom_maybe_add_ai_assistant_banner' );
291
292/**
293 * Record the current user's dismissal of the AI assistant banner.
294 *
295 * @param bool $automatic Whether both AI tools were enabled.
296 */
297function wpcom_ai_assistant_banner_record_dismissal( $automatic = false ) {
298    update_user_meta( get_current_user_id(), 'wpcom_ai_assistant_banner_dismissed', $automatic ? time() : 1 );
299}
300
301/**
302 * AJAX handler to dismiss the AI assistant banner.
303 */
304function wpcom_dismiss_ai_assistant_banner() {
305    check_ajax_referer( 'dismiss_ai_assistant_banner', 'nonce' );
306    wpcom_ai_assistant_banner_record_dismissal();
307    wp_send_json_success( null, 200, JSON_UNESCAPED_SLASHES );
308}
309add_action( 'wp_ajax_dismiss_ai_assistant_banner', 'wpcom_dismiss_ai_assistant_banner' );