Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.98% |
6 / 86 |
|
10.00% |
1 / 10 |
CRAP | |
0.00% |
0 / 1 |
| AI_Launchpad | |
7.50% |
6 / 80 |
|
10.00% |
1 / 10 |
561.02 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| is_ai_launchpad_request | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
12 | |||
| is_eligible | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| has_paid_plan | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| was_ai_onboarded | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| is_enabled_for_site | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_menu | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| load_wp_build | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| enqueue_jwt_initial_state | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| fix_boot_import_map_ordering | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- Feature entry file, named after the feature, also holds the AI_Launchpad bootstrap class. |
| 2 | /** |
| 3 | * AI Launchpad: an AI-tailored onboarding tasklist on a top-level wp-admin page. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 11 | use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; |
| 12 | |
| 13 | // helpers.php defines the shared option reader the listeners depend on, so it |
| 14 | // loads first. The REST controller and completion listeners self-register on |
| 15 | // their own hooks (rest_api_init / init) at load time, and must run on every |
| 16 | // request regardless of which admin page is showing. |
| 17 | require_once __DIR__ . '/helpers.php'; |
| 18 | require_once __DIR__ . '/eligibility.php'; |
| 19 | require_once __DIR__ . '/class-ai-launchpad-rest.php'; |
| 20 | require_once __DIR__ . '/class-ai-launchpad-listeners.php'; |
| 21 | require_once __DIR__ . '/class-ai-launchpad-theme-listener.php'; |
| 22 | require_once __DIR__ . '/class-ai-launchpad-dev-enable.php'; |
| 23 | |
| 24 | /** |
| 25 | * Registers the AI Launchpad admin page and its wp-build assets. |
| 26 | */ |
| 27 | class AI_Launchpad { |
| 28 | |
| 29 | /** |
| 30 | * Admin page slug. The `-wp-admin` suffix is the wp-build convention for |
| 31 | * pages that integrate with the standard wp-admin chrome: the generated |
| 32 | * page PHP only enqueues its assets when `$_GET['page']` matches |
| 33 | * `<page-id>-wp-admin`. |
| 34 | */ |
| 35 | const MENU_SLUG = 'ai-launchpad-wp-admin'; |
| 36 | |
| 37 | /** |
| 38 | * Render callback generated by wp-build in build/pages/ai-launchpad/page-wp-admin.php. |
| 39 | */ |
| 40 | const RENDER_CALLBACK = 'jetpack_mu_wpcom_ai_launchpad_wp_admin_render_page'; |
| 41 | |
| 42 | /** |
| 43 | * Initialize the feature. |
| 44 | */ |
| 45 | public static function init() { |
| 46 | add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) ); |
| 47 | |
| 48 | if ( ! self::is_ai_launchpad_request() ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | self::load_wp_build(); |
| 53 | self::fix_boot_import_map_ordering(); |
| 54 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_jwt_initial_state' ), 20 ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Whether the current request targets the AI Launchpad admin page. |
| 59 | * |
| 60 | * @return bool |
| 61 | */ |
| 62 | private static function is_ai_launchpad_request() { |
| 63 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 64 | return is_admin() && isset( $_GET['page'] ) && self::MENU_SLUG === $_GET['page']; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Whether the current site is eligible for the AI Launchpad. |
| 69 | * |
| 70 | * MVP gate: paid plan, not already AI-onboarded, and explicitly enabled for |
| 71 | * the site via the `wpcom_ai_launchpad_enabled` option (set per-site over |
| 72 | * wp-cli). Replaces the earlier automattician/blog-sticker check, which did |
| 73 | * not work on Atomic: `is_automattician()` is undefined there and blog |
| 74 | * stickers require the wpcom sandbox. The option works identically on Simple |
| 75 | * and Atomic and is context-independent (admin, REST, and CLI agree). |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | public static function is_eligible() { |
| 80 | static $eligible = null; |
| 81 | |
| 82 | if ( null === $eligible ) { |
| 83 | // Cheapest gate first: the per-site option disqualifies the vast |
| 84 | // majority of sites with a single option read, before the more |
| 85 | // expensive purchases lookup in has_paid_plan() runs on every admin |
| 86 | // page. Memoized since the result is stable for the request. |
| 87 | $eligible = self::is_enabled_for_site() |
| 88 | && ! self::was_ai_onboarded() |
| 89 | && self::has_paid_plan(); |
| 90 | } |
| 91 | |
| 92 | return $eligible; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Whether the site has a paid plan (bundle purchase). |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | private static function has_paid_plan() { |
| 101 | if ( ! function_exists( 'wpcom_get_site_purchases' ) ) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | $bundles = wp_list_filter( wpcom_get_site_purchases(), array( 'product_type' => 'bundle' ) ); |
| 106 | |
| 107 | return ! empty( $bundles ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Whether the site already went through an AI onboarding flow. |
| 112 | * |
| 113 | * @return bool |
| 114 | */ |
| 115 | private static function was_ai_onboarded() { |
| 116 | return get_option( 'site_intent' ) === 'ai-assembler' || get_option( 'site_creation_flow' ) === 'ai-site-builder'; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Whether the AI Launchpad has been explicitly enabled for this site. |
| 121 | * |
| 122 | * Set per-site with `wp option update wpcom_ai_launchpad_enabled 1`. |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | private static function is_enabled_for_site() { |
| 127 | return (bool) get_option( 'wpcom_ai_launchpad_enabled' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Register the top-level admin menu item for eligible users. |
| 132 | */ |
| 133 | public static function register_menu() { |
| 134 | if ( ! self::is_eligible() ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | // The render callback only exists when build/build.php has been loaded, |
| 139 | // which happens on the AI Launchpad page itself. On other admin screens |
| 140 | // the menu just needs a registered slug. |
| 141 | $callback = function_exists( self::RENDER_CALLBACK ) |
| 142 | ? self::RENDER_CALLBACK |
| 143 | : '__return_empty_string'; |
| 144 | |
| 145 | add_menu_page( |
| 146 | /** "AI Launchpad" is a product name, do not translate. */ |
| 147 | 'AI Launchpad', |
| 148 | 'AI Launchpad', |
| 149 | 'manage_options', |
| 150 | self::MENU_SLUG, |
| 151 | $callback, |
| 152 | 'dashicons-list-view' |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Load the wp-build generated asset registrations and the boot polyfills. |
| 158 | */ |
| 159 | private static function load_wp_build() { |
| 160 | $wp_build_index = dirname( __DIR__, 3 ) . '/build/build.php'; |
| 161 | |
| 162 | if ( ! file_exists( $wp_build_index ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | require_once $wp_build_index; |
| 167 | |
| 168 | // Register polyfills for WP < 7.0 (must run before enqueue). |
| 169 | WP_Build_Polyfills::register( |
| 170 | 'jetpack-mu-wpcom', |
| 171 | array_merge( |
| 172 | WP_Build_Polyfills::SCRIPT_HANDLES, |
| 173 | WP_Build_Polyfills::MODULE_IDS |
| 174 | ) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Attach the JWT preconditions to the page's prerequisites script: |
| 180 | * `JP_CONNECTION_INITIAL_STATE` (apiNonce + siteSuffix for requestJwt()) |
| 181 | * and `Jetpack_Editor_Initial_State.wpcomBlogId`. |
| 182 | */ |
| 183 | public static function enqueue_jwt_initial_state() { |
| 184 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 185 | |
| 186 | if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | Connection_Initial_State::render_script( $handle ); |
| 191 | |
| 192 | wp_localize_script( |
| 193 | $handle, |
| 194 | 'Jetpack_Editor_Initial_State', |
| 195 | array( |
| 196 | 'wpcomBlogId' => get_wpcom_blog_id(), |
| 197 | ) |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Fix import map ordering for the wp-build boot script. |
| 203 | * |
| 204 | * In wp-admin, _wp_footer_scripts (classic scripts) and print_import_map |
| 205 | * both hook into admin_print_footer_scripts at priority 10, but |
| 206 | * _wp_footer_scripts is registered first. This causes the inline |
| 207 | * import("@wordpress/boot") to execute before the import map exists. |
| 208 | * |
| 209 | * This fix moves the import() call from the classic inline script to a |
| 210 | * <script type="module"> printed at priority 20 (after the import map). |
| 211 | * |
| 212 | * @todo Remove once @wordpress/build ships with the loader.js fix upstream |
| 213 | * (WordPress/gutenberg#76870) and Jetpack updates the dependency. |
| 214 | */ |
| 215 | private static function fix_boot_import_map_ordering() { |
| 216 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 217 | |
| 218 | add_action( |
| 219 | 'admin_enqueue_scripts', |
| 220 | static function () use ( $handle ) { |
| 221 | $data = wp_scripts()->get_data( $handle, 'after' ); |
| 222 | if ( empty( $data ) ) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Find and extract the import("@wordpress/boot") inline script. |
| 227 | $boot_script = null; |
| 228 | $remaining = array(); |
| 229 | foreach ( $data as $line ) { |
| 230 | if ( strpos( $line, '@wordpress/boot' ) !== false ) { |
| 231 | $boot_script = $line; |
| 232 | } else { |
| 233 | $remaining[] = $line; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if ( $boot_script === null ) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | // Remove from the classic script handle. |
| 242 | wp_scripts()->add_data( $handle, 'after', $remaining ); |
| 243 | |
| 244 | // Re-emit as a module script after the import map. |
| 245 | add_action( |
| 246 | 'admin_print_footer_scripts', |
| 247 | static function () use ( $boot_script ) { |
| 248 | wp_print_inline_script_tag( $boot_script, array( 'type' => 'module' ) ); |
| 249 | }, |
| 250 | 20 |
| 251 | ); |
| 252 | }, |
| 253 | PHP_INT_MAX |
| 254 | ); |
| 255 | } |
| 256 | } |