wp/wp-admin/includes/class-wp-plugin-install-list-table.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php
       
     2 /**
       
     3  * Plugin Installer List Table class.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage List_Table
       
     7  * @since 3.1.0
       
     8  * @access private
       
     9  */
       
    10 class WP_Plugin_Install_List_Table extends WP_List_Table {
       
    11 
       
    12 	function ajax_user_can() {
       
    13 		return current_user_can('install_plugins');
       
    14 	}
       
    15 
       
    16 	function prepare_items() {
       
    17 		include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
       
    18 
       
    19 		global $tabs, $tab, $paged, $type, $term;
       
    20 
       
    21 		wp_reset_vars( array( 'tab' ) );
       
    22 
       
    23 		$paged = $this->get_pagenum();
       
    24 
       
    25 		$per_page = 30;
       
    26 
       
    27 		// These are the tabs which are shown on the page
       
    28 		$tabs = array();
       
    29 		$tabs['dashboard'] = __( 'Search' );
       
    30 		if ( 'search' == $tab )
       
    31 			$tabs['search']	= __( 'Search Results' );
       
    32 		$tabs['upload']    = __( 'Upload' );
       
    33 		$tabs['featured']  = _x( 'Featured', 'Plugin Installer' );
       
    34 		$tabs['popular']   = _x( 'Popular', 'Plugin Installer' );
       
    35 		$tabs['new']       = _x( 'Newest', 'Plugin Installer' );
       
    36 		$tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
       
    37 
       
    38 		$nonmenu_tabs = array( 'plugin-information' ); //Valid actions to perform which do not have a Menu item.
       
    39 
       
    40 		/**
       
    41 		 * Filter the tabs shown on the Plugin Install screen.
       
    42 		 *
       
    43 		 * @since 2.7.0
       
    44 		 *
       
    45 		 * @param array $tabs The tabs shown on the Plugin Install screen. Defaults are 'dashboard', 'search',
       
    46 		 *                    'upload', 'featured', 'popular', 'new', and 'favorites'.
       
    47 		 */
       
    48 		$tabs = apply_filters( 'install_plugins_tabs', $tabs );
       
    49 
       
    50 		/**
       
    51 		 * Filter tabs not associated with a menu item on the Plugin Install screen.
       
    52 		 *
       
    53 		 * @since 2.7.0
       
    54 		 *
       
    55 		 * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
       
    56 		 */
       
    57 		$nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
       
    58 
       
    59 		// If a non-valid menu tab has been selected, And it's not a non-menu action.
       
    60 		if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
       
    61 			$tab = key( $tabs );
       
    62 
       
    63 		$args = array( 'page' => $paged, 'per_page' => $per_page );
       
    64 
       
    65 		switch ( $tab ) {
       
    66 			case 'search':
       
    67 				$type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
       
    68 				$term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
       
    69 
       
    70 				switch ( $type ) {
       
    71 					case 'tag':
       
    72 						$args['tag'] = sanitize_title_with_dashes( $term );
       
    73 						break;
       
    74 					case 'term':
       
    75 						$args['search'] = $term;
       
    76 						break;
       
    77 					case 'author':
       
    78 						$args['author'] = $term;
       
    79 						break;
       
    80 				}
       
    81 
       
    82 				add_action( 'install_plugins_table_header', 'install_search_form', 10, 0 );
       
    83 				break;
       
    84 
       
    85 			case 'featured':
       
    86 			case 'popular':
       
    87 			case 'new':
       
    88 				$args['browse'] = $tab;
       
    89 				break;
       
    90 
       
    91 			case 'favorites':
       
    92 				$user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
       
    93 				update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
       
    94 				if ( $user )
       
    95 					$args['user'] = $user;
       
    96 				else
       
    97 					$args = false;
       
    98 
       
    99 				add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
       
   100 				break;
       
   101 
       
   102 			default:
       
   103 				$args = false;
       
   104 				break;
       
   105 		}
       
   106 
       
   107 		/**
       
   108 		 * Filter API request arguments for each Plugin Install screen tab.
       
   109 		 *
       
   110 		 * The dynamic portion of the hook name, $tab, refers to the plugin install tabs.
       
   111 		 * Default tabs are 'dashboard', 'search', 'upload', 'featured', 'popular', 'new',
       
   112 		 * and 'favorites'.
       
   113 		 *
       
   114 		 * @since 3.7.0
       
   115 		 *
       
   116 		 * @param array|bool $args Plugin Install API arguments.
       
   117 		 */
       
   118 		$args = apply_filters( "install_plugins_table_api_args_$tab", $args );
       
   119 
       
   120 		if ( !$args )
       
   121 			return;
       
   122 
       
   123 		$api = plugins_api( 'query_plugins', $args );
       
   124 
       
   125 		if ( is_wp_error( $api ) )
       
   126 			wp_die( $api->get_error_message() . '</p> <p class="hide-if-no-js"><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
       
   127 
       
   128 		$this->items = $api->plugins;
       
   129 
       
   130 		$this->set_pagination_args( array(
       
   131 			'total_items' => $api->info['results'],
       
   132 			'per_page' => $per_page,
       
   133 		) );
       
   134 	}
       
   135 
       
   136 	function no_items() {
       
   137 		_e( 'No plugins match your request.' );
       
   138 	}
       
   139 
       
   140 	function get_views() {
       
   141 		global $tabs, $tab;
       
   142 
       
   143 		$display_tabs = array();
       
   144 		foreach ( (array) $tabs as $action => $text ) {
       
   145 			$class = ( $action == $tab ) ? ' class="current"' : '';
       
   146 			$href = self_admin_url('plugin-install.php?tab=' . $action);
       
   147 			$display_tabs['plugin-install-'.$action] = "<a href='$href'$class>$text</a>";
       
   148 		}
       
   149 
       
   150 		return $display_tabs;
       
   151 	}
       
   152 
       
   153 	function display_tablenav( $which ) {
       
   154 		if ( 'top' ==  $which ) { ?>
       
   155 			<div class="tablenav top">
       
   156 				<div class="alignleft actions">
       
   157 					<?php
       
   158 					/**
       
   159 					 * Fires before the Plugin Install table header pagination is displayed.
       
   160 					 *
       
   161 					 * @since 2.7.0
       
   162 					 */
       
   163 					do_action( 'install_plugins_table_header' ); ?>
       
   164 				</div>
       
   165 				<?php $this->pagination( $which ); ?>
       
   166 				<br class="clear" />
       
   167 			</div>
       
   168 		<?php } else { ?>
       
   169 			<div class="tablenav bottom">
       
   170 				<?php $this->pagination( $which ); ?>
       
   171 				<br class="clear" />
       
   172 			</div>
       
   173 		<?php
       
   174 		}
       
   175 	}
       
   176 
       
   177 	function get_table_classes() {
       
   178 		extract( $this->_args );
       
   179 
       
   180 		return array( 'widefat', $plural );
       
   181 	}
       
   182 
       
   183 	function get_columns() {
       
   184 		return array(
       
   185 			'name'        => _x( 'Name', 'plugin name' ),
       
   186 			'version'     => __( 'Version' ),
       
   187 			'rating'      => __( 'Rating' ),
       
   188 			'description' => __( 'Description' ),
       
   189 		);
       
   190 	}
       
   191 
       
   192 	function display_rows() {
       
   193 		$plugins_allowedtags = array(
       
   194 			'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
       
   195 			'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
       
   196 			'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
       
   197 			'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
       
   198 		);
       
   199 
       
   200 		list( $columns, $hidden ) = $this->get_column_info();
       
   201 
       
   202 		$style = array();
       
   203 		foreach ( $columns as $column_name => $column_display_name ) {
       
   204 			$style[ $column_name ] = in_array( $column_name, $hidden ) ? 'style="display:none;"' : '';
       
   205 		}
       
   206 
       
   207 		foreach ( (array) $this->items as $plugin ) {
       
   208 			if ( is_object( $plugin ) )
       
   209 				$plugin = (array) $plugin;
       
   210 
       
   211 			$title = wp_kses( $plugin['name'], $plugins_allowedtags );
       
   212 			//Limit description to 400char, and remove any HTML.
       
   213 			$description = strip_tags( $plugin['description'] );
       
   214 			if ( strlen( $description ) > 400 )
       
   215 				$description = mb_substr( $description, 0, 400 ) . '&#8230;';
       
   216 			//remove any trailing entities
       
   217 			$description = preg_replace( '/&[^;\s]{0,6}$/', '', $description );
       
   218 			//strip leading/trailing & multiple consecutive lines
       
   219 			$description = trim( $description );
       
   220 			$description = preg_replace( "|(\r?\n)+|", "\n", $description );
       
   221 			//\n => <br>
       
   222 			$description = nl2br( $description );
       
   223 			$version = wp_kses( $plugin['version'], $plugins_allowedtags );
       
   224 
       
   225 			$name = strip_tags( $title . ' ' . $version );
       
   226 
       
   227 			$author = $plugin['author'];
       
   228 			if ( ! empty( $plugin['author'] ) )
       
   229 				$author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '.</cite>';
       
   230 
       
   231 			$author = wp_kses( $author, $plugins_allowedtags );
       
   232 
       
   233 			$action_links = array();
       
   234 			$action_links[] = '<a href="' . self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
       
   235 								'&amp;TB_iframe=true&amp;width=600&amp;height=550' ) . '" class="thickbox" title="' .
       
   236 								esc_attr( sprintf( __( 'More information about %s' ), $name ) ) . '">' . __( 'Details' ) . '</a>';
       
   237 
       
   238 			if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
       
   239 				$status = install_plugin_install_status( $plugin );
       
   240 
       
   241 				switch ( $status['status'] ) {
       
   242 					case 'install':
       
   243 						if ( $status['url'] )
       
   244 							$action_links[] = '<a class="install-now" href="' . $status['url'] . '" title="' . esc_attr( sprintf( __( 'Install %s' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
       
   245 						break;
       
   246 					case 'update_available':
       
   247 						if ( $status['url'] )
       
   248 							$action_links[] = '<a href="' . $status['url'] . '" title="' . esc_attr( sprintf( __( 'Update to version %s' ), $status['version'] ) ) . '">' . sprintf( __( 'Update Now' ), $status['version'] ) . '</a>';
       
   249 						break;
       
   250 					case 'latest_installed':
       
   251 					case 'newer_installed':
       
   252 						$action_links[] = '<span title="' . esc_attr__( 'This plugin is already installed and is up to date' ) . ' ">' . _x( 'Installed', 'plugin' ) . '</span>';
       
   253 						break;
       
   254 				}
       
   255 			}
       
   256 
       
   257 			/**
       
   258 			 * Filter the install action links for a plugin.
       
   259 			 *
       
   260 			 * @since 2.7.0
       
   261 			 *
       
   262 			 * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
       
   263 			 * @param array $plugin       The plugin currently being listed.
       
   264 			 */
       
   265 			$action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
       
   266 		?>
       
   267 		<tr>
       
   268 			<td class="name column-name"<?php echo $style['name']; ?>><strong><?php echo $title; ?></strong>
       
   269 				<div class="action-links"><?php if ( !empty( $action_links ) ) echo implode( ' | ', $action_links ); ?></div>
       
   270 			</td>
       
   271 			<td class="vers column-version"<?php echo $style['version']; ?>><?php echo $version; ?></td>
       
   272 			<td class="vers column-rating"<?php echo $style['rating']; ?>>
       
   273 				<div class="star-holder" title="<?php printf( _n( '(based on %s rating)', '(based on %s ratings)', $plugin['num_ratings'] ), number_format_i18n( $plugin['num_ratings'] ) ) ?>">
       
   274 					<div class="star star-rating" style="width: <?php echo esc_attr( str_replace( ',', '.', $plugin['rating'] ) ); ?>px"></div>
       
   275 				</div>
       
   276 			</td>
       
   277 			<td class="desc column-description"<?php echo $style['description']; ?>><?php echo $description, $author; ?></td>
       
   278 		</tr>
       
   279 		<?php
       
   280 		}
       
   281 	}
       
   282 }