Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.43% covered (danger)
5.43%
5 / 92
11.11% covered (danger)
11.11%
1 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
AI_Launchpad
6.33% covered (danger)
6.33%
5 / 79
11.11% covered (danger)
11.11%
1 / 9
497.41
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 6
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
 was_ai_onboarded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 is_enabled_for_site
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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
 fix_boot_import_map_ordering
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
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
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__ . '/eligibility.php';
16require_once __DIR__ . '/class-ai-launchpad-memberships.php';
17require_once __DIR__ . '/class-ai-launchpad-rest.php';
18require_once __DIR__ . '/class-ai-launchpad-listeners.php';
19require_once __DIR__ . '/class-ai-launchpad-theme-listener.php';
20require_once __DIR__ . '/class-ai-launchpad-social-listener.php';
21require_once __DIR__ . '/class-ai-launchpad-subscribers-listener.php';
22require_once __DIR__ . '/class-ai-launchpad-subscribe-block-listener.php';
23require_once __DIR__ . '/class-ai-launchpad-about-page-listener.php';
24require_once __DIR__ . '/class-ai-launchpad-gallery-page-listener.php';
25require_once __DIR__ . '/class-ai-launchpad-first-post-listener.php';
26require_once __DIR__ . '/class-ai-launchpad-dev-enable.php';
27
28/**
29 * Registers the AI Launchpad admin page and its wp-build assets.
30 */
31class 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, and explicitly enabled for the site via the `wpcom_ai_launchpad_enabled` option.
72     * The paid-plan requirement is temporarily lifted (see below).
73     *
74     * @return bool
75     */
76    public static function is_eligible() {
77        static $eligible = null;
78
79        if ( null === $eligible ) {
80            // TEMPORARY: the paid-plan gate is lifted so the AI Launchpad is available on all plans, including free.
81            // Revert this commit to re-require a paid bundle (the removed has_paid_plan() check).
82            $eligible = self::is_enabled_for_site()
83                && ! self::was_ai_onboarded();
84        }
85
86        return $eligible;
87    }
88
89    /**
90     * Whether the site already went through an AI onboarding flow.
91     *
92     * @return bool
93     */
94    private static function was_ai_onboarded() {
95        return get_option( 'site_intent' ) === 'ai-assembler' || get_option( 'site_creation_flow' ) === 'ai-site-builder';
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        return (bool) get_option( 'wpcom_ai_launchpad_enabled' );
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            2.01
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
187    /**
188     * Fix import map ordering for the wp-build boot script.
189     *
190     * In wp-admin both _wp_footer_scripts and print_import_map hook admin_print_footer_scripts at priority 10, but
191     * _wp_footer_scripts runs first, so the inline import("@wordpress/boot") executes before the import map exists.
192     * This moves the import() call to a <script type="module"> printed at priority 20, after the import map.
193     *
194     * @todo Remove once @wordpress/build ships the loader.js fix upstream
195     *       (WordPress/gutenberg#76870) and Jetpack updates the dependency.
196     */
197    private static function fix_boot_import_map_ordering() {
198        $handle = self::MENU_SLUG . '-prerequisites';
199
200        add_action(
201            'admin_enqueue_scripts',
202            static function () use ( $handle ) {
203                $data = wp_scripts()->get_data( $handle, 'after' );
204                if ( empty( $data ) ) {
205                    return;
206                }
207
208                $boot_script = null;
209                $remaining   = array();
210                foreach ( $data as $line ) {
211                    if ( strpos( $line, '@wordpress/boot' ) !== false ) {
212                        $boot_script = $line;
213                    } else {
214                        $remaining[] = $line;
215                    }
216                }
217
218                if ( $boot_script === null ) {
219                    return;
220                }
221
222                wp_scripts()->add_data( $handle, 'after', $remaining );
223
224                // Re-emit as a module script after the import map.
225                add_action(
226                    'admin_print_footer_scripts',
227                    static function () use ( $boot_script ) {
228                        wp_print_inline_script_tag( $boot_script, array( 'type' => 'module' ) );
229                    },
230                    20
231                );
232            },
233            PHP_INT_MAX
234        );
235    }
236}