web/wp-admin/includes/plugin-install.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    10  * Retrieve plugin installer pages from WordPress Plugins API.
    10  * Retrieve plugin installer pages from WordPress Plugins API.
    11  *
    11  *
    12  * It is possible for a plugin to override the Plugin API result with three
    12  * It is possible for a plugin to override the Plugin API result with three
    13  * filters. Assume this is for plugins, which can extend on the Plugin Info to
    13  * filters. Assume this is for plugins, which can extend on the Plugin Info to
    14  * offer more choices. This is very powerful and must be used with care, when
    14  * offer more choices. This is very powerful and must be used with care, when
    15  * overridding the filters.
    15  * overriding the filters.
    16  *
    16  *
    17  * The first filter, 'plugins_api_args', is for the args and gives the action as
    17  * The first filter, 'plugins_api_args', is for the args and gives the action as
    18  * the second parameter. The hook for 'plugins_api_args' must ensure that an
    18  * the second parameter. The hook for 'plugins_api_args' must ensure that an
    19  * object is returned.
    19  * object is returned.
    20  *
    20  *
    22  *
    22  *
    23  * @since 2.7.0
    23  * @since 2.7.0
    24  *
    24  *
    25  * @param string $action
    25  * @param string $action
    26  * @param array|object $args Optional. Arguments to serialize for the Plugin Info API.
    26  * @param array|object $args Optional. Arguments to serialize for the Plugin Info API.
    27  * @return mixed
    27  * @return object plugins_api response object on success, WP_Error on failure.
    28  */
    28  */
    29 function plugins_api($action, $args = null) {
    29 function plugins_api($action, $args = null) {
    30 
    30 
    31 	if( is_array($args) )
    31 	if ( is_array($args) )
    32 		$args = (object)$args;
    32 		$args = (object)$args;
    33 
    33 
    34 	if ( !isset($args->per_page) )
    34 	if ( !isset($args->per_page) )
    35 		$args->per_page = 24;
    35 		$args->per_page = 24;
    36 
    36 
    37 	$args = apply_filters('plugins_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
    37 	// Allows a plugin to override the WordPress.org API entirely.
    38 	$res = apply_filters('plugins_api', false, $action, $args); //NOTE: Allows a plugin to completely override the builtin WordPress.org API.
    38 	// Use the filter 'plugins_api_result' to merely add results.
    39 
    39 	// Please ensure that a object is returned from the following filters.
    40 	if ( ! $res ) {
    40 	$args = apply_filters('plugins_api_args', $args, $action);
    41 		$request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
    41 	$res = apply_filters('plugins_api', false, $action, $args);
       
    42 
       
    43 	if ( false === $res ) {
       
    44 		$request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) );
    42 		if ( is_wp_error($request) ) {
    45 		if ( is_wp_error($request) ) {
    43 			$res = new WP_Error('plugins_api_failed', __('An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>'), $request->get_error_message() );
    46 			$res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
    44 		} else {
    47 		} else {
    45 			$res = unserialize($request['body']);
    48 			$res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
    46 			if ( ! $res )
    49 			if ( ! is_object( $res ) && ! is_array( $res ) )
    47 				$res = new WP_Error('plugins_api_failed', __('An unknown error occurred'), $request['body']);
    50 				$res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
    48 		}
    51 		}
    49 	} elseif ( !is_wp_error($res) ) {
    52 	} elseif ( !is_wp_error($res) ) {
    50 		$res->external = true;
    53 		$res->external = true;
    51 	}
    54 	}
    52 
    55 
    60  *
    63  *
    61  * @param array $args
    64  * @param array $args
    62  * @return array
    65  * @return array
    63  */
    66  */
    64 function install_popular_tags( $args = array() ) {
    67 function install_popular_tags( $args = array() ) {
    65 	if ( ! ($cache = wp_cache_get('popular_tags', 'api')) && ! ($cache = get_option('wporg_popular_tags')) )
    68 	$key = md5(serialize($args));
    66 		add_option('wporg_popular_tags', array(), '', 'no'); ///No autoload.
    69 	if ( false !== ($tags = get_site_transient('poptags_' . $key) ) )
    67 
    70 		return $tags;
    68 	if ( $cache && $cache->timeout + 3 * 60 * 60 > time() )
       
    69 		return $cache->cached;
       
    70 
    71 
    71 	$tags = plugins_api('hot_tags', $args);
    72 	$tags = plugins_api('hot_tags', $args);
    72 
    73 
    73 	if ( is_wp_error($tags) )
    74 	if ( is_wp_error($tags) )
    74 		return $tags;
    75 		return $tags;
    75 
    76 
    76 	$cache = (object) array('timeout' => time(), 'cached' => $tags);
    77 	set_site_transient('poptags_' . $key, $tags, 10800); // 3 * 60 * 60 = 10800
    77 
       
    78 	update_option('wporg_popular_tags', $cache);
       
    79 	wp_cache_set('popular_tags', $cache, 'api');
       
    80 
    78 
    81 	return $tags;
    79 	return $tags;
    82 }
    80 }
    83 add_action('install_plugins_search', 'install_search', 10, 1);
    81 
    84 
       
    85 /**
       
    86  * Display search results and display as tag cloud.
       
    87  *
       
    88  * @since 2.7.0
       
    89  *
       
    90  * @param string $page
       
    91  */
       
    92 function install_search($page) {
       
    93 	$type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
       
    94 	$term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
       
    95 
       
    96 	$args = array();
       
    97 
       
    98 	switch( $type ){
       
    99 		case 'tag':
       
   100 			$args['tag'] = sanitize_title_with_dashes($term);
       
   101 			break;
       
   102 		case 'term':
       
   103 			$args['search'] = $term;
       
   104 			break;
       
   105 		case 'author':
       
   106 			$args['author'] = $term;
       
   107 			break;
       
   108 	}
       
   109 
       
   110 	$args['page'] = $page;
       
   111 
       
   112 	$api = plugins_api('query_plugins', $args);
       
   113 
       
   114 	if ( is_wp_error($api) )
       
   115 		wp_die($api);
       
   116 
       
   117 	add_action('install_plugins_table_header', 'install_search_form');
       
   118 
       
   119 	display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
       
   120 
       
   121 	return;
       
   122 }
       
   123 
       
   124 add_action('install_plugins_dashboard', 'install_dashboard');
       
   125 function install_dashboard() {
    82 function install_dashboard() {
   126 	?>
    83 	?>
   127 	<p><?php _e('Plugins extend and expand the functionality of WordPress. You may automatically install plugins from the <a href="http://wordpress.org/extend/plugins/">WordPress Plugin Directory</a> or upload a plugin in .zip format via this page.') ?></p>
    84 	<p><?php printf( __( 'Plugins extend and expand the functionality of WordPress. You may automatically install plugins from the <a href="http://wordpress.org/extend/plugins/">WordPress Plugin Directory</a> or upload a plugin in .zip format via <a href="%s">this page</a>.' ), self_admin_url( 'plugin-install.php?tab=upload' ) ); ?></p>
   128 
    85 
   129 	<h4><?php _e('Search') ?></h4>
    86 	<h4><?php _e('Search') ?></h4>
   130 	<p class="install-help"><?php _e('Search for plugins by keyword, author, or tag.') ?></p>
    87 	<?php install_search_form( false ); ?>
   131 	<?php install_search_form(); ?>
       
   132 
    88 
   133 	<h4><?php _e('Popular tags') ?></h4>
    89 	<h4><?php _e('Popular tags') ?></h4>
   134 	<p class="install-help"><?php _e('You may also browse based on the most popular tags in the Plugin Directory:') ?></p>
    90 	<p class="install-help"><?php _e('You may also browse based on the most popular tags in the Plugin Directory:') ?></p>
   135 	<?php
    91 	<?php
   136 
    92 
   137 	$api_tags = install_popular_tags();
    93 	$api_tags = install_popular_tags();
   138 
    94 
   139 	//Set up the tags in a way which can be interprated by wp_generate_tag_cloud()
       
   140 	$tags = array();
       
   141 	foreach ( (array)$api_tags as $tag )
       
   142 		$tags[ $tag['name'] ] = (object) array(
       
   143 								'link' => esc_url( admin_url('plugin-install.php?tab=search&type=tag&s=' . urlencode($tag['name'])) ),
       
   144 								'name' => $tag['name'],
       
   145 								'id' => sanitize_title_with_dashes($tag['name']),
       
   146 								'count' => $tag['count'] );
       
   147 	echo '<p class="popular-tags">';
    95 	echo '<p class="popular-tags">';
   148 	echo wp_generate_tag_cloud($tags, array( 'single_text' => __('%d plugin'), 'multiple_text' => __('%d plugins') ) );
    96 	if ( is_wp_error($api_tags) ) {
       
    97 		echo $api_tags->get_error_message();
       
    98 	} else {
       
    99 		//Set up the tags in a way which can be interpreted by wp_generate_tag_cloud()
       
   100 		$tags = array();
       
   101 		foreach ( (array)$api_tags as $tag )
       
   102 			$tags[ $tag['name'] ] = (object) array(
       
   103 									'link' => esc_url( self_admin_url('plugin-install.php?tab=search&type=tag&s=' . urlencode($tag['name'])) ),
       
   104 									'name' => $tag['name'],
       
   105 									'id' => sanitize_title_with_dashes($tag['name']),
       
   106 									'count' => $tag['count'] );
       
   107 		echo wp_generate_tag_cloud($tags, array( 'single_text' => __('%s plugin'), 'multiple_text' => __('%s plugins') ) );
       
   108 	}
   149 	echo '</p><br class="clear" />';
   109 	echo '</p><br class="clear" />';
   150 }
   110 }
       
   111 add_action('install_plugins_dashboard', 'install_dashboard');
   151 
   112 
   152 /**
   113 /**
   153  * Display search form for searching plugins.
   114  * Display search form for searching plugins.
   154  *
   115  *
   155  * @since 2.7.0
   116  * @since 2.7.0
   156  */
   117  */
   157 function install_search_form(){
   118 function install_search_form( $type_selector = true ) {
   158 	$type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
   119 	$type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : 'term';
   159 	$term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
   120 	$term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
   160 
   121 
   161 	?><form id="search-plugins" method="post" action="<?php echo admin_url('plugin-install.php?tab=search'); ?>">
   122 	?><form id="search-plugins" method="get" action="">
       
   123 		<input type="hidden" name="tab" value="search" />
       
   124 		<?php if ( $type_selector ) : ?>
   162 		<select name="type" id="typeselector">
   125 		<select name="type" id="typeselector">
   163 			<option value="term"<?php selected('term', $type) ?>><?php _e('Term'); ?></option>
   126 			<option value="term"<?php selected('term', $type) ?>><?php _e('Keyword'); ?></option>
   164 			<option value="author"<?php selected('author', $type) ?>><?php _e('Author'); ?></option>
   127 			<option value="author"<?php selected('author', $type) ?>><?php _e('Author'); ?></option>
   165 			<option value="tag"<?php selected('tag', $type) ?>><?php echo _x('Tag', 'Plugin Installer'); ?></option>
   128 			<option value="tag"<?php selected('tag', $type) ?>><?php _ex('Tag', 'Plugin Installer'); ?></option>
   166 		</select>
   129 		</select>
   167 		<input type="text" name="s" value="<?php echo esc_attr($term) ?>" />
   130 		<?php endif; ?>
       
   131 		<input type="search" name="s" value="<?php echo esc_attr($term) ?>" />
   168 		<label class="screen-reader-text" for="plugin-search-input"><?php _e('Search Plugins'); ?></label>
   132 		<label class="screen-reader-text" for="plugin-search-input"><?php _e('Search Plugins'); ?></label>
   169 		<input type="submit" id="plugin-search-input" name="search" value="<?php esc_attr_e('Search Plugins'); ?>" class="button" />
   133 		<?php submit_button( __( 'Search Plugins' ), 'button', 'plugin-search-input', false ); ?>
   170 	</form><?php
   134 	</form><?php
   171 }
   135 }
   172 
   136 
   173 add_action('install_plugins_featured', 'install_featured', 10, 1);
       
   174 /**
       
   175  * Display featured plugins.
       
   176  *
       
   177  * @since 2.7.0
       
   178  *
       
   179  * @param string $page
       
   180  */
       
   181 function install_featured($page = 1) {
       
   182 	$args = array('browse' => 'featured', 'page' => $page);
       
   183 	$api = plugins_api('query_plugins', $args);
       
   184 	if ( is_wp_error($api) )
       
   185 		wp_die($api);
       
   186 	display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
       
   187 }
       
   188 
       
   189 add_action('install_plugins_popular', 'install_popular', 10, 1);
       
   190 /**
       
   191  * Display popular plugins.
       
   192  *
       
   193  * @since 2.7.0
       
   194  *
       
   195  * @param string $page
       
   196  */
       
   197 function install_popular($page = 1) {
       
   198 	$args = array('browse' => 'popular', 'page' => $page);
       
   199 	$api = plugins_api('query_plugins', $args);
       
   200 	display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
       
   201 }
       
   202 
       
   203 add_action('install_plugins_upload', 'install_plugins_upload', 10, 1);
       
   204 /**
   137 /**
   205  * Upload from zip
   138  * Upload from zip
   206  * @since 2.8.0
   139  * @since 2.8.0
   207  *
   140  *
   208  * @param string $page
   141  * @param string $page
   209  */
   142  */
   210 function install_plugins_upload( $page = 1 ) {
   143 function install_plugins_upload( $page = 1 ) {
   211 ?>
   144 ?>
   212 	<h4><?php _e('Install a plugin in .zip format') ?></h4>
   145 	<h4><?php _e('Install a plugin in .zip format') ?></h4>
   213 	<p class="install-help"><?php _e('If you have a plugin in a .zip format, You may install it by uploading it here.') ?></p>
   146 	<p class="install-help"><?php _e('If you have a plugin in a .zip format, you may install it by uploading it here.') ?></p>
   214 	<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-plugin') ?>">
   147 	<form method="post" enctype="multipart/form-data" action="<?php echo self_admin_url('update.php?action=upload-plugin') ?>">
   215 		<?php wp_nonce_field( 'plugin-upload') ?>
   148 		<?php wp_nonce_field( 'plugin-upload') ?>
   216 		<label class="screen-reader-text" for="pluginzip"><?php _e('Plugin zip file'); ?></label>
   149 		<label class="screen-reader-text" for="pluginzip"><?php _e('Plugin zip file'); ?></label>
   217 		<input type="file" id="pluginzip" name="pluginzip" />
   150 		<input type="file" id="pluginzip" name="pluginzip" />
   218 		<input type="submit" class="button" value="<?php esc_attr_e('Install Now') ?>" />
   151 		<input type="submit" class="button" value="<?php esc_attr_e('Install Now') ?>" />
   219 	</form>
   152 	</form>
   220 <?php
   153 <?php
   221 }
   154 }
   222 
   155 add_action('install_plugins_upload', 'install_plugins_upload', 10, 1);
   223 add_action('install_plugins_new', 'install_new', 10, 1);
   156 
   224 /**
   157 /**
   225  * Display new plugins.
   158  * Display plugin content based on plugin list.
   226  *
   159  *
   227  * @since 2.7.0
   160  * @since 2.7.0
   228  *
   161  */
   229  * @param string $page
   162 function display_plugins_table() {
   230  */
   163 	global $wp_list_table;
   231 function install_new($page = 1) {
   164 
   232 	$args = array('browse' => 'new', 'page' => $page);
   165 	$wp_list_table->display();
   233 	$api = plugins_api('query_plugins', $args);
   166 }
       
   167 add_action('install_plugins_search', 'display_plugins_table');
       
   168 add_action('install_plugins_featured', 'display_plugins_table');
       
   169 add_action('install_plugins_popular', 'display_plugins_table');
       
   170 add_action('install_plugins_new', 'display_plugins_table');
       
   171 
       
   172 /**
       
   173  * Determine the status we can perform on a plugin.
       
   174  *
       
   175  * @since 3.0.0
       
   176  */
       
   177 function install_plugin_install_status($api, $loop = false) {
       
   178 	// this function is called recursively, $loop prevents further loops.
       
   179 	if ( is_array($api) )
       
   180 		$api = (object) $api;
       
   181 
       
   182 	//Default to a "new" plugin
       
   183 	$status = 'install';
       
   184 	$url = false;
       
   185 
       
   186 	//Check to see if this plugin is known to be installed, and has an update awaiting it.
       
   187 	$update_plugins = get_site_transient('update_plugins');
       
   188 	if ( isset( $update_plugins->response ) ) {
       
   189 		foreach ( (array)$update_plugins->response as $file => $plugin ) {
       
   190 			if ( $plugin->slug === $api->slug ) {
       
   191 				$status = 'update_available';
       
   192 				$update_file = $file;
       
   193 				$version = $plugin->new_version;
       
   194 				if ( current_user_can('update_plugins') )
       
   195 					$url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $update_file), 'upgrade-plugin_' . $update_file);
       
   196 				break;
       
   197 			}
       
   198 		}
       
   199 	}
       
   200 
       
   201 	if ( 'install' == $status ) {
       
   202 		if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
       
   203 			$installed_plugin = get_plugins('/' . $api->slug);
       
   204 			if ( empty($installed_plugin) ) {
       
   205 				if ( current_user_can('install_plugins') )
       
   206 					$url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug);
       
   207 			} else {
       
   208 				$key = array_shift( $key = array_keys($installed_plugin) ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers
       
   209 				if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ){
       
   210 					$status = 'latest_installed';
       
   211 				} elseif ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '<') ) {
       
   212 					$status = 'newer_installed';
       
   213 					$version = $installed_plugin[ $key ]['Version'];
       
   214 				} else {
       
   215 					//If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh
       
   216 					if ( ! $loop ) {
       
   217 						delete_site_transient('update_plugins');
       
   218 						wp_update_plugins();
       
   219 						return install_plugin_install_status($api, true);
       
   220 					}
       
   221 				}
       
   222 			}
       
   223 		} else {
       
   224 			// "install" & no directory with that slug
       
   225 			if ( current_user_can('install_plugins') )
       
   226 				$url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug);
       
   227 		}
       
   228 	}
       
   229 	if ( isset($_GET['from']) )
       
   230 		$url .= '&amp;from=' . urlencode(stripslashes($_GET['from']));
       
   231 
       
   232 	return compact('status', 'url', 'version');
       
   233 }
       
   234 
       
   235 /**
       
   236  * Display plugin information in dialog box form.
       
   237  *
       
   238  * @since 2.7.0
       
   239  */
       
   240 function install_plugin_information() {
       
   241 	global $tab;
       
   242 
       
   243 	$api = plugins_api('plugin_information', array('slug' => stripslashes( $_REQUEST['plugin'] ) ));
       
   244 
   234 	if ( is_wp_error($api) )
   245 	if ( is_wp_error($api) )
   235 		wp_die($api);
   246 		wp_die($api);
   236 	display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
   247 
   237 }
   248 	$plugins_allowedtags = array(
   238 add_action('install_plugins_updated', 'install_updated', 10, 1);
   249 		'a' => array( 'href' => array(), 'title' => array(), 'target' => array() ),
   239 
   250 		'abbr' => array( 'title' => array() ), 'acronym' => array( 'title' => array() ),
   240 
   251 		'code' => array(), 'pre' => array(), 'em' => array(), 'strong' => array(),
   241 /**
   252 		'div' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(),
   242  * Display recently updated plugins.
   253 		'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
   243  *
   254 		'img' => array( 'src' => array(), 'class' => array(), 'alt' => array() )
   244  * @since 2.7.0
   255 	);
   245  *
   256 
   246  * @param string $page
   257 	$plugins_section_titles = array(
   247  */
   258 		'description'  => _x('Description',  'Plugin installer section title'),
   248 function install_updated($page = 1) {
   259 		'installation' => _x('Installation', 'Plugin installer section title'),
   249 	$args = array('browse' => 'updated', 'page' => $page);
   260 		'faq'          => _x('FAQ',          'Plugin installer section title'),
   250 	$api = plugins_api('query_plugins', $args);
   261 		'screenshots'  => _x('Screenshots',  'Plugin installer section title'),
   251 	display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
   262 		'changelog'    => _x('Changelog',    'Plugin installer section title'),
   252 }
   263 		'other_notes'  => _x('Other Notes',  'Plugin installer section title')
   253 
   264 	);
   254 /**
   265 
   255  * Display plugin content based on plugin list.
       
   256  *
       
   257  * @since 2.7.0
       
   258  *
       
   259  * @param array $plugins List of plugins.
       
   260  * @param string $page
       
   261  * @param int $totalpages Number of pages.
       
   262  */
       
   263 function display_plugins_table($plugins, $page = 1, $totalpages = 1){
       
   264 	$type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
       
   265 	$term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
       
   266 
       
   267 	$plugins_allowedtags = array('a' => array('href' => array(),'title' => array(), 'target' => array()),
       
   268 								'abbr' => array('title' => array()),'acronym' => array('title' => array()),
       
   269 								'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
       
   270 								'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array());
       
   271 
       
   272 ?>
       
   273 	<div class="tablenav">
       
   274 		<div class="alignleft actions">
       
   275 		<?php do_action('install_plugins_table_header'); ?>
       
   276 		</div>
       
   277 		<?php
       
   278 			$url = esc_url($_SERVER['REQUEST_URI']);
       
   279 			if ( ! empty($term) )
       
   280 				$url = add_query_arg('s', $term, $url);
       
   281 			if ( ! empty($type) )
       
   282 				$url = add_query_arg('type', $type, $url);
       
   283 
       
   284 			$page_links = paginate_links( array(
       
   285 				'base' => add_query_arg('paged', '%#%', $url),
       
   286 				'format' => '',
       
   287 				'prev_text' => __('&laquo;'),
       
   288 				'next_text' => __('&raquo;'),
       
   289 				'total' => $totalpages,
       
   290 				'current' => $page
       
   291 			));
       
   292 
       
   293 			if ( $page_links )
       
   294 				echo "\t\t<div class='tablenav-pages'>$page_links</div>";
       
   295 ?>
       
   296 		<br class="clear" />
       
   297 	</div>
       
   298 	<table class="widefat" id="install-plugins" cellspacing="0">
       
   299 		<thead>
       
   300 			<tr>
       
   301 				<th scope="col" class="name"><?php _e('Name'); ?></th>
       
   302 				<th scope="col" class="num"><?php _e('Version'); ?></th>
       
   303 				<th scope="col" class="num"><?php _e('Rating'); ?></th>
       
   304 				<th scope="col" class="desc"><?php _e('Description'); ?></th>
       
   305 				<th scope="col" class="action-links"><?php _e('Actions'); ?></th>
       
   306 			</tr>
       
   307 		</thead>
       
   308 
       
   309 		<tfoot>
       
   310 			<tr>
       
   311 				<th scope="col" class="name"><?php _e('Name'); ?></th>
       
   312 				<th scope="col" class="num"><?php _e('Version'); ?></th>
       
   313 				<th scope="col" class="num"><?php _e('Rating'); ?></th>
       
   314 				<th scope="col" class="desc"><?php _e('Description'); ?></th>
       
   315 				<th scope="col" class="action-links"><?php _e('Actions'); ?></th>
       
   316 			</tr>
       
   317 		</tfoot>
       
   318 
       
   319 		<tbody class="plugins">
       
   320 		<?php
       
   321 			if( empty($plugins) )
       
   322 				echo '<tr><td colspan="5">', __('No plugins match your request.'), '</td></tr>';
       
   323 
       
   324 			foreach( (array) $plugins as $plugin ){
       
   325 				if ( is_object($plugin) )
       
   326 					$plugin = (array) $plugin;
       
   327 
       
   328 				$title = wp_kses($plugin['name'], $plugins_allowedtags);
       
   329 				//Limit description to 400char, and remove any HTML.
       
   330 				$description = strip_tags($plugin['description']);
       
   331 				if ( strlen($description) > 400 )
       
   332 					$description = mb_substr($description, 0, 400) . '&#8230;';
       
   333 				//remove any trailing entities
       
   334 				$description = preg_replace('/&[^;\s]{0,6}$/', '', $description);
       
   335 				//strip leading/trailing & multiple consecutive lines
       
   336 				$description = trim($description);
       
   337 				$description = preg_replace("|(\r?\n)+|", "\n", $description);
       
   338 				//\n => <br>
       
   339 				$description = nl2br($description);
       
   340 				$version = wp_kses($plugin['version'], $plugins_allowedtags);
       
   341 
       
   342 				$name = strip_tags($title . ' ' . $version);
       
   343 
       
   344 				$author = $plugin['author'];
       
   345 				if( ! empty($plugin['author']) )
       
   346 					$author = ' <cite>' . sprintf( __('By %s'), $author ) . '.</cite>';
       
   347 
       
   348 				$author = wp_kses($author, $plugins_allowedtags);
       
   349 
       
   350 				if( isset($plugin['homepage']) )
       
   351 					$title = '<a target="_blank" href="' . esc_attr($plugin['homepage']) . '">' . $title . '</a>';
       
   352 
       
   353 				$action_links = array();
       
   354 				$action_links[] = '<a href="' . admin_url('plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
       
   355 									'&amp;TB_iframe=true&amp;width=600&amp;height=550') . '" class="thickbox onclick" title="' .
       
   356 									esc_attr($name) . '">' . __('Install') . '</a>';
       
   357 
       
   358 				$action_links = apply_filters('plugin_install_action_links', $action_links, $plugin);
       
   359 			?>
       
   360 			<tr>
       
   361 				<td class="name"><?php echo $title; ?></td>
       
   362 				<td class="vers"><?php echo $version; ?></td>
       
   363 				<td class="vers">
       
   364 					<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'])) ?>">
       
   365 						<div class="star star-rating" style="width: <?php echo esc_attr($plugin['rating']) ?>px"></div>
       
   366 						<div class="star star5"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('5 stars') ?>" /></div>
       
   367 						<div class="star star4"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('4 stars') ?>" /></div>
       
   368 						<div class="star star3"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('3 stars') ?>" /></div>
       
   369 						<div class="star star2"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('2 stars') ?>" /></div>
       
   370 						<div class="star star1"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('1 star') ?>" /></div>
       
   371 					</div>
       
   372 				</td>
       
   373 				<td class="desc"><?php echo $description, $author; ?></td>
       
   374 				<td class="action-links"><?php if ( !empty($action_links) )	echo implode(' | ', $action_links); ?></td>
       
   375 			</tr>
       
   376 			<?php
       
   377 			}
       
   378 			?>
       
   379 		</tbody>
       
   380 	</table>
       
   381 
       
   382 	<div class="tablenav">
       
   383 		<?php if ( $page_links )
       
   384 				echo "\t\t<div class='tablenav-pages'>$page_links</div>"; ?>
       
   385 		<br class="clear" />
       
   386 	</div>
       
   387 
       
   388 <?php
       
   389 }
       
   390 
       
   391 add_action('install_plugins_pre_plugin-information', 'install_plugin_information');
       
   392 
       
   393 /**
       
   394  * Display plugin information in dialog box form.
       
   395  *
       
   396  * @since 2.7.0
       
   397  */
       
   398 function install_plugin_information() {
       
   399 	global $tab;
       
   400 
       
   401 	$api = plugins_api('plugin_information', array('slug' => stripslashes( $_REQUEST['plugin'] ) ));
       
   402 
       
   403 	if ( is_wp_error($api) )
       
   404 		wp_die($api);
       
   405 
       
   406 	$plugins_allowedtags = array('a' => array('href' => array(), 'title' => array(), 'target' => array()),
       
   407 								'abbr' => array('title' => array()), 'acronym' => array('title' => array()),
       
   408 								'code' => array(), 'pre' => array(), 'em' => array(), 'strong' => array(),
       
   409 								'div' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(),
       
   410 								'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
       
   411 								'img' => array('src' => array(), 'class' => array(), 'alt' => array()));
       
   412 	//Sanitize HTML
   266 	//Sanitize HTML
   413 	foreach ( (array)$api->sections as $section_name => $content )
   267 	foreach ( (array)$api->sections as $section_name => $content )
   414 		$api->sections[$section_name] = wp_kses($content, $plugins_allowedtags);
   268 		$api->sections[$section_name] = wp_kses($content, $plugins_allowedtags);
   415 	foreach ( array('version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug') as $key )
   269 	foreach ( array( 'version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug' ) as $key ) {
   416 		$api->$key = wp_kses($api->$key, $plugins_allowedtags);
   270 		if ( isset( $api->$key ) )
       
   271 			$api->$key = wp_kses( $api->$key, $plugins_allowedtags );
       
   272 	}
   417 
   273 
   418 	$section = isset($_REQUEST['section']) ? stripslashes( $_REQUEST['section'] ) : 'description'; //Default to the Description tab, Do not translate, API returns English.
   274 	$section = isset($_REQUEST['section']) ? stripslashes( $_REQUEST['section'] ) : 'description'; //Default to the Description tab, Do not translate, API returns English.
   419 	if( empty($section) || ! isset($api->sections[ $section ]) )
   275 	if ( empty($section) || ! isset($api->sections[ $section ]) )
   420 		$section = array_shift( $section_titles = array_keys((array)$api->sections) );
   276 		$section = array_shift( $section_titles = array_keys((array)$api->sections) );
   421 
   277 
   422 	iframe_header( __('Plugin Install') );
   278 	iframe_header( __('Plugin Install') );
   423 	echo "<div id='$tab-header'>\n";
   279 	echo "<div id='$tab-header'>\n";
   424 	echo "<ul id='sidemenu'>\n";
   280 	echo "<ul id='sidemenu'>\n";
   425 	foreach ( (array)$api->sections as $section_name => $content ) {
   281 	foreach ( (array)$api->sections as $section_name => $content ) {
   426 
   282 
   427 		$title = $section_name;
   283 		if ( isset( $plugins_section_titles[ $section_name ] ) )
   428 		$title = ucwords(str_replace('_', ' ', $title));
   284 			$title = $plugins_section_titles[ $section_name ];
       
   285 		else
       
   286 			$title = ucwords( str_replace( '_', ' ', $section_name ) );
   429 
   287 
   430 		$class = ( $section_name == $section ) ? ' class="current"' : '';
   288 		$class = ( $section_name == $section ) ? ' class="current"' : '';
   431 		$href = add_query_arg( array('tab' => $tab, 'section' => $section_name) );
   289 		$href = add_query_arg( array('tab' => $tab, 'section' => $section_name) );
   432 		$href = esc_url($href);
   290 		$href = esc_url($href);
   433 		$san_title = esc_attr(sanitize_title_with_dashes($title));
   291 		$san_section = esc_attr( $section_name );
   434 		echo "\t<li><a name='$san_title' target='' href='$href'$class>$title</a></li>\n";
   292 		echo "\t<li><a name='$san_section' href='$href' $class>$title</a></li>\n";
   435 	}
   293 	}
   436 	echo "</ul>\n";
   294 	echo "</ul>\n";
   437 	echo "</div>\n";
   295 	echo "</div>\n";
   438 	?>
   296 	?>
   439 	<div class="alignright fyi">
   297 	<div class="alignright fyi">
   440 		<?php if ( ! empty($api->download_link) ) : ?>
   298 		<?php if ( ! empty($api->download_link) && ( current_user_can('install_plugins') || current_user_can('update_plugins') ) ) : ?>
   441 		<p class="action-button">
   299 		<p class="action-button">
   442 		<?php
   300 		<?php
   443 			//Default to a "new" plugin
   301 		$status = install_plugin_install_status($api);
   444 			$type = 'install';
   302 		switch ( $status['status'] ) {
   445 			//Check to see if this plugin is known to be installed, and has an update awaiting it.
   303 			case 'install':
   446 			$update_plugins = get_transient('update_plugins');
   304 				if ( $status['url'] )
   447 			if ( is_object( $update_plugins ) ) {
   305 					echo '<a href="' . $status['url'] . '" target="_parent">' . __('Install Now') . '</a>';
   448 				foreach ( (array)$update_plugins->response as $file => $plugin ) {
   306 				break;
   449 					if ( $plugin->slug === $api->slug ) {
   307 			case 'update_available':
   450 						$type = 'update_available';
   308 				if ( $status['url'] )
   451 						$update_file = $file;
   309 					echo '<a href="' . $status['url'] . '" target="_parent">' . __('Install Update Now') .'</a>';
   452 						break;
   310 				break;
   453 					}
   311 			case 'newer_installed':
   454 				}
   312 				echo '<a>' . sprintf(__('Newer Version (%s) Installed'), $status['version']) . '</a>';
   455 			}
   313 				break;
   456 			if ( 'install' == $type && is_dir( WP_PLUGIN_DIR  . '/' . $api->slug ) ) {
   314 			case 'latest_installed':
   457 				$installed_plugin = get_plugins('/' . $api->slug);
   315 				echo '<a>' . __('Latest Version Installed') . '</a>';
   458 				if ( ! empty($installed_plugin) ) {
   316 				break;
   459 					$key = array_shift( $key = array_keys($installed_plugin) ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers
   317 		}
   460 					if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ){
   318 		?>
   461 						$type = 'latest_installed';
       
   462 					} elseif ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '<') ) {
       
   463 						$type = 'newer_installed';
       
   464 						$newer_version = $installed_plugin[ $key ]['Version'];
       
   465 					} else {
       
   466 						//If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh
       
   467 						delete_transient('update_plugins');
       
   468 						$update_file = $api->slug . '/' . $key; //This code branch only deals with a plugin which is in a folder the same name as its slug, Doesnt support plugins which have 'non-standard' names
       
   469 						$type = 'update_available';
       
   470 					}
       
   471 				}
       
   472 			}
       
   473 
       
   474 			switch ( $type ) :
       
   475 				default:
       
   476 				case 'install':
       
   477 					if ( current_user_can('install_plugins') ) :
       
   478 				?><a href="<?php echo wp_nonce_url(admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug) ?>" target="_parent"><?php _e('Install Now') ?></a><?php
       
   479 					endif;
       
   480 				break;
       
   481 				case 'update_available':
       
   482 					if ( current_user_can('update_plugins') ) :
       
   483 						?><a href="<?php echo wp_nonce_url(admin_url('update.php?action=upgrade-plugin&plugin=' . $update_file), 'upgrade-plugin_' . $update_file) ?>" target="_parent"><?php _e('Install Update Now') ?></a><?php
       
   484 					endif;
       
   485 				break;
       
   486 				case 'newer_installed':
       
   487 					if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
       
   488 					?><a><?php printf(__('Newer Version (%s) Installed'), $newer_version) ?></a><?php
       
   489 					endif;
       
   490 				break;
       
   491 				case 'latest_installed':
       
   492 					if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
       
   493 					?><a><?php _e('Latest Version Installed') ?></a><?php
       
   494 					endif;
       
   495 				break;
       
   496 			endswitch; ?>
       
   497 		</p>
   319 		</p>
   498 		<?php endif; ?>
   320 		<?php endif; ?>
   499 		<h2 class="mainheader"><?php _e('FYI') ?></h2>
   321 		<h2 class="mainheader"><?php /* translators: For Your Information */ _e('FYI') ?></h2>
   500 		<ul>
   322 		<ul>
   501 <?php if ( ! empty($api->version) ) : ?>
   323 <?php if ( ! empty($api->version) ) : ?>
   502 			<li><strong><?php _e('Version:') ?></strong> <?php echo $api->version ?></li>
   324 			<li><strong><?php _e('Version:') ?></strong> <?php echo $api->version ?></li>
   503 <?php endif; if ( ! empty($api->author) ) : ?>
   325 <?php endif; if ( ! empty($api->author) ) : ?>
   504 			<li><strong><?php _e('Author:') ?></strong> <?php echo links_add_target($api->author, '_blank') ?></li>
   326 			<li><strong><?php _e('Author:') ?></strong> <?php echo links_add_target($api->author, '_blank') ?></li>
   512 <?php endif; if ( ! empty($api->downloaded) ) : ?>
   334 <?php endif; if ( ! empty($api->downloaded) ) : ?>
   513 			<li><strong><?php _e('Downloaded:') ?></strong> <?php printf(_n('%s time', '%s times', $api->downloaded), number_format_i18n($api->downloaded)) ?></li>
   335 			<li><strong><?php _e('Downloaded:') ?></strong> <?php printf(_n('%s time', '%s times', $api->downloaded), number_format_i18n($api->downloaded)) ?></li>
   514 <?php endif; if ( ! empty($api->slug) && empty($api->external) ) : ?>
   336 <?php endif; if ( ! empty($api->slug) && empty($api->external) ) : ?>
   515 			<li><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $api->slug ?>/"><?php _e('WordPress.org Plugin Page &#187;') ?></a></li>
   337 			<li><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $api->slug ?>/"><?php _e('WordPress.org Plugin Page &#187;') ?></a></li>
   516 <?php endif; if ( ! empty($api->homepage) ) : ?>
   338 <?php endif; if ( ! empty($api->homepage) ) : ?>
   517 			<li><a target="_blank" href="<?php echo $api->homepage ?>"><?php _e('Plugin Homepage  &#187;') ?></a></li>
   339 			<li><a target="_blank" href="<?php echo $api->homepage ?>"><?php _e('Plugin Homepage &#187;') ?></a></li>
   518 <?php endif; ?>
   340 <?php endif; ?>
   519 		</ul>
   341 		</ul>
   520 		<?php if ( ! empty($api->rating) ) : ?>
   342 		<?php if ( ! empty($api->rating) ) : ?>
   521 		<h2><?php _e('Average Rating') ?></h2>
   343 		<h2><?php _e('Average Rating') ?></h2>
   522 		<div class="star-holder" title="<?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?>">
   344 		<div class="star-holder" title="<?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?>">
   523 			<div class="star star-rating" style="width: <?php echo esc_attr($api->rating) ?>px"></div>
   345 			<div class="star star-rating" style="width: <?php echo esc_attr( str_replace( ',', '.', $api->rating ) ); ?>px"></div>
   524 			<div class="star star5"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('5 stars') ?>" /></div>
       
   525 			<div class="star star4"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('4 stars') ?>" /></div>
       
   526 			<div class="star star3"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('3 stars') ?>" /></div>
       
   527 			<div class="star star2"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('2 stars') ?>" /></div>
       
   528 			<div class="star star1"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('1 star') ?>" /></div>
       
   529 		</div>
   346 		</div>
   530 		<small><?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?></small>
   347 		<small><?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?></small>
   531 		<?php endif; ?>
   348 		<?php endif; ?>
   532 	</div>
   349 	</div>
   533 	<div id="section-holder" class="wrap">
   350 	<div id="section-holder" class="wrap">
   537 
   354 
   538 		else if ( !empty($api->requires) && version_compare( substr($GLOBALS['wp_version'], 0, strlen($api->requires)), $api->requires, '<') )
   355 		else if ( !empty($api->requires) && version_compare( substr($GLOBALS['wp_version'], 0, strlen($api->requires)), $api->requires, '<') )
   539 			echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has <strong>not been marked as compatible</strong> with your version of WordPress.') . '</p></div>';
   356 			echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has <strong>not been marked as compatible</strong> with your version of WordPress.') . '</p></div>';
   540 
   357 
   541 		foreach ( (array)$api->sections as $section_name => $content ) {
   358 		foreach ( (array)$api->sections as $section_name => $content ) {
   542 			$title = $section_name;
   359 
   543 			$title[0] = strtoupper($title[0]);
   360 			if ( isset( $plugins_section_titles[ $section_name ] ) )
   544 			$title = str_replace('_', ' ', $title);
   361 				$title = $plugins_section_titles[ $section_name ];
       
   362 			else
       
   363 				$title = ucwords( str_replace( '_', ' ', $section_name ) );
   545 
   364 
   546 			$content = links_add_base_url($content, 'http://wordpress.org/extend/plugins/' . $api->slug . '/');
   365 			$content = links_add_base_url($content, 'http://wordpress.org/extend/plugins/' . $api->slug . '/');
   547 			$content = links_add_target($content, '_blank');
   366 			$content = links_add_target($content, '_blank');
   548 
   367 
   549 			$san_title = esc_attr(sanitize_title_with_dashes($title));
   368 			$san_section = esc_attr( $section_name );
   550 
   369 
   551 			$display = ( $section_name == $section ) ? 'block' : 'none';
   370 			$display = ( $section_name == $section ) ? 'block' : 'none';
   552 
   371 
   553 			echo "\t<div id='section-{$san_title}' class='section' style='display: {$display};'>\n";
   372 			echo "\t<div id='section-{$san_section}' class='section' style='display: {$display};'>\n";
   554 			echo "\t\t<h2 class='long-header'>$title</h2>";
   373 			echo "\t\t<h2 class='long-header'>$title</h2>";
   555 			echo $content;
   374 			echo $content;
   556 			echo "\t</div>\n";
   375 			echo "\t</div>\n";
   557 		}
   376 		}
   558 	echo "</div>\n";
   377 	echo "</div>\n";
   559 
   378 
   560 	iframe_footer();
   379 	iframe_footer();
   561 	exit;
   380 	exit;
   562 }
   381 }
       
   382 add_action('install_plugins_pre_plugin-information', 'install_plugin_information');