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