Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
jetpack_content_guidelines_ai_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3 * Content Guidelines AI — Jetpack AI integration.
4 *
5 * Enqueues a standalone JS bundle on the Content Guidelines admin page
6 * that adds AI-powered guideline generation via Jetpack.
7 *
8 * @package automattic/jetpack
9 */
10
11use Automattic\Jetpack\Assets;
12use Automattic\Jetpack\Status\Host;
13use Automattic\Jetpack\Status\Visitor;
14use Automattic\Jetpack\Tracking;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20/**
21 * Enqueue content-guidelines-ai script on the Content Guidelines admin page.
22 *
23 * @since 16.0
24 *
25 * @param string $hook_suffix The current admin page hook suffix.
26 */
27function jetpack_content_guidelines_ai_enqueue_scripts( $hook_suffix ) {
28    if ( 'settings_page_guidelines-wp-admin' !== $hook_suffix ) {
29        return;
30    }
31
32    // Content Guidelines AI is only offered on WordPress.com platform sites
33    // (Simple and Atomic) — a hard gate the jetpack_ai_enabled filter below
34    // cannot widen. Free-tier Simple/Atomic sites still load the bundle so
35    // the upgrade path can be shown — the paid-plan requirement is enforced
36    // by the suggest-guidelines API.
37    if ( ! ( new Host() )->is_wpcom_platform() ) {
38        return;
39    }
40
41    // Bail when Jetpack AI is disabled for the site: the AI settings master
42    // switch and other site-wide disables ride this filter.
43    /** This filter is documented in projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php */
44    if ( ! apply_filters( 'jetpack_ai_enabled', true ) ) {
45        return;
46    }
47
48    // Bail when build artifacts are missing rather than enqueueing a script
49    // with guessed (and likely wrong) dependencies.
50    if ( ! file_exists( JETPACK__PLUGIN_DIR . '_inc/build/content-guidelines-ai.min.asset.php' ) ) {
51        return;
52    }
53
54    // The bundle records Tracks events via @automattic/jetpack-analytics,
55    // which only queues into window._tkq. Enqueue the Tracks client (w.js)
56    // so events send without relying on whichever platform widgets happen
57    // to load it.
58    Tracking::register_tracks_functions_scripts( true );
59
60    // Handles dependencies/version from the asset file, JS translations for
61    // the text domain, the CSS (including the .rtl.css variant), and style
62    // dependencies derived from the script's.
63    Assets::register_script(
64        'jetpack-content-guidelines-ai',
65        '_inc/build/content-guidelines-ai.min.js',
66        JETPACK__PLUGIN_FILE,
67        array(
68            'in_footer'  => true,
69            'textdomain' => 'jetpack',
70            'css_path'   => '_inc/build/content-guidelines-ai.css',
71            'enqueue'    => true,
72        )
73    );
74
75    // Jetpack_AI_Helper backs both preloaded values below: the per-user
76    // "banner dismissed" flag and the AI feature state.
77    if ( ! class_exists( 'Jetpack_AI_Helper' ) && is_readable( JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php' ) ) {
78        require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php';
79    }
80
81    // Preload the per-user "banner dismissed" flag so the empty-state banner
82    // doesn't flash before an async read. Persisted via the
83    // guidelines-banner-dismissed REST endpoint, but read via Jetpack_AI_Helper
84    // rather than the endpoint class: on Simple sites the wpcom-endpoints
85    // classes are only loaded in REST requests, so a class_exists() check on
86    // the endpoint always failed here and permanently suppressed the banner
87    // and upgrade notice.
88    $initial_state = array(
89        'bannerDismissed' => class_exists( 'Jetpack_AI_Helper' ) && Jetpack_AI_Helper::is_guidelines_banner_dismissed(),
90        // Tags every Tracks event fired from this page as internal traffic. The
91        // feature is new and low-volume, so Automattician testing is otherwise a
92        // visible share of the numbers with no way to filter it out after the fact.
93        'isA11n'          => ( new Visitor() )->is_tracking_automattician(),
94    );
95
96    // Resolve the AI feature state server-side so the bundle can hydrate the
97    // wordpress-com/plans store at boot and render the correct locked/unlocked
98    // buttons on first paint, with no client-side plan fetch. On WordPress.com
99    // Simple this is a synchronous local lookup; on Atomic/self-hosted it is
100    // transient-cached with a blocking remote refresh when cold. On failure
101    // the key is omitted and the bundle renders no AI UI for this load (fail
102    // closed) rather than guessing.
103    if ( class_exists( 'Jetpack_AI_Helper' ) ) {
104        $ai_feature = Jetpack_AI_Helper::get_ai_assistance_feature();
105        if ( ! is_wp_error( $ai_feature ) ) {
106            $initial_state['aiFeature'] = $ai_feature;
107        }
108    }
109
110    wp_add_inline_script(
111        'jetpack-content-guidelines-ai',
112        'window.jetpackContentGuidelinesAi = ' . wp_json_encode(
113            $initial_state,
114            JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
115        ) . ';',
116        'before'
117    );
118}
119
120add_action( 'admin_enqueue_scripts', 'jetpack_content_guidelines_ai_enqueue_scripts' );