Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 206
0.00% covered (danger)
0.00%
0 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Modules_List_Table
0.00% covered (danger)
0.00%
0 / 202
0.00% covered (danger)
0.00%
0 / 19
2256
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 1
20
 js_templates
0.00% covered (danger)
0.00%
0 / 19
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
 single_row
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 get_table_classes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 column_cb
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 column_icon
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 column_name
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
30
 column_description
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 column_module_tags
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 column_default
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 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    /** Constructor. */
24    public function __construct() {
25        parent::__construct();
26
27        Jetpack::init();
28
29        if ( $this->compat_fields && is_array( $this->compat_fields ) ) {
30            array_push( $this->compat_fields, 'all_items' );
31        }
32
33        /**
34         * Filters the list of modules available to be displayed in the Jetpack Settings screen.
35         *
36         * @since 3.0.0
37         *
38         * @param array $modules Array of Jetpack modules.
39         */
40        $this->all_items       = apply_filters( 'jetpack_modules_list_table_items', Jetpack_Admin::init()->get_modules() );
41        $this->items           = $this->all_items;
42        $this->items           = $this->filter_displayed_table_items( $this->items );
43        $this->_column_headers = array( $this->get_columns(), array(), array(), 'name' );
44        // 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()`.
45        $modal_info = isset( $_GET['info'] ) ? wp_unslash( $_GET['info'] ) : false;
46
47        // Adding in a hidden h1 heading for screen-readers.
48        ?>
49        <h1 class="screen-reader-text"><?php esc_html_e( 'Jetpack Modules List', 'jetpack' ); ?></h1>
50        <?php
51
52        wp_register_script(
53            'models.jetpack-modules',
54            Assets::get_file_url_for_environment(
55                '_inc/build/jetpack-modules.models.min.js',
56                '_inc/jetpack-modules.models.js'
57            ),
58            array( 'jquery', 'backbone' ),
59            JETPACK__VERSION,
60            false // @todo Can this be put in the footer?
61        );
62        wp_register_script(
63            'views.jetpack-modules',
64            Assets::get_file_url_for_environment(
65                '_inc/build/jetpack-modules.views.min.js',
66                '_inc/jetpack-modules.views.js'
67            ),
68            array( 'jquery', 'backbone', 'wp-util' ),
69            JETPACK__VERSION,
70            false // @todo Can this be put in the footer?
71        );
72        wp_register_script(
73            'jetpack-modules-list-table',
74            Assets::get_file_url_for_environment(
75                '_inc/build/jetpack-modules.min.js',
76                '_inc/jetpack-modules.js'
77            ),
78            array(
79                'views.jetpack-modules',
80                'models.jetpack-modules',
81                'jquery',
82            ),
83            JETPACK__VERSION,
84            true
85        );
86
87        wp_localize_script(
88            'jetpack-modules-list-table',
89            'jetpackModulesData',
90            array(
91                'modules'   => Jetpack::get_translated_modules( $this->all_items ),
92                'i18n'      => array(
93                    'search_placeholder' => __( 'Search modules…', 'jetpack' ),
94                ),
95                'modalinfo' => $this->module_info_check( $modal_info, $this->all_items ),
96                'nonces'    => array(
97                    'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ),
98                ),
99            )
100        );
101
102        wp_enqueue_script( 'jetpack-modules-list-table' );
103
104        /**
105         * Filters the js_templates callback value.
106         *
107         * @since 3.6.0
108         *
109         * @param array array( $this, 'js_templates' ) js_templates callback.
110         */
111        add_action( 'admin_footer', apply_filters( 'jetpack_modules_list_table_js_template_callback', array( $this, 'js_templates' ) ), 9 );
112    }
113
114    /**
115     * Output row template.
116     */
117    public function js_templates() {
118        ?>
119        <script type="text/html" id="tmpl-Jetpack_Modules_List_Table_Template">
120            <# var i = 0;
121            if ( data.items.length ) {
122            _.each( data.items, function( item, key, list ) {
123                if ( item === undefined ) return; #>
124                <tr class="jetpack-module <# if ( ++i % 2 ) { #> alternate<# } #><# if ( item.activated ) { #> active<# } #><# if ( ! item.available ) { #> unavailable<# } #>" id="{{{ item.module }}}">
125                    <th scope="row" class="check-column">
126                        <input type="checkbox" name="modules[]" value="{{{ item.module }}}" {{{ item.disabled }}} />
127                    </th>
128                    <td class='name column-name'>
129                        <p class='info'><a href="{{{item.learn_more_button}}}" target="blank" style="text-decoration: none;">{{{ item.name }}}</a></p>
130                        <div class="row-actions">
131                        <# if ( item.configurable ) { #>
132                            <span class='configure'>{{{ item.configurable }}}</span>
133                        <# } #>
134                        <# if ( item.activated && 'vaultpress' !== item.module && item.available ) { #>
135                            <span class='delete'><a class="dops-button is-compact" href="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>?page=jetpack&#038;action=deactivate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.deactivate_nonce }}}"><?php esc_html_e( 'Deactivate', 'jetpack' ); ?></a></span>
136                        <# } else if ( item.available ) { #>
137                            <span class='activate'><a class="dops-button is-compact" href="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>?page=jetpack&#038;action=activate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.activate_nonce }}}"><?php esc_html_e( 'Activate', 'jetpack' ); ?></a></span>
138                        <# } #>
139                        <# if ( ! item.available ) { #>
140                            <p class='unavailable_reason'>{{{ item.unavailable_reason }}}</p>
141                        <# } #>
142                        </div>
143                    </td>
144                </tr>
145                <#
146            });
147            } else {
148                #>
149                <tr class="no-modules-found">
150                    <td colspan="2"><?php esc_html_e( 'No Modules Found', 'jetpack' ); ?></td>
151                </tr>
152                <#
153            }
154            #>
155        </script>
156        <?php
157    }
158
159    /**
160     * Get views data.
161     *
162     * @return array Maps identifier to display HTML.
163     */
164    public function get_views() {
165        /** This filter is already documented in class.jetpack-modules-list-table.php */
166        $modules              = apply_filters( 'jetpack_modules_list_table_items', Jetpack_Admin::init()->get_modules() );
167        $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' );
168        $module_tags          = array_merge( ...array_values( $array_of_module_tags ) );
169        $module_tags          = array_map( 'jetpack_get_module_i18n_tag', $module_tags );
170        $module_tags_unique   = array_count_values( $module_tags );
171        ksort( $module_tags_unique );
172
173        $format = '<a href="%3$s" %4$s data-title="%1$s">%1$s</a> <span class="count">(%2$s)</span>';
174        $title  = __( 'All', 'jetpack' );
175        $count  = is_countable( $modules ) ? count( $modules ) : 0;
176        $url    = esc_url( remove_query_arg( 'module_tag' ) );
177        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
178        $views = array(
179            'all' => sprintf( $format, $title, $count, $url, 'class="all"' ),
180        );
181        foreach ( $module_tags_unique as $title => $count ) {
182            $key           = sanitize_title( $title );
183            $display_title = esc_html( wptexturize( $title ) );
184            $url           = esc_url( add_query_arg( 'module_tag', rawurlencode( $title ) ) );
185            $views[ $key ] = sprintf( $format, $display_title, $count, $url, '' );
186        }
187        return $views;
188    }
189
190    /**
191     * Output views HTML.
192     */
193    public function views() {
194        $views = $this->get_views();
195
196        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
197        $module_tag = empty( $_GET['module_tag'] ) ? 'all' : sanitize_title( wp_unslash( $_GET['module_tag'] ) );
198
199        echo "<ul class='subsubsub'>\n";
200        foreach ( $views as $class => $view ) {
201            $class_name = $class;
202            if ( $class === $module_tag ) {
203                $class_name .= ' current';
204            }
205
206            $views[ $class ] = "\t<li class='$class_name'>$view</li>";
207        }
208        echo implode( "\n", $views ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Is HTML. Escaping happens in get_views().
209        echo '</ul>';
210    }
211
212    /**
213     * Filter a modules array for displayed items.
214     *
215     * @param array $modules Modules.
216     * @return array Displayed modules.
217     */
218    public function filter_displayed_table_items( $modules ) {
219        return array_filter( $modules, array( $this, 'is_module_displayed' ) );
220    }
221
222    /**
223     * Determine if a module is displayed.
224     *
225     * @param array $module Module data.
226     * @return bool
227     */
228    public static function is_module_displayed( $module ) {
229        // Handle module tag based filtering.
230        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
231        if ( ! empty( $_REQUEST['module_tag'] ) ) {
232            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a view, not a model or controller.
233            $module_tag = sanitize_text_field( wp_unslash( $_REQUEST['module_tag'] ) );
234            if ( ! in_array( $module_tag, array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ), true ) ) {
235                return false;
236            }
237        }
238
239        // If nothing rejected it, include it!
240        return true;
241    }
242
243    /**
244     * Sort callback to put modules with `requires_connection` last.
245     *
246     * @param array $module1 Module data.
247     * @param array $module2 Module data.
248     * @return int Indicating the relative ordering of module1 and module2.
249     */
250    public static function sort_requires_connection_last( $module1, $module2 ) {
251        if ( (bool) $module1['requires_connection'] === (bool) $module2['requires_connection'] ) {
252            return 0;
253        }
254        if ( $module1['requires_connection'] ) {
255            return 1;
256        }
257        if ( $module2['requires_connection'] ) {
258            return -1;
259        }
260
261        return 0;
262    }
263
264    /**
265     * Get table columns.
266     *
267     * @return string[] Column name to header HTML.
268     */
269    public function get_columns() {
270        $columns = array(
271            'cb'   => '<input type="checkbox" />',
272            'name' => __( 'Name', 'jetpack' ),
273        );
274        return $columns;
275    }
276
277    /**
278     * Get bulk actions for the table.
279     *
280     * @return string[] Actions, code => text.
281     */
282    public function get_bulk_actions() {
283        $actions = array(
284            'bulk-activate'   => __( 'Activate', 'jetpack' ),
285            'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),
286        );
287        return $actions;
288    }
289
290    /**
291     * Print a single row of the table.
292     *
293     * @param object|array $item Item.
294     */
295    public function single_row( $item ) {
296        static $i  = 0;
297        $row_class = ( ( ++$i ) % 2 ) ? ' alternate' : '';
298
299        if ( ! empty( $item['activated'] ) ) {
300            $row_class .= ' active';
301        }
302
303        if ( ! Jetpack_Admin::is_module_available( $item ) ) {
304            $row_class .= ' unavailable';
305        }
306
307        echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">';
308        $this->single_row_columns( $item );
309        echo '</tr>';
310    }
311
312    /**
313     * Table classes.
314     *
315     * @return string[] HTML.
316     */
317    public function get_table_classes() {
318        return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed' );
319    }
320
321    /**
322     * Column checkbox.
323     *
324     * @param object|array $item Item.
325     * @return string HTML.
326     */
327    public function column_cb( $item ) {
328        if ( ! Jetpack_Admin::is_module_available( $item ) ) {
329            return '';
330        }
331
332        return sprintf( '<input type="checkbox" name="modules[]" value="%s" />', $item['module'] );
333    }
334
335    /**
336     * Column icon.
337     *
338     * @return string HTML.
339     */
340    public function column_icon() {
341        $badge_text = '';
342        $free_text  = '';
343        ob_start();
344        ?>
345        <a href="#TB_inline?width=600&height=550&inlineId=more-info-module-settings-modal" class="thickbox">
346            <div class="module-image">
347                <p><span class="module-image-badge"><?php echo esc_html( $badge_text ); ?></span><span class="module-image-free" style="display: none"><?php echo esc_html( $free_text ); ?></span></p>
348            </div>
349        </a>
350        <?php
351        return ob_get_clean();
352    }
353
354    /**
355     * Column name.
356     *
357     * @param object|array $item Item.
358     * @return string HTML.
359     */
360    public function column_name( $item ) {
361        $actions = array(
362            'info' => sprintf( '<a href="%s" target="blank">%s</a>', esc_url( $item['learn_more_button'] ), esc_html__( 'Feature Info', 'jetpack' ) ),
363        );
364
365        if ( ! empty( $item['configurable'] ) ) {
366            $actions['configure'] = $item['configurable'];
367        }
368
369        if ( empty( $item['activated'] ) && Jetpack_Admin::is_module_available( $item ) ) {
370            $url                 = wp_nonce_url(
371                Jetpack::admin_url(
372                    array(
373                        'page'   => 'jetpack',
374                        'action' => 'activate',
375                        'module' => $item['module'],
376                    )
377                ),
378                'jetpack_activate-' . $item['module']
379            );
380            $actions['activate'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Activate', 'jetpack' ) );
381        } elseif ( ! empty( $item['activated'] ) ) {
382            $url               = wp_nonce_url(
383                Jetpack::admin_url(
384                    array(
385                        'page'   => 'jetpack',
386                        'action' => 'deactivate',
387                        'module' => $item['module'],
388                    )
389                ),
390                'jetpack_deactivate-' . $item['module']
391            );
392            $actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Deactivate', 'jetpack' ) );
393        }
394
395        return $this->row_actions( $actions ) . wptexturize( $item['name'] );
396    }
397
398    /**
399     * Column description.
400     *
401     * @param object|array $item Item.
402     * @return string HTML.
403     */
404    public function column_description( $item ) {
405        ob_start();
406        // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
407        /** This action is documented in class.jetpack-admin.php */
408        echo apply_filters( 'jetpack_short_module_description', $item['description'], $item['module'] );
409        /** This action is documented in class.jetpack-admin.php */
410        do_action( 'jetpack_learn_more_button_' . $item['module'] );
411        echo '<div id="more-info-' . $item['module'] . '" class="more-info">';
412        /** This action is documented in class.jetpack-admin.php */
413        do_action( 'jetpack_module_more_info_' . $item['module'] );
414        echo '</div>';
415        // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
416        return ob_get_clean();
417    }
418
419    /**
420     * Return module tags HTML.
421     *
422     * @param object|array $item Item.
423     * @return string HTML.
424     */
425    public function column_module_tags( $item ) {
426        $module_tags = array();
427        foreach ( array_map( 'jetpack_get_module_i18n_tag', $item['module_tags'] ) as $module_tag ) {
428            $module_tags[] = sprintf( '<a href="%3$s" data-title="%2$s">%1$s</a>', esc_html( $module_tag ), esc_attr( $module_tag ), esc_url( add_query_arg( 'module_tag', rawurlencode( $module_tag ) ) ) );
429        }
430        return implode( ', ', $module_tags );
431    }
432
433    /**
434     * Column default value.
435     *
436     * @param object|array $item Item.
437     * @param string       $column_name Column name.
438     * @return string
439     */
440    public function column_default( $item, $column_name ) {
441        switch ( $column_name ) {
442            case 'icon':
443            case 'name':
444            case 'description':
445                return '';
446            default:
447                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
448                return print_r( $item, true );
449        }
450    }
451
452    /**
453     * Check if the info parameter provided in the URL corresponds to an actual module.
454     *
455     * @param string|false $info Info parameter.
456     * @param array        $modules Modules array.
457     * @return string|false
458     */
459    public function module_info_check( $info, $modules ) {
460        if ( ! $info ) {
461            return false;
462        } elseif ( array_key_exists( $info, $modules ) ) {
463            return $info;
464        }
465    }
466
467    /**
468     * Core switched their `display_tablenav()` method to protected, so we can't access it directly.
469     * Instead, let's include an access function to make it doable without errors!
470     *
471     * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622
472     *
473     * @param string $which Which nav table to display.
474     * @return mixed
475     */
476    public function unprotected_display_tablenav( $which = 'top' ) {
477        return $this->display_tablenav( $which );
478    }
479}