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