Projects : mp-wp : mp-wp_svg-screenshots-and-errorreporting-r2

mp-wp/wp-admin/includes/plugin-install.php

Dir - Raw

1<?php
2/**
3 * WordPress Plugin Install Administration API
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/**
10 * Retrieve plugin installer pages from WordPress Plugins API.
11 *
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.
16 *
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
19 * object is returned.
20 *
21 * The second filter, 'plugins_api', is the result that would be returned.
22 *
23 * @since 2.7.0
24 *
25 * @param string $action
26 * @param array|object $args Optional. Arguments to serialize for the Plugin Info API.
27 * @return mixed
28 */
29function plugins_api($action, $args = null) {
30
31 if( is_array($args) )
32 $args = (object)$args;
33
34 if ( !isset($args->per_page) )
35 $args->per_page = 24;
36
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.
39
40 if ( ! $res ) {
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 occured during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>'), $request->get_error_message() );
44 } else {
45 $res = unserialize($request['body']);
46 if ( ! $res )
47 $res = new WP_Error('plugins_api_failed', __('An unknown error occured'), $request['body']);
48 }
49 }
50
51 return apply_filters('plugins_api_result', $res, $action, $args);
52}
53
54/**
55 * Retrieve popular WordPress plugin tags.
56 *
57 * @since 2.7.0
58 *
59 * @param array $args
60 * @return array
61 */
62function 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.
65
66 if ( $cache && $cache->timeout + 3 * 60 * 60 > time() )
67 return $cache->cached;
68
69 $tags = plugins_api('hot_tags', $args);
70
71 if ( is_wp_error($tags) )
72 return $tags;
73
74 $cache = (object) array('timeout' => time(), 'cached' => $tags);
75
76 update_option('wporg_popular_tags', $cache);
77 wp_cache_set('popular_tags', $cache, 'api');
78
79 return $tags;
80}
81add_action('install_plugins_search', 'install_search', 10, 1);
82
83/**
84 * Display search results and display as tag cloud.
85 *
86 * @since 2.7.0
87 *
88 * @param string $page
89 */
90function install_search($page) {
91 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
92 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
93
94 $args = array();
95
96 switch( $type ){
97 case 'tag':
98 $args['tag'] = sanitize_title_with_dashes($term);
99 break;
100 case 'term':
101 $args['search'] = $term;
102 break;
103 case 'author':
104 $args['author'] = $term;
105 break;
106 }
107
108 $args['page'] = $page;
109
110 $api = plugins_api('query_plugins', $args);
111
112 if ( is_wp_error($api) )
113 wp_die($api);
114
115 add_action('install_plugins_table_header', 'install_search_form');
116
117 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
118
119 return;
120}
121
122add_action('install_plugins_dashboard', 'install_dashboard');
123function install_dashboard() {
124 ?>
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>
126
127 <h4><?php _e('Search') ?></h4>
128 <?php install_search_form('<a href="' . add_query_arg('show-help', !isset($_REQUEST['show-help'])) .'" onclick="jQuery(\'#search-help\').toggle(); return false;">' . __('[need help?]') . '</a>') ?>
129 <div id="search-help" style="display: <?php echo isset($_REQUEST['show-help']) ? 'block' : 'none'; ?>;">
130 <p> <?php _e('You may search based on 3 criteria:') ?><br />
131 <?php _e('<strong>Term:</strong> Searches plugins names and descriptions for the specified term') ?><br />
132 <?php _e('<strong>Tag:</strong> Searches for plugins tagged as such') ?><br />
133 <?php _e('<strong>Author:</strong> Searches for plugins created by the Author, or which the Author contributed to.') ?></p>
134 </div>
135
136 <h4><?php _e('Install a plugin in .zip format') ?></h4>
137 <p><?php _e('If you have a plugin in a .zip format, You may install it by uploading it here.') ?></p>
138 <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('plugin-install.php?tab=upload') ?>">
139 <?php wp_nonce_field( 'plugin-upload') ?>
140 <input type="file" name="pluginzip" />
141 <input type="submit" class="button" value="<?php _e('Install Now') ?>" />
142 </form>
143
144 <h4><?php _e('Popular tags') ?></h4>
145 <p><?php _e('You may also browse based on the most popular tags in the Plugin Directory:') ?></p>
146 <?php
147
148 $api_tags = install_popular_tags();
149
150 //Set up the tags in a way which can be interprated by wp_generate_tag_cloud()
151 $tags = array();
152 foreach ( (array)$api_tags as $tag )
153 $tags[ $tag['name'] ] = (object) array(
154 'link' => clean_url( admin_url('plugin-install.php?tab=search&type=tag&s=' . urlencode($tag['name'])) ),
155 'name' => $tag['name'],
156 'id' => sanitize_title_with_dashes($tag['name']),
157 'count' => $tag['count'] );
158 echo wp_generate_tag_cloud($tags, array( 'single_text' => __('%d plugin'), 'multiple_text' => __('%d plugins') ) );
159}
160
161/**
162 * Display search form for searching plugins.
163 *
164 * @since 2.7.0
165 */
166function install_search_form(){
167 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
168 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
169
170 ?><form id="search-plugins" method="post" action="<?php echo admin_url('plugin-install.php?tab=search') ?>">
171 <select name="type" id="typeselector">
172 <option value="term"<?php selected('term', $type) ?>><?php _e('Term') ?></option>
173 <option value="author"<?php selected('author', $type) ?>><?php _e('Author') ?></option>
174 <option value="tag"<?php selected('tag', $type) ?>><?php _e('Tag') ?></option>
175 </select>
176 <input type="text" name="s" id="search-field" value="<?php echo attribute_escape($term) ?>" />
177 <input type="submit" name="search" value="<?php echo attribute_escape(__('Search')) ?>" class="button" />
178 </form><?php
179}
180
181add_action('install_plugins_featured', 'install_featured', 10, 1);
182/**
183 * Display featured plugins.
184 *
185 * @since 2.7.0
186 *
187 * @param string $page
188 */
189function install_featured($page = 1) {
190 $args = array('browse' => 'featured', 'page' => $page);
191 $api = plugins_api('query_plugins', $args);
192 if ( is_wp_error($api) )
193 wp_die($api);
194 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
195}
196
197add_action('install_plugins_popular', 'install_popular', 10, 1);
198/**
199 * Display popular plugins.
200 *
201 * @since 2.7.0
202 *
203 * @param string $page
204 */
205function install_popular($page = 1) {
206 $args = array('browse' => 'popular', 'page' => $page);
207 $api = plugins_api('query_plugins', $args);
208 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
209}
210
211add_action('install_plugins_new', 'install_new', 10, 1);
212/**
213 * Display new plugins.
214 *
215 * @since 2.7.0
216 *
217 * @param string $page
218 */
219function install_new($page = 1) {
220 $args = array('browse' => 'new', 'page' => $page);
221 $api = plugins_api('query_plugins', $args);
222 if ( is_wp_error($api) )
223 wp_die($api);
224 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
225}
226add_action('install_plugins_updated', 'install_updated', 10, 1);
227
228
229/**
230 * Display recently updated plugins.
231 *
232 * @since 2.7.0
233 *
234 * @param string $page
235 */
236function install_updated($page = 1) {
237 $args = array('browse' => 'updated', 'page' => $page);
238 $api = plugins_api('query_plugins', $args);
239 display_plugins_table($api->plugins, $api->info['page'], $api->info['pages']);
240}
241
242/**
243 * Display plugin content based on plugin list.
244 *
245 * @since 2.7.0
246 *
247 * @param array $plugins List of plugins.
248 * @param string $page
249 * @param int $totalpages Number of pages.
250 */
251function display_plugins_table($plugins, $page = 1, $totalpages = 1){
252 $type = isset($_REQUEST['type']) ? stripslashes( $_REQUEST['type'] ) : '';
253 $term = isset($_REQUEST['s']) ? stripslashes( $_REQUEST['s'] ) : '';
254
255 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array(), 'target' => array()),
256 'abbr' => array('title' => array()),'acronym' => array('title' => array()),
257 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array());
258
259?>
260 <div class="tablenav">
261 <div class="alignleft actions">
262 <?php do_action('install_plugins_table_header'); ?>
263 </div>
264 <?php
265 $url = clean_url($_SERVER['REQUEST_URI']);
266 if ( ! empty($term) )
267 $url = add_query_arg('s', $term, $url);
268 if ( ! empty($type) )
269 $url = add_query_arg('type', $type, $url);
270
271 $page_links = paginate_links( array(
272 'base' => add_query_arg('paged', '%#%', $url),
273 'format' => '',
274 'prev_text' => __('&laquo;'),
275 'next_text' => __('&raquo;'),
276 'total' => $totalpages,
277 'current' => $page
278 ));
279
280 if ( $page_links )
281 echo "\t\t<div class='tablenav-pages'>$page_links</div>";
282?>
283 <br class="clear" />
284 </div>
285 <table class="widefat" id="install-plugins" cellspacing="0">
286 <thead>
287 <tr>
288 <th scope="col" class="name"><?php _e('Name'); ?></th>
289 <th scope="col" class="num"><?php _e('Version'); ?></th>
290 <th scope="col" class="num"><?php _e('Rating'); ?></th>
291 <th scope="col" class="desc"><?php _e('Description'); ?></th>
292 <th scope="col" class="action-links"><?php _e('Actions'); ?></th>
293 </tr>
294 </thead>
295
296 <tfoot>
297 <tr>
298 <th scope="col" class="name"><?php _e('Name'); ?></th>
299 <th scope="col" class="num"><?php _e('Version'); ?></th>
300 <th scope="col" class="num"><?php _e('Rating'); ?></th>
301 <th scope="col" class="desc"><?php _e('Description'); ?></th>
302 <th scope="col" class="action-links"><?php _e('Actions'); ?></th>
303 </tr>
304 </tfoot>
305
306 <tbody class="plugins">
307 <?php
308 if( empty($plugins) )
309 echo '<tr><td colspan="5">', __('No plugins match your request.'), '</td></tr>';
310
311 foreach( (array) $plugins as $plugin ){
312 if ( is_object($plugin) )
313 $plugin = (array) $plugin;
314
315 $title = wp_kses($plugin['name'], $plugins_allowedtags);
316 $description = wp_kses($plugin['description'], $plugins_allowedtags);
317 $version = wp_kses($plugin['version'], $plugins_allowedtags);
318
319 $name = strip_tags($title . ' ' . $version);
320
321 $author = $plugin['author'];
322 if( ! empty($plugin['author']) )
323 $author = ' <cite>' . sprintf( __('By %s'), $author ) . '.</cite>';
324
325 $author = wp_kses($author, $plugins_allowedtags);
326
327 if( isset($plugin['homepage']) )
328 $title = '<a target="_blank" href="' . attribute_escape($plugin['homepage']) . '">' . $title . '</a>';
329
330 $action_links = array();
331 $action_links[] = '<a href="' . admin_url('plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
332 '&amp;TB_iframe=true&amp;width=600&amp;height=800') . '" class="thickbox onclick" title="' .
333 attribute_escape($name) . '">' . __('Install') . '</a>';
334
335 $action_links = apply_filters('plugin_install_action_links', $action_links, $plugin);
336 ?>
337 <tr>
338 <td class="name"><?php echo $title; ?></td>
339 <td class="vers"><?php echo $version; ?></td>
340 <td class="vers">
341 <div class="star-holder" title="<?php printf(__ngettext('(based on %s rating)', '(based on %s ratings)', $plugin['num_ratings']), number_format_i18n($plugin['num_ratings'])) ?>">
342 <div class="star star-rating" style="width: <?php echo attribute_escape($plugin['rating']) ?>px"></div>
343 <div class="star star5"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('5 stars') ?>" /></div>
344 <div class="star star4"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('4 stars') ?>" /></div>
345 <div class="star star3"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('3 stars') ?>" /></div>
346 <div class="star star2"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('2 stars') ?>" /></div>
347 <div class="star star1"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('1 star') ?>" /></div>
348 </div>
349 </td>
350 <td class="desc"><p><?php echo $description, $author; ?></p></td>
351 <td class="action-links"><?php if ( !empty($action_links) ) echo implode(' | ', $action_links); ?></td>
352 </tr>
353 <?php
354 }
355 ?>
356 </tbody>
357 </table>
358
359 <div class="tablenav">
360 <?php if ( $page_links )
361 echo "\t\t<div class='tablenav-pages'>$page_links</div>"; ?>
362 <br class="clear" />
363 </div>
364
365<?php
366}
367
368add_action('install_plugins_pre_plugin-information', 'install_plugin_information');
369
370/**
371 * Display plugin information in dialog box form.
372 *
373 * @since 2.7.0
374 */
375function install_plugin_information() {
376 global $tab;
377
378 $api = plugins_api('plugin_information', array('slug' => stripslashes( $_REQUEST['plugin'] ) ));
379
380 if ( is_wp_error($api) )
381 wp_die($api);
382
383 $plugins_allowedtags = array('a' => array('href' => array(), 'title' => array(), 'target' => array()),
384 'abbr' => array('title' => array()), 'acronym' => array('title' => array()),
385 'code' => array(), 'pre' => array(), 'em' => array(), 'strong' => array(),
386 'div' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(),
387 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
388 'img' => array('src' => array(), 'class' => array(), 'alt' => array()));
389 //Sanitize HTML
390 foreach ( (array)$api->sections as $section_name => $content )
391 $api->sections[$section_name] = wp_kses($content, $plugins_allowedtags);
392 foreach ( array('version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug') as $key )
393 $api->$key = wp_kses($api->$key, $plugins_allowedtags);
394
395 $section = isset($_REQUEST['section']) ? stripslashes( $_REQUEST['section'] ) : 'description'; //Default to the Description tab, Do not translate, API returns English.
396 if( empty($section) || ! isset($api->sections[ $section ]) )
397 $section = array_shift( $section_titles = array_keys((array)$api->sections) );
398
399 iframe_header( __('Plugin Install') );
400 echo "<div id='$tab-header'>\n";
401 echo "<ul id='sidemenu'>\n";
402 foreach ( (array)$api->sections as $section_name => $content ) {
403
404 $title = $section_name;
405 $title = ucwords(str_replace('_', ' ', $title));
406
407 $class = ( $section_name == $section ) ? ' class="current"' : '';
408 $href = add_query_arg( array('tab' => $tab, 'section' => $section_name) );
409 $href = clean_url($href);
410 $san_title = attribute_escape(sanitize_title_with_dashes($title));
411 echo "\t<li><a name='$san_title' target='' href='$href'$class>$title</a></li>\n";
412 }
413 echo "</ul>\n";
414 echo "</div>\n";
415 ?>
416 <div class="alignright fyi">
417 <?php if ( ! empty($api->download_link) ) : ?>
418 <p class="action-button">
419 <?php
420 //Default to a "new" plugin
421 $type = 'install';
422 //Check to see if this plugin is known to be installed, and has an update awaiting it.
423 $update_plugins = get_option('update_plugins');
424 foreach ( (array)$update_plugins->response as $file => $plugin ) {
425 if ( $plugin->slug === $api->slug ) {
426 $type = 'update_available';
427 $update_file = $file;
428 break;
429 }
430 }
431 if ( 'install' == $type && is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
432 $installed_plugin = get_plugins('/' . $api->slug);
433 if ( ! empty($installed_plugin) ) {
434 $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
435 if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '>') ){
436 $type = 'latest_installed';
437 } elseif ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '<') ) {
438 $type = 'newer_installed';
439 $newer_version = $installed_plugin[ $key ]['Version'];
440 } else {
441 //If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh
442 delete_option('update_plugins');
443 $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
444 $type = 'update_available';
445 }
446 }
447 }
448
449 switch ( $type ) :
450 default:
451 case 'install':
452 if ( current_user_can('install_plugins') ) :
453 ?><a href="<?php echo wp_nonce_url(admin_url('plugin-install.php?tab=install&plugin=' . $api->slug), 'install-plugin_' . $api->slug) ?>" target="_parent"><?php _e('Install Now') ?></a><?php
454 endif;
455 break;
456 case 'newer_installed':
457 if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
458 ?><a><?php printf(__('Newer Version (%s) Installed'), $newer_version) ?></a><?php
459 endif;
460 break;
461 case 'latest_installed':
462 if ( current_user_can('install_plugins') || current_user_can('update_plugins') ) :
463 ?><a><?php _e('Latest Version Installed') ?></a><?php
464 endif;
465 break;
466 endswitch; ?>
467 </p>
468 <?php endif; ?>
469 <h2 class="mainheader"><?php _e('FYI') ?></h2>
470 <ul>
471<?php if ( ! empty($api->version) ) : ?>
472 <li><strong><?php _e('Version:') ?></strong> <?php echo $api->version ?></li>
473<?php endif; if ( ! empty($api->author) ) : ?>
474 <li><strong><?php _e('Author:') ?></strong> <?php echo links_add_target($api->author, '_blank') ?></li>
475<?php endif; if ( ! empty($api->last_updated) ) : ?>
476 <li><strong><?php _e('Last Updated:') ?></strong> <span title="<?php echo $api->last_updated ?>"><?php
477 printf( __('%s ago'), human_time_diff(strtotime($api->last_updated)) ) ?></span></li>
478<?php endif; if ( ! empty($api->requires) ) : ?>
479 <li><strong><?php _e('Requires WordPress Version:') ?></strong> <?php printf(__('%s or higher'), $api->requires) ?></li>
480<?php endif; if ( ! empty($api->tested) ) : ?>
481 <li><strong><?php _e('Compatible up to:') ?></strong> <?php echo $api->tested ?></li>
482<?php endif; if ( ! empty($api->downloaded) ) : ?>
483 <li><strong><?php _e('Downloaded:') ?></strong> <?php printf(__ngettext('%s time', '%s times', $api->downloaded), number_format_i18n($api->downloaded)) ?></li>
484<?php endif; if ( ! empty($api->slug) ) : ?>
485 <li><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $api->slug ?>/"><?php _e('WordPress.org Plugin Page &#187;') ?></a></li>
486<?php endif; if ( ! empty($api->homepage) ) : ?>
487 <li><a target="_blank" href="<?php echo $api->homepage ?>"><?php _e('Plugin Homepage &#187;') ?></a></li>
488<?php endif; ?>
489 </ul>
490 <h2><?php _e('Average Rating') ?></h2>
491 <div class="star-holder" title="<?php printf(__ngettext('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?>">
492 <div class="star star-rating" style="width: <?php echo attribute_escape($api->rating) ?>px"></div>
493 <div class="star star5"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('5 stars') ?>" /></div>
494 <div class="star star4"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('4 stars') ?>" /></div>
495 <div class="star star3"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('3 stars') ?>" /></div>
496 <div class="star star2"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('2 stars') ?>" /></div>
497 <div class="star star1"><img src="<?php echo admin_url('images/star.svg'); ?>" alt="<?php _e('1 star') ?>" /></div>
498 </div>
499 <small><?php printf(__ngettext('(based on %s rating)', '(based on %s ratings)', $api->num_ratings), number_format_i18n($api->num_ratings)); ?></small>
500 </div>
501 <div id="section-holder" class="wrap">
502 <?php
503 if ( version_compare($GLOBALS['wp_version'], $api->tested, '>') )
504 echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has <strong>not been tested</strong> with your current version of WordPress.') . '</p></div>';
505 else if ( version_compare($GLOBALS['wp_version'], $api->requires, '<') )
506 echo '<div class="updated"><p>' . __('<strong>Warning:</strong> This plugin has not been marked as <strong>compatible</strong> with your version of WordPress.') . '</p></div>';
507 foreach ( (array)$api->sections as $section_name => $content ) {
508 $title = $section_name;
509 $title[0] = strtoupper($title[0]);
510 $title = str_replace('_', ' ', $title);
511
512 $content = links_add_base_url($content, 'http://wordpress.org/extend/plugins/' . $api->slug . '/');
513 $content = links_add_target($content, '_blank');
514
515 $san_title = attribute_escape(sanitize_title_with_dashes($title));
516
517 $display = ( $section_name == $section ) ? 'block' : 'none';
518
519 echo "\t<div id='section-{$san_title}' class='section' style='display: {$display};'>\n";
520 echo "\t\t<h2 class='long-header'>$title</h2>";
521 echo $content;
522 echo "\t</div>\n";
523 }
524 echo "</div>\n";
525
526 iframe_footer();
527 exit;
528}
529
530
531add_action('install_plugins_upload', 'upload_plugin');
532function upload_plugin() {
533
534 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
535 wp_die($uploads['error']);
536
537 if ( !empty($_FILES) )
538 $filename = $_FILES['pluginzip']['name'];
539 else if ( isset($_GET['package']) )
540 $filename = $_GET['package'];
541
542 check_admin_referer('plugin-upload');
543
544 echo '<div class="wrap">';
545 echo '<h2>', sprintf( __('Installing Plugin from file: %s'), basename($filename) ), '</h2>';
546
547 //Handle a newly uploaded file, Else assume it was
548 if ( !empty($_FILES) ) {
549 $filename = wp_unique_filename( $uploads['basedir'], $filename );
550 $local_file = $uploads['basedir'] . '/' . $filename;
551
552 // Move the file to the uploads dir
553 if ( false === @ move_uploaded_file( $_FILES['pluginzip']['tmp_name'], $local_file) )
554 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
555 } else {
556 $local_file = $uploads['basedir'] . '/' . $filename;
557 }
558
559 do_plugin_install_local_package($local_file, $filename);
560 echo '</div>';
561}
562
563add_action('install_plugins_install', 'install_plugin');
564
565/**
566 * Display plugin link and execute install.
567 *
568 * @since 2.7.0
569 */
570function install_plugin() {
571
572 $plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
573
574 check_admin_referer('install-plugin_' . $plugin);
575 $api = plugins_api('plugin_information', array('slug' => $plugin, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
576
577 if ( is_wp_error($api) )
578 wp_die($api);
579
580 echo '<div class="wrap">';
581 echo '<h2>', sprintf( __('Installing Plugin: %s'), $api->name . ' ' . $api->version ), '</h2>';
582
583 do_plugin_install($api->download_link, $api);
584 echo '</div>';
585
586}
587
588/**
589 * Retrieve plugin and install.
590 *
591 * @since 2.7.0
592 *
593 * @param string $download_url Download URL.
594 * @param object $plugin_information Optional. Plugin information
595 */
596function do_plugin_install($download_url, $plugin_information = null) {
597 global $wp_filesystem;
598
599 if ( empty($download_url) ) {
600 show_message( __('No plugin Specified') );
601 return;
602 }
603
604 $plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
605
606 $url = 'plugin-install.php?tab=install';
607 $url = add_query_arg(array('plugin' => $plugin, 'plugin_name' => stripslashes( $_REQUEST['plugin_name'] ), 'download_url' => stripslashes( $_REQUEST['download_url'] ) ), $url);
608
609 $url = wp_nonce_url($url, 'install-plugin_' . $plugin);
610 if ( false === ($credentials = request_filesystem_credentials($url)) )
611 return;
612
613 if ( ! WP_Filesystem($credentials) ) {
614 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
615 return;
616 }
617
618 if ( $wp_filesystem->errors->get_error_code() ) {
619 foreach ( $wp_filesystem->errors->get_error_messages() as $message )
620 show_message($message);
621 return;
622 }
623
624 $result = wp_install_plugin( $download_url, 'show_message' );
625
626 if ( is_wp_error($result) ) {
627 show_message($result);
628 show_message( __('Installation Failed') );
629 } else {
630 show_message( sprintf(__('Successfully installed the plugin <strong>%s %s</strong>.'), $plugin_information->name, $plugin_information->version) );
631 $plugin_file = $result;
632
633 $install_actions = apply_filters('install_plugin_complete_actions', array(
634 'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . attribute_escape(__('Activate this plugin')) . '" target="_parent">' . __('Activate Plugin') . '</a>',
635 'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
636 ), $plugin_information, $plugin_file);
637 if ( ! empty($install_actions) )
638 show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
639 }
640}
641
642/**
643 * Install a plugin from a local file.
644 *
645 * @since 2.7.0
646 *
647 * @param string $package Local Plugin zip
648 * @param string $filename Optional. Original filename
649 * @param object $plugin_information Optional. Plugin information
650 */
651function do_plugin_install_local_package($package, $filename = '') {
652 global $wp_filesystem;
653
654 if ( empty($package) ) {
655 show_message( __('No plugin Specified') );
656 return;
657 }
658
659 if ( empty($filename) )
660 $filename = basename($package);
661
662 $url = 'plugin-install.php?tab=upload';
663 $url = add_query_arg(array('package' => $filename), $url);
664
665 $url = wp_nonce_url($url, 'plugin-upload');
666 if ( false === ($credentials = request_filesystem_credentials($url)) )
667 return;
668
669 if ( ! WP_Filesystem($credentials) ) {
670 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
671 return;
672 }
673
674 if ( $wp_filesystem->errors->get_error_code() ) {
675 foreach ( $wp_filesystem->errors->get_error_messages() as $message )
676 show_message($message);
677 return;
678 }
679
680 $result = wp_install_plugin_local_package( $package, 'show_message' );
681
682 if ( is_wp_error($result) ) {
683 show_message($result);
684 show_message( __('Installation Failed') );
685 } else {
686 show_message( __('Successfully installed the plugin.') );
687 $plugin_file = $result;
688
689 $install_actions = apply_filters('install_plugin_complete_actions', array(
690 'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . __('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
691 'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . __('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
692 ), array(), $plugin_file);
693 if ( ! empty($install_actions) )
694 show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
695 }
696}
697
698/**
699 * Install plugin.
700 *
701 * @since 2.7.0
702 *
703 * @param string $package
704 * @param string $feedback Optional.
705 * @return mixed.
706 */
707function wp_install_plugin($package, $feedback = '') {
708 global $wp_filesystem;
709
710 if ( !empty($feedback) )
711 add_filter('install_feedback', $feedback);
712
713 // Is a filesystem accessor setup?
714 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
715 WP_Filesystem();
716
717 if ( ! is_object($wp_filesystem) )
718 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
719
720 if ( $wp_filesystem->errors->get_error_code() )
721 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
722
723 //Get the base plugin folder
724 $plugins_dir = $wp_filesystem->wp_plugins_dir();
725 if ( empty($plugins_dir) )
726 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
727
728 //And the same for the Content directory.
729 $content_dir = $wp_filesystem->wp_content_dir();
730 if( empty($content_dir) )
731 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
732
733 $plugins_dir = trailingslashit( $plugins_dir );
734 $content_dir = trailingslashit( $content_dir );
735
736 if ( empty($package) )
737 return new WP_Error('no_package', __('Install package not available.'));
738
739 // Download the package
740 apply_filters('install_feedback', sprintf(__('Downloading plugin package from %s'), $package));
741 $download_file = download_url($package);
742
743 if ( is_wp_error($download_file) )
744 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
745
746 $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
747
748 // Clean up working directory
749 if ( $wp_filesystem->is_dir($working_dir) )
750 $wp_filesystem->delete($working_dir, true);
751
752 apply_filters('install_feedback', __('Unpacking the plugin package'));
753 // Unzip package to working directory
754 $result = unzip_file($download_file, $working_dir);
755
756 // Once extracted, delete the package
757 @unlink($download_file);
758
759 if ( is_wp_error($result) ) {
760 $wp_filesystem->delete($working_dir, true);
761 return $result;
762 }
763
764 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
765 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
766
767 if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
768 $wp_filesystem->delete($working_dir, true);
769 return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
770 }
771
772 apply_filters('install_feedback', __('Installing the plugin'));
773 // Copy new version of plugin into place.
774 $result = copy_dir($working_dir, $plugins_dir);
775 if ( is_wp_error($result) ) {
776 $wp_filesystem->delete($working_dir, true);
777 return $result;
778 }
779
780 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
781 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
782
783 // Remove working directory
784 $wp_filesystem->delete($working_dir, true);
785
786 if( empty($filelist) )
787 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
788
789 $folder = $filelist[0];
790 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
791 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
792
793 //Return the plugin files name.
794 return $folder . '/' . $pluginfiles[0];
795}
796
797/**
798 * Install plugin from local package
799 *
800 * @since 2.7.0
801 *
802 * @param string $package
803 * @param string $feedback Optional.
804 * @return mixed.
805 */
806function wp_install_plugin_local_package($package, $feedback = '') {
807 global $wp_filesystem;
808
809 if ( !empty($feedback) )
810 add_filter('install_feedback', $feedback);
811
812 // Is a filesystem accessor setup?
813 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
814 WP_Filesystem();
815
816 if ( ! is_object($wp_filesystem) )
817 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
818
819 if ( $wp_filesystem->errors->get_error_code() )
820 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
821
822 //Get the base plugin folder
823 $plugins_dir = $wp_filesystem->wp_plugins_dir();
824 if ( empty($plugins_dir) )
825 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
826
827 //And the same for the Content directory.
828 $content_dir = $wp_filesystem->wp_content_dir();
829 if( empty($content_dir) )
830 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
831
832 $plugins_dir = trailingslashit( $plugins_dir );
833 $content_dir = trailingslashit( $content_dir );
834
835 if ( empty($package) )
836 return new WP_Error('no_package', __('Install package not available.'));
837
838 $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
839
840 // Clean up working directory
841 if ( $wp_filesystem->is_dir($working_dir) )
842 $wp_filesystem->delete($working_dir, true);
843
844 apply_filters('install_feedback', __('Unpacking the plugin package'));
845 // Unzip package to working directory
846 $result = unzip_file($package, $working_dir);
847
848 // Once extracted, delete the package
849 unlink($package);
850
851 if ( is_wp_error($result) ) {
852 $wp_filesystem->delete($working_dir, true);
853 return $result;
854 }
855
856 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
857 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
858
859 if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
860 $wp_filesystem->delete($working_dir, true);
861 return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
862 }
863
864 apply_filters('install_feedback', __('Installing the plugin'));
865 // Copy new version of plugin into place.
866 $result = copy_dir($working_dir, $plugins_dir);
867 if ( is_wp_error($result) ) {
868 $wp_filesystem->delete($working_dir, true);
869 return $result;
870 }
871
872 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
873 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
874
875 // Remove working directory
876 $wp_filesystem->delete($working_dir, true);
877
878 if( empty($filelist) )
879 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
880
881 $folder = $filelist[0];
882 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
883 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
884
885 //Return the plugin files name.
886 return $folder . '/' . $pluginfiles[0];
887}
888
889?>