Completed
Push — update/aag-views ( bf67f2...b39db8 )
by
unknown
24:45 queued 15:23
created

Jetpack_Modules_List_Table::get_views()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 32
rs 5.3846
1
<?php
2
3
if ( ! class_exists( 'WP_List_Table' ) )
4
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
5
6
class Jetpack_Modules_List_Table extends WP_List_Table {
7
8
	function __construct() {
9
		parent::__construct();
10
11
		Jetpack::init();
12
13
		// In WP 4.2 WP_List_Table will be sanitizing which values are __set()
14
		global $wp_version;
15
		if ( version_compare( $wp_version, '4.2-z', '>=' ) && $this->compat_fields && is_array( $this->compat_fields ) ) {
16
			array_push( $this->compat_fields, 'all_items' );
17
		}
18
19
		$this->items = $this->all_items = Jetpack_Admin::init()->get_modules();
20
		$this->items = $this->filter_displayed_table_items( $this->items );
21
		/**
22
		 * Filters the list of modules available to be displayed in the Jetpack Settings screen.
23
		 *
24
		 * @since 3.0.0
25
		 *
26
		 * @param array $this->items Array of Jetpack modules.
27
		 */
28
		$this->items = apply_filters( 'jetpack_modules_list_table_items', $this->items );
29
		$this->_column_headers = array( $this->get_columns(), array(), array(), 'name' );
30
		$modal_info = isset( $_GET['info'] ) ? $_GET['info'] : false;
31
32
		wp_register_script(
33
			'models.jetpack-modules',
34
			plugins_url( '_inc/jetpack-modules.models.js', JETPACK__PLUGIN_FILE ),
35
			array( 'backbone', 'underscore' ),
36
			JETPACK__VERSION
37
		);
38
		wp_register_script(
39
			'views.jetpack-modules',
40
			plugins_url( '_inc/jetpack-modules.views.js', JETPACK__PLUGIN_FILE ),
41
			array( 'backbone', 'underscore', 'wp-util' ),
42
			JETPACK__VERSION
43
		);
44
		wp_register_script(
45
			'jetpack-modules-list-table',
46
			plugins_url( '_inc/jetpack-modules.js', JETPACK__PLUGIN_FILE ),
47
			array(
48
				'views.jetpack-modules',
49
				'models.jetpack-modules',
50
				'jquery',
51
			),
52
			JETPACK__VERSION,
53
			true
54
		);
55
56
		wp_localize_script( 'jetpack-modules-list-table', 'jetpackModulesData', array(
57
			'modules' => Jetpack::get_translated_modules( $this->all_items ),
58
			'i18n'    => array(
59
				'search_placeholder' => __( 'Search Modules…', 'jetpack' ),
60
			),
61
			'modalinfo' => $this->module_info_check( $modal_info, $this->all_items ),
62
			'nonces'  => array(
63
				'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ),
64
			),
65
			'coreIconAvailable' => Jetpack::jetpack_site_icon_available_in_core(),
66
		) );
67
68
		wp_enqueue_script( 'jetpack-modules-list-table' );
69
70
		/**
71
		 * Filters the js_templates callback value.
72
		 *
73
		 * @since 3.6.0
74
		 *
75
		 * @param array array( $this, 'js_templates' ) js_templates callback.
76
		 */
77
		add_action( 'admin_footer', apply_filters( 'jetpack_modules_list_table_js_template_callback', array( $this, 'js_templates' ) ), 9 );
78
	}
79
80
	function js_templates() {
81
		?>
82
		<script type="text/html" id="tmpl-Jetpack_Modules_List_Table_Template">
83
			<# var i = 0;
84
			if ( data.items.length ) {
85
			_.each( data.items, function( item, key, list ) {
86
				if ( item === undefined ) return;
87
				if ( jetpackModulesData.coreIconAvailable && 'site-icon' == item.module ) { #>
88
				<tr class="jetpack-module deprecated <# if ( ++i % 2 ) { #> alternate<# } #>" id="site-icon-deprecated">
89
					<th scope="row" class="check-column">
90
					<input type="checkbox" name="modules[]" value="{{{ item.module }}}" disabled />
91
					</th>
92
					<td class='name column-name'>
93
						<span class='info'>{{{ item.name }}}</span>
94
						<div class="row-actions">
95
							<span class="dep-msg"><?php _ex( 'WordPress now has Site Icon built in!', '"Site Icon" is the feature name.', 'jetpack' ); ?></span>
96
							<span class='configure'><a href="<?php esc_html_e( admin_url( 'customize.php?autofocus[control]=site_icon' ), 'jetpack' ); ?>"><?php _e( 'Configure' , 'jetpack' ); ?></a></span>
97
						</div>
98
					</td>
99
				</tr>
100
				<# return; } #>
101
				<tr class="jetpack-module <# if ( ++i % 2 ) { #> alternate<# } #><# if ( item.activated ) { #> active<# } #><# if ( ! item.available ) { #> unavailable<# } #>" id="{{{ item.module }}}">
102
					<th scope="row" class="check-column">
103
						<input type="checkbox" name="modules[]" value="{{{ item.module }}}" />
104
					</th>
105
					<td class='name column-name'>
106
						<span class='info'><a href="{{{item.learn_more_button}}}" target="blank">{{{ item.name }}}</a></span>
107
						<div class="row-actions">
108
						<# if ( item.configurable ) { #>
109
							<span class='configure'>{{{ item.configurable }}}</span>
110
						<# } #>
111
						<# if ( item.activated && 'vaultpress' !== item.module && item.available ) { #>
112
							<span class='delete'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&#038;action=deactivate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.deactivate_nonce }}}"><?php _e( 'Deactivate', 'jetpack' ); ?></a></span>
113
						<# } else if ( item.available ) { #>
114
							<span class='activate'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&#038;action=activate&#038;module={{{ item.module }}}&#038;_wpnonce={{{ item.activate_nonce }}}"><?php _e( 'Activate', 'jetpack' ); ?></a></span>
115
						<# } #>
116
						</div>
117
					</td>
118
				</tr>
119
				<#
120
			});
121
			} else {
122
				#>
123
				<tr class="no-modules-found">
124
					<td colspan="2"><?php esc_html_e( 'No Modules Found' , 'jetpack' ); ?></td>
125
				</tr>
126
				<#
127
			}
128
			#>
129
		</script>
130
		<?php
131
	}
132
133
	function get_views() {
134
		$modules              = Jetpack_Admin::init()->get_modules();
135
		$array_of_module_tags = wp_list_pluck( $modules, 'module_tags' );
136
		$module_tags          = call_user_func_array( 'array_merge', $array_of_module_tags );
137
		$module_tags_unique   = array_count_values( $module_tags );
138
		ksort( $module_tags_unique );
139
140
		$format  = '<a href="%3$s"%4$s data-title="%1$s">%1$s <span class="count">(%2$s)</span></a>';
141
		$title   = __( 'All', 'jetpack' );
142
		$count   = count( $modules );
143
		$url     = esc_url( remove_query_arg( 'module_tag' ) );
144
		$current = empty( $_GET['module_tag'] ) ? ' class="current all"' : ' class="all"';
145
		$views   = array(
146
			'all' => sprintf( $format, $title, $count, $url, $current ),
147
		);
148
		foreach ( $module_tags_unique as $title => $count ) {
149
			if ( 'Jumpstart' == $title ) {
150
				continue;
151
			}
152
			$key           = sanitize_title( $title );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 11 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
153
			if ( 'centralized-management' === $key && Jetpack::is_module_active( 'manage' ) ) {
154
				continue;
155
			}
156
			$display_title = esc_html( wptexturize( $title ) );
157
			$url           = esc_url( add_query_arg( 'module_tag', urlencode( $title ) ) );
158
			$current       = '';
159
			if ( ! empty( $_GET['module_tag'] ) && $title == $_GET['module_tag'] )
160
				$current   = ' class="current"';
161
			$views[ $key ] = sprintf( $format, $display_title, $count, $url, $current );
162
		}
163
		return $views;
164
	}
165
166
	function views() {
167
		$views = $this->get_views();
168
169
		echo "<ul class='subsubsub'>\n";
170
		foreach ( $views as $class => $view ) {
171
			$views[ $class ] = "\t<li class='$class'>$view</li>";
172
		}
173
		echo implode( "\n", $views ) . "\n";
174
		echo "</ul>";
175
	}
176
177
	function filter_displayed_table_items( $modules ) {
178
		return array_filter( $modules, array( $this, 'is_module_displayed' ) );
179
	}
180
181
	static function is_module_available( $module ) {
182
		if ( ! is_array( $module ) || empty( $module ) )
183
			return false;
184
185
		if ( Jetpack::is_development_mode() ) {
186
			return ! ( $module['requires_connection'] );
187
		} else {
188
			return Jetpack::is_active();
189
		}
190
	}
191
192
	static function is_module_displayed( $module ) {
193
		// Handle module tag based filtering.
194
		if ( ! empty( $_REQUEST['module_tag'] ) ) {
195
			$module_tag = sanitize_text_field( $_REQUEST['module_tag'] );
196
			if ( ! in_array( $module_tag, $module['module_tags'] ) )
197
				return false;
198
		}
199
200
		// If nothing rejected it, include it!
201
		return true;
202
	}
203
204 View Code Duplication
	static function sort_requires_connection_last( $module1, $module2 ) {
205
		if ( $module1['requires_connection'] == $module2['requires_connection'] )
206
			return 0;
207
		if ( $module1['requires_connection'] )
208
			return 1;
209
		if ( $module2['requires_connection'] )
210
			return -1;
211
212
		return 0;
213
	}
214
215
	function get_columns() {
216
		$columns = array(
217
			'cb'          => '<input type="checkbox" />',
218
			'name'        => __( 'Name', 'jetpack' ),
219
		);
220
		return $columns;
221
	}
222
223
	function get_bulk_actions() {
224
		$actions = array(
225
			'bulk-activate'   => __( 'Activate',   'jetpack' ),
226
			'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),
227
		);
228
		return $actions;
229
	}
230
231
	function single_row( $item ) {
232
		static $i = 0;
233
		$row_class = ( ++$i % 2 ) ? ' alternate' : '';
234
235
		if ( ! empty( $item['activated'] )  )
236
			$row_class .= ' active';
237
238
		if ( ! $this->is_module_available( $item ) )
239
			$row_class .= ' unavailable';
240
241
		echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">';
242
		$this->single_row_columns( $item );
243
		echo '</tr>';
244
	}
245
246
	function get_table_classes() {
247
		return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed', 'jetpack-modules' );
248
	}
249
250
	function column_cb( $item ) {
251
		if ( ! $this->is_module_available( $item ) )
252
			return '';
253
254
		return sprintf( '<input type="checkbox" name="modules[]" value="%s" />', $item['module'] );
255
	}
256
257
	function column_icon( $item ) {
258
		$badge_text = $free_text = '';
259
		ob_start();
260
		?>
261
		<a href="#TB_inline?width=600&height=550&inlineId=more-info-module-settings-modal" class="thickbox">
262
			<div class="module-image">
263
				<p><span class="module-image-badge"><?php echo $badge_text; ?></span><span class="module-image-free" style="display: none"><?php echo $free_text; ?></span></p>
264
			</div>
265
		</a>
266
		<?php
267
		return ob_get_clean();
268
269
	}
270
271
	function column_name( $item ) {
272
		$actions = array(
273
			'info' => sprintf( '<a href="%s" target="blank">%s</a>', esc_url( $item['learn_more_button'] ), esc_html__( 'Feature Info', 'jetpack' ) ),
274
		);
275
276
		if ( ! empty( $item['configurable'] ) ) {
277
			$actions['configure'] = $item['configurable'];
278
		}
279
280
		if ( empty( $item['activated'] ) && $this->is_module_available( $item ) ) {
281
			$url = wp_nonce_url(
282
				Jetpack::admin_url( array(
283
					'page'   => 'jetpack',
284
					'action' => 'activate',
285
					'module' => $item['module'],
286
				) ),
287
				'jetpack_activate-' . $item['module']
288
			);
289
			$actions['activate'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Activate', 'jetpack' ) );
290
		} elseif ( ! empty( $item['activated'] ) ) {
291
			$url = wp_nonce_url(
292
				Jetpack::admin_url( array(
293
					'page'   => 'jetpack',
294
					'action' => 'deactivate',
295
					'module' => $item['module'],
296
				) ),
297
				'jetpack_deactivate-' . $item['module']
298
			);
299
			$actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Deactivate', 'jetpack' ) );
300
		}
301
302
		return $this->row_actions( $actions ) . wptexturize( $item['name'] );
303
	}
304
305
	function column_description( $item ) {
306
		ob_start();
307
		/** This action is documented in class.jetpack-admin.php */
308
		echo apply_filters( 'jetpack_short_module_description', $item['description'], $item['module'] );
309
		/** This action is documented in class.jetpack-admin.php */
310
		do_action( 'jetpack_learn_more_button_' . $item['module'] );
311
		echo '<div id="more-info-' . $item['module'] . '" class="more-info">';
312
		/** This action is documented in class.jetpack-admin.php */
313
		do_action( 'jetpack_module_more_info_' . $item['module'] );
314
		echo '</div>';
315
		return ob_get_clean();
316
	}
317
318
	function column_module_tags( $item ) {
319
		$module_tags = array();
320
		foreach( $item['module_tags'] as $module_tag ) {
321
			$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', urlencode( $module_tag ) ) ) );
322
		}
323
		return implode( ', ', $module_tags );
324
	}
325
326
	function column_default( $item, $column_name ) {
327
		switch ( $column_name ) {
328
			case 'icon':
329
			case 'name':
330
			case 'description':
331
				break;
332
			default:
333
				return print_r( $item, true );
334
		}
335
	}
336
337
	//Check if the info parameter provided in the URL corresponds to an actual module
338
	function module_info_check( $info = false, $modules ) {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
339
		if ( false == $info ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
340
			return false;
341
		} else if ( array_key_exists( $info, $modules ) ) {
342
			return $info;
343
		}
344
	}
345
346
	/**
347
	 * Core switched their `display_tablenav()` method to protected, so we can't access it directly.
348
	 * Instead, let's include an access function to make it doable without errors!
349
	 *
350
	 * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622
351
	 *
352
	 * @param string $which
353
	 *
354
	 * @return mixed
355
	 */
356
	function unprotected_display_tablenav( $which = 'top' ) {
357
		return $this->display_tablenav( $which );
358
	}
359
360
}
361