PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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