Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.52% covered (warning)
50.52%
49 / 97
25.00% covered (danger)
25.00%
1 / 4
CRAP
n/a
0 / 0
wpcom_dashboard_is_domain_upsell_eligible
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
load_wpcom_dashboard_widgets
86.00% covered (warning)
86.00%
43 / 50
0.00% covered (danger)
0.00%
0 / 1
13.46
enqueue_wpcom_dashboard_widgets
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
2
render_wpcom_dashboard_widget
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Load the widgets of Dashboard for WordPress.com sites.
4 *
5 * @package automattic/jetpack-mu-plugins
6 */
7
8use Automattic\Jetpack\Status\Host;
9
10/**
11 * Whether the current admin gets the domain upsell: a launched site with no custom domain.
12 *
13 * @return bool
14 */
15function wpcom_dashboard_is_domain_upsell_eligible() {
16    return is_user_member_of_blog()
17        && 'unlaunched' !== get_option( 'launch-status' )
18        && ! get_option( 'wpcom_is_staging_site' )
19        && ! ( new Host() )->is_p2_site()
20        && ! wpcom_has_blog_sticker( 'a4a-is-dev-site', get_current_blog_id() )
21        && ! wpcom_site_has_mapped_custom_domain();
22}
23
24/**
25 * Load all wpcom dashboard widgets.
26 */
27function load_wpcom_dashboard_widgets() {
28    if ( ! current_user_can( 'manage_options' ) ) {
29        return;
30    }
31
32    $tasks = wpcom_dashboard_is_domain_upsell_eligible() ? array( 'home-task-domain-upsell' ) : array();
33
34    enqueue_wpcom_dashboard_widgets( array( 'tasks' => $tasks ) );
35
36    $wpcom_dashboard_widgets = array(
37        array(
38            'id'       => 'wpcom_site_preview_widget',
39            'name'     => __( 'Site', 'jetpack-mu-wpcom' ),
40            'context'  => 'side',
41            'priority' => 'high',
42        ),
43    );
44
45    $launchpad_context = 'wpadmin-dashboard-widget';
46    $checklist_slug    = get_option( 'site_intent' );
47
48    // The AI Launchpad's Site Setup screen supersedes this widget: showing both surfaces two
49    // different checklists under the same name. Dismissing the AI Launchpad now means
50    // no-guidance (see below), not a fallback to this widget.
51    $has_ai_launchpad = function_exists( 'wpcom_ai_launchpad_is_eligible' )
52        && wpcom_ai_launchpad_is_eligible();
53
54    // No-guidance sites get no launchpad surface; unlike the AI Launchpad there is no dismissal fallback.
55    require_once __DIR__ . '/../../common/launchpad-no-guidance.php';
56    $is_no_guidance = wpcom_launchpad_is_no_guidance();
57
58    if (
59        defined( 'IS_WPCOM' ) && IS_WPCOM &&
60        ! $has_ai_launchpad &&
61        ! $is_no_guidance &&
62        get_option( 'launch-status', 'launched' ) !== 'launched' &&
63        ! empty( wpcom_get_launchpad_checklist_by_checklist_slug( $checklist_slug, $launchpad_context ) ) &&
64        ! wpcom_launchpad_is_task_list_dismissed( $checklist_slug )
65    ) {
66        $wpcom_dashboard_widgets[] = array(
67            'id'       => 'wpcom_launchpad_widget',
68            'name'     => __( 'Site Setup', 'jetpack-mu-wpcom' ),
69            'context'  => 'normal',
70            'priority' => 'high',
71        );
72    }
73
74    if ( ! empty( $tasks ) ) {
75        $wpcom_dashboard_widgets[] = array(
76            'id'       => 'wpcom_general_tasks_widget',
77            'name'     => __( 'Suggestions', 'jetpack-mu-wpcom' ),
78            'context'  => 'normal',
79            'priority' => 'high',
80        );
81    }
82
83    foreach ( $wpcom_dashboard_widgets as $wpcom_dashboard_widget ) {
84        wp_add_dashboard_widget(
85            $wpcom_dashboard_widget['id'],
86            $wpcom_dashboard_widget['name'],
87            'render_wpcom_dashboard_widget',
88            null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. See https://core.trac.wordpress.org/ticket/52539.
89            array(
90                'id'   => $wpcom_dashboard_widget['id'],
91                'name' => $wpcom_dashboard_widget['name'],
92            ),
93            $wpcom_dashboard_widget['context'],
94            $wpcom_dashboard_widget['priority']
95        );
96    }
97}
98add_action( 'wp_dashboard_setup', 'load_wpcom_dashboard_widgets' );
99
100/**
101 * Enqueue the assets of the wpcom dashboard widgets.
102 *
103 * @param array $args Settings to pass.
104 */
105function enqueue_wpcom_dashboard_widgets( $args = array() ) {
106    $handle = jetpack_mu_wpcom_enqueue_assets( 'wpcom-dashboard-widgets', array( 'js', 'css' ) );
107
108    $bundles      = wp_list_filter( wpcom_get_site_purchases(), array( 'product_type' => 'bundle' ) );
109    $current_plan = array_pop( $bundles );
110
111    $data = wp_json_encode(
112        array(
113            'siteName'        => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
114            'siteUrl'         => home_url(),
115            'siteIconUrl'     => get_site_icon_url( 38 ),
116            'isBlockTheme'    => wp_is_block_theme(),
117            'siteDomain'      => wp_parse_url( home_url(), PHP_URL_HOST ),
118            'siteIntent'      => get_option( 'site_intent' ),
119            'sitePlan'        => $current_plan,
120            'hasCustomDomain' => wpcom_site_has_feature( 'custom-domain' ),
121            'tasks'           => $args['tasks'],
122            'domainUpsellImg' => plugins_url( 'images/illustration-domain-upsell.svg', __FILE__ ),
123            'blogId'          => get_current_blog_id(),
124            'wpcomBlogId'     => get_wpcom_blog_id(),
125        ),
126        JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
127    );
128
129    wp_add_inline_script(
130        $handle,
131        "var JETPACK_MU_WPCOM_DASHBOARD_WIDGETS = $data;var configData = {};",
132        'before'
133    );
134}
135
136/**
137 * Render the container of the wpcom dashboard widget.
138 *
139 * @param WP_Post $post The post object.
140 * @param array   $callback_args The callback args of the render function.
141 */
142function render_wpcom_dashboard_widget( $post, $callback_args ) {
143    $args         = $callback_args['args'];
144    $widget_id    = $args['id'] . '_main';
145    $widget_class = $args['class'] ?? $args['id'];
146    $widget_name  = $args['name'];
147
148    $warning = sprintf(
149        /* translators: The name of the widget. */
150        __( 'Your %s widget requires JavaScript to function properly.', 'jetpack-mu-wpcom' ),
151        $widget_name
152    );
153
154    ?>
155    <div>
156        <div class="hide-if-js">
157            <?php echo esc_html( $warning ); ?>
158        </div>
159        <div
160            id="<?php echo esc_attr( $widget_id ); ?>"
161            class="<?php echo esc_attr( $widget_class ); ?> hide-if-no-js"
162            style="height: 100%">
163        </div>
164    </div>
165    <?php
166}