PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/jetpack/class.jetpack-modules-list-table.php

https://gitlab.com/relacilia/cakra
PHP | 362 lines | 285 code | 43 blank | 34 comment | 39 complexity | f59937a4be68d6bd6d084eb3d7ece41a MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'WP_List_Table' ) )
  3. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  4. class Jetpack_Modules_List_Table extends WP_List_Table {
  5. function __construct() {
  6. parent::__construct();
  7. Jetpack::init();
  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. *
  18. * @since 3.0.0
  19. *
  20. * @param array $this->items Array of Jetpack modules.
  21. */
  22. $this->items = apply_filters( 'jetpack_modules_list_table_items', $this->items );
  23. $this->_column_headers = array( $this->get_columns(), array(), array(), 'name' );
  24. $modal_info = isset( $_GET['info'] ) ? $_GET['info'] : false;
  25. wp_register_script(
  26. 'models.jetpack-modules',
  27. plugins_url( '_inc/jetpack-modules.models.js', JETPACK__PLUGIN_FILE ),
  28. array( 'backbone', 'underscore' ),
  29. JETPACK__VERSION
  30. );
  31. wp_register_script(
  32. 'views.jetpack-modules',
  33. plugins_url( '_inc/jetpack-modules.views.js', JETPACK__PLUGIN_FILE ),
  34. array( 'backbone', 'underscore', 'wp-util' ),
  35. JETPACK__VERSION
  36. );
  37. wp_register_script(
  38. 'jetpack-modules-list-table',
  39. plugins_url( '_inc/jetpack-modules.js', JETPACK__PLUGIN_FILE ),
  40. array(
  41. 'views.jetpack-modules',
  42. 'models.jetpack-modules',
  43. 'jquery',
  44. ),
  45. JETPACK__VERSION,
  46. true
  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. wp_enqueue_script( 'jetpack-modules-list-table' );
  60. /**
  61. * Filters the js_templates callback value.
  62. *
  63. * @since 3.6.0
  64. *
  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&#038;action=deactivate&#038;module={{{ item.module }}}&#038;_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&#038;action=activate&#038;module={{{ item.module }}}&#038;_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. #>
  118. </script>
  119. <?php
  120. }
  121. function get_views() {
  122. $modules = Jetpack_Admin::init()->get_modules();
  123. $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' );
  124. $module_tags = call_user_func_array( 'array_merge', $array_of_module_tags );
  125. $module_tags_unique = array_count_values( $module_tags );
  126. ksort( $module_tags_unique );
  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. }
  139. $key = sanitize_title( $title );
  140. $display_title = esc_html( wptexturize( $title ) );
  141. $url = esc_url( add_query_arg( 'module_tag', urlencode( $title ) ) );
  142. $current = '';
  143. if ( ! empty( $_GET['module_tag'] ) && $title == $_GET['module_tag'] )
  144. $current = ' class="current"';
  145. $views[ $key ] = sprintf( $format, $display_title, $count, $url, $current );
  146. }
  147. return $views;
  148. }
  149. function views() {
  150. $views = $this->get_views();
  151. echo "<ul class='subsubsub'>\n";
  152. foreach ( $views as $class => $view ) {
  153. $views[ $class ] = "\t<li class='$class'>$view</li>";
  154. }
  155. echo implode( "\n", $views ) . "\n";
  156. echo "</ul>";
  157. }
  158. function filter_displayed_table_items( $modules ) {
  159. return array_filter( $modules, array( $this, 'is_module_displayed' ) );
  160. }
  161. static function is_module_available( $module ) {
  162. if ( ! is_array( $module ) || empty( $module ) )
  163. return false;
  164. if ( Jetpack::is_development_mode() ) {
  165. return ! ( $module['requires_connection'] );
  166. } else {
  167. return Jetpack::is_active();
  168. }
  169. }
  170. static function is_module_displayed( $module ) {
  171. // Handle module tag based filtering.
  172. if ( ! empty( $_REQUEST['module_tag'] ) ) {
  173. $module_tag = sanitize_text_field( $_REQUEST['module_tag'] );
  174. if ( ! in_array( $module_tag, $module['module_tags'] ) )
  175. return false;
  176. }
  177. // If nothing rejected it, include it!
  178. return true;
  179. }
  180. static function sort_requires_connection_last( $module1, $module2 ) {
  181. if ( $module1['requires_connection'] == $module2['requires_connection'] )
  182. return 0;
  183. if ( $module1['requires_connection'] )
  184. return 1;
  185. if ( $module2['requires_connection'] )
  186. return -1;
  187. return 0;
  188. }
  189. function get_columns() {
  190. $columns = array(
  191. 'cb' => '<input type="checkbox" />',
  192. 'name' => __( 'Name', 'jetpack' ),
  193. );
  194. return $columns;
  195. }
  196. function get_bulk_actions() {
  197. $actions = array(
  198. 'bulk-activate' => __( 'Activate', 'jetpack' ),
  199. 'bulk-deactivate' => __( 'Deactivate', 'jetpack' ),
  200. );
  201. return $actions;
  202. }
  203. function single_row( $item ) {
  204. static $i = 0;
  205. $row_class = ( ++$i % 2 ) ? ' alternate' : '';
  206. if ( ! empty( $item['activated'] ) )
  207. $row_class .= ' active';
  208. if ( ! $this->is_module_available( $item ) )
  209. $row_class .= ' unavailable';
  210. echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">';
  211. $this->single_row_columns( $item );
  212. echo '</tr>';
  213. }
  214. function get_table_classes() {
  215. return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed', 'jetpack-modules' );
  216. }
  217. function column_cb( $item ) {
  218. if ( ! $this->is_module_available( $item ) )
  219. return '';
  220. return sprintf( '<input type="checkbox" name="modules[]" value="%s" />', $item['module'] );
  221. }
  222. function column_icon( $item ) {
  223. $badge_text = $free_text = '';
  224. ob_start();
  225. ?>
  226. <a href="#TB_inline?width=600&height=550&inlineId=more-info-module-settings-modal" class="thickbox">
  227. <div class="module-image">
  228. <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>
  229. </div>
  230. </a>
  231. <?php
  232. return ob_get_clean();
  233. }
  234. function column_name( $item ) {
  235. $actions = array(
  236. 'info' => sprintf( '<a href="%s">%s</a>', esc_url( '#' ), esc_html__( 'Module Info', 'jetpack' ) ),
  237. );
  238. if ( ! empty( $item['configurable'] ) ) {
  239. $actions['configure'] = $item['configurable'];
  240. }
  241. if ( empty( $item['activated'] ) && $this->is_module_available( $item ) ) {
  242. $url = wp_nonce_url(
  243. Jetpack::admin_url( array(
  244. 'page' => 'jetpack',
  245. 'action' => 'activate',
  246. 'module' => $item['module'],
  247. ) ),
  248. 'jetpack_activate-' . $item['module']
  249. );
  250. $actions['activate'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Activate', 'jetpack' ) );
  251. } elseif ( ! empty( $item['activated'] ) ) {
  252. $url = wp_nonce_url(
  253. Jetpack::admin_url( array(
  254. 'page' => 'jetpack',
  255. 'action' => 'deactivate',
  256. 'module' => $item['module'],
  257. ) ),
  258. 'jetpack_deactivate-' . $item['module']
  259. );
  260. $actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Deactivate', 'jetpack' ) );
  261. }
  262. return $this->row_actions( $actions ) . wptexturize( $item['name'] );
  263. }
  264. function column_description( $item ) {
  265. ob_start();
  266. /** This action is documented in class.jetpack-admin.php */
  267. echo apply_filters( 'jetpack_short_module_description', $item['description'], $item['module'] );
  268. /** This action is documented in class.jetpack-admin.php */
  269. do_action( 'jetpack_learn_more_button_' . $item['module'] );
  270. echo '<div id="more-info-' . $item['module'] . '" class="more-info">';
  271. if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $item['module'] ) ) {
  272. /** This action is documented in class.jetpack-admin.php */
  273. do_action( 'jetpack_module_more_info_connected_' . $item['module'] );
  274. } else {
  275. /** This action is documented in class.jetpack-admin.php */
  276. do_action( 'jetpack_module_more_info_' . $item['module'] );
  277. }
  278. echo '</div>';
  279. return ob_get_clean();
  280. }
  281. function column_module_tags( $item ) {
  282. $module_tags = array();
  283. foreach( $item['module_tags'] as $module_tag ) {
  284. $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 ) ) ) );
  285. }
  286. return implode( ', ', $module_tags );
  287. }
  288. function column_default( $item, $column_name ) {
  289. switch ( $column_name ) {
  290. case 'icon':
  291. case 'name':
  292. case 'description':
  293. break;
  294. default:
  295. return print_r( $item, true );
  296. }
  297. }
  298. //Check if the info parameter provided in the URL corresponds to an actual module
  299. function module_info_check( $info = false, $modules ) {
  300. if ( false == $info ) {
  301. return false;
  302. } else if ( array_key_exists( $info, $modules ) ) {
  303. return $info;
  304. }
  305. }
  306. /**
  307. * Core switched their `display_tablenav()` method to protected, so we can't access it directly.
  308. * Instead, let's include an access function to make it doable without errors!
  309. *
  310. * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622
  311. *
  312. * @param string $which
  313. *
  314. * @return mixed
  315. */
  316. function unprotected_display_tablenav( $which = 'top' ) {
  317. return $this->display_tablenav( $which );
  318. }
  319. }