Jetpack_Modules_List_Table

The Jetpack by WordPress.com Jetpack Modules List Table class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-modules-list-table.php  
  1. class Jetpack_Modules_List_Table extends WP_List_Table { 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. Jetpack::init(); 
  7.  
  8. // In WP 4.2 WP_List_Table will be sanitizing which values are __set() 
  9. global $wp_version; 
  10. if ( version_compare( $wp_version, '4.2-z', '>=' ) && $this->compat_fields && is_array( $this->compat_fields ) ) { 
  11. array_push( $this->compat_fields, 'all_items' ); 
  12.  
  13. $this->items = $this->all_items = Jetpack_Admin::init()->get_modules(); 
  14. $this->items = $this->filter_displayed_table_items( $this->items ); 
  15. /** 
  16. * Filters the list of modules available to be displayed in the Jetpack Settings screen. 
  17. * @since 3.0.0 
  18. * @param array $this->items Array of Jetpack modules. 
  19. */ 
  20. $this->items = apply_filters( 'jetpack_modules_list_table_items', $this->items ); 
  21. $this->_column_headers = array( $this->get_columns(), array(), array(), 'name' ); 
  22. $modal_info = isset( $_GET['info'] ) ? $_GET['info'] : false; 
  23.  
  24. wp_register_script( 
  25. 'models.jetpack-modules',  
  26. plugins_url( '_inc/jetpack-modules.models.js', JETPACK__PLUGIN_FILE ),  
  27. array( 'backbone', 'underscore' ),  
  28. JETPACK__VERSION 
  29. ); 
  30. wp_register_script( 
  31. 'views.jetpack-modules',  
  32. plugins_url( '_inc/jetpack-modules.views.js', JETPACK__PLUGIN_FILE ),  
  33. array( 'backbone', 'underscore', 'wp-util' ),  
  34. JETPACK__VERSION 
  35. ); 
  36. wp_register_script( 
  37. 'jetpack-modules-list-table',  
  38. plugins_url( '_inc/jetpack-modules.js', JETPACK__PLUGIN_FILE ),  
  39. array( 
  40. 'views.jetpack-modules',  
  41. 'models.jetpack-modules',  
  42. 'jquery',  
  43. ),  
  44. JETPACK__VERSION,  
  45. true 
  46. ); 
  47.  
  48. wp_localize_script( 'jetpack-modules-list-table', 'jetpackModulesData', array( 
  49. 'modules' => $this->all_items,  
  50. 'i18n' => array( 
  51. 'search_placeholder' => __( 'Search Modules*', 'jetpack' ),  
  52. ),  
  53. 'modalinfo' => $this->module_info_check( $modal_info, $this->all_items ),  
  54. 'nonces' => array( 
  55. 'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ),  
  56. ),  
  57. 'coreIconAvailable' => Jetpack::jetpack_site_icon_available_in_core(),  
  58. ) ); 
  59.  
  60. wp_enqueue_script( 'jetpack-modules-list-table' ); 
  61.  
  62. /** 
  63. * Filters the js_templates callback value. 
  64. * @since 3.6.0 
  65. * @param array array( $this, 'js_templates' ) js_templates callback. 
  66. */ 
  67. add_action( 'admin_footer', apply_filters( 'jetpack_modules_list_table_js_template_callback', array( $this, 'js_templates' ) ), 9 ); 
  68.  
  69. function js_templates() { 
  70. ?> 
  71. <script type="text/html" id="tmpl-Jetpack_Modules_List_Table_Template"> 
  72. <# var i = 0; 
  73. if ( data.items.length ) { 
  74. _.each( data.items, function( item, key, list ) { 
  75. if ( item === undefined ) return; 
  76. if ( jetpackModulesData.coreIconAvailable && 'site-icon' == item.module ) { #> 
  77. <tr class="jetpack-module deprecated <# if ( ++i % 2 ) { #> alternate<# } #>" id="site-icon-deprecated"> 
  78. <th scope="row" class="check-column"> 
  79. <input type="checkbox" name="modules[]" value="{{{ item.module }}}" disabled /> 
  80. </th> 
  81. <td class='name column-name'> 
  82. <span class='info'>{{{ item.name }}}</span> 
  83. <div class="row-actions"> 
  84. <span class="dep-msg"><?php _ex( 'WordPress now has Site Icon built in!', '"Site Icon" is the feature name.', 'jetpack' ); ?></span> 
  85. <span class='configure'><a href="<?php esc_html_e( admin_url( 'customize.php?autofocus[control]=site_icon' ), 'jetpack' ); ?>"><?php _e( 'Configure' , 'jetpack' ); ?></a></span> 
  86. </div> 
  87. </td> 
  88. </tr> 
  89. <# return; } #> 
  90. <tr class="jetpack-module <# if ( ++i % 2 ) { #> alternate<# } #><# if ( item.activated ) { #> active<# } #><# if ( ! item.available ) { #> unavailable<# } #>" id="{{{ item.module }}}"> 
  91. <th scope="row" class="check-column"> 
  92. <input type="checkbox" name="modules[]" value="{{{ item.module }}}" /> 
  93. </th> 
  94. <td class='name column-name'> 
  95. <span class='info'><a href="#">{{{ item.name }}}</a></span> 
  96. <div class="row-actions"> 
  97. <# if ( item.configurable ) { #> 
  98. <span class='configure'>{{{ item.configurable }}}</span> 
  99. <# } #> 
  100. <# if ( item.activated && 'vaultpress' !== item.module && item.available ) { #> 
  101. <span class='delete'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&action=deactivate&module={{{ item.module }}}&_wpnonce={{{ item.deactivate_nonce }}}"><?php _e( 'Deactivate', 'jetpack' ); ?></a></span> 
  102. <# } else if ( item.available ) { #> 
  103. <span class='activate'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&action=activate&module={{{ item.module }}}&_wpnonce={{{ item.activate_nonce }}}"><?php _e( 'Activate', 'jetpack' ); ?></a></span> 
  104. <# } #> 
  105. </div> 
  106. </td> 
  107. </tr> 
  108. <# 
  109. }); 
  110. } else { 
  111. #> 
  112. <tr class="no-modules-found"> 
  113. <td colspan="2"><?php esc_html_e( 'No Modules Found' , 'jetpack' ); ?></td> 
  114. </tr> 
  115. <# 
  116. #> 
  117. </script> 
  118. <?php 
  119.  
  120. function get_views() { 
  121. $modules = Jetpack_Admin::init()->get_modules(); 
  122. $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' ); 
  123. $module_tags = call_user_func_array( 'array_merge', $array_of_module_tags ); 
  124. $module_tags_unique = array_count_values( $module_tags ); 
  125. ksort( $module_tags_unique ); 
  126.  
  127. $format = '<a href="%3$s"%4$s data-title="%1$s">%1$s <span class="count">(%2$s)</span></a>'; 
  128. $title = __( 'All', 'jetpack' ); 
  129. $count = count( $modules ); 
  130. $url = esc_url( remove_query_arg( 'module_tag' ) ); 
  131. $current = empty( $_GET['module_tag'] ) ? ' class="current all"' : ' class="all"'; 
  132. $views = array( 
  133. 'all' => sprintf( $format, $title, $count, $url, $current ),  
  134. ); 
  135. foreach ( $module_tags_unique as $title => $count ) { 
  136. if ( 'Jumpstart' == $title ) { 
  137. continue; 
  138. $key = sanitize_title( $title ); 
  139. $display_title = esc_html( wptexturize( $title ) ); 
  140. $url = esc_url( add_query_arg( 'module_tag', urlencode( $title ) ) ); 
  141. $current = ''; 
  142. if ( ! empty( $_GET['module_tag'] ) && $title == $_GET['module_tag'] ) 
  143. $current = ' class="current"'; 
  144. $views[ $key ] = sprintf( $format, $display_title, $count, $url, $current ); 
  145. return $views; 
  146.  
  147. function views() { 
  148. $views = $this->get_views(); 
  149.  
  150. echo "<ul class='subsubsub'>\n"; 
  151. foreach ( $views as $class => $view ) { 
  152. $views[ $class ] = "\t<li class='$class'>$view</li>"; 
  153. echo implode( "\n", $views ) . "\n"; 
  154. echo "</ul>"; 
  155.  
  156. function filter_displayed_table_items( $modules ) { 
  157. return array_filter( $modules, array( $this, 'is_module_displayed' ) ); 
  158.  
  159. static function is_module_available( $module ) { 
  160. if ( ! is_array( $module ) || empty( $module ) ) 
  161. return false; 
  162.  
  163. if ( Jetpack::is_development_mode() ) { 
  164. return ! ( $module['requires_connection'] ); 
  165. } else { 
  166. return Jetpack::is_active(); 
  167.  
  168. static function is_module_displayed( $module ) { 
  169. // Handle module tag based filtering. 
  170. if ( ! empty( $_REQUEST['module_tag'] ) ) { 
  171. $module_tag = sanitize_text_field( $_REQUEST['module_tag'] ); 
  172. if ( ! in_array( $module_tag, $module['module_tags'] ) ) 
  173. return false; 
  174.  
  175. // If nothing rejected it, include it! 
  176. return true; 
  177.  
  178. static function sort_requires_connection_last( $module1, $module2 ) { 
  179. if ( $module1['requires_connection'] == $module2['requires_connection'] ) 
  180. return 0; 
  181. if ( $module1['requires_connection'] ) 
  182. return 1; 
  183. if ( $module2['requires_connection'] ) 
  184. return -1; 
  185.  
  186. return 0; 
  187.  
  188. function get_columns() { 
  189. $columns = array( 
  190. 'cb' => '<input type="checkbox" />',  
  191. 'name' => __( 'Name', 'jetpack' ),  
  192. ); 
  193. return $columns; 
  194.  
  195. function get_bulk_actions() { 
  196. $actions = array( 
  197. 'bulk-activate' => __( 'Activate', 'jetpack' ),  
  198. 'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),  
  199. ); 
  200. return $actions; 
  201.  
  202. function single_row( $item ) { 
  203. static $i = 0; 
  204. $row_class = ( ++$i % 2 ) ? ' alternate' : ''; 
  205.  
  206. if ( ! empty( $item['activated'] ) ) 
  207. $row_class .= ' active'; 
  208.  
  209. if ( ! $this->is_module_available( $item ) ) 
  210. $row_class .= ' unavailable'; 
  211.  
  212. echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">'; 
  213. $this->single_row_columns( $item ); 
  214. echo '</tr>'; 
  215.  
  216. function get_table_classes() { 
  217. return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed', 'jetpack-modules' ); 
  218.  
  219. function column_cb( $item ) { 
  220. if ( ! $this->is_module_available( $item ) ) 
  221. return ''; 
  222.  
  223. return sprintf( '<input type="checkbox" name="modules[]" value="%s" />', $item['module'] ); 
  224.  
  225. function column_icon( $item ) { 
  226. $badge_text = $free_text = ''; 
  227. ob_start(); 
  228. ?> 
  229. <a href="#TB_inline?width=600&height=550&inlineId=more-info-module-settings-modal" class="thickbox"> 
  230. <div class="module-image"> 
  231. <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> 
  232. </div> 
  233. </a> 
  234. <?php 
  235. return ob_get_clean(); 
  236.  
  237.  
  238. function column_name( $item ) { 
  239. $actions = array( 
  240. 'info' => sprintf( '<a href="%s">%s</a>', esc_url( '#' ), esc_html__( 'Module Info', 'jetpack' ) ),  
  241. ); 
  242.  
  243. if ( ! empty( $item['configurable'] ) ) { 
  244. $actions['configure'] = $item['configurable']; 
  245.  
  246. if ( empty( $item['activated'] ) && $this->is_module_available( $item ) ) { 
  247. $url = wp_nonce_url( 
  248. Jetpack::admin_url( array( 
  249. 'page' => 'jetpack',  
  250. 'action' => 'activate',  
  251. 'module' => $item['module'],  
  252. ) ),  
  253. 'jetpack_activate-' . $item['module'] 
  254. ); 
  255. $actions['activate'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Activate', 'jetpack' ) ); 
  256. } elseif ( ! empty( $item['activated'] ) ) { 
  257. $url = wp_nonce_url( 
  258. Jetpack::admin_url( array( 
  259. 'page' => 'jetpack',  
  260. 'action' => 'deactivate',  
  261. 'module' => $item['module'],  
  262. ) ),  
  263. 'jetpack_deactivate-' . $item['module'] 
  264. ); 
  265. $actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Deactivate', 'jetpack' ) ); 
  266.  
  267. return $this->row_actions( $actions ) . wptexturize( $item['name'] ); 
  268.  
  269. function column_description( $item ) { 
  270. ob_start(); 
  271. /** This action is documented in class.jetpack-admin.php */ 
  272. echo apply_filters( 'jetpack_short_module_description', $item['description'], $item['module'] ); 
  273. /** This action is documented in class.jetpack-admin.php */ 
  274. do_action( 'jetpack_learn_more_button_' . $item['module'] ); 
  275. echo '<div id="more-info-' . $item['module'] . '" class="more-info">'; 
  276. if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $item['module'] ) ) { 
  277. /** This action is documented in class.jetpack-admin.php */ 
  278. do_action( 'jetpack_module_more_info_connected_' . $item['module'] ); 
  279. } else { 
  280. /** This action is documented in class.jetpack-admin.php */ 
  281. do_action( 'jetpack_module_more_info_' . $item['module'] ); 
  282. echo '</div>'; 
  283. return ob_get_clean(); 
  284.  
  285. function column_module_tags( $item ) { 
  286. $module_tags = array(); 
  287. foreach( $item['module_tags'] as $module_tag ) { 
  288. $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 ) ) ) ); 
  289. return implode( ', ', $module_tags ); 
  290.  
  291. function column_default( $item, $column_name ) { 
  292. switch ( $column_name ) { 
  293. case 'icon': 
  294. case 'name': 
  295. case 'description': 
  296. break; 
  297. default: 
  298. return print_r( $item, true ); 
  299.  
  300. //Check if the info parameter provided in the URL corresponds to an actual module 
  301. function module_info_check( $info = false, $modules ) { 
  302. if ( false == $info ) { 
  303. return false; 
  304. } else if ( array_key_exists( $info, $modules ) ) { 
  305. return $info; 
  306.  
  307. /** 
  308. * Core switched their `display_tablenav()` method to protected, so we can't access it directly. 
  309. * Instead, let's include an access function to make it doable without errors! 
  310. * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622 
  311. * @param string $which 
  312. * @return mixed 
  313. */ 
  314. function unprotected_display_tablenav( $which = 'top' ) { 
  315. return $this->display_tablenav( $which ); 
  316.