Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.64% covered (danger)
0.64%
2 / 312
0.00% covered (danger)
0.00%
0 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Modules_List_Table
0.00% covered (danger)
0.00%
0 / 308
0.00% covered (danger)
0.00%
0 / 21
4422
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 / 29
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
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 filter_displayed_table_items
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_module_displayed
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 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 / 52
0.00% covered (danger)
0.00%
0 / 1
342
 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 ) { #><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                            </div>
270                            <div class="jetpack-module__meta">
271                                <span class="jetpack-module__badges">
272                                    <# if ( item.recommended && item.available ) { #><span class="jetpack-module__badge is-recommended"><?php esc_html_e( 'Recommended', 'jetpack' ); ?></span><# } #>
273                                    <# if ( item.available ) { #>
274                                        <# 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><# } #>
275                                    <# } #>
276                                </span>
277                                <div class="row-actions">
278                                    <# if ( item.configurable ) { #>
279                                        <span class="configure">{{{ item.configurable }}}</span>
280                                    <# } #>
281                                    <# if ( item.available && item.protect_page_url ) { #>
282                                        <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>
283                                    <# } else if ( item.available ) { #>
284                                        <# if ( item.activated ) { #>
285                                            <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>
286                                        <# } else { #>
287                                            <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>
288                                        <# } #>
289                                    <# } else { #>
290                                        <span class="jetpack-module__unavailable">{{ item.unavailable_reason }}</span>
291                                    <# } #>
292                                </div>
293                            </div>
294                        </div>
295                    </td>
296                </tr>
297                <#
298            });
299            } else {
300                #>
301                <tr class="no-modules-found">
302                    <td colspan="2"><?php esc_html_e( 'No Modules Found', 'jetpack' ); ?></td>
303                </tr>
304                <#
305            }
306            #>
307        </script>
308        <?php
309    }
310
311    /**
312     * Get views data.
313     *
314     * @return array Maps identifier to display HTML.
315     */
316    public function get_views() {
317        /** This filter is already documented in class.jetpack-modules-list-table.php */
318        $modules              = apply_filters( 'jetpack_modules_list_table_items', Jetpack_Admin::init()->get_modules() );
319        $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' );
320        $module_tags          = array_merge( ...array_values( $array_of_module_tags ) );
321        $module_tags          = array_map( 'jetpack_get_module_i18n_tag', $module_tags );
322        $module_tags_unique   = array_count_values( $module_tags );
323        ksort( $module_tags_unique );
324
325        $format = '<a href="%3$s" %4$s data-title="%1$s">%1$s</a> <span class="count">(%2$s)</span>';
326        $title  = __( 'All', 'jetpack' );
327        $count  = is_countable( $modules ) ? count( $modules ) : 0;
328        $url    = esc_url( remove_query_arg( 'module_tag' ) );
329        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
330        $views = array(
331            'all' => sprintf( $format, $title, $count, $url, 'class="all"' ),
332        );
333        foreach ( $module_tags_unique as $title => $count ) {
334            $key           = sanitize_title( $title );
335            $display_title = esc_html( wptexturize( $title ) );
336            $url           = esc_url( add_query_arg( 'module_tag', rawurlencode( $title ) ) );
337            $views[ $key ] = sprintf( $format, $display_title, $count, $url, '' );
338        }
339        return $views;
340    }
341
342    /**
343     * Output views HTML.
344     */
345    public function views() {
346        $views = $this->get_views();
347
348        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
349        $module_tag = empty( $_GET['module_tag'] ) ? 'all' : sanitize_title( wp_unslash( $_GET['module_tag'] ) );
350
351        echo "<ul class='subsubsub'>\n";
352        foreach ( $views as $class => $view ) {
353            $class_name = $class;
354            if ( $class === $module_tag ) {
355                $class_name .= ' current';
356            }
357
358            $views[ $class ] = "\t<li class='$class_name'>$view</li>";
359        }
360        echo implode( "\n", $views ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Is HTML. Escaping happens in get_views().
361        echo '</ul>';
362    }
363
364    /**
365     * Filter a modules array for displayed items.
366     *
367     * @param array $modules Modules.
368     * @return array Displayed modules.
369     */
370    public function filter_displayed_table_items( $modules ) {
371        return array_filter( $modules, array( $this, 'is_module_displayed' ) );
372    }
373
374    /**
375     * Determine if a module is displayed.
376     *
377     * @param array $module Module data.
378     * @return bool
379     */
380    public static function is_module_displayed( $module ) {
381        // Handle module tag based filtering.
382        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
383        if ( ! empty( $_REQUEST['module_tag'] ) ) {
384            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
385            $module_tag = sanitize_text_field( wp_unslash( $_REQUEST['module_tag'] ) );
386            if ( ! in_array( $module_tag, array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ), true ) ) {
387                return false;
388            }
389        }
390
391        // If nothing rejected it, include it!
392        return true;
393    }
394
395    /**
396     * Sort callback to put modules with `requires_connection` last.
397     *
398     * @param array $module1 Module data.
399     * @param array $module2 Module data.
400     * @return int Indicating the relative ordering of module1 and module2.
401     */
402    public static function sort_requires_connection_last( $module1, $module2 ) {
403        if ( (bool) $module1['requires_connection'] === (bool) $module2['requires_connection'] ) {
404            return 0;
405        }
406        if ( $module1['requires_connection'] ) {
407            return 1;
408        }
409        if ( $module2['requires_connection'] ) {
410            return -1;
411        }
412
413        return 0;
414    }
415
416    /**
417     * Get table columns.
418     *
419     * @return string[] Column name to header HTML.
420     */
421    public function get_columns() {
422        $columns = array(
423            'cb'   => '<input type="checkbox" />',
424            'name' => __( 'Name', 'jetpack' ),
425        );
426        return $columns;
427    }
428
429    /**
430     * Get bulk actions for the table.
431     *
432     * @return string[] Actions, code => text.
433     */
434    public function get_bulk_actions() {
435        $actions = array(
436            'bulk-activate'   => __( 'Activate', 'jetpack' ),
437            'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),
438        );
439        return $actions;
440    }
441
442    /**
443     * Print the module rows grouped by product group.
444     *
445     * Overrides WP_List_Table::display_rows() so the server-rendered (no-JS)
446     * output matches the grouped, card-style layout produced by the Backbone
447     * template.
448     */
449    public function display_rows() {
450        $buckets = array();
451        foreach ( self::PRODUCT_GROUP_ORDER as $group ) {
452            $buckets[ $group ] = array();
453        }
454
455        foreach ( $this->items as $item ) {
456            $group               = self::get_module_product_group( $item['module'] );
457            $buckets[ $group ][] = $item;
458        }
459
460        foreach ( $buckets as $group => $items ) {
461            if ( empty( $items ) ) {
462                continue;
463            }
464            $this->group_header_row( $group );
465            foreach ( $items as $item ) {
466                $this->single_row( $item );
467            }
468        }
469    }
470
471    /**
472     * Print a product group header row.
473     *
474     * @param string $group Product group slug.
475     */
476    protected function group_header_row( $group ) {
477        printf(
478            '<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>',
479            esc_attr( $group ),
480            esc_html( self::get_product_group_name( $group ) )
481        );
482    }
483
484    /**
485     * Print a single row of the table.
486     *
487     * @param object|array $item Item.
488     */
489    public function single_row( $item ) {
490        static $i  = 0;
491        $row_class = ( ( ++$i ) % 2 ) ? ' alternate' : '';
492
493        $available = Jetpack_Admin::is_module_available( $item );
494
495        if ( ! empty( $item['activated'] ) ) {
496            $row_class .= ' active';
497        }
498        if ( ! $available ) {
499            $row_class .= ' unavailable';
500        }
501
502        echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">';
503
504        echo '<th scope="row" class="check-column">';
505        // Disabled, not dropped, so the row's name stays in line with the others.
506        if ( $available && ! empty( $item['protect_page_url'] ) ) {
507            echo '<input type="checkbox" disabled />';
508        } elseif ( $available ) {
509            printf( '<input type="checkbox" name="modules[]" value="%s" />', esc_attr( $item['module'] ) );
510        }
511        echo '</th>';
512
513        echo '<td class="name column-name">';
514        echo '<div class="jetpack-module__body">';
515
516        echo '<div class="jetpack-module__info">';
517        if ( ! empty( $item['learn_more_button'] ) ) {
518            printf(
519                '<a class="jetpack-module__name" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
520                esc_url( $item['learn_more_button'] ),
521                wp_kses_post( wptexturize( $item['name'] ) )
522            );
523        } else {
524            echo '<span class="jetpack-module__name">' . wp_kses_post( wptexturize( $item['name'] ) ) . '</span>';
525        }
526        if ( ! empty( $item['description'] ) ) {
527            echo '<span class="jetpack-module__description">' . esc_html( wp_strip_all_tags( $item['description'] ) ) . '</span>';
528        }
529        echo '</div>';
530
531        echo '<div class="jetpack-module__meta">';
532        echo '<span class="jetpack-module__badges">';
533        if ( $available && self::is_module_recommended( $item ) ) {
534            echo '<span class="jetpack-module__badge is-recommended">' . esc_html__( 'Recommended', 'jetpack' ) . '</span>';
535        }
536        if ( $available ) {
537            if ( ! empty( $item['activated'] ) ) {
538                echo '<span class="jetpack-module__badge is-active">' . esc_html__( 'Active', 'jetpack' ) . '</span>';
539            } else {
540                echo '<span class="jetpack-module__badge is-inactive">' . esc_html__( 'Inactive', 'jetpack' ) . '</span>';
541            }
542        }
543        echo '</span>';
544
545        echo '<div class="row-actions">';
546        if ( ! empty( $item['configurable'] ) ) {
547            echo '<span class="configure">' . wp_kses_post( $item['configurable'] ) . '</span>';
548        }
549        if ( $available && ! empty( $item['protect_page_url'] ) ) {
550            printf(
551                '<a class="button button-small jetpack-module__protect-link" href="%1$s">%2$s</a>',
552                esc_url( $item['protect_page_url'] ),
553                esc_html( _x( 'Manage in Protect', '"Protect" is a product name.', 'jetpack' ) )
554            );
555        } elseif ( $available ) {
556            echo $this->get_module_toggle_html( $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in helper.
557        } elseif ( ! empty( $item['unavailable_reason'] ) ) {
558            echo '<span class="jetpack-module__unavailable">' . wp_kses_post( $item['unavailable_reason'] ) . '</span>';
559        }
560        // Close .row-actions, .jetpack-module__meta, and .jetpack-module__body.
561        echo '</div></div></div>';
562        echo '</td>';
563        echo '</tr>';
564    }
565
566    /**
567     * Build the activate/deactivate toggle control markup for a module row.
568     *
569     * @param object|array $item Module data.
570     * @return string HTML.
571     */
572    protected function get_module_toggle_html( $item ) {
573        $is_active = ! empty( $item['activated'] );
574
575        if ( $is_active ) {
576            $url   = wp_nonce_url(
577                Jetpack::admin_url(
578                    array(
579                        'page'   => 'jetpack',
580                        'action' => 'deactivate',
581                        'module' => $item['module'],
582                    )
583                ),
584                'jetpack_deactivate-' . $item['module']
585            );
586            $label = sprintf(
587                /* translators: %s: Jetpack module name. */
588                __( 'Deactivate %s', 'jetpack' ),
589                $item['name']
590            );
591            $class = 'jetpack-module__toggle is-active';
592            $aria  = 'true';
593        } else {
594            $url   = wp_nonce_url(
595                Jetpack::admin_url(
596                    array(
597                        'page'   => 'jetpack',
598                        'action' => 'activate',
599                        'module' => $item['module'],
600                    )
601                ),
602                'jetpack_activate-' . $item['module']
603            );
604            $label = sprintf(
605                /* translators: %s: Jetpack module name. */
606                __( 'Activate %s', 'jetpack' ),
607                $item['name']
608            );
609            $class = 'jetpack-module__toggle';
610            $aria  = 'false';
611        }
612
613        return sprintf(
614            '<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>',
615            esc_attr( $class ),
616            esc_attr( $aria ),
617            esc_url( $url ),
618            esc_html( $label )
619        );
620    }
621
622    /**
623     * Table classes.
624     *
625     * @return string[] HTML.
626     */
627    public function get_table_classes() {
628        return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed' );
629    }
630
631    /**
632     * Check if the info parameter provided in the URL corresponds to an actual module.
633     *
634     * @param string|false $info Info parameter.
635     * @param array        $modules Modules array.
636     * @return string|false
637     */
638    public function module_info_check( $info, $modules ) {
639        if ( ! $info ) {
640            return false;
641        } elseif ( array_key_exists( $info, $modules ) ) {
642            return $info;
643        }
644    }
645
646    /**
647     * Core switched their `display_tablenav()` method to protected, so we can't access it directly.
648     * Instead, let's include an access function to make it doable without errors!
649     *
650     * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622
651     *
652     * @param string $which Which nav table to display.
653     * @return mixed
654     */
655    public function unprotected_display_tablenav( $which = 'top' ) {
656        return $this->display_tablenav( $which );
657    }
658}