Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.66% covered (danger)
19.66%
23 / 117
16.67% covered (danger)
16.67%
1 / 6
CRAP
n/a
0 / 0
wpcom_launch_button_is_migration_in_progress
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
wpcom_should_show_launch_button
17.39% covered (danger)
17.39%
4 / 23
0.00% covered (danger)
0.00%
0 / 1
160.32
wpcom_add_launch_button_to_admin_bar
12.50% covered (danger)
12.50%
2 / 16
0.00% covered (danger)
0.00%
0 / 1
14.72
wpcom_enqueue_components_styles
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
wpcom_enqueue_launch_button_assets
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
30
wpcom_enqueue_admin_launch_button_assets
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Adds a "launch site" button to the admin bar.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9use Automattic\Jetpack\Jetpack_Mu_Wpcom\Common;
10
11/**
12 * Whether a migration into the current site is pending or under way.
13 *
14 * Mirrors wpcom's migration_flow_get_state(): only the latest flow sticker counts, so a finished or cancelled migration doesn't.
15 *
16 * @return bool
17 */
18function wpcom_launch_button_is_migration_in_progress(): bool {
19    // Latest first, as wpcom ranks them.
20    $flow_stickers = array(
21        'migration-cancelled-difm' => false,
22        'migration-cancelled-diy'  => false,
23        'migration-completed-difm' => false,
24        'migration-completed-diy'  => false,
25        'migration-started-difm'   => true,
26        'migration-started-diy'    => true,
27        'migration-pending-difm'   => true,
28        'migration-pending-diy'    => true,
29        'migration-pending'        => false,
30        'migration-in-progress'    => true,
31    );
32
33    $blog_id = get_current_blog_id();
34    foreach ( $flow_stickers as $sticker => $in_progress ) {
35        if ( wpcom_has_blog_sticker( $sticker, $blog_id ) ) {
36            return $in_progress;
37        }
38    }
39
40    return false;
41}
42
43/**
44 * Determine whether the launch button should be shown/enhanced on this request.
45 *
46 * @return bool
47 */
48function wpcom_should_show_launch_button(): bool {
49    $current_blog_id = get_current_blog_id();
50
51    if ( function_exists( 'is_graylisted' ) && is_graylisted( $current_blog_id ) ) {
52        return false;
53    }
54
55    if ( ! is_user_logged_in() ) {
56        return false;
57    }
58
59    if ( ! is_user_member_of_blog( get_current_user_id(), $current_blog_id ) ) {
60        return false;
61    }
62
63    if ( ! current_user_can( 'manage_options' ) ) {
64        return false;
65    }
66
67    if ( function_exists( 'has_blog_sticker' ) && has_blog_sticker( 'difm-lite-in-progress' ) ) {
68        return false;
69    }
70
71    // No button for agency-managed sites.
72    if ( ! empty( get_option( 'is_fully_managed_agency_site' ) ) ) {
73        return false;
74    }
75
76    if ( get_option( 'launch-status' ) !== 'unlaunched' ) {
77        return false;
78    }
79
80    if ( $current_blog_id === 1 && defined( 'IS_WPCOM' ) && IS_WPCOM ) {
81        return false;
82    }
83
84    // Launching mid-migration would publish a half-moved site.
85    if ( wpcom_launch_button_is_migration_in_progress() ) {
86        return false;
87    }
88
89    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a context check, not a form submission.
90    $is_preview = isset( $_GET['preview'] ) && 'true' === sanitize_text_field( wp_unslash( $_GET['preview'] ) );
91    if ( $is_preview ) {
92        return false;
93    }
94
95    return true;
96}
97
98/**
99 * Adds a "launch site" button to the admin bar.
100 *
101 * @param WP_Admin_Bar $admin_bar The WordPress admin bar.
102 */
103function wpcom_add_launch_button_to_admin_bar( WP_Admin_Bar $admin_bar ) {
104    if ( ! wpcom_should_show_launch_button() ) {
105        return;
106    }
107
108    $icon = '<svg viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
109        <path
110            fill-rule="evenodd"
111            clip-rule="evenodd"
112            d="M10.6242 9.74354L7.62419 12.1261V13.2995C7.62419 13.4418 7.77653 13.5322 7.90147 13.4641L10.5265 12.0322C10.5867 11.9994 10.6242 11.9363 10.6242 11.8676V9.74354ZM6.49919 12.0875L3.91203 9.50037H2.7001C1.70383 9.50037 1.07079 8.43399 1.54786 7.55937L2.97968 4.93437C3.20967 4.51272 3.65161 4.25036 4.13191 4.25036H7.17569C9.1325 2.16798 11.3176 0.754637 14.1427 0.531305C14.9004 0.471402 15.5282 1.09911 15.4682 1.85687C15.2449 4.68199 13.8316 6.86706 11.7492 8.82386V11.8676C11.7492 12.3479 11.4868 12.7899 11.0652 13.0199L8.44018 14.4517C7.56557 14.9288 6.49919 14.2957 6.49919 13.2995V12.0875ZM6.25602 5.37536H4.13191C4.0633 5.37536 4.00017 5.41284 3.96731 5.47308L2.53549 8.09808C2.46734 8.22303 2.55777 8.37536 2.7001 8.37536H3.87344L6.25602 5.37536Z"
113        />
114        <path d="M0.498047 13.3962C0.498047 12.2341 1.44011 11.2921 2.60221 11.2921C3.76431 11.2921 4.70638 12.2341 4.70638 13.3962C4.70638 14.5583 3.76431 15.5004 2.60221 15.5004H1.06055C0.749887 15.5004 0.498047 15.2486 0.498047 14.9379V13.3962Z" />
115    </svg>';
116
117    $admin_bar->add_menu(
118        array(
119            'id'     => 'launch-site',
120            'parent' => null,
121            'group'  => null,
122            'title'  => '<span class="ab-icon">' . $icon . '</span><span class="ab-label">' . __( 'Launch site', 'jetpack-mu-wpcom' ) . '</span>',
123            // The JS button reuses this href, refreshing only its `back_to`.
124            // The Omnibar fetches this node over REST, where the request URL is no page to go back to.
125            'href'   => Common\wpcom_get_launch_site_url( defined( 'REST_REQUEST' ) && REST_REQUEST ? array() : array( 'back_to' => Common\wpcom_get_launch_site_back_to() ) ),
126            'meta'   => array(
127                // Use `admin-color-modern` to keep the button always in blueberry (modern scheme).
128                'class' => 'launch-site admin-color-modern',
129            ),
130        )
131    );
132}
133
134/**
135 * Enqueue wp-components styles and the celebration modal bundle CSS.
136 *
137 * The celebration modal uses @wordpress/components (Modal, Button, etc.).
138 * On the frontend, wp-components CSS is not loaded automatically, so we enqueue
139 * it here together with the compiled bundle CSS that contains the modal's own styles.
140 */
141function wpcom_enqueue_components_styles() {
142    if ( ! wpcom_should_show_launch_button() ) {
143        return;
144    }
145
146    // Enqueue WordPress's built-in wp-components stylesheet.
147    // In admin contexts it may already be queued; wp_enqueue_style() is idempotent.
148    wp_enqueue_style( 'wp-components' );
149
150    // Enqueue the compiled bundle CSS (contains the celebration modal styles).
151    $css_file = is_rtl() ? 'adminbar-launch-button.rtl.css' : 'adminbar-launch-button.css';
152    $css_path = Jetpack_Mu_Wpcom::BASE_DIR . 'build/adminbar-launch-button/' . $css_file;
153
154    // Fall back to LTR file if RTL file is missing.
155    if ( ! file_exists( $css_path ) ) {
156        $css_file = 'adminbar-launch-button.css';
157        $css_path = Jetpack_Mu_Wpcom::BASE_DIR . 'build/adminbar-launch-button/' . $css_file;
158    }
159
160    if ( file_exists( $css_path ) ) {
161        wp_enqueue_style(
162            'adminbar-launch-button-bundle',
163            plugins_url( 'build/adminbar-launch-button/' . $css_file, Jetpack_Mu_Wpcom::BASE_FILE ),
164            array( 'wp-components' ),
165            filemtime( $css_path )
166        );
167    }
168}
169
170/**
171 * Enqueue the necessary assets for the admin bar button.
172 */
173function wpcom_enqueue_launch_button_assets() {
174    if ( ! wpcom_should_show_launch_button() ) {
175        return;
176    }
177
178    $version = filemtime( __DIR__ . '/style.css' );
179    wp_enqueue_style( 'launch-banner', plugins_url( 'style.css', __FILE__ ), array(), $version );
180
181    $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/adminbar-launch-button/adminbar-launch-button.asset.php';
182
183    wp_enqueue_script(
184        'adminbar-launch-button',
185        plugins_url( 'build/adminbar-launch-button/adminbar-launch-button.js', Jetpack_Mu_Wpcom::BASE_FILE ),
186        $asset_file['dependencies'] ?? array(),
187        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/adminbar-launch-button/adminbar-launch-button.js' ),
188        true
189    );
190
191    // Load the translations to avoid wp.i18n overwriting server-side translations
192    // with English strings, and in general make it possible to use translations
193    // in JS.
194    wp_set_script_translations( 'adminbar-launch-button', 'jetpack-mu-wpcom' );
195
196    $bundles      = function_exists( 'wpcom_get_site_purchases' ) ? wp_list_filter( wpcom_get_site_purchases(), array( 'product_type' => 'bundle' ) ) : array();
197    $current_plan = array_pop( $bundles );
198
199    $launch_button_data = wp_json_encode(
200        array(
201            'blogId'          => get_current_blog_id(),
202            'siteUrl'         => home_url(),
203            'siteName'        => get_bloginfo( 'name' ),
204            'siteDomain'      => wp_parse_url( home_url(), PHP_URL_HOST ),
205            'sitePlan'        => $current_plan,
206            'hasCustomDomain' => function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'custom-domain' ),
207            'isTrial'         => function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'trial' ),
208        ),
209        JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
210    );
211
212    wp_add_inline_script(
213        'adminbar-launch-button',
214        "var JETPACK_LAUNCH_BUTTON_DATA = $launch_button_data;",
215        'before'
216    );
217
218    Common\wpcom_enqueue_tracking_scripts( 'adminbar-launch-button' );
219}
220
221/**
222 * Enqueue a wp-admin only script that flags the launch button as being in wp-admin.
223 *
224 * This is used to determine whether to reload the page when the celebration modal is closed.
225 */
226function wpcom_enqueue_admin_launch_button_assets() {
227    if ( ! wpcom_should_show_launch_button() ) {
228        return;
229    }
230
231    wp_add_inline_script(
232        'adminbar-launch-button',
233        'var JETPACK_LAUNCH_BUTTON_DATA_ADMIN = true;',
234        'before'
235    );
236}
237
238add_action( 'admin_bar_menu', 'wpcom_add_launch_button_to_admin_bar', 500 );
239add_action( 'wp_enqueue_scripts', 'wpcom_enqueue_components_styles' );
240add_action( 'wp_enqueue_scripts', 'wpcom_enqueue_launch_button_assets' );
241add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_components_styles' );
242add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_launch_button_assets' );
243add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_admin_launch_button_assets' );