Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.94% covered (danger)
6.94%
5 / 72
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
AI_Launchpad
9.43% covered (danger)
9.43%
5 / 53
14.29% covered (danger)
14.29%
1 / 7
258.68
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 is_ai_launchpad_request
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
 is_eligible
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 is_enabled_for_site
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 register_menu
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 load_wp_build
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_jwt_initial_state
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
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
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
11use 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.
14require_once __DIR__ . '/helpers.php';
15require_once __DIR__ . '/../../common/class-launchpad-personalization-experiment.php';
16require_once __DIR__ . '/eligibility.php';
17require_once __DIR__ . '/class-ai-launchpad-memberships.php';
18require_once __DIR__ . '/class-ai-launchpad-task-registry.php';
19require_once __DIR__ . '/class-ai-launchpad-rest.php';
20require_once __DIR__ . '/class-ai-launchpad-listeners.php';
21require_once __DIR__ . '/class-ai-launchpad-theme-listener.php';
22require_once __DIR__ . '/class-ai-launchpad-social-listener.php';
23require_once __DIR__ . '/class-ai-launchpad-subscribers-listener.php';
24require_once __DIR__ . '/class-ai-launchpad-subscribe-block-listener.php';
25require_once __DIR__ . '/class-ai-launchpad-about-page-listener.php';
26require_once __DIR__ . '/class-ai-launchpad-gallery-page-listener.php';
27require_once __DIR__ . '/class-ai-launchpad-contact-page-listener.php';
28require_once __DIR__ . '/class-ai-launchpad-events-page-listener.php';
29require_once __DIR__ . '/class-ai-launchpad-video-page-listener.php';
30require_once __DIR__ . '/class-ai-launchpad-portfolio-piece-listener.php';
31require_once __DIR__ . '/class-ai-launchpad-first-post-listener.php';
32require_once __DIR__ . '/class-ai-launchpad-dev-enable.php';
33
34/**
35 * Registers the AI Launchpad admin page and its wp-build assets.
36 */
37class AI_Launchpad {
38
39    /**
40     * Admin page slug. The `-wp-admin` suffix is the wp-build convention for pages that integrate with wp-admin chrome.
41     */
42    const MENU_SLUG = 'site-setup-wp-admin';
43
44    /**
45     * Render callback generated by wp-build in build/pages/site-setup/page-wp-admin.php.
46     */
47    const RENDER_CALLBACK = 'jetpack_mu_wpcom_site_setup_wp_admin_render_page';
48
49    /**
50     * Initialize the feature.
51     */
52    public static function init() {
53        add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) );
54
55        if ( ! self::is_ai_launchpad_request() ) {
56            return;
57        }
58
59        self::load_wp_build();
60        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_jwt_initial_state' ), 20 );
61    }
62
63    /**
64     * Whether the current request targets the AI Launchpad admin page.
65     *
66     * @return bool
67     */
68    private static function is_ai_launchpad_request() {
69        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
70        return is_admin() && isset( $_GET['page'] ) && self::MENU_SLUG === $_GET['page'];
71    }
72
73    /**
74     * Whether the current site is eligible for the AI Launchpad.
75     *
76     * Gate: enabled for the site (see is_enabled_for_site()) and not dismissed (skipping the
77     * wizard dismisses it, reverting the site to the regular launchpad).
78     *
79     * @return bool
80     */
81    public static function is_eligible() {
82        static $eligible = null;
83
84        if ( null === $eligible ) {
85            $eligible = self::is_enabled_for_site()
86                && ! get_option( \AI_Launchpad_REST::OPTION_DISMISSED );
87        }
88
89        return $eligible;
90    }
91
92    /**
93     * Whether the AI Launchpad has been explicitly enabled for this site.
94     *
95     * Set per-site with `wp option update wpcom_ai_launchpad_enabled 1`.
96     *
97     * @return bool
98     */
99    private static function is_enabled_for_site() {
100        // Explicit per-site switch: set at site creation for the ai_launchpad onboarding
101        // cohort, by the ?enable-ai-launchpad=1 dev handler, or manually.
102        if ( (bool) get_option( 'wpcom_ai_launchpad_enabled' ) ) {
103            return true;
104        }
105
106        return 'ai_launchpad' === Launchpad_Personalization_Experiment::get_variation();
107    }
108
109    /**
110     * Register the top-level admin menu item for eligible users.
111     */
112    public static function register_menu() {
113        if ( ! self::is_eligible() ) {
114            return;
115        }
116
117        // Once every task is done, drop the launchpad from the sidebar. Separate from eligibility on purpose — the
118        // enable option stays set, so re-tailoring or a reset brings it back. Reads the latched flag, no rebuild.
119        if ( get_option( \AI_Launchpad_REST::OPTION_COMPLETED ) ) {
120            return;
121        }
122
123        // The render callback only exists once build/build.php is loaded, which happens on the AI Launchpad page itself.
124        $callback = function_exists( self::RENDER_CALLBACK )
125            ? self::RENDER_CALLBACK
126            : '__return_empty_string';
127
128        // The "border" glyph from @wordpress/icons — the same icon the launchpad list uses for a to-do task.
129        $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>';
130
131        add_menu_page(
132            __( 'Site Setup', 'jetpack-mu-wpcom' ),
133            __( 'Site Setup', 'jetpack-mu-wpcom' ),
134            'manage_options',
135            self::MENU_SLUG,
136            $callback,
137            '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.
138            1 // Above Dashboard (position 2): setup is the site's primary surface until it's done.
139        );
140    }
141
142    /**
143     * Load the wp-build generated asset registrations and the boot polyfills.
144     */
145    private static function load_wp_build() {
146        $wp_build_index = dirname( __DIR__, 3 ) . '/build/build.php';
147
148        if ( ! file_exists( $wp_build_index ) ) {
149            return;
150        }
151
152        require_once $wp_build_index;
153
154        // Register polyfills for WP < 7.0 (must run before enqueue).
155        WP_Build_Polyfills::register(
156            'jetpack-mu-wpcom',
157            array_merge(
158                WP_Build_Polyfills::SCRIPT_HANDLES,
159                WP_Build_Polyfills::MODULE_IDS
160            )
161        );
162    }
163
164    /**
165     * Attach the JWT preconditions to the page's prerequisites script:
166     * `JP_CONNECTION_INITIAL_STATE` (apiNonce + siteSuffix for requestJwt())
167     * and `Jetpack_Editor_Initial_State.wpcomBlogId`.
168     */
169    public static function enqueue_jwt_initial_state() {
170        $handle = self::MENU_SLUG . '-prerequisites';
171
172        if ( ! wp_script_is( $handle, 'registered' ) ) {
173            return;
174        }
175
176        Connection_Initial_State::render_script( $handle );
177
178        wp_localize_script(
179            $handle,
180            'Jetpack_Editor_Initial_State',
181            array(
182                'wpcomBlogId' => get_wpcom_blog_id(),
183            )
184        );
185    }
186}