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