Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
5.56% |
5 / 90 |
|
12.50% |
1 / 8 |
CRAP | |
0.00% |
0 / 1 |
| AI_Launchpad | |
7.04% |
5 / 71 |
|
12.50% |
1 / 8 |
447.93 | |
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% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| is_enabled_for_site | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | |||
| enqueue_tracks | |
0.00% |
0 / 17 |
|
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\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | use Automattic\Jetpack\Terms_Of_Service; |
| 15 | use Automattic\Jetpack\Tracking; |
| 16 | use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; |
| 17 | |
| 18 | // helpers.php defines the shared option reader the listeners depend on, so it loads first. |
| 19 | require_once __DIR__ . '/helpers.php'; |
| 20 | require_once __DIR__ . '/../../common/class-launchpad-personalization-experiment.php'; |
| 21 | require_once __DIR__ . '/eligibility.php'; |
| 22 | require_once __DIR__ . '/class-ai-launchpad-memberships.php'; |
| 23 | require_once __DIR__ . '/class-ai-launchpad-task-registry.php'; |
| 24 | require_once __DIR__ . '/class-ai-launchpad-rest.php'; |
| 25 | require_once __DIR__ . '/class-ai-launchpad-listeners.php'; |
| 26 | require_once __DIR__ . '/class-ai-launchpad-theme-listener.php'; |
| 27 | require_once __DIR__ . '/class-ai-launchpad-social-listener.php'; |
| 28 | require_once __DIR__ . '/class-ai-launchpad-subscribers-listener.php'; |
| 29 | require_once __DIR__ . '/class-ai-launchpad-subscribe-block-listener.php'; |
| 30 | require_once __DIR__ . '/class-ai-launchpad-about-page-listener.php'; |
| 31 | require_once __DIR__ . '/class-ai-launchpad-gallery-page-listener.php'; |
| 32 | require_once __DIR__ . '/class-ai-launchpad-contact-page-listener.php'; |
| 33 | require_once __DIR__ . '/class-ai-launchpad-events-page-listener.php'; |
| 34 | require_once __DIR__ . '/class-ai-launchpad-video-page-listener.php'; |
| 35 | require_once __DIR__ . '/class-ai-launchpad-portfolio-piece-listener.php'; |
| 36 | require_once __DIR__ . '/class-ai-launchpad-first-post-listener.php'; |
| 37 | require_once __DIR__ . '/class-ai-launchpad-dev-enable.php'; |
| 38 | |
| 39 | /** |
| 40 | * Registers the AI Launchpad admin page and its wp-build assets. |
| 41 | */ |
| 42 | class AI_Launchpad { |
| 43 | |
| 44 | /** |
| 45 | * Admin page slug. The `-wp-admin` suffix is the wp-build convention for pages that integrate with wp-admin chrome. |
| 46 | */ |
| 47 | const MENU_SLUG = 'site-setup-wp-admin'; |
| 48 | |
| 49 | /** |
| 50 | * Render callback generated by wp-build in build/pages/site-setup/page-wp-admin.php. |
| 51 | */ |
| 52 | const RENDER_CALLBACK = 'jetpack_mu_wpcom_site_setup_wp_admin_render_page'; |
| 53 | |
| 54 | /** |
| 55 | * Initialize the feature. |
| 56 | */ |
| 57 | public static function init() { |
| 58 | add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) ); |
| 59 | |
| 60 | if ( ! self::is_ai_launchpad_request() ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | self::load_wp_build(); |
| 65 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_jwt_initial_state' ), 20 ); |
| 66 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_tracks' ), 20 ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Whether the current request targets the AI Launchpad admin page. |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | private static function is_ai_launchpad_request() { |
| 75 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 76 | return is_admin() && isset( $_GET['page'] ) && self::MENU_SLUG === $_GET['page']; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Whether the current site is eligible for the AI Launchpad. |
| 81 | * |
| 82 | * Gate: enabled for the site (see is_enabled_for_site()) and not dismissed (skipping the |
| 83 | * wizard dismisses it, reverting the site to the regular launchpad). |
| 84 | * |
| 85 | * @return bool |
| 86 | */ |
| 87 | public static function is_eligible() { |
| 88 | static $eligible = null; |
| 89 | |
| 90 | if ( null === $eligible ) { |
| 91 | $eligible = self::is_enabled_for_site() |
| 92 | && ! get_option( \AI_Launchpad_REST::OPTION_DISMISSED ); |
| 93 | } |
| 94 | |
| 95 | return $eligible; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Whether the AI Launchpad has been explicitly enabled for this site. |
| 100 | * |
| 101 | * Set per-site with `wp option update wpcom_ai_launchpad_enabled 1`. |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | private static function is_enabled_for_site() { |
| 106 | // Explicit per-site switch: set at site creation for the ai_launchpad onboarding |
| 107 | // cohort, by the ?enable-ai-launchpad=1 dev handler, or manually. |
| 108 | if ( (bool) get_option( 'wpcom_ai_launchpad_enabled' ) ) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return 'ai_launchpad' === Launchpad_Personalization_Experiment::get_variation(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Register the top-level admin menu item for eligible users. |
| 117 | */ |
| 118 | public static function register_menu() { |
| 119 | if ( ! self::is_eligible() ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Once every task is done, drop the launchpad from the sidebar. Separate from eligibility on purpose — the |
| 124 | // enable option stays set, so re-tailoring or a reset brings it back. Reads the latched flag, no rebuild. |
| 125 | if ( get_option( \AI_Launchpad_REST::OPTION_COMPLETED ) ) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // The render callback only exists once build/build.php is loaded, which happens on the AI Launchpad page itself. |
| 130 | $callback = function_exists( self::RENDER_CALLBACK ) |
| 131 | ? self::RENDER_CALLBACK |
| 132 | : '__return_empty_string'; |
| 133 | |
| 134 | // The "border" glyph from @wordpress/icons — the same icon the launchpad list uses for a to-do task. |
| 135 | $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>'; |
| 136 | |
| 137 | add_menu_page( |
| 138 | __( 'Site Setup', 'jetpack-mu-wpcom' ), |
| 139 | __( 'Site Setup', 'jetpack-mu-wpcom' ), |
| 140 | 'manage_options', |
| 141 | self::MENU_SLUG, |
| 142 | $callback, |
| 143 | '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. |
| 144 | 1 // Above Dashboard (position 2): setup is the site's primary surface until it's done. |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Load the wp-build generated asset registrations and the boot polyfills. |
| 150 | */ |
| 151 | private static function load_wp_build() { |
| 152 | $wp_build_index = dirname( __DIR__, 3 ) . '/build/build.php'; |
| 153 | |
| 154 | if ( ! file_exists( $wp_build_index ) ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | require_once $wp_build_index; |
| 159 | |
| 160 | // Register polyfills for WP < 7.0 (must run before enqueue). |
| 161 | WP_Build_Polyfills::register( |
| 162 | 'jetpack-mu-wpcom', |
| 163 | array_merge( |
| 164 | WP_Build_Polyfills::SCRIPT_HANDLES, |
| 165 | WP_Build_Polyfills::MODULE_IDS |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Attach the JWT preconditions to the page's prerequisites script: |
| 172 | * `JP_CONNECTION_INITIAL_STATE` (apiNonce + siteSuffix for requestJwt()) |
| 173 | * and `Jetpack_Editor_Initial_State.wpcomBlogId`. |
| 174 | */ |
| 175 | public static function enqueue_jwt_initial_state() { |
| 176 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 177 | |
| 178 | if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | Connection_Initial_State::render_script( $handle ); |
| 183 | |
| 184 | wp_localize_script( |
| 185 | $handle, |
| 186 | 'Jetpack_Editor_Initial_State', |
| 187 | array( |
| 188 | 'wpcomBlogId' => get_wpcom_blog_id(), |
| 189 | ) |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Attach the page's Tracks preconditions: the transport itself on Atomic, and the standard |
| 195 | * event properties as a global the client recorder reads. |
| 196 | * |
| 197 | * On Simple, wpcom's stats.php loads the Tracks transport on every admin page and pushes |
| 198 | * identifyUser and the blog_id super prop with it. On Atomic nothing does — wpcomsh only |
| 199 | * enqueues jp-tracks inside the editor — so `window._tkq` stays an ordinary array, every |
| 200 | * push accumulates in it, and the whole client-side funnel is discarded on unload. |
| 201 | * |
| 202 | * The transport is only enqueued where tracking is actually permitted: "not Simple" also |
| 203 | * covers jurassic.ninja / jurassic.tube and self-hosted sites running this package, where |
| 204 | * the server recorder already stays silent under a declined ToS or offline mode. Gating |
| 205 | * here mirrors `wpcom_enqueue_tracking_scripts()` in `src/common/index.php`. The props/ |
| 206 | * identity global below stays ungated: it is inert without the transport. |
| 207 | */ |
| 208 | public static function enqueue_tracks() { |
| 209 | $handle = self::MENU_SLUG . '-prerequisites'; |
| 210 | |
| 211 | if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | if ( ! ( new Host() )->is_wpcom_simple() ) { |
| 216 | $tracking = new Tracking( 'jetpack-mu-wpcom', new Connection_Manager() ); |
| 217 | if ( $tracking->should_enable_tracking( new Terms_Of_Service(), new Status() ) ) { |
| 218 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // JSON rather than wp_localize_script(), which stringifies every value: blog_id would |
| 223 | // reach Tracks as a string instead of an int. |
| 224 | $bootstrap = wp_json_encode( |
| 225 | array( |
| 226 | 'props' => wpcom_ai_launchpad_standard_props(), |
| 227 | 'identity' => wpcom_ai_launchpad_tracks_identity(), |
| 228 | ), |
| 229 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 230 | ); |
| 231 | |
| 232 | // WordPress concatenates every `before` inline script queued for this handle into one |
| 233 | // <script> tag, so a false here (encode failure) would emit a syntax error that also |
| 234 | // takes out the other scripts sharing the handle — including JP_CONNECTION_INITIAL_STATE |
| 235 | // and Jetpack_Editor_Initial_State.wpcomBlogId, the JWT and blog id the page needs to |
| 236 | // function at all. Fall back to an empty object so this global stays inert instead. |
| 237 | if ( false === $bootstrap ) { |
| 238 | $bootstrap = '{}'; |
| 239 | } |
| 240 | |
| 241 | wp_add_inline_script( $handle, 'window.wpcomAiLaunchpadTracks = ' . $bootstrap . ';', 'before' ); |
| 242 | } |
| 243 | } |