Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.43% covered (danger)
4.43%
14 / 316
4.76% covered (danger)
4.76%
1 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Modules_List_Table
3.85% covered (danger)
3.85%
12 / 312
4.76% covered (danger)
4.76%
1 / 21
4810.46
0.00% covered (danger)
0.00%
0 / 1
 get_module_product_groups_map
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
2
 get_module_product_group
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_product_group_name
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 is_module_recommended
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 hide_legacy_vaultpress_module
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 1
42
 js_templates
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
2
 get_views
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 views
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 filter_displayed_table_items
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_module_displayed
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
7.46
 sort_requires_connection_last
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 get_columns
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 get_bulk_actions
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 display_rows
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 group_header_row
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 single_row
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
552
 get_module_toggle_html
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
6
 get_table_classes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 module_info_check
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 unprotected_display_tablenav
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Jetpack modules list table.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Assets;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14if ( ! class_exists( 'WP_List_Table' ) ) {
15    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
16}
17
18/**
19 * Jetpack modules list table.
20 */
21class Jetpack_Modules_List_Table extends WP_List_Table {
22
23    /**
24     * Order in which product groups are displayed on the page.
25     *
26     * @var string[]
27     */
28    const PRODUCT_GROUP_ORDER = array( 'growth', 'performance', 'security', 'other' );
29
30    /**
31     * Map of module slug to product group.
32     *
33     * Modeled on the Offline Mode dashboard grouping so the modules page reads
34     * as a set of focused product areas. Any module not listed falls back to
35     * the "other" group.
36     *
37     * @return array<string, string> Module slug => product group slug.
38     */
39    public static function get_module_product_groups_map() {
40        return array(
41            // Grow your audience.
42            'blaze'                 => 'growth',
43            'comment-likes'         => 'growth',
44            'comments'              => 'growth',
45            'contact-form'          => 'growth',
46            'gravatar-hovercards'   => 'growth',
47            'likes'                 => 'growth',
48            'publicize'             => 'growth',
49            'related-posts'         => 'growth',
50            'sharedaddy'            => 'growth',
51            'simple-payments'       => 'growth',
52            'stats'                 => 'growth',
53            'subscriptions'         => 'growth',
54            'woocommerce-analytics' => 'growth',
55            'wordads'               => 'growth',
56            'wpcom-reader'          => 'growth',
57
58            // Speed up your site.
59            'carousel'              => 'performance',
60            'google-fonts'          => 'performance',
61            'infinite-scroll'       => 'performance',
62            'photon'                => 'performance',
63            'photon-cdn'            => 'performance',
64            'search'                => 'performance',
65            'tiled-gallery'         => 'performance',
66            'videopress'            => 'performance',
67
68            // Protect your site.
69            'account-protection'    => 'security',
70            'monitor'               => 'security',
71            'notes'                 => 'security',
72            'protect'               => 'security',
73            'sso'                   => 'security',
74            'vaultpress'            => 'security',
75            'waf'                   => 'security',
76        );
77    }
78
79    /**
80     * Get the product group slug for a given module.
81     *
82     * @param string $slug Module slug.
83     * @return string Product group slug.
84     */
85    public static function get_module_product_group( $slug ) {
86        $map = self::get_module_product_groups_map();
87
88        return $map[ $slug ] ?? 'other';
89    }
90
91    /**
92     * Get the translated display name for a product group.
93     *
94     * @param string $group Product group slug.
95     * @return string Translated group name.
96     */
97    public static function get_product_group_name( $group ) {
98        switch ( $group ) {
99            case 'growth':
100                return __( 'Grow your audience', 'jetpack' );
101            case 'performance':
102                return __( 'Speed up your site', 'jetpack' );
103            case 'security':
104                return __( 'Protect your site', 'jetpack' );
105            default:
106                return __( 'Other features', 'jetpack' );
107        }
108    }
109
110    /**
111     * Whether a module is flagged as recommended.
112     *
113     * @param array $module Module data (with raw, untranslated module tags).
114     * @return bool
115     */
116    public static function is_module_recommended( $module ) {
117        return ! empty( $module['module_tags'] ) && in_array( 'Recommended', (array) $module['module_tags'], true );
118    }
119
120    /**
121     * Remove the legacy `vaultpress` module from the modules screen.
122     *
123     * The `vaultpress` module is a leftover stub for the original standalone
124     * VaultPress plugin. It is hard-coded as never activatable (see
125     * Jetpack_Admin::is_module_available()) and is unrelated to the modern
126     * VaultPress Backup product, which is delivered through My Jetpack and the
127     * standalone Jetpack VaultPress Backup plugin rather than a Jetpack module.
128     * Showing a permanently-unavailable row that merely reuses the modern
129     * product's name is misleading, so we drop it from this screen only. Other
130     * surfaces (REST, the React dashboard, standalone plugins) are unaffected.
131     *
132     * @param array $modules Array of Jetpack modules keyed by slug.
133     * @return array Modules without the legacy `vaultpress` entry.
134     */
135    public static function hide_legacy_vaultpress_module( $modules ) {
136        unset( $modules['vaultpress'] );
137        return $modules;
138    }
139
140    /** Constructor. */
141    public function __construct() {
142        parent::__construct();
143
144        Jetpack::init();
145
146        // Scope this to the modules screen so REST/React/standalone surfaces keep the legacy slug.
147        add_filter( 'jetpack_modules_list_table_items', array( __CLASS__, 'hide_legacy_vaultpress_module' ) );
148
149        if ( $this->compat_fields && is_array( $this->compat_fields ) ) {
150            array_push( $this->compat_fields, 'all_items' );
151        }
152
153        /**
154         * Filters the list of modules available to be displayed in the Jetpack Settings screen.
155         *
156         * @since 3.0.0
157         *
158         * @param array $modules Array of Jetpack modules.
159         */
160        $this->all_items       = apply_filters( 'jetpack_modules_list_table_items', Jetpack_Admin::init()->get_modules() );
161        $this->items           = $this->all_items;
162        $this->items           = $this->filter_displayed_table_items( $this->items );
163        $this->_column_headers = array( $this->get_columns(), array(), array(), 'name' );
164        // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce: This is a view, not a model or controller. InputNotSanitized: Sanitized below via `$this->module_info_check()`.
165        $modal_info = isset( $_GET['info'] ) ? wp_unslash( $_GET['info'] ) : false;
166
167        // Adding in a hidden h1 heading for screen-readers.
168        ?>
169        <h1 class="screen-reader-text"><?php esc_html_e( 'Jetpack Modules List', 'jetpack' ); ?></h1>
170        <?php
171
172        wp_register_script(
173            'models.jetpack-modules',
174            Assets::get_file_url_for_environment(
175                '_inc/build/jetpack-modules.models.min.js',
176                '_inc/jetpack-modules.models.js'
177            ),
178            array( 'jquery', 'backbone' ),
179            JETPACK__VERSION,
180            false // @todo Can this be put in the footer?
181        );
182        wp_register_script(
183            'views.jetpack-modules',
184            Assets::get_file_url_for_environment(
185                '_inc/build/jetpack-modules.views.min.js',
186                '_inc/jetpack-modules.views.js'
187            ),
188            array( 'jquery', 'backbone', 'wp-util' ),
189            JETPACK__VERSION,
190            false // @todo Can this be put in the footer?
191        );
192        wp_register_script(
193            'jetpack-modules-list-table',
194            Assets::get_file_url_for_environment(
195                '_inc/build/jetpack-modules.min.js',
196                '_inc/jetpack-modules.js'
197            ),
198            array(
199                'views.jetpack-modules',
200                'models.jetpack-modules',
201                'jquery',
202            ),
203            JETPACK__VERSION,
204            true
205        );
206
207        $modules_for_js = (array) Jetpack::get_translated_modules( $this->all_items );
208        foreach ( array_keys( $modules_for_js ) as $slug ) {
209            $group = self::get_module_product_group( $slug );
210
211            $modules_for_js[ $slug ]['product_group']      = $group;
212            $modules_for_js[ $slug ]['product_group_name'] = self::get_product_group_name( $group );
213            // Detect recommendation from the raw (untranslated) module tags.
214            $modules_for_js[ $slug ]['recommended'] = isset( $this->all_items[ $slug ] ) && self::is_module_recommended( $this->all_items[ $slug ] );
215        }
216
217        wp_localize_script(
218            'jetpack-modules-list-table',
219            'jetpackModulesData',
220            array(
221                'modules'   => $modules_for_js,
222                'i18n'      => array(
223                    'search_placeholder' => __( 'Search modules…', 'jetpack' ),
224                ),
225                'modalinfo' => $this->module_info_check( $modal_info, $this->all_items ),
226                'nonces'    => array(
227                    'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ),
228                ),
229            )
230        );
231
232        wp_enqueue_script( 'jetpack-modules-list-table' );
233
234        /**
235         * Filters the js_templates callback value.
236         *
237         * @since 3.6.0
238         *
239         * @param array array( $this, 'js_templates' ) js_templates callback.
240         */
241        add_action( 'admin_footer', apply_filters( 'jetpack_modules_list_table_js_template_callback', array( $this, 'js_templates' ) ), 9 );
242    }
243
244    /**
245     * Output row template.
246     */
247    public function js_templates() {
248        ?>
249        <script type="text/html" id="tmpl-Jetpack_Modules_List_Table_Template">
250            <# var i = 0; var currentGroup = null;
251            if ( data.items.length ) {
252            _.each( data.items, function( item, key, list ) {
253                if ( item === undefined ) return;
254                if ( item.product_group !== currentGroup ) {
255                    currentGroup = item.product_group; #>
256                    <tr class="jetpack-module-group-header" data-group="{{ item.product_group }}">
257                        <td colspan="2"><h2 class="jetpack-module-group-header__title">{{ item.product_group_name }}</h2></td>
258                    </tr>
259                <# } #>
260                <tr class="jetpack-module<# if ( ++i % 2 ) { #> alternate<# } #><# if ( item.activated ) { #> active<# } #><# if ( ! item.available ) { #> unavailable<# } #>" id="{{{ item.module }}}">
261                    <th scope="row" class="check-column">
262                        <# if ( item.available && ( item.protect_page_url || item.deactivation_note ) ) { #><input type="checkbox" disabled /><# } else if ( item.available ) { #><input type="checkbox" name="modules[]" value="{{{ item.module }}}" {{{ item.disabled }}} /><# } #>
263                    </th>
264                    <td class="name column-name">
265                        <div class="jetpack-module__body">
266                            <div class="jetpack-module__info">
267                                <# if ( item.learn_more_button ) { #><a class="jetpack-module__name" href="{{{ item.learn_more_button }}}" target="_blank" rel="noopener noreferrer">{{ item.name }}</a><# } else { #><span class="jetpack-module__name">{{ item.name }}</span><# } #>
268                                <# if ( item.description ) { #><span class="jetpack-module__description">{{ item.description }}</span><# } #>
269                                <# if ( item.available && item.deactivation_note ) { #><span class="jetpack-module__note">{{ item.deactivation_note }}</span><# } #>
270                            </div>
271                            <div class="jetpack-module__meta">
272                                <span class="jetpack-module__badges">
273                                    <# if ( item.recommended && item.available ) { #><span class="jetpack-module__badge is-recommended"><?php esc_html_e( 'Recommended', 'jetpack' ); ?></span><# } #>
274                                    <# if ( item.available ) { #>
275                                        <# if ( item.activated ) { #><span class="jetpack-module__badge is-active"><?php esc_html_e( 'Active', 'jetpack' ); ?></span><# } else { #><span class="jetpack-module__badge is-inactive"><?php esc_html_e( 'Inactive', 'jetpack' ); ?></span><# } #>
276                                    <# } #>
277                                </span>
278                                <div class="row-actions">
279                                    <# if ( item.configurable ) { #>
280                                        <span class="configure">{{{ item.configurable }}}</span>
281                                    <# } #>
282                                    <# if ( item.available && item.protect_page_url ) { #>
283                                        <a class="button button-small jetpack-module__protect-link" href="{{ item.protect_page_url }}"><?php echo esc_html( _x( 'Manage in Protect', '"Protect" is a product name.', 'jetpack' ) ); ?></a>
284                                    <# } else if ( item.available && ! item.deactivation_note ) { #>
285                                        <# if ( item.activated ) { #>
286                                            <a class="jetpack-module__toggle is-active" role="switch" aria-checked="true" href="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>?page=jetpack&#038;action=deactivate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.deactivate_nonce }}}"><span class="jetpack-module__toggle-track"><span class="jetpack-module__toggle-thumb"></span></span><span class="screen-reader-text"><?php esc_html_e( 'Deactivate', 'jetpack' ); ?></span></a>
287                                        <# } else { #>
288                                            <a class="jetpack-module__toggle" role="switch" aria-checked="false" href="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>?page=jetpack&#038;action=activate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.activate_nonce }}}"><span class="jetpack-module__toggle-track"><span class="jetpack-module__toggle-thumb"></span></span><span class="screen-reader-text"><?php esc_html_e( 'Activate', 'jetpack' ); ?></span></a>
289                                        <# } #>
290                                    <# } else if ( ! item.available ) { #>
291                                        <span class="jetpack-module__unavailable">{{ item.unavailable_reason }}</span>
292                                    <# } #>
293                                </div>
294                            </div>
295                        </div>
296                    </td>
297                </tr>
298                <#
299            });
300            } else {
301                #>
302                <tr class="no-modules-found">
303                    <td colspan="2"><?php esc_html_e( 'No Modules Found', 'jetpack' ); ?></td>
304                </tr>
305                <#
306            }
307            #>
308        </script>
309        <?php
310    }
311
312    /**
313     * Get views data.
314     *
315     * @return array Maps identifier to display HTML.
316     */
317    public function get_views() {
318        /** This filter is already documented in class.jetpack-modules-list-table.php */
319        $modules              = apply_filters( 'jetpack_modules_list_table_items', Jetpack_Admin::init()->get_modules() );
320        $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' );
321        $module_tags          = array_merge( ...array_values( $array_of_module_tags ) );
322        $module_tags          = array_map( 'jetpack_get_module_i18n_tag', $module_tags );
323        $module_tags_unique   = array_count_values( $module_tags );
324        ksort( $module_tags_unique );
325
326        $format = '<a href="%3$s" %4$s data-title="%1$s">%1$s</a> <span class="count">(%2$s)</span>';
327        $title  = __( 'All', 'jetpack' );
328        $count  = is_countable( $modules ) ? count( $modules ) : 0;
329        $url    = esc_url( remove_query_arg( 'module_tag' ) );
330        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
331        $views = array(
332            'all' => sprintf( $format, $title, $count, $url, 'class="all"' ),
333        );
334        foreach ( $module_tags_unique as $title => $count ) {
335            $key           = sanitize_title( $title );
336            $display_title = esc_html( wptexturize( $title ) );
337            $url           = esc_url( add_query_arg( 'module_tag', rawurlencode( $title ) ) );
338            $views[ $key ] = sprintf( $format, $display_title, $count, $url, '' );
339        }
340        return $views;
341    }
342
343    /**
344     * Output views HTML.
345     */
346    public function views() {
347        $views = $this->get_views();
348
349        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
350        $module_tag = empty( $_GET['module_tag'] ) || ! is_string( $_GET['module_tag'] ) ? 'all' : sanitize_title( wp_unslash( $_GET['module_tag'] ) );
351
352        echo "<ul class='subsubsub'>\n";
353        foreach ( $views as $class => $view ) {
354            $class_name = $class;
355            if ( $class === $module_tag ) {
356                $class_name .= ' current';
357            }
358
359            $views[ $class ] = "\t<li class='$class_name'>$view</li>";
360        }
361        echo implode( "\n", $views ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Is HTML. Escaping happens in get_views().
362        echo '</ul>';
363    }
364
365    /**
366     * Filter a modules array for displayed items.
367     *
368     * @param array $modules Modules.
369     * @return array Displayed modules.
370     */
371    public function filter_displayed_table_items( $modules ) {
372        return array_filter( $modules, array( $this, 'is_module_displayed' ) );
373    }
374
375    /**
376     * Determine if a module is displayed.
377     *
378     * @param array $module Module data.
379     * @return bool
380     */
381    public static function is_module_displayed( $module ) {
382        // Handle module tag based filtering.
383        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
384        if ( ! empty( $_REQUEST['module_tag'] ) && is_string( $_REQUEST['module_tag'] ) ) {
385            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
386            $module_tag = sanitize_text_field( wp_unslash( $_REQUEST['module_tag'] ) );
387            if ( ! in_array( $module_tag, array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ), true ) ) {
388                return false;
389            }
390        }
391
392        // If nothing rejected it, include it!
393        return true;
394    }
395
396    /**
397     * Sort callback to put modules with `requires_connection` last.
398     *
399     * @param array $module1 Module data.
400     * @param array $module2 Module data.
401     * @return int Indicating the relative ordering of module1 and module2.
402     */
403    public static function sort_requires_connection_last( $module1, $module2 ) {
404        if ( (bool) $module1['requires_connection'] === (bool) $module2['requires_connection'] ) {
405            return 0;
406        }
407        if ( $module1['requires_connection'] ) {
408            return 1;
409        }
410        if ( $module2['requires_connection'] ) {
411            return -1;
412        }
413
414        return 0;
415    }
416
417    /**
418     * Get table columns.
419     *
420     * @return string[] Column name to header HTML.
421     */
422    public function get_columns() {
423        $columns = array(
424            'cb'   => '<input type="checkbox" />',
425            'name' => __( 'Name', 'jetpack' ),
426        );
427        return $columns;
428    }
429
430    /**
431     * Get bulk actions for the table.
432     *
433     * @return string[] Actions, code => text.
434     */
435    public function get_bulk_actions() {
436        $actions = array(
437            'bulk-activate'   => __( 'Activate', 'jetpack' ),
438            'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),
439        );
440        return $actions;
441    }
442
443    /**
444     * Print the module rows grouped by product group.
445     *
446     * Overrides WP_List_Table::display_rows() so the server-rendered (no-JS)
447     * output matches the grouped, card-style layout produced by the Backbone
448     * template.
449     */
450    public function display_rows() {
451        $buckets = array();
452        foreach ( self::PRODUCT_GROUP_ORDER as $group ) {
453            $buckets[ $group ] = array();
454        }
455
456        foreach ( $this->items as $item ) {
457            $group               = self::get_module_product_group( $item['module'] );
458            $buckets[ $group ][] = $item;
459        }
460
461        foreach ( $buckets as $group => $items ) {
462            if ( empty( $items ) ) {
463                continue;
464            }
465            $this->group_header_row( $group );
466            foreach ( $items as $item ) {
467                $this->single_row( $item );
468            }
469        }
470    }
471
472    /**
473     * Print a product group header row.
474     *
475     * @param string $group Product group slug.
476     */
477    protected function group_header_row( $group ) {
478        printf(
479            '<tr class="jetpack-module-group-header" data-group="%1$s"><td colspan="2"><h2 class="jetpack-module-group-header__title">%2$s</h2></td></tr>',
480            esc_attr( $group ),
481            esc_html( self::get_product_group_name( $group ) )
482        );
483    }
484
485    /**
486     * Print a single row of the table.
487     *
488     * @param object|array $item Item.
489     */
490    public function single_row( $item ) {
491        static $i  = 0;
492        $row_class = ( ( ++$i ) % 2 ) ? ' alternate' : '';
493
494        $available = Jetpack_Admin::is_module_available( $item );
495
496        if ( ! empty( $item['activated'] ) ) {
497            $row_class .= ' active';
498        }
499        if ( ! $available ) {
500            $row_class .= ' unavailable';
501        }
502
503        echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">';
504
505        // Switching it changes nothing, so the row offers no switch.
506        $no_switch = ! empty( $item['deactivation_note'] );
507
508        echo '<th scope="row" class="check-column">';
509        // Disabled, not dropped, so the row's name stays in line with the others.
510        if ( $available && ( ! empty( $item['protect_page_url'] ) || $no_switch ) ) {
511            echo '<input type="checkbox" disabled />';
512        } elseif ( $available ) {
513            printf( '<input type="checkbox" name="modules[]" value="%s" />', esc_attr( $item['module'] ) );
514        }
515        echo '</th>';
516
517        echo '<td class="name column-name">';
518        echo '<div class="jetpack-module__body">';
519
520        echo '<div class="jetpack-module__info">';
521        if ( ! empty( $item['learn_more_button'] ) ) {
522            printf(
523                '<a class="jetpack-module__name" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
524                esc_url( $item['learn_more_button'] ),
525                wp_kses_post( wptexturize( $item['name'] ) )
526            );
527        } else {
528            echo '<span class="jetpack-module__name">' . wp_kses_post( wptexturize( $item['name'] ) ) . '</span>';
529        }
530        if ( ! empty( $item['description'] ) ) {
531            echo '<span class="jetpack-module__description">' . esc_html( wp_strip_all_tags( $item['description'] ) ) . '</span>';
532        }
533        if ( $available && $no_switch ) {
534            echo '<span class="jetpack-module__note">' . esc_html( $item['deactivation_note'] ) . '</span>';
535        }
536        echo '</div>';
537
538        echo '<div class="jetpack-module__meta">';
539        echo '<span class="jetpack-module__badges">';
540        if ( $available && self::is_module_recommended( $item ) ) {
541            echo '<span class="jetpack-module__badge is-recommended">' . esc_html__( 'Recommended', 'jetpack' ) . '</span>';
542        }
543        if ( $available ) {
544            if ( ! empty( $item['activated'] ) ) {
545                echo '<span class="jetpack-module__badge is-active">' . esc_html__( 'Active', 'jetpack' ) . '</span>';
546            } else {
547                echo '<span class="jetpack-module__badge is-inactive">' . esc_html__( 'Inactive', 'jetpack' ) . '</span>';
548            }
549        }
550        echo '</span>';
551
552        echo '<div class="row-actions">';
553        if ( ! empty( $item['configurable'] ) ) {
554            echo '<span class="configure">' . wp_kses_post( $item['configurable'] ) . '</span>';
555        }
556        if ( $available && ! empty( $item['protect_page_url'] ) ) {
557            printf(
558                '<a class="button button-small jetpack-module__protect-link" href="%1$s">%2$s</a>',
559                esc_url( $item['protect_page_url'] ),
560                esc_html( _x( 'Manage in Protect', '"Protect" is a product name.', 'jetpack' ) )
561            );
562        } elseif ( $available && ! $no_switch ) {
563            echo $this->get_module_toggle_html( $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in helper.
564        } elseif ( ! $available && ! empty( $item['unavailable_reason'] ) ) {
565            echo '<span class="jetpack-module__unavailable">' . wp_kses_post( $item['unavailable_reason'] ) . '</span>';
566        }
567        // Close .row-actions, .jetpack-module__meta, and .jetpack-module__body.
568        echo '</div></div></div>';
569        echo '</td>';
570        echo '</tr>';
571    }
572
573    /**
574     * Build the activate/deactivate toggle control markup for a module row.
575     *
576     * @param object|array $item Module data.
577     * @return string HTML.
578     */
579    protected function get_module_toggle_html( $item ) {
580        $is_active = ! empty( $item['activated'] );
581
582        if ( $is_active ) {
583            $url   = wp_nonce_url(
584                Jetpack::admin_url(
585                    array(
586                        'page'   => 'jetpack',
587                        'action' => 'deactivate',
588                        'module' => $item['module'],
589                    )
590                ),
591                'jetpack_deactivate-' . $item['module']
592            );
593            $label = sprintf(
594                /* translators: %s: Jetpack module name. */
595                __( 'Deactivate %s', 'jetpack' ),
596                $item['name']
597            );
598            $class = 'jetpack-module__toggle is-active';
599            $aria  = 'true';
600        } else {
601            $url   = wp_nonce_url(
602                Jetpack::admin_url(
603                    array(
604                        'page'   => 'jetpack',
605                        'action' => 'activate',
606                        'module' => $item['module'],
607                    )
608                ),
609                'jetpack_activate-' . $item['module']
610            );
611            $label = sprintf(
612                /* translators: %s: Jetpack module name. */
613                __( 'Activate %s', 'jetpack' ),
614                $item['name']
615            );
616            $class = 'jetpack-module__toggle';
617            $aria  = 'false';
618        }
619
620        return sprintf(
621            '<a class="%1$s" role="switch" aria-checked="%2$s" href="%3$s"><span class="jetpack-module__toggle-track"><span class="jetpack-module__toggle-thumb"></span></span><span class="screen-reader-text">%4$s</span></a>',
622            esc_attr( $class ),
623            esc_attr( $aria ),
624            esc_url( $url ),
625            esc_html( $label )
626        );
627    }
628
629    /**
630     * Table classes.
631     *
632     * @return string[] HTML.
633     */
634    public function get_table_classes() {
635        return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed' );
636    }
637
638    /**
639     * Check if the info parameter provided in the URL corresponds to an actual module.
640     *
641     * @param string|false $info Info parameter.
642     * @param array        $modules Modules array.
643     * @return string|false
644     */
645    public function module_info_check( $info, $modules ) {
646        if ( ! $info ) {
647            return false;
648        } elseif ( array_key_exists( $info, $modules ) ) {
649            return $info;
650        }
651    }
652
653    /**
654     * Core switched their `display_tablenav()` method to protected, so we can't access it directly.
655     * Instead, let's include an access function to make it doable without errors!
656     *
657     * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622
658     *
659     * @param string $which Which nav table to display.
660     * @return mixed
661     */
662    public function unprotected_display_tablenav( $which = 'top' ) {
663        return $this->display_tablenav( $which );
664    }
665}