PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-plugins-list-table.php

https://github.com/muskmelon/Greemo
PHP | 458 lines | 355 code | 92 blank | 11 comment | 110 complexity | 42f2ad3993def0fdfe768af91782d654 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugins List Table class.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. * @access private
  9. */
  10. class WP_Plugins_List_Table extends WP_List_Table {
  11. function __construct() {
  12. global $status, $page;
  13. $default_status = get_user_option( 'plugins_last_view' );
  14. if ( empty( $default_status ) )
  15. $default_status = 'all';
  16. $status = isset( $_REQUEST['plugin_status'] ) ? $_REQUEST['plugin_status'] : $default_status;
  17. if ( !in_array( $status, array( 'all', 'active', 'inactive', 'recently_activated', 'upgrade', 'network', 'mustuse', 'dropins', 'search' ) ) )
  18. $status = 'all';
  19. if ( $status != $default_status && 'search' != $status )
  20. update_user_meta( get_current_user_id(), 'plugins_last_view', $status );
  21. if ( isset($_REQUEST['s']) )
  22. $_SERVER['REQUEST_URI'] = add_query_arg('s', stripslashes($_REQUEST['s']) );
  23. $page = $this->get_pagenum();
  24. parent::__construct( array(
  25. 'plural' => 'plugins',
  26. ) );
  27. }
  28. function get_table_classes() {
  29. return array( 'widefat', $this->_args['plural'] );
  30. }
  31. function ajax_user_can() {
  32. if ( is_multisite() ) {
  33. $menu_perms = get_site_option( 'menu_items', array() );
  34. if ( empty( $menu_perms['plugins'] ) && ! is_super_admin() )
  35. return false;
  36. }
  37. return current_user_can('activate_plugins');
  38. }
  39. function prepare_items() {
  40. global $status, $plugins, $totals, $page, $orderby, $order, $s;
  41. wp_reset_vars( array( 'orderby', 'order', 's' ) );
  42. $plugins = array(
  43. 'all' => apply_filters( 'all_plugins', get_plugins() ),
  44. 'search' => array(),
  45. 'active' => array(),
  46. 'inactive' => array(),
  47. 'recently_activated' => array(),
  48. 'upgrade' => array(),
  49. 'mustuse' => array(),
  50. 'dropins' => array()
  51. );
  52. $screen = get_current_screen();
  53. if ( ! is_multisite() || ( $screen->is_network && current_user_can('manage_network_plugins') ) ) {
  54. if ( apply_filters( 'show_advanced_plugins', true, 'mustuse' ) )
  55. $plugins['mustuse'] = get_mu_plugins();
  56. if ( apply_filters( 'show_advanced_plugins', true, 'dropins' ) )
  57. $plugins['dropins'] = get_dropins();
  58. $current = get_site_transient( 'update_plugins' );
  59. foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
  60. if ( isset( $current->response[ $plugin_file ] ) )
  61. $plugins['upgrade'][ $plugin_file ] = $plugin_data;
  62. }
  63. }
  64. set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), 86400 );
  65. $recently_activated = get_option( 'recently_activated', array() );
  66. $one_week = 7*24*60*60;
  67. foreach ( $recently_activated as $key => $time )
  68. if ( $time + $one_week < time() )
  69. unset( $recently_activated[$key] );
  70. update_option( 'recently_activated', $recently_activated );
  71. foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
  72. // Filter into individual sections
  73. if ( is_multisite() && is_network_only_plugin( $plugin_file ) && !$screen->is_network ) {
  74. unset( $plugins['all'][ $plugin_file] );
  75. } elseif ( is_plugin_active_for_network($plugin_file) && !$screen->is_network ) {
  76. unset( $plugins['all'][ $plugin_file ] );
  77. } elseif ( is_multisite() && is_network_only_plugin( $plugin_file ) && !current_user_can( 'manage_network_plugins' ) ) {
  78. $plugins['network'][ $plugin_file ] = $plugin_data;
  79. } elseif ( ( !$screen->is_network && is_plugin_active( $plugin_file ) )
  80. || ( $screen->is_network && is_plugin_active_for_network( $plugin_file ) ) ) {
  81. $plugins['active'][ $plugin_file ] = $plugin_data;
  82. } else {
  83. if ( !$screen->is_network && isset( $recently_activated[ $plugin_file ] ) ) // Was the plugin recently activated?
  84. $plugins['recently_activated'][ $plugin_file ] = $plugin_data;
  85. $plugins['inactive'][ $plugin_file ] = $plugin_data;
  86. }
  87. }
  88. if ( !current_user_can( 'update_plugins' ) )
  89. $plugins['upgrade'] = array();
  90. if ( $s ) {
  91. $status = 'search';
  92. $plugins['search'] = array_filter( $plugins['all'], array( &$this, '_search_callback' ) );
  93. }
  94. $totals = array();
  95. foreach ( $plugins as $type => $list )
  96. $totals[ $type ] = count( $list );
  97. if ( empty( $plugins[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
  98. $status = 'all';
  99. $this->items = array();
  100. foreach ( $plugins[ $status ] as $plugin_file => $plugin_data ) {
  101. // Translate, Don't Apply Markup, Sanitize HTML
  102. $this->items[$plugin_file] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
  103. }
  104. $total_this_page = $totals[ $status ];
  105. if ( $orderby ) {
  106. $orderby = ucfirst( $orderby );
  107. $order = strtoupper( $order );
  108. uasort( $this->items, array( &$this, '_order_callback' ) );
  109. }
  110. $plugins_per_page = $this->get_items_per_page( str_replace( '-', '_', $screen->id . '_per_page' ), 999 );
  111. $start = ( $page - 1 ) * $plugins_per_page;
  112. if ( $total_this_page > $plugins_per_page )
  113. $this->items = array_slice( $this->items, $start, $plugins_per_page );
  114. $this->set_pagination_args( array(
  115. 'total_items' => $total_this_page,
  116. 'per_page' => $plugins_per_page,
  117. ) );
  118. }
  119. function _search_callback( $plugin ) {
  120. static $term;
  121. if ( is_null( $term ) )
  122. $term = stripslashes( $_REQUEST['s'] );
  123. foreach ( $plugin as $value )
  124. if ( stripos( $value, $term ) !== false )
  125. return true;
  126. return false;
  127. }
  128. function _order_callback( $plugin_a, $plugin_b ) {
  129. global $orderby, $order;
  130. $a = $plugin_a[$orderby];
  131. $b = $plugin_b[$orderby];
  132. if ( $a == $b )
  133. return 0;
  134. if ( 'DESC' == $order )
  135. return ( $a < $b ) ? 1 : -1;
  136. else
  137. return ( $a < $b ) ? -1 : 1;
  138. }
  139. function no_items() {
  140. global $plugins;
  141. if ( !empty( $plugins['all'] ) )
  142. _e( 'No plugins found.' );
  143. else
  144. _e( 'You do not appear to have any plugins available at this time.' );
  145. }
  146. function get_columns() {
  147. global $status;
  148. return array(
  149. 'cb' => !in_array( $status, array( 'mustuse', 'dropins' ) ) ? '<input type="checkbox" />' : '',
  150. 'name' => __( 'Plugin' ),
  151. 'description' => __( 'Description' ),
  152. );
  153. }
  154. function get_sortable_columns() {
  155. return array();
  156. }
  157. function get_views() {
  158. global $totals, $status;
  159. $status_links = array();
  160. foreach ( $totals as $type => $count ) {
  161. if ( !$count )
  162. continue;
  163. switch ( $type ) {
  164. case 'all':
  165. $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins' );
  166. break;
  167. case 'active':
  168. $text = _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', $count );
  169. break;
  170. case 'recently_activated':
  171. $text = _n( 'Recently Active <span class="count">(%s)</span>', 'Recently Active <span class="count">(%s)</span>', $count );
  172. break;
  173. case 'inactive':
  174. $text = _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count );
  175. break;
  176. case 'network':
  177. $text = _n( 'Network <span class="count">(%s)</span>', 'Network <span class="count">(%s)</span>', $count );
  178. break;
  179. case 'mustuse':
  180. $text = _n( 'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', $count );
  181. break;
  182. case 'dropins':
  183. $text = _n( 'Drop-ins <span class="count">(%s)</span>', 'Drop-ins <span class="count">(%s)</span>', $count );
  184. break;
  185. case 'upgrade':
  186. $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
  187. break;
  188. }
  189. if ( 'search' != $type ) {
  190. $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
  191. add_query_arg('plugin_status', $type, 'plugins.php'),
  192. ( $type == $status ) ? ' class="current"' : '',
  193. sprintf( $text, number_format_i18n( $count ) )
  194. );
  195. }
  196. }
  197. return $status_links;
  198. }
  199. function get_bulk_actions() {
  200. global $status;
  201. $actions = array();
  202. $screen = get_current_screen();
  203. if ( 'active' != $status ) {
  204. $action = $screen->is_network ? 'network-activate-selected' : 'activate-selected';
  205. $actions[ $action ] = __( 'Activate' );
  206. }
  207. if ( 'inactive' != $status && 'recent' != $status )
  208. $actions['deactivate-selected'] = __( 'Deactivate' );
  209. if ( !is_multisite() || $screen->is_network ) {
  210. if ( current_user_can( 'update_plugins' ) )
  211. $actions['update-selected'] = __( 'Update' );
  212. if ( current_user_can( 'delete_plugins' ) && ( 'active' != $status ) )
  213. $actions['delete-selected'] = __( 'Delete' );
  214. }
  215. return $actions;
  216. }
  217. function bulk_actions( $which ) {
  218. global $status;
  219. if ( in_array( $status, array( 'mustuse', 'dropins' ) ) )
  220. return;
  221. parent::bulk_actions( $which );
  222. }
  223. function extra_tablenav( $which ) {
  224. global $status;
  225. if ( ! in_array($status, array('recently_activated', 'mustuse', 'dropins') ) )
  226. return;
  227. echo '<div class="alignleft actions">';
  228. if ( 'recently_activated' == $status )
  229. submit_button( __( 'Clear List' ), 'secondary', 'clear-recent-list', false );
  230. elseif ( 'top' == $which && 'mustuse' == $status )
  231. echo '<p>' . sprintf( __( 'Files in the <code>%s</code> directory are executed automatically.' ), str_replace( ABSPATH, '/', WPMU_PLUGIN_DIR ) ) . '</p>';
  232. elseif ( 'top' == $which && 'dropins' == $status )
  233. echo '<p>' . sprintf( __( 'Drop-ins are advanced plugins in the <code>%s</code> directory that replace WordPress functionality when present.' ), str_replace( ABSPATH, '', WP_CONTENT_DIR ) ) . '</p>';
  234. echo '</div>';
  235. }
  236. function current_action() {
  237. if ( isset($_POST['clear-recent-list']) )
  238. return 'clear-recent-list';
  239. return parent::current_action();
  240. }
  241. function display_rows() {
  242. global $status;
  243. $screen = get_current_screen();
  244. if ( is_multisite() && !$screen->is_network && in_array( $status, array( 'mustuse', 'dropins' ) ) )
  245. return;
  246. foreach ( $this->items as $plugin_file => $plugin_data )
  247. $this->single_row( $plugin_file, $plugin_data );
  248. }
  249. function single_row( $plugin_file, $plugin_data ) {
  250. global $status, $page, $s;
  251. $context = $status;
  252. $screen = get_current_screen();
  253. // preorder
  254. $actions = array(
  255. 'network_deactivate' => '', 'deactivate' => '',
  256. 'network_only' => '', 'activate' => '',
  257. 'network_activate' => '',
  258. 'edit' => '',
  259. 'delete' => '',
  260. );
  261. if ( 'mustuse' == $context ) {
  262. $is_active = true;
  263. } elseif ( 'dropins' == $context ) {
  264. $dropins = _get_dropins();
  265. $plugin_name = $plugin_file;
  266. if ( $plugin_file != $plugin_data['Name'] )
  267. $plugin_name .= '<br/>' . $plugin_data['Name'];
  268. if ( true === ( $dropins[ $plugin_file ][1] ) ) { // Doesn't require a constant
  269. $is_active = true;
  270. $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
  271. } elseif ( constant( $dropins[ $plugin_file ][1] ) ) { // Constant is true
  272. $is_active = true;
  273. $description = '<p><strong>' . $dropins[ $plugin_file ][0] . '</strong></p>';
  274. } else {
  275. $is_active = false;
  276. $description = '<p><strong>' . $dropins[ $plugin_file ][0] . ' <span class="attention">' . __('Inactive:') . '</span></strong> ' . sprintf( __( 'Requires <code>%s</code> in <code>wp-config.php</code>.' ), "define('" . $dropins[ $plugin_file ][1] . "', true);" ) . '</p>';
  277. }
  278. if ( $plugin_data['Description'] )
  279. $description .= '<p>' . $plugin_data['Description'] . '</p>';
  280. } else {
  281. $is_active_for_network = is_plugin_active_for_network($plugin_file);
  282. if ( $screen->is_network )
  283. $is_active = $is_active_for_network;
  284. else
  285. $is_active = is_plugin_active( $plugin_file );
  286. if ( $is_active_for_network && !is_super_admin() && !$screen->is_network )
  287. return;
  288. if ( $screen->is_network ) {
  289. if ( $is_active_for_network ) {
  290. if ( current_user_can( 'manage_network_plugins' ) )
  291. $actions['network_deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Network Deactivate') . '</a>';
  292. } else {
  293. if ( current_user_can( 'manage_network_plugins' ) )
  294. $actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" class="edit">' . __('Network Activate') . '</a>';
  295. if ( current_user_can( 'delete_plugins' ) && ! is_plugin_active( $plugin_file ) )
  296. $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&amp;checked[]=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
  297. }
  298. } else {
  299. if ( $is_active ) {
  300. $actions['deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Deactivate') . '</a>';
  301. } else {
  302. $actions['activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" class="edit">' . __('Activate') . '</a>';
  303. if ( ! is_multisite() && current_user_can('delete_plugins') )
  304. $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&amp;checked[]=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
  305. } // end if $is_active
  306. } // end if $screen->is_network
  307. if ( ( ! is_multisite() || $screen->is_network ) && current_user_can('edit_plugins') && is_writable(WP_PLUGIN_DIR . '/' . $plugin_file) )
  308. $actions['edit'] = '<a href="plugin-editor.php?file=' . $plugin_file . '" title="' . esc_attr__('Open this file in the Plugin Editor') . '" class="edit">' . __('Edit') . '</a>';
  309. } // end if $context
  310. $prefix = $screen->is_network ? 'network_admin_' : '';
  311. $actions = apply_filters( $prefix . 'plugin_action_links', array_filter( $actions ), $plugin_file, $plugin_data, $context );
  312. $actions = apply_filters( $prefix . "plugin_action_links_$plugin_file", $actions, $plugin_file, $plugin_data, $context );
  313. $class = $is_active ? 'active' : 'inactive';
  314. $checkbox_id = "checkbox_" . md5($plugin_data['Name']);
  315. $checkbox = in_array( $status, array( 'mustuse', 'dropins' ) ) ? '' : "<input type='checkbox' name='checked[]' value='" . esc_attr( $plugin_file ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select') . " " . $plugin_data['Name'] . "</label>";
  316. if ( 'dropins' != $context ) {
  317. $description = '<p>' . ( $plugin_data['Description'] ? $plugin_data['Description'] : '&nbsp;' ) . '</p>';
  318. $plugin_name = $plugin_data['Name'];
  319. }
  320. $id = sanitize_title( $plugin_name );
  321. echo "<tr id='$id' class='$class'>";
  322. list( $columns, $hidden ) = $this->get_column_info();
  323. foreach ( $columns as $column_name => $column_display_name ) {
  324. $style = '';
  325. if ( in_array( $column_name, $hidden ) )
  326. $style = ' style="display:none;"';
  327. switch ( $column_name ) {
  328. case 'cb':
  329. echo "<th scope='row' class='check-column'>$checkbox</th>";
  330. break;
  331. case 'name':
  332. echo "<td class='plugin-title'$style><strong>$plugin_name</strong>";
  333. echo $this->row_actions( $actions, true );
  334. echo "</td>";
  335. break;
  336. case 'description':
  337. echo "<td class='column-description desc'$style>
  338. <div class='plugin-description'>$description</div>
  339. <div class='$class second plugin-version-author-uri'>";
  340. $plugin_meta = array();
  341. if ( !empty( $plugin_data['Version'] ) )
  342. $plugin_meta[] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
  343. if ( !empty( $plugin_data['Author'] ) ) {
  344. $author = $plugin_data['Author'];
  345. if ( !empty( $plugin_data['AuthorURI'] ) )
  346. $author = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
  347. $plugin_meta[] = sprintf( __( 'By %s' ), $author );
  348. }
  349. if ( ! empty( $plugin_data['PluginURI'] ) )
  350. $plugin_meta[] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin site' ) . '">' . __( 'Visit plugin site' ) . '</a>';
  351. $plugin_meta = apply_filters( 'plugin_row_meta', $plugin_meta, $plugin_file, $plugin_data, $status );
  352. echo implode( ' | ', $plugin_meta );
  353. echo "</div></td>";
  354. break;
  355. default:
  356. echo "<td class='$column_name column-$column_name'$style>";
  357. do_action( 'manage_plugins_custom_column', $column_name, $plugin_file, $plugin_data );
  358. echo "</td>";
  359. }
  360. }
  361. echo "</tr>";
  362. do_action( 'after_plugin_row', $plugin_file, $plugin_data, $status );
  363. do_action( "after_plugin_row_$plugin_file", $plugin_file, $plugin_data, $status );
  364. }
  365. }
  366. ?>