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']);
49 } elseif ( !is_wp_error($res) ) {
50 $res->external = true;
53 return apply_filters('plugins_api_result', $res, $action, $args);
57 * Retrieve popular WordPress plugin tags.
64 function install_popular_tags( $args = array() ) {
65 if ( ! ($cache = wp_cache_get('popular_tags', 'api')) && ! ($cache = get_option('wporg_popular_tags')) )
66 add_option('wporg_popular_tags', array(), '', 'no'); ///No autoload.
68 if ( $cache && $cache->timeout + 3 * 60 * 60 > time() )
69 return $cache->cached;
71 $tags = plugins_api('hot_tags', $args);
73 if ( is_wp_error($tags) )
76 $cache = (object) array('timeout' => time(), 'cached' => $tags);
78 update_option('wporg_popular_tags', $cache);
79 wp_cache_set('popular_tags', $cache, 'api');
83 add_action('install_plugins_search', 'install_search', 10, 1);
86 * Display search results and display as tag cloud.
92 function install_search($page) {
93 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
94 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
100 $args['tag'] = sanitize_title_with_dashes($term);
103 $args['search'] = $term;
106 $args['author'] = $term;
110 $args['page'] = $page;
112 $api = plugins_api('query_plugins', $args);
114 if ( is_wp_error($api) )
117 add_action('install_plugins_table_header', 'install_search_form');
119 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
124 add_action('install_plugins_dashboard', 'install_dashboard');
125 function install_dashboard() {
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>
129 <h4><?php _e('Search') ?></h4>
130 <p class="install-help"><?php _e('Search for plugins by keyword, author, or tag.') ?></p>
131 <?php install_search_form(); ?>
133 <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>
137 $api_tags = install_popular_tags();
139 //Set up the tags in a way which can be interprated by wp_generate_tag_cloud()
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">';
148 echo wp_generate_tag_cloud($tags, array( 'single_text' => __('%d plugin'), 'multiple_text' => __('%d plugins') ) );
149 echo '</p><br class="clear" />';
153 * Display search form for searching plugins.
157 function install_search_form(){
158 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
159 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
161 ?><form id="search-plugins" method="post" action="<?php echo admin_url('plugin-install.php?tab=search'); ?>">
162 <select name="type" id="typeselector">
163 <option value="term"<?php selected('term', $type) ?>><?php _e('Term'); ?></option>
164 <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>
167 <input type="text" name="s" value="<?php echo esc_attr($term) ?>" />
168 <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" />
173 add_action('install_plugins_featured', 'install_featured', 10, 1);
175 * Display featured plugins.
179 * @param string $page
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) )
186 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
189 add_action('install_plugins_popular', 'install_popular', 10, 1);
191 * Display popular plugins.
195 * @param string $page
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']);
203 add_action('install_plugins_upload', 'install_plugins_upload', 10, 1);
208 * @param string $page
210 function install_plugins_upload( $page = 1 ) {
212 <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>
214 <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-plugin') ?>">
215 <?php wp_nonce_field( 'plugin-upload') ?>
216 <label class="screen-reader-text" for="pluginzip"><?php _e('Plugin zip file'); ?></label>
217 <input type="file" id="pluginzip" name="pluginzip" />
218 <input type="submit" class="button" value="<?php esc_attr_e('Install Now') ?>" />
223 add_action('install_plugins_new', 'install_new', 10, 1);
225 * Display new plugins.
229 * @param string $page
231 function install_new($page = 1) {
232 $args = array('browse' => 'new', 'page' => $page);
233 $api = plugins_api('query_plugins', $args);
234 if ( is_wp_error($api) )
236 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
238 add_action('install_plugins_updated', 'install_updated', 10, 1);
242 * Display recently updated plugins.
246 * @param string $page
248 function install_updated($page = 1) {
249 $args = array('browse' => 'updated', 'page' => $page);
250 $api = plugins_api('query_plugins', $args);
251 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
255 * Display plugin content based on plugin list.
259 * @param array $plugins List of plugins.
260 * @param string $page
261 * @param int $totalpages Number of pages.
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'] ) : '';
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());
273 <div class="tablenav">
274 <div class="alignleft actions">
275 <?php do_action('install_plugins_table_header'); ?>
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);
284 $page_links = paginate_links( array(
285 'base' => add_query_arg('paged', '%#%', $url),
287 'prev_text' => __('«'),
288 'next_text' => __('»'),
289 'total' => $totalpages,
294 echo "\t\t<div class='tablenav-pages'>$page_links</div>";
298 <table class="widefat" id="install-plugins" cellspacing="0">
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>
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>
319 <tbody class="plugins">
321 if( empty($plugins) )
322 echo '<tr><td colspan="5">', __('No plugins match your request.'), '</td></tr>';
324 foreach( (array) $plugins as $plugin ){
325 if ( is_object($plugin) )
326 $plugin = (array) $plugin;
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) . '…';
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);
339 $description = nl2br($description);
340 $version = wp_kses($plugin['version'], $plugins_allowedtags);
342 $name = strip_tags($title . ' ' . $version);
344 $author = $plugin['author'];
345 if( ! empty($plugin['author']) )
346 $author = ' <cite>' . sprintf( __('By %s'), $author ) . '.</cite>';
348 $author = wp_kses($author, $plugins_allowedtags);
350 if( isset($plugin['homepage']) )
351 $title = '<a target="_blank" href="' . esc_attr($plugin['homepage']) . '">' . $title . '</a>';
353 $action_links = array();
354 $action_links[] = '<a href="' . admin_url('plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
355 '&TB_iframe=true&width=600&height=550') . '" class="thickbox onclick" title="' .
356 esc_attr($name) . '">' . __('Install') . '</a>';
358 $action_links = apply_filters('plugin_install_action_links', $action_links, $plugin);
361 <td class="name"><?php echo $title; ?></td>
362 <td class="vers"><?php echo $version; ?></td>
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>
373 <td class="desc"><?php echo $description, $author; ?></td>
374 <td class="action-links"><?php if ( !empty($action_links) ) echo implode(' | ', $action_links); ?></td>
382 <div class="tablenav">
383 <?php if ( $page_links )
384 echo "\t\t<div class='tablenav-pages'>$page_links</div>"; ?>
391 add_action('install_plugins_pre_plugin-information', 'install_plugin_information');
394 * Display plugin information in dialog box form.
398 function install_plugin_information() {
401 $api = plugins_api('plugin_information', array('slug' => stripslashes( $_REQUEST['plugin'] ) ));
403 if ( is_wp_error($api) )
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()));
413 foreach ( (array)$api->sections as $section_name => $content )
414 $api->sections[$section_name] = wp_kses($content, $plugins_allowedtags);
415 foreach ( array('version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug') as $key )
416 $api->$key = wp_kses($api->$key, $plugins_allowedtags);
418 $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 ]) )
420 $section = array_shift( $section_titles = array_keys((array)$api->sections) );
422 iframe_header( __('Plugin Install') );
423 echo "<div id='$tab-header'>\n";
424 echo "<ul id='sidemenu'>\n";
425 foreach ( (array)$api->sections as $section_name => $content ) {
427 $title = $section_name;
428 $title = ucwords(str_replace('_', ' ', $title));
430 $class = ( $section_name == $section ) ? ' class="current"' : '';
431 $href = add_query_arg( array('tab' => $tab, 'section' => $section_name) );
432 $href = esc_url($href);
433 $san_title = esc_attr(sanitize_title_with_dashes($title));
434 echo "\t<li><a name='$san_title' target='' href='$href'$class>$title</a></li>\n";
439 <div class="alignright fyi">
440 <?php if ( ! empty($api->download_link) ) : ?>
441 <p class="action-button">
443 //Default to a "new" plugin
445 //Check to see if this plugin is known to be installed, and has an update awaiting it.
446 $update_plugins = get_transient('update_plugins');
447 if ( is_object( $update_plugins ) ) {
448 foreach ( (array)$update_plugins->response as $file => $plugin ) {
449 if ( $plugin->slug === $api->slug ) {
450 $type = 'update_available';
451 $update_file = $file;
456 if ( 'install' == $type && is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
457 $installed_plugin = get_plugins('/' . $api->slug);
458 if ( ! empty($installed_plugin) ) {
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
460 if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ){
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'];
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';
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
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
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
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
499 <h2 class="mainheader"><?php _e('FYI') ?></h2>
501 <?php if ( ! empty($api->version) ) : ?>
502 <li><strong><?php _e('Version:') ?></strong> <?php echo $api->version ?></li>
503 <?php endif; if ( ! empty($api->author) ) : ?>
504 <li><strong><?php _e('Author:') ?></strong> <?php echo links_add_target($api->author, '_blank') ?></li>
505 <?php endif; if ( ! empty($api->last_updated) ) : ?>
506 <li><strong><?php _e('Last Updated:') ?></strong> <span title="<?php echo $api->last_updated ?>"><?php
507 printf( __('%s ago'), human_time_diff(strtotime($api->last_updated)) ) ?></span></li>
508 <?php endif; if ( ! empty($api->requires) ) : ?>
509 <li><strong><?php _e('Requires WordPress Version:') ?></strong> <?php printf(__('%s or higher'), $api->requires) ?></li>
510 <?php endif; if ( ! empty($api->tested) ) : ?>
511 <li><strong><?php _e('Compatible up to:') ?></strong> <?php echo $api->tested ?></li>
512 <?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>
514 <?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 »') ?></a></li>
516 <?php endif; if ( ! empty($api->homepage) ) : ?>
517 <li><a target="_blank" href="<?php echo $api->homepage ?>"><?php _e('Plugin Homepage »') ?></a></li>
520 <?php if ( ! empty($api->rating) ) : ?>
521 <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)); ?>">
523 <div class="star star-rating" style="width: <?php echo esc_attr($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>
530 <small><?php printf(_n('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?></small>
533 <div id="section-holder" class="wrap">
535 if ( !empty($api->tested) && version_compare( substr($GLOBALS['wp_version'], 0, strlen($api->tested)), $api->tested, '>') )
536 echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has <strong>not been tested</strong> with your current version of WordPress.') . '</p></div>';
538 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>';
541 foreach ( (array)$api->sections as $section_name => $content ) {
542 $title = $section_name;
543 $title[0] = strtoupper($title[0]);
544 $title = str_replace('_', ' ', $title);
546 $content = links_add_base_url($content, 'http://wordpress.org/extend/plugins/' . $api->slug . '/');
547 $content = links_add_target($content, '_blank');
549 $san_title = esc_attr(sanitize_title_with_dashes($title));
551 $display = ( $section_name == $section ) ? 'block' : 'none';
553 echo "\t<div id='section-{$san_title}' class='section' style='display: {$display};'>\n";
554 echo "\t\t<h2 class='long-header'>$title</h2>";