Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.45% |
6 / 93 |
|
11.11% |
1 / 9 |
CRAP | |
0.00% |
0 / 1 |
| AI_Launchpad | |
7.50% |
6 / 80 |
|
11.11% |
1 / 9 |
519.66 | |
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 | |||
| 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 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| 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 loads first. |
| 14 | require_once __DIR__ . '/helpers.php'; |
| 15 | require_once __DIR__ . '/eligibility.php'; |
| 16 | require_once __DIR__ . '/class-ai-launchpad-memberships.php'; |
| 17 | require_once __DIR__ . '/class-ai-launchpad-rest.php'; |
| 18 | require_once __DIR__ . '/class-ai-launchpad-listeners.php'; |
| 19 | require_once __DIR__ . '/class-ai-launchpad-theme-listener.php'; |
| 20 | require_once __DIR__ . '/class-ai-launchpad-social-listener.php'; |
| 21 | require_once __DIR__ . '/class-ai-launchpad-subscribers-listener.php'; |
| 22 | require_once __DIR__ . '/class-ai-launchpad-subscribe-block-listener.php'; |
| 23 | require_once __DIR__ . '/class-ai-launchpad-about-page-listener.php'; |
| 24 | require_once __DIR__ . '/class-ai-launchpad-gallery-page-listener.php'; |
| 25 | require_once __DIR__ . '/class-ai-launchpad-first-post-listener.php'; |
| 26 | require_once __DIR__ . '/class-ai-launchpad-dev-enable.php'; |
| 27 | |
| 28 | /** |
| 29 | * Registers the AI Launchpad admin page and its wp-build assets. |
| 30 | */ |
| 31 | class AI_Launchpad { |
| 32 | |
| 33 | /** |
| 34 | * Admin page slug. The `-wp-admin` suffix is the wp-build convention for pages that integrate with wp-admin chrome. |
| 35 | */ |
| 36 | const MENU_SLUG = 'site-setup-wp-admin'; |
| 37 | |
| 38 | /** |
| 39 | * Render callback generated by wp-build in build/pages/site-setup/page-wp-admin.php. |
| 40 | */ |
| 41 | const RENDER_CALLBACK = 'jetpack_mu_wpcom_site_setup_wp_admin_render_page'; |
| 42 | |
| 43 | /** |
| 44 | * Initialize the feature. |
| 45 | */ |
| 46 | public static function init() { |
| 47 | add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) ); |
| 48 | |
| 49 | if ( ! self::is_ai_launchpad_request() ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | self::load_wp_build(); |
| 54 | self::fix_boot_import_map_ordering(); |
| 55 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_jwt_initial_state' ), 20 ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Whether the current request targets the AI Launchpad admin page. |
| 60 | * |
| 61 | * @return bool |
| 62 | */ |
| 63 | private static function is_ai_launchpad_request() { |
| 64 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 65 | return is_admin() && isset( $_GET['page'] ) && self::MENU_SLUG === $_GET['page']; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Whether the current site is eligible for the AI Launchpad. |
| 70 | * |
| 71 | * Gate: not already AI-onboarded, not dismissed (skipping the wizard dismisses it, reverting the site |
| 72 | * to the regular launchpad), and explicitly enabled for the site via the `wpcom_ai_launchpad_enabled` |
| 73 | * option. The paid-plan requirement is temporarily lifted (see below). |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | public static function is_eligible() { |
| 78 | static $eligible = null; |
| 79 | |
| 80 | if ( null === $eligible ) { |
| 81 | // TEMPORARY: the paid-plan gate is lifted so the AI Launchpad is available on all plans, including free. |
| 82 | // Revert this commit to re-require a paid bundle (the removed has_paid_plan() check). |
| 83 | $eligible = self::is_enabled_for_site() |
| 84 | && ! self::was_ai_onboarded() |
| 85 | && ! get_option( \AI_Launchpad_REST::OPTION_DISMISSED ); |
| 86 | } |
| 87 | |
| 88 | return $eligible; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Whether the site already went through an AI onboarding flow. |
| 93 | * |
| 94 | * @return bool |
| 95 | */ |
| 96 | private static function was_ai_onboarded() { |
| 97 | return get_option( 'site_intent' ) === 'ai-assembler' || get_option( 'site_creation_flow' ) === 'ai-site-builder'; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Whether the AI Launchpad has been explicitly enabled for this site. |
| 102 | * |
| 103 | * Set per-site with `wp option update wpcom_ai_launchpad_enabled 1`. |
| 104 | * |
| 105 | * @return bool |
| 106 | */ |
| 107 | private static function is_enabled_for_site() { |
| 108 | return (bool) get_option( 'wpcom_ai_launchpad_enabled' ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register the top-level admin menu item for eligible users. |
| 113 | */ |
| 114 | public static function register_menu() { |
| 115 | if ( ! self::is_eligible() ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | // Once every task is done, drop the launchpad from the sidebar. Separate from eligibility on purpose — the |
| 120 | // enable option stays set, so re-tailoring or a reset brings it back. Reads the latched flag, no rebuild. |
| 121 | if ( get_option( \AI_Launchpad_REST::OPTION_COMPLETED ) ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // The render callback only exists once build/build.php is loaded, which happens on the AI Launchpad page itself. |
| 126 | $callback = function_exists( self::RENDER_CALLBACK ) |
| 127 | ? self::RENDER_CALLBACK |
| 128 | : '__return_empty_string'; |
| 129 | |
| 130 | // The "border" glyph from @wordpress/icons — the same icon the launchpad list uses for a to-do task. |
| 131 | $icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#fff" d="m6.6 15.6-1.2.8c.6.9 1.3 1.6 2.2 2.2l.8-1.2c-.7-.5-1.3-1.1-1.8-1.8zM5.5 12c0-.4 0-.9.1-1.3l-1.5-.3c0 .5-.1 1.1-.1 1.6s.1 1.1.2 1.6l1.5-.3c-.2-.4-.2-.9-.2-1.3zm11.9-3.6 1.2-.8c-.6-.9-1.3-1.6-2.2-2.2l-.8 1.2c.7.5 1.3 1.1 1.8 1.8zM5.3 7.6l1.2.8c.5-.7 1.1-1.3 1.8-1.8l-.7-1.3c-.9.6-1.7 1.4-2.3 2.3zm14.5 2.8-1.5.3c.1.4.1.8.1 1.3s0 .9-.1 1.3l1.5.3c.1-.5.2-1 .2-1.6s-.1-1.1-.2-1.6zM12 18.5c-.4 0-.9 0-1.3-.1l-.3 1.5c.5.1 1 .2 1.6.2s1.1-.1 1.6-.2l-.3-1.5c-.4.1-.9.1-1.3.1zm3.6-1.1.8 1.2c.9-.6 1.6-1.3 2.2-2.2l-1.2-.8c-.5.7-1.1 1.3-1.8 1.8zM10.4 4.2l.3 1.5c.4-.1.8-.1 1.3-.1s.9 0 1.3.1l.3-1.5c-.5-.1-1.1-.2-1.6-.2s-1.1.1-1.6.2z"/></svg>'; |
| 132 | |
| 133 | add_menu_page( |
| 134 | __( 'Site Setup', 'jetpack-mu-wpcom' ), |
| 135 | __( 'Site Setup', 'jetpack-mu-wpcom' ), |
| 136 | 'manage_options', |
| 137 | self::MENU_SLUG, |
| 138 | $callback, |
| 139 | 'data:image/svg+xml;base64,' . base64_encode( $icon ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Encoding an inline SVG for the menu icon data URI. |
| 140 | 2.01 |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Load the wp-build generated asset registrations and the boot polyfills. |
| 146 | */ |
| 147 | private static function load_wp_build() { |
| 148 | $wp_build_index = dirname( __DIR__, 3 ) . '/build/build.php'; |
| 149 | |
| 150 | if ( ! file_exists( $wp_build_index ) ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | require_once $wp_build_index; |
| 155 | |
| 156 | // Register polyfills for WP < 7.0 (must run before enqueue). |
| 157 | WP_Build_Polyfills::register( |
| 158 | 'jetpack-mu-wpcom', |
| 159 | array_merge( |
| 160 | WP_Build_Polyfills::SCRIPT_HANDLES, |
| 161 | WP_Build_Polyfills::MODULE_IDS |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Attach the JWT preconditions to the page's prerequisites script: |
| 168 | * `JP_CONNECTION_INITIAL_STATE` (apiNonce + siteSuffix for requestJwt()) |
| 169 | * and `Jetpack_Editor_Initial_State.wpcomBlogId`. |
| 170 | */ |
| 171 | public static function enqueue_jwt_initial_state() { |
| 172 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 173 | |
| 174 | if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | Connection_Initial_State::render_script( $handle ); |
| 179 | |
| 180 | wp_localize_script( |
| 181 | $handle, |
| 182 | 'Jetpack_Editor_Initial_State', |
| 183 | array( |
| 184 | 'wpcomBlogId' => get_wpcom_blog_id(), |
| 185 | ) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Fix import map ordering for the wp-build boot script. |
| 191 | * |
| 192 | * In wp-admin both _wp_footer_scripts and print_import_map hook admin_print_footer_scripts at priority 10, but |
| 193 | * _wp_footer_scripts runs first, so the inline import("@wordpress/boot") executes before the import map exists. |
| 194 | * This moves the import() call to a <script type="module"> printed at priority 20, after the import map. |
| 195 | * |
| 196 | * @todo Remove once @wordpress/build ships the loader.js fix upstream |
| 197 | * (WordPress/gutenberg#76870) and Jetpack updates the dependency. |
| 198 | */ |
| 199 | private static function fix_boot_import_map_ordering() { |
| 200 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 201 | |
| 202 | add_action( |
| 203 | 'admin_enqueue_scripts', |
| 204 | static function () use ( $handle ) { |
| 205 | $data = wp_scripts()->get_data( $handle, 'after' ); |
| 206 | if ( empty( $data ) ) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | $boot_script = null; |
| 211 | $remaining = array(); |
| 212 | foreach ( $data as $line ) { |
| 213 | if ( strpos( $line, '@wordpress/boot' ) !== false ) { |
| 214 | $boot_script = $line; |
| 215 | } else { |
| 216 | $remaining[] = $line; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if ( $boot_script === null ) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | wp_scripts()->add_data( $handle, 'after', $remaining ); |
| 225 | |
| 226 | // Re-emit as a module script after the import map. |
| 227 | add_action( |
| 228 | 'admin_print_footer_scripts', |
| 229 | static function () use ( $boot_script ) { |
| 230 | wp_print_inline_script_tag( $boot_script, array( 'type' => 'module' ) ); |
| 231 | }, |
| 232 | 20 |
| 233 | ); |
| 234 | }, |
| 235 | PHP_INT_MAX |
| 236 | ); |
| 237 | } |
| 238 | } |