3 * WordPress Plugin Install Administration API
6 * @subpackage Administration
10 * Retrieve plugin installer pages from WordPress Plugins API.
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
14 * offer more choices. This is very powerful and must be used with care, when
15 * overridding the filters.
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
21 * The second filter, 'plugins_api', is the result that would be returned.
25 * @param string $action
26 * @param array|object $args Optional. Arguments to serialize for the Plugin Info API.
29 function plugins_api($action, $args = null) {
32 $args = (object)$args;
34 if ( !isset($args->per_page) )
37 $args = apply_filters('plugins_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
38 $res = apply_filters('plugins_api', false, $action, $args); //NOTE: Allows a plugin to completely override the builtin WordPress.org API.
41 $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
42 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() );
45 $res = unserialize($request['body']);
47 $res = new WP_Error('plugins_api_failed', __('An unknown error occurred'), $request['body']);
51 return apply_filters('plugins_api_result', $res, $action, $args);
55 * Retrieve popular WordPress plugin tags.
62 function install_popular_tags( $args = array() ) {
63 if ( ! ($cache = wp_cache_get('popular_tags', 'api')) && ! ($cache = get_option('wporg_popular_tags')) )
64 add_option('wporg_popular_tags', array(), '', 'no'); ///No autoload.
66 if ( $cache && $cache->timeout + 3 * 60 * 60 > time() )
67 return $cache->cached;
69 $tags = plugins_api('hot_tags', $args);
71 if ( is_wp_error($tags) )
74 $cache = (object) array('timeout' => time(), 'cached' => $tags);
76 update_option('wporg_popular_tags', $cache);
77 wp_cache_set('popular_tags', $cache, 'api');
81 add_action('install_plugins_search', 'install_search', 10, 1);
84 * Display search results and display as tag cloud.
90 function install_search($page) {
91 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
92 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
98 $args['tag'] = sanitize_title_with_dashes($term);
101 $args['search'] = $term;
104 $args['author'] = $term;
108 $args['page'] = $page;
110 $api = plugins_api('query_plugins', $args);
112 if ( is_wp_error($api) )
115 add_action('install_plugins_table_header', 'install_search_form');
117 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
122 add_action('install_plugins_dashboard', 'install_dashboard');
123 function install_dashboard() {
125 <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>
127 <h4><?php _e('Search') ?></h4>
128 <p class="install-help"><?php _e('Search for plugins by keyword, author, or tag.') ?></p>
129 <?php install_search_form(); ?>
131 <h4><?php _e('Popular tags') ?></h4>
132 <p class="install-help"><?php _e('You may also browse based on the most popular tags in the Plugin Directory:') ?></p>
135 $api_tags = install_popular_tags();
137 //Set up the tags in a way which can be interprated by wp_generate_tag_cloud()
139 foreach ( (array)$api_tags as $tag )
140 $tags[ $tag['name'] ] = (object) array(
141 'link' => esc_url( admin_url('plugin-install.php?tab=search&type=tag&s=' . urlencode($tag['name'])) ),
142 'name' => $tag['name'],
143 'id' => sanitize_title_with_dashes($tag['name']),
144 'count' => $tag['count'] );
145 echo '<p class="popular-tags">';
146 echo wp_generate_tag_cloud($tags, array( 'single_text' => __('%d plugin'), 'multiple_text' => __('%d plugins') ) );
147 echo '</p><br class="clear" />';
151 * Display search form for searching plugins.
155 function install_search_form(){
156 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
157 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
159 ?><form id="search-plugins" method="post" action="<?php echo admin_url('plugin-install.php?tab=search'); ?>">
160 <select name="type" id="typeselector">
161 <option value="term"<?php selected('term', $type) ?>><?php _e('Term'); ?></option>
162 <option value="author"<?php selected('author', $type) ?>><?php _e('Author'); ?></option>
163 <option value="tag"<?php selected('tag', $type) ?>><?php echo _x('Tag', 'Plugin Installer'); ?></option>
165 <input type="text" name="s" value="<?php echo esc_attr($term) ?>" />
166 <label class="screen-reader-text" for="plugin-search-input"><?php _e('Search Plugins'); ?></label>
167 <input type="submit" id="plugin-search-input" name="search" value="<?php esc_attr_e('Search Plugins'); ?>" class="button" />
171 add_action('install_plugins_featured', 'install_featured', 10, 1);
173 * Display featured plugins.
177 * @param string $page
179 function install_featured($page = 1) {
180 $args = array('browse' => 'featured', 'page' => $page);
181 $api = plugins_api('query_plugins', $args);
182 if ( is_wp_error($api) )
184 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
187 add_action('install_plugins_popular', 'install_popular', 10, 1);
189 * Display popular plugins.
193 * @param string $page
195 function install_popular($page = 1) {
196 $args = array('browse' => 'popular', 'page' => $page);
197 $api = plugins_api('query_plugins', $args);
198 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
201 add_action('install_plugins_upload', 'install_plugins_upload', 10, 1);
206 * @param string $page
208 function install_plugins_upload( $page = 1 ) {
210 <h4><?php _e('Install a plugin in .zip format') ?></h4>
211 <p class="install-help"><?php _e('If you have a plugin in a .zip format, You may install it by uploading it here.') ?></p>
212 <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-plugin') ?>">
213 <?php wp_nonce_field( 'plugin-upload') ?>
214 <label class="screen-reader-text" for="pluginzip"><?php _e('Plugin zip file'); ?></label>
215 <input type="file" id="pluginzip" name="pluginzip" />
216 <input type="submit" class="button" value="<?php esc_attr_e('Install Now') ?>" />
221 add_action('install_plugins_new', 'install_new', 10, 1);
223 * Display new plugins.
227 * @param string $page
229 function install_new($page = 1) {
230 $args = array('browse' => 'new', 'page' => $page);
231 $api = plugins_api('query_plugins', $args);
232 if ( is_wp_error($api) )
234 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
236 add_action('install_plugins_updated', 'install_updated', 10, 1);
240 * Display recently updated plugins.
244 * @param string $page
246 function install_updated($page = 1) {
247 $args = array('browse' => 'updated', 'page' => $page);
248 $api = plugins_api('query_plugins', $args);
249 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
253 * Display plugin content based on plugin list.
257 * @param array $plugins List of plugins.
258 * @param string $page
259 * @param int $totalpages Number of pages.
261 function display_plugins_table($plugins, $page = 1, $totalpages = 1){
262 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
263 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
265 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array(), 'target' => array()),
266 'abbr' => array('title' => array()),'acronym' => array('title' => array()),
267 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
268 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array());
271 <div class="tablenav">
272 <div class="alignleft actions">
273 <?php do_action('install_plugins_table_header'); ?>
276 $url = esc_url($_SERVER['REQUEST_URI']);
277 if ( ! empty($term) )
278 $url = add_query_arg('s', $term, $url);
279 if ( ! empty($type) )
280 $url = add_query_arg('type', $type, $url);
282 $page_links = paginate_links( array(
283 'base' => add_query_arg('paged', '%#%', $url),
285 'prev_text' => __('«'),
286 'next_text' => __('»'),
287 'total' => $totalpages,
292 echo "\t\t<div class='tablenav-pages'>$page_links</div>";
296 <table class="widefat" id="install-plugins" cellspacing="0">
299 <th scope="col" class="name"><?php _e('Name'); ?></th>
300 <th scope="col" class="num"><?php _e('Version'); ?></th>
301 <th scope="col" class="num"><?php _e('Rating'); ?></th>
302 <th scope="col" class="desc"><?php _e('Description'); ?></th>
303 <th scope="col" class="action-links"><?php _e('Actions'); ?></th>
309 <th scope="col" class="name"><?php _e('Name'); ?></th>
310 <th scope="col" class="num"><?php _e('Version'); ?></th>
311 <th scope="col" class="num"><?php _e('Rating'); ?></th>
312 <th scope="col" class="desc"><?php _e('Description'); ?></th>
313 <th scope="col" class="action-links"><?php _e('Actions'); ?></th>
317 <tbody class="plugins">
319 if( empty($plugins) )
320 echo '<tr><td colspan="5">', __('No plugins match your request.'), '</td></tr>';
322 foreach( (array) $plugins as $plugin ){
323 if ( is_object($plugin) )
324 $plugin = (array) $plugin;
326 $title = wp_kses($plugin['name'], $plugins_allowedtags);
327 //Limit description to 400char, and remove any HTML.
328 $description = strip_tags($plugin['description']);
329 if ( strlen($description) > 400 )
330 $description = mb_substr($description, 0, 400) . '…';
331 //remove any trailing entities
332 $description = preg_replace('/&[^;\s]{0,6}$/', '', $description);
333 //strip leading/trailing & multiple consecutive lines
334 $description = trim($description);
335 $description = preg_replace("|(\r?\n)+|", "\n", $description);
337 $description = nl2br($description);
338 $version = wp_kses($plugin['version'], $plugins_allowedtags);
340 $name = strip_tags($title . ' ' . $version);
342 $author = $plugin['author'];
343 if( ! empty($plugin['author']) )
344 $author = ' <cite>' . sprintf( __('By %s'), $author ) . '.</cite>';
346 $author = wp_kses($author, $plugins_allowedtags);
348 if( isset($plugin['homepage']) )
349 $title = '<a target="_blank" href="' . esc_attr($plugin['homepage']) . '">' . $title . '</a>';
351 $action_links = array();
352 $action_links[] = '<a href="' . admin_url('plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
353 '&TB_iframe=true&width=600&height=550') . '" class="thickbox onclick" title="' .
354 esc_attr($name) . '">' . __('Install') . '</a>';
356 $action_links = apply_filters('plugin_install_action_links', $action_links, $plugin);
359 <td class="name"><?php echo $title; ?></td>
360 <td class="vers"><?php echo $version; ?></td>
362 <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'])) ?>">
363 <div class="star star-rating" style="width: <?php echo esc_attr($plugin['rating']) ?>px"></div>
364 <div class="star star5"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('5 stars') ?>" /></div>
365 <div class="star star4"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('4 stars') ?>" /></div>
366 <div class="star star3"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('3 stars') ?>" /></div>
367 <div class="star star2"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('2 stars') ?>" /></div>
368 <div class="star star1"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('1 star') ?>" /></div>
371 <td class="desc"><?php echo $description, $author; ?></td>
372 <td class="action-links"><?php if ( !empty($action_links) ) echo implode(' | ', $action_links); ?></td>
380 <div class="tablenav">
381 <?php if ( $page_links )
382 echo "\t\t<div class='tablenav-pages'>$page_links</div>"; ?>
389 add_action('install_plugins_pre_plugin-information', 'install_plugin_information');
392 * Display plugin information in dialog box form.
396 function install_plugin_information() {
399 $api = plugins_api('plugin_information', array('slug' => stripslashes( $_REQUEST['plugin'] ) ));
401 if ( is_wp_error($api) )
404 $plugins_allowedtags = array('a' => array('href' => array(), 'title' => array(), 'target' => array()),
405 'abbr' => array('title' => array()), 'acronym' => array('title' => array()),
406 'code' => array(), 'pre' => array(), 'em' => array(), 'strong' => array(),
407 'div' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(),
408 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
409 'img' => array('src' => array(), 'class' => array(), 'alt' => array()));
411 foreach ( (array)$api->sections as $section_name => $content )
412 $api->sections[$section_name] = wp_kses($content, $plugins_allowedtags);
413 foreach ( array('version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug') as $key )
414 $api->$key = wp_kses($api->$key, $plugins_allowedtags);
416 $section = isset($_REQUEST['section']) ? stripslashes( $_REQUEST['section'] ) : 'description'; //Default to the Description tab, Do not translate, API returns English.
417 if( empty($section) || ! isset($api->sections[ $section ]) )
418 $section = array_shift( $section_titles = array_keys((array)$api->sections) );
420 iframe_header( __('Plugin Install') );
421 echo "<div id='$tab-header'>\n";
422 echo "<ul id='sidemenu'>\n";
423 foreach ( (array)$api->sections as $section_name => $content ) {
425 $title = $section_name;
426 $title = ucwords(str_replace('_', ' ', $title));
428 $class = ( $section_name == $section ) ? ' class="current"' : '';
429 $href = add_query_arg( array('tab' => $tab, 'section' => $section_name) );
430 $href = esc_url($href);
431 $san_title = esc_attr(sanitize_title_with_dashes($title));
432 echo "\t<li><a name='$san_title' target='' href='$href'$class>$title</a></li>\n";
437 <div class="alignright fyi">
438 <?php if ( ! empty($api->download_link) ) : ?>
439 <p class="action-button">
441 //Default to a "new" plugin
443 //Check to see if this plugin is known to be installed, and has an update awaiting it.
444 $update_plugins = get_option('update_plugins');
445 if ( is_object( $update_plugins ) ) {
446 foreach ( (array)$update_plugins->response as $file => $plugin ) {
447 if ( $plugin->slug === $api->slug ) {
448 $type = 'update_available';
449 $update_file = $file;
454 if ( 'install' == $type && is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
455 $installed_plugin = get_plugins('/' . $api->slug);
456 if ( ! empty($installed_plugin) ) {
457 $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
458 if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ){
459 $type = 'latest_installed';
460 } elseif ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '<') ) {
461 $type = 'newer_installed';
462 $newer_version = $installed_plugin[ $key ]['Version'];
464 //If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh
465 delete_option('update_plugins');
466 $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
467 $type = 'update_available';
475 if ( current_user_can('install_plugins') ) :
476 ?><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 case 'update_available':
480 if ( current_user_can('update_plugins') ) :
481 ?><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 case 'newer_installed':
485 if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
486 ?><a><?php printf(__('Newer Version (%s) Installed'), $newer_version) ?></a><?php
489 case 'latest_installed':
490 if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
491 ?><a><?php _e('Latest Version Installed') ?></a><?php
497 <h2 class="mainheader"><?php _e('FYI') ?></h2>
499 <?php if ( ! empty($api->version) ) : ?>
500 <li><strong><?php _e('Version:') ?></strong> <?php echo $api->version ?></li>
501 <?php endif; if ( ! empty($api->author) ) : ?>
502 <li><strong><?php _e('Author:') ?></strong> <?php echo links_add_target($api->author, '_blank') ?></li>
503 <?php endif; if ( ! empty($api->last_updated) ) : ?>
504 <li><strong><?php _e('Last Updated:') ?></strong> <span title="<?php echo $api->last_updated ?>"><?php
505 printf( __('%s ago'), human_time_diff(strtotime($api->last_updated)) ) ?></span></li>
506 <?php endif; if ( ! empty($api->requires) ) : ?>
507 <li><strong><?php _e('Requires WordPress Version:') ?></strong> <?php printf(__('%s or higher'), $api->requires) ?></li>
508 <?php endif; if ( ! empty($api->tested) ) : ?>
509 <li><strong><?php _e('Compatible up to:') ?></strong> <?php echo $api->tested ?></li>
510 <?php endif; if ( ! empty($api->downloaded) ) : ?>
511 <li><strong><?php _e('Downloaded:') ?></strong> <?php printf(_n('%s time', '%s times', $api->downloaded), number_format_i18n($api->downloaded)) ?></li>
512 <?php endif; if ( ! empty($api->slug) ) : ?>
513 <li><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $api->slug ?>/"><?php _e('WordPress.org Plugin Page »') ?></a></li>
514 <?php endif; if ( ! empty($api->homepage) ) : ?>
515 <li><a target="_blank" href="<?php echo $api->homepage ?>"><?php _e('Plugin Homepage »') ?></a></li>
518 <h2><?php _e('Average Rating') ?></h2>
519 <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)); ?>">
520 <div class="star star-rating" style="width: <?php echo esc_attr($api->rating) ?>px"></div>
521 <div class="star star5"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('5 stars') ?>" /></div>
522 <div class="star star4"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('4 stars') ?>" /></div>
523 <div class="star star3"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('3 stars') ?>" /></div>
524 <div class="star star2"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('2 stars') ?>" /></div>
525 <div class="star star1"><img src="<?php echo admin_url('images/star.gif'); ?>" alt="<?php _e('1 star') ?>" /></div>
527 <small><?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?></small>
529 <div id="section-holder" class="wrap">
531 if ( !empty($api->tested) && version_compare( substr($GLOBALS['wp_version'], 0, strlen($api->tested)), $api->tested, '>') )
532 echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has <strong>not been tested</strong> with your current version of WordPress.') . '</p></div>';
534 else if ( !empty($api->requires) && version_compare( substr($GLOBALS['wp_version'], 0, strlen($api->requires)), $api->requires, '<') )
535 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>';
537 foreach ( (array)$api->sections as $section_name => $content ) {
538 $title = $section_name;
539 $title[0] = strtoupper($title[0]);
540 $title = str_replace('_', ' ', $title);
542 $content = links_add_base_url($content, 'http://wordpress.org/extend/plugins/' . $api->slug . '/');
543 $content = links_add_target($content, '_blank');
545 $san_title = esc_attr(sanitize_title_with_dashes($title));
547 $display = ( $section_name == $section ) ? 'block' : 'none';
549 echo "\t<div id='section-{$san_title}' class='section' style='display: {$display};'>\n";
550 echo "\t\t<h2 class='long-header'>$title</h2>";