PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.0
Jetpack – WP Security, Backup, Speed, & Growth v16.0
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / _inc / content-guidelines-ai.php
content-guidelines-ai.php
103 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Visitor;
13 use Automattic\Jetpack\Tracking;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 0 );
17 }
18
19 /**
20 * Check if the current user is an Automattician.
21 *
22 * - Simple sites: wpcom_is_proxied_request() + is_automattician()
23 * - Atomic sites: Visitor::is_automattician_feature_flags_only()
24 *
25 * @return bool
26 */
27 function jetpack_content_guidelines_ai_is_automattician() {
28 // Simple sites.
29 if ( function_exists( 'wpcom_is_proxied_request' )
30 && wpcom_is_proxied_request()
31 && function_exists( 'is_automattician' )
32 && is_automattician()
33 ) {
34 return true;
35 }
36
37 // Atomic sites.
38 return ( new Visitor() )->is_automattician_feature_flags_only();
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 // Temporarily gate to Automatticians only during internal rollout.
54 if ( ! jetpack_content_guidelines_ai_is_automattician() ) {
55 return;
56 }
57
58 // Bail when build artifacts are missing rather than enqueueing a script
59 // with guessed (and likely wrong) dependencies.
60 if ( ! file_exists( JETPACK__PLUGIN_DIR . '_inc/build/content-guidelines-ai.min.asset.php' ) ) {
61 return;
62 }
63
64 // The bundle records Tracks events via @automattic/jetpack-analytics,
65 // which only queues into window._tkq. Enqueue the Tracks client (w.js)
66 // so events send without relying on whichever platform widgets happen
67 // to load it.
68 Tracking::register_tracks_functions_scripts( true );
69
70 // Handles dependencies/version from the asset file, JS translations for
71 // the text domain, the CSS (including the .rtl.css variant), and style
72 // dependencies derived from the script's.
73 Assets::register_script(
74 'jetpack-content-guidelines-ai',
75 '_inc/build/content-guidelines-ai.min.js',
76 JETPACK__PLUGIN_FILE,
77 array(
78 'in_footer' => true,
79 'textdomain' => 'jetpack',
80 'css_path' => '_inc/build/content-guidelines-ai.css',
81 'enqueue' => true,
82 )
83 );
84
85 // Preload the per-user "banner dismissed" flag so the empty-state banner
86 // doesn't flash before an async read. Persisted via the
87 // guidelines-banner-dismissed REST endpoint. Defaults to dismissed so a
88 // load error hides the banner rather than showing it on every visit.
89 $banner_dismissed = class_exists( 'WPCOM_REST_API_V2_Endpoint_Guidelines_Banner_Dismissed' )
90 ? WPCOM_REST_API_V2_Endpoint_Guidelines_Banner_Dismissed::is_dismissed()
91 : true;
92 wp_add_inline_script(
93 'jetpack-content-guidelines-ai',
94 'window.jetpackContentGuidelinesAi = ' . wp_json_encode(
95 array( 'bannerDismissed' => $banner_dismissed ),
96 JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
97 ) . ';',
98 'before'
99 );
100 }
101
102 add_action( 'admin_enqueue_scripts', 'jetpack_content_guidelines_ai_enqueue_scripts' );
103