plugin-install.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. /**
  3. * WordPress Plugin Install Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Retrieves plugin installer pages from the WordPress.org Plugins API.
  10. *
  11. * It is possible for a plugin to override the Plugin API result with three
  12. * filters. Assume this is for plugins, which can extend on the Plugin Info to
  13. * offer more choices. This is very powerful and must be used with care when
  14. * overriding the filters.
  15. *
  16. * The first filter, {@see 'plugins_api_args'}, is for the args and gives the action
  17. * as the second parameter. The hook for {@see 'plugins_api_args'} must ensure that
  18. * an object is returned.
  19. *
  20. * The second filter, {@see 'plugins_api'}, allows a plugin to override the WordPress.org
  21. * Plugin Installation API entirely. If `$action` is 'query_plugins' or 'plugin_information',
  22. * an object MUST be passed. If `$action` is 'hot_tags' or 'hot_categories', an array MUST
  23. * be passed.
  24. *
  25. * Finally, the third filter, {@see 'plugins_api_result'}, makes it possible to filter the
  26. * response object or array, depending on the `$action` type.
  27. *
  28. * Supported arguments per action:
  29. *
  30. * | Argument Name | query_plugins | plugin_information | hot_tags | hot_categories |
  31. * | -------------------- | :-----------: | :----------------: | :------: | :------------: |
  32. * | `$slug` | No | Yes | No | No |
  33. * | `$per_page` | Yes | No | No | No |
  34. * | `$page` | Yes | No | No | No |
  35. * | `$number` | No | No | Yes | Yes |
  36. * | `$search` | Yes | No | No | No |
  37. * | `$tag` | Yes | No | No | No |
  38. * | `$author` | Yes | No | No | No |
  39. * | `$user` | Yes | No | No | No |
  40. * | `$browse` | Yes | No | No | No |
  41. * | `$locale` | Yes | Yes | No | No |
  42. * | `$installed_plugins` | Yes | No | No | No |
  43. * | `$is_ssl` | Yes | Yes | No | No |
  44. * | `$fields` | Yes | Yes | No | No |
  45. *
  46. * @since 2.7.0
  47. *
  48. * @param string $action API action to perform: 'query_plugins', 'plugin_information',
  49. * 'hot_tags' or 'hot_categories'.
  50. * @param array|object $args {
  51. * Optional. Array or object of arguments to serialize for the Plugin Info API.
  52. *
  53. * @type string $slug The plugin slug. Default empty.
  54. * @type int $per_page Number of plugins per page. Default 24.
  55. * @type int $page Number of current page. Default 1.
  56. * @type int $number Number of tags or categories to be queried.
  57. * @type string $search A search term. Default empty.
  58. * @type string $tag Tag to filter plugins. Default empty.
  59. * @type string $author Username of an plugin author to filter plugins. Default empty.
  60. * @type string $user Username to query for their favorites. Default empty.
  61. * @type string $browse Browse view: 'popular', 'new', 'beta', 'recommended'.
  62. * @type string $locale Locale to provide context-sensitive results. Default is the value
  63. * of get_locale().
  64. * @type string $installed_plugins Installed plugins to provide context-sensitive results.
  65. * @type bool $is_ssl Whether links should be returned with https or not. Default false.
  66. * @type array $fields {
  67. * Array of fields which should or should not be returned.
  68. *
  69. * @type bool $short_description Whether to return the plugin short description. Default true.
  70. * @type bool $description Whether to return the plugin full description. Default false.
  71. * @type bool $sections Whether to return the plugin readme sections: description, installation,
  72. * FAQ, screenshots, other notes, and changelog. Default false.
  73. * @type bool $tested Whether to return the 'Compatible up to' value. Default true.
  74. * @type bool $requires Whether to return the required WordPress version. Default true.
  75. * @type bool $requires_php Whether to return the required PHP version. Default true.
  76. * @type bool $rating Whether to return the rating in percent and total number of ratings.
  77. * Default true.
  78. * @type bool $ratings Whether to return the number of rating for each star (1-5). Default true.
  79. * @type bool $downloaded Whether to return the download count. Default true.
  80. * @type bool $downloadlink Whether to return the download link for the package. Default true.
  81. * @type bool $last_updated Whether to return the date of the last update. Default true.
  82. * @type bool $added Whether to return the date when the plugin was added to the wordpress.org
  83. * repository. Default true.
  84. * @type bool $tags Whether to return the assigned tags. Default true.
  85. * @type bool $compatibility Whether to return the WordPress compatibility list. Default true.
  86. * @type bool $homepage Whether to return the plugin homepage link. Default true.
  87. * @type bool $versions Whether to return the list of all available versions. Default false.
  88. * @type bool $donate_link Whether to return the donation link. Default true.
  89. * @type bool $reviews Whether to return the plugin reviews. Default false.
  90. * @type bool $banners Whether to return the banner images links. Default false.
  91. * @type bool $icons Whether to return the icon links. Default false.
  92. * @type bool $active_installs Whether to return the number of active installations. Default false.
  93. * @type bool $group Whether to return the assigned group. Default false.
  94. * @type bool $contributors Whether to return the list of contributors. Default false.
  95. * }
  96. * }
  97. * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
  98. * {@link https://developer.wordpress.org/reference/functions/plugins_api/ function reference article}
  99. * for more information on the make-up of possible return values depending on the value of `$action`.
  100. */
  101. function plugins_api( $action, $args = array() ) {
  102. // include an unmodified $wp_version
  103. include( ABSPATH . WPINC . '/version.php' );
  104. if ( is_array( $args ) ) {
  105. $args = (object) $args;
  106. }
  107. if ( 'query_plugins' == $action ) {
  108. if ( ! isset( $args->per_page ) ) {
  109. $args->per_page = 24;
  110. }
  111. }
  112. if ( ! isset( $args->locale ) ) {
  113. $args->locale = get_user_locale();
  114. }
  115. if ( ! isset( $args->wp_version ) ) {
  116. $args->wp_version = substr( $wp_version, 0, 3 ); // X.y
  117. }
  118. /**
  119. * Filters the WordPress.org Plugin Installation API arguments.
  120. *
  121. * Important: An object MUST be returned to this filter.
  122. *
  123. * @since 2.7.0
  124. *
  125. * @param object $args Plugin API arguments.
  126. * @param string $action The type of information being requested from the Plugin Installation API.
  127. */
  128. $args = apply_filters( 'plugins_api_args', $args, $action );
  129. /**
  130. * Filters the response for the current WordPress.org Plugin Installation API request.
  131. *
  132. * Passing a non-false value will effectively short-circuit the WordPress.org API request.
  133. *
  134. * If `$action` is 'query_plugins' or 'plugin_information', an object MUST be passed.
  135. * If `$action` is 'hot_tags' or 'hot_categories', an array should be passed.
  136. *
  137. * @since 2.7.0
  138. *
  139. * @param false|object|array $result The result object or array. Default false.
  140. * @param string $action The type of information being requested from the Plugin Installation API.
  141. * @param object $args Plugin API arguments.
  142. */
  143. $res = apply_filters( 'plugins_api', false, $action, $args );
  144. if ( false === $res ) {
  145. $url = 'http://api.wordpress.org/plugins/info/1.2/';
  146. $url = add_query_arg(
  147. array(
  148. 'action' => $action,
  149. 'request' => $args,
  150. ),
  151. $url
  152. );
  153. $http_url = $url;
  154. $ssl = wp_http_supports( array( 'ssl' ) );
  155. if ( $ssl ) {
  156. $url = set_url_scheme( $url, 'https' );
  157. }
  158. $http_args = array(
  159. 'timeout' => 15,
  160. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  161. );
  162. $request = wp_remote_get( $url, $http_args );
  163. if ( $ssl && is_wp_error( $request ) ) {
  164. trigger_error(
  165. sprintf(
  166. /* translators: %s: Support forums URL. */
  167. __( '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="%s">support forums</a>.' ),
  168. __( 'https://wordpress.org/support/forums/' )
  169. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  170. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  171. );
  172. $request = wp_remote_get( $http_url, $http_args );
  173. }
  174. if ( is_wp_error( $request ) ) {
  175. $res = new WP_Error(
  176. 'plugins_api_failed',
  177. sprintf(
  178. /* translators: %s: Support forums URL. */
  179. __( '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="%s">support forums</a>.' ),
  180. __( 'https://wordpress.org/support/forums/' )
  181. ),
  182. $request->get_error_message()
  183. );
  184. } else {
  185. $res = json_decode( wp_remote_retrieve_body( $request ), true );
  186. if ( is_array( $res ) ) {
  187. // Object casting is required in order to match the info/1.0 format.
  188. $res = (object) $res;
  189. } elseif ( null === $res ) {
  190. $res = new WP_Error(
  191. 'plugins_api_failed',
  192. sprintf(
  193. /* translators: %s: Support forums URL. */
  194. __( '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="%s">support forums</a>.' ),
  195. __( 'https://wordpress.org/support/forums/' )
  196. ),
  197. wp_remote_retrieve_body( $request )
  198. );
  199. }
  200. if ( isset( $res->error ) ) {
  201. $res = new WP_Error( 'plugins_api_failed', $res->error );
  202. }
  203. }
  204. } elseif ( ! is_wp_error( $res ) ) {
  205. $res->external = true;
  206. }
  207. /**
  208. * Filters the Plugin Installation API response results.
  209. *
  210. * @since 2.7.0
  211. *
  212. * @param object|WP_Error $res Response object or WP_Error.
  213. * @param string $action The type of information being requested from the Plugin Installation API.
  214. * @param object $args Plugin API arguments.
  215. */
  216. return apply_filters( 'plugins_api_result', $res, $action, $args );
  217. }
  218. /**
  219. * Retrieve popular WordPress plugin tags.
  220. *
  221. * @since 2.7.0
  222. *
  223. * @param array $args
  224. * @return array
  225. */
  226. function install_popular_tags( $args = array() ) {
  227. $key = md5( serialize( $args ) );
  228. $tags = get_site_transient( 'poptags_' . $key );
  229. if ( false !== $tags ) {
  230. return $tags;
  231. }
  232. $tags = plugins_api( 'hot_tags', $args );
  233. if ( is_wp_error( $tags ) ) {
  234. return $tags;
  235. }
  236. set_site_transient( 'poptags_' . $key, $tags, 3 * HOUR_IN_SECONDS );
  237. return $tags;
  238. }
  239. /**
  240. * @since 2.7.0
  241. */
  242. function install_dashboard() {
  243. ?>
  244. <p>
  245. <?php
  246. printf(
  247. /* translators: %s: https://wordpress.org/plugins/ */
  248. __( 'Plugins extend and expand the functionality of WordPress. You may automatically install plugins from the <a href="%s">WordPress Plugin Directory</a> or upload a plugin in .zip format by clicking the button at the top of this page.' ),
  249. __( 'https://wordpress.org/plugins/' )
  250. );
  251. ?>
  252. </p>
  253. <?php display_plugins_table(); ?>
  254. <div class="plugins-popular-tags-wrapper">
  255. <h2><?php _e( 'Popular tags' ); ?></h2>
  256. <p><?php _e( 'You may also browse based on the most popular tags in the Plugin Directory:' ); ?></p>
  257. <?php
  258. $api_tags = install_popular_tags();
  259. echo '<p class="popular-tags">';
  260. if ( is_wp_error( $api_tags ) ) {
  261. echo $api_tags->get_error_message();
  262. } else {
  263. //Set up the tags in a way which can be interpreted by wp_generate_tag_cloud()
  264. $tags = array();
  265. foreach ( (array) $api_tags as $tag ) {
  266. $url = self_admin_url( 'plugin-install.php?tab=search&type=tag&s=' . urlencode( $tag['name'] ) );
  267. $data = array(
  268. 'link' => esc_url( $url ),
  269. 'name' => $tag['name'],
  270. 'slug' => $tag['slug'],
  271. 'id' => sanitize_title_with_dashes( $tag['name'] ),
  272. 'count' => $tag['count'],
  273. );
  274. $tags[ $tag['name'] ] = (object) $data;
  275. }
  276. echo wp_generate_tag_cloud(
  277. $tags,
  278. array(
  279. /* translators: %s: Number of plugins. */
  280. 'single_text' => __( '%s plugin' ),
  281. /* translators: %s: Number of plugins. */
  282. 'multiple_text' => __( '%s plugins' ),
  283. )
  284. );
  285. }
  286. echo '</p><br class="clear" /></div>';
  287. }
  288. /**
  289. * Displays a search form for searching plugins.
  290. *
  291. * @since 2.7.0
  292. * @since 4.6.0 The `$type_selector` parameter was deprecated.
  293. *
  294. * @param bool $deprecated Not used.
  295. */
  296. function install_search_form( $deprecated = true ) {
  297. $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
  298. $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
  299. ?>
  300. <form class="search-form search-plugins" method="get">
  301. <input type="hidden" name="tab" value="search" />
  302. <label class="screen-reader-text" for="typeselector"><?php _e( 'Search plugins by:' ); ?></label>
  303. <select name="type" id="typeselector">
  304. <option value="term"<?php selected( 'term', $type ); ?>><?php _e( 'Keyword' ); ?></option>
  305. <option value="author"<?php selected( 'author', $type ); ?>><?php _e( 'Author' ); ?></option>
  306. <option value="tag"<?php selected( 'tag', $type ); ?>><?php _ex( 'Tag', 'Plugin Installer' ); ?></option>
  307. </select>
  308. <label><span class="screen-reader-text"><?php _e( 'Search Plugins' ); ?></span>
  309. <input type="search" name="s" value="<?php echo esc_attr( $term ); ?>" class="wp-filter-search" placeholder="<?php esc_attr_e( 'Search plugins...' ); ?>" />
  310. </label>
  311. <?php submit_button( __( 'Search Plugins' ), 'hide-if-js', false, false, array( 'id' => 'search-submit' ) ); ?>
  312. </form>
  313. <?php
  314. }
  315. /**
  316. * Upload from zip
  317. *
  318. * @since 2.8.0
  319. */
  320. function install_plugins_upload() {
  321. ?>
  322. <div class="upload-plugin">
  323. <p class="install-help"><?php _e( 'If you have a plugin in a .zip format, you may install it by uploading it here.' ); ?></p>
  324. <form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo self_admin_url( 'update.php?action=upload-plugin' ); ?>">
  325. <?php wp_nonce_field( 'plugin-upload' ); ?>
  326. <label class="screen-reader-text" for="pluginzip"><?php _e( 'Plugin zip file' ); ?></label>
  327. <input type="file" id="pluginzip" name="pluginzip" />
  328. <?php submit_button( __( 'Install Now' ), '', 'install-plugin-submit', false ); ?>
  329. </form>
  330. </div>
  331. <?php
  332. }
  333. /**
  334. * Show a username form for the favorites page
  335. *
  336. * @since 3.5.0
  337. */
  338. function install_plugins_favorites_form() {
  339. $user = get_user_option( 'wporg_favorites' );
  340. $action = 'save_wporg_username_' . get_current_user_id();
  341. ?>
  342. <p class="install-help"><?php _e( 'If you have marked plugins as favorites on WordPress.org, you can browse them here.' ); ?></p>
  343. <form method="get">
  344. <input type="hidden" name="tab" value="favorites" />
  345. <p>
  346. <label for="user"><?php _e( 'Your WordPress.org username:' ); ?></label>
  347. <input type="search" id="user" name="user" value="<?php echo esc_attr( $user ); ?>" />
  348. <input type="submit" class="button" value="<?php esc_attr_e( 'Get Favorites' ); ?>" />
  349. <input type="hidden" id="wporg-username-nonce" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( $action ) ); ?>" />
  350. </p>
  351. </form>
  352. <?php
  353. }
  354. /**
  355. * Display plugin content based on plugin list.
  356. *
  357. * @since 2.7.0
  358. *
  359. * @global WP_List_Table $wp_list_table
  360. */
  361. function display_plugins_table() {
  362. global $wp_list_table;
  363. switch ( current_filter() ) {
  364. case 'install_plugins_favorites':
  365. if ( empty( $_GET['user'] ) && ! get_user_option( 'wporg_favorites' ) ) {
  366. return;
  367. }
  368. break;
  369. case 'install_plugins_recommended':
  370. echo '<p>' . __( 'These suggestions are based on the plugins you and other users have installed.' ) . '</p>';
  371. break;
  372. case 'install_plugins_beta':
  373. printf(
  374. /* translators: %s: URL to "Features as Plugins" page. */
  375. '<p>' . __( 'You are using a development version of WordPress. These feature plugins are also under development. <a href="%s">Learn more</a>.' ) . '</p>',
  376. 'https://make.wordpress.org/core/handbook/about/release-cycle/features-as-plugins/'
  377. );
  378. break;
  379. }
  380. ?>
  381. <form id="plugin-filter" method="post">
  382. <?php $wp_list_table->display(); ?>
  383. </form>
  384. <?php
  385. }
  386. /**
  387. * Determine the status we can perform on a plugin.
  388. *
  389. * @since 3.0.0
  390. *
  391. * @param array|object $api Data about the plugin retrieved from the API.
  392. * @param bool $loop Optional. Disable further loops. Default false.
  393. * @return array {
  394. * Plugin installation status data.
  395. *
  396. * @type string $status Status of a plugin. Could be one of 'install', 'update_available', 'latest_installed' or 'newer_installed'.
  397. * @type string $url Plugin installation URL.
  398. * @type string $version The most recent version of the plugin.
  399. * @type string $file Plugin filename relative to the plugins directory.
  400. * }
  401. */
  402. function install_plugin_install_status( $api, $loop = false ) {
  403. // This function is called recursively, $loop prevents further loops.
  404. if ( is_array( $api ) ) {
  405. $api = (object) $api;
  406. }
  407. // Default to a "new" plugin
  408. $status = 'install';
  409. $url = false;
  410. $update_file = false;
  411. $version = '';
  412. /*
  413. * Check to see if this plugin is known to be installed,
  414. * and has an update awaiting it.
  415. */
  416. $update_plugins = get_site_transient( 'update_plugins' );
  417. if ( isset( $update_plugins->response ) ) {
  418. foreach ( (array) $update_plugins->response as $file => $plugin ) {
  419. if ( $plugin->slug === $api->slug ) {
  420. $status = 'update_available';
  421. $update_file = $file;
  422. $version = $plugin->new_version;
  423. if ( current_user_can( 'update_plugins' ) ) {
  424. $url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . $update_file ), 'upgrade-plugin_' . $update_file );
  425. }
  426. break;
  427. }
  428. }
  429. }
  430. if ( 'install' == $status ) {
  431. if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
  432. $installed_plugin = get_plugins( '/' . $api->slug );
  433. if ( empty( $installed_plugin ) ) {
  434. if ( current_user_can( 'install_plugins' ) ) {
  435. $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
  436. }
  437. } else {
  438. $key = array_keys( $installed_plugin );
  439. $key = reset( $key ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers
  440. $update_file = $api->slug . '/' . $key;
  441. if ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '=' ) ) {
  442. $status = 'latest_installed';
  443. } elseif ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '<' ) ) {
  444. $status = 'newer_installed';
  445. $version = $installed_plugin[ $key ]['Version'];
  446. } else {
  447. //If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh
  448. if ( ! $loop ) {
  449. delete_site_transient( 'update_plugins' );
  450. wp_update_plugins();
  451. return install_plugin_install_status( $api, true );
  452. }
  453. }
  454. }
  455. } else {
  456. // "install" & no directory with that slug
  457. if ( current_user_can( 'install_plugins' ) ) {
  458. $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
  459. }
  460. }
  461. }
  462. if ( isset( $_GET['from'] ) ) {
  463. $url .= '&amp;from=' . urlencode( wp_unslash( $_GET['from'] ) );
  464. }
  465. $file = $update_file;
  466. return compact( 'status', 'url', 'version', 'file' );
  467. }
  468. /**
  469. * Display plugin information in dialog box form.
  470. *
  471. * @since 2.7.0
  472. *
  473. * @global string $tab
  474. */
  475. function install_plugin_information() {
  476. global $tab;
  477. if ( empty( $_REQUEST['plugin'] ) ) {
  478. return;
  479. }
  480. $api = plugins_api(
  481. 'plugin_information',
  482. array(
  483. 'slug' => wp_unslash( $_REQUEST['plugin'] ),
  484. )
  485. );
  486. if ( is_wp_error( $api ) ) {
  487. wp_die( $api );
  488. }
  489. $plugins_allowedtags = array(
  490. 'a' => array(
  491. 'href' => array(),
  492. 'title' => array(),
  493. 'target' => array(),
  494. ),
  495. 'abbr' => array( 'title' => array() ),
  496. 'acronym' => array( 'title' => array() ),
  497. 'code' => array(),
  498. 'pre' => array(),
  499. 'em' => array(),
  500. 'strong' => array(),
  501. 'div' => array( 'class' => array() ),
  502. 'span' => array( 'class' => array() ),
  503. 'p' => array(),
  504. 'br' => array(),
  505. 'ul' => array(),
  506. 'ol' => array(),
  507. 'li' => array(),
  508. 'h1' => array(),
  509. 'h2' => array(),
  510. 'h3' => array(),
  511. 'h4' => array(),
  512. 'h5' => array(),
  513. 'h6' => array(),
  514. 'img' => array(
  515. 'src' => array(),
  516. 'class' => array(),
  517. 'alt' => array(),
  518. ),
  519. 'blockquote' => array( 'cite' => true ),
  520. );
  521. $plugins_section_titles = array(
  522. 'description' => _x( 'Description', 'Plugin installer section title' ),
  523. 'installation' => _x( 'Installation', 'Plugin installer section title' ),
  524. 'faq' => _x( 'FAQ', 'Plugin installer section title' ),
  525. 'screenshots' => _x( 'Screenshots', 'Plugin installer section title' ),
  526. 'changelog' => _x( 'Changelog', 'Plugin installer section title' ),
  527. 'reviews' => _x( 'Reviews', 'Plugin installer section title' ),
  528. 'other_notes' => _x( 'Other Notes', 'Plugin installer section title' ),
  529. );
  530. // Sanitize HTML
  531. foreach ( (array) $api->sections as $section_name => $content ) {
  532. $api->sections[ $section_name ] = wp_kses( $content, $plugins_allowedtags );
  533. }
  534. foreach ( array( 'version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug' ) as $key ) {
  535. if ( isset( $api->$key ) ) {
  536. $api->$key = wp_kses( $api->$key, $plugins_allowedtags );
  537. }
  538. }
  539. $_tab = esc_attr( $tab );
  540. $section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description'; // Default to the Description tab, Do not translate, API returns English.
  541. if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
  542. $section_titles = array_keys( (array) $api->sections );
  543. $section = reset( $section_titles );
  544. }
  545. iframe_header( __( 'Plugin Installation' ) );
  546. $_with_banner = '';
  547. if ( ! empty( $api->banners ) && ( ! empty( $api->banners['low'] ) || ! empty( $api->banners['high'] ) ) ) {
  548. $_with_banner = 'with-banner';
  549. $low = empty( $api->banners['low'] ) ? $api->banners['high'] : $api->banners['low'];
  550. $high = empty( $api->banners['high'] ) ? $api->banners['low'] : $api->banners['high'];
  551. ?>
  552. <style type="text/css">
  553. #plugin-information-title.with-banner {
  554. background-image: url( <?php echo esc_url( $low ); ?> );
  555. }
  556. @media only screen and ( -webkit-min-device-pixel-ratio: 1.5 ) {
  557. #plugin-information-title.with-banner {
  558. background-image: url( <?php echo esc_url( $high ); ?> );
  559. }
  560. }
  561. </style>
  562. <?php
  563. }
  564. echo '<div id="plugin-information-scrollable">';
  565. echo "<div id='{$_tab}-title' class='{$_with_banner}'><div class='vignette'></div><h2>{$api->name}</h2></div>";
  566. echo "<div id='{$_tab}-tabs' class='{$_with_banner}'>\n";
  567. foreach ( (array) $api->sections as $section_name => $content ) {
  568. if ( 'reviews' === $section_name && ( empty( $api->ratings ) || 0 === array_sum( (array) $api->ratings ) ) ) {
  569. continue;
  570. }
  571. if ( isset( $plugins_section_titles[ $section_name ] ) ) {
  572. $title = $plugins_section_titles[ $section_name ];
  573. } else {
  574. $title = ucwords( str_replace( '_', ' ', $section_name ) );
  575. }
  576. $class = ( $section_name === $section ) ? ' class="current"' : '';
  577. $href = add_query_arg(
  578. array(
  579. 'tab' => $tab,
  580. 'section' => $section_name,
  581. )
  582. );
  583. $href = esc_url( $href );
  584. $san_section = esc_attr( $section_name );
  585. echo "\t<a name='$san_section' href='$href' $class>$title</a>\n";
  586. }
  587. echo "</div>\n";
  588. ?>
  589. <div id="<?php echo $_tab; ?>-content" class='<?php echo $_with_banner; ?>'>
  590. <div class="fyi">
  591. <ul>
  592. <?php if ( ! empty( $api->version ) ) { ?>
  593. <li><strong><?php _e( 'Version:' ); ?></strong> <?php echo $api->version; ?></li>
  594. <?php } if ( ! empty( $api->author ) ) { ?>
  595. <li><strong><?php _e( 'Author:' ); ?></strong> <?php echo links_add_target( $api->author, '_blank' ); ?></li>
  596. <?php } if ( ! empty( $api->last_updated ) ) { ?>
  597. <li><strong><?php _e( 'Last Updated:' ); ?></strong>
  598. <?php
  599. /* translators: %s: Human-readable time difference. */
  600. printf( __( '%s ago' ), human_time_diff( strtotime( $api->last_updated ) ) );
  601. ?>
  602. </li>
  603. <?php } if ( ! empty( $api->requires ) ) { ?>
  604. <li>
  605. <strong><?php _e( 'Requires WordPress Version:' ); ?></strong>
  606. <?php
  607. /* translators: %s: Version number. */
  608. printf( __( '%s or higher' ), $api->requires );
  609. ?>
  610. </li>
  611. <?php } if ( ! empty( $api->tested ) ) { ?>
  612. <li><strong><?php _e( 'Compatible up to:' ); ?></strong> <?php echo $api->tested; ?></li>
  613. <?php } if ( ! empty( $api->requires_php ) ) { ?>
  614. <li>
  615. <strong><?php _e( 'Requires PHP Version:' ); ?></strong>
  616. <?php
  617. /* translators: %s: Version number. */
  618. printf( __( '%s or higher' ), $api->requires_php );
  619. ?>
  620. </li>
  621. <?php } if ( isset( $api->active_installs ) ) { ?>
  622. <li><strong><?php _e( 'Active Installations:' ); ?></strong>
  623. <?php
  624. if ( $api->active_installs >= 1000000 ) {
  625. $active_installs_millions = floor( $api->active_installs / 1000000 );
  626. printf(
  627. /* translators: %s: Number of millions. */
  628. _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
  629. number_format_i18n( $active_installs_millions )
  630. );
  631. } elseif ( 0 == $api->active_installs ) {
  632. _ex( 'Less Than 10', 'Active plugin installations' );
  633. } else {
  634. echo number_format_i18n( $api->active_installs ) . '+';
  635. }
  636. ?>
  637. </li>
  638. <?php } if ( ! empty( $api->slug ) && empty( $api->external ) ) { ?>
  639. <li><a target="_blank" href="<?php echo __( 'https://wordpress.org/plugins/' ) . $api->slug; ?>/"><?php _e( 'WordPress.org Plugin Page &#187;' ); ?></a></li>
  640. <?php } if ( ! empty( $api->homepage ) ) { ?>
  641. <li><a target="_blank" href="<?php echo esc_url( $api->homepage ); ?>"><?php _e( 'Plugin Homepage &#187;' ); ?></a></li>
  642. <?php } if ( ! empty( $api->donate_link ) && empty( $api->contributors ) ) { ?>
  643. <li><a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a></li>
  644. <?php } ?>
  645. </ul>
  646. <?php if ( ! empty( $api->rating ) ) { ?>
  647. <h3><?php _e( 'Average Rating' ); ?></h3>
  648. <?php
  649. wp_star_rating(
  650. array(
  651. 'rating' => $api->rating,
  652. 'type' => 'percent',
  653. 'number' => $api->num_ratings,
  654. )
  655. );
  656. ?>
  657. <p aria-hidden="true" class="fyi-description">
  658. <?php
  659. printf(
  660. /* translators: %s: Number of ratings. */
  661. _n( '(based on %s rating)', '(based on %s ratings)', $api->num_ratings ),
  662. number_format_i18n( $api->num_ratings )
  663. );
  664. ?>
  665. </p>
  666. <?php
  667. }
  668. if ( ! empty( $api->ratings ) && array_sum( (array) $api->ratings ) > 0 ) {
  669. ?>
  670. <h3><?php _e( 'Reviews' ); ?></h3>
  671. <p class="fyi-description"><?php _e( 'Read all reviews on WordPress.org or write your own!' ); ?></p>
  672. <?php
  673. foreach ( $api->ratings as $key => $ratecount ) {
  674. // Avoid div-by-zero.
  675. $_rating = $api->num_ratings ? ( $ratecount / $api->num_ratings ) : 0;
  676. $aria_label = esc_attr(
  677. sprintf(
  678. /* translators: 1: Number of stars (used to determine singular/plural), 2: Number of reviews. */
  679. _n(
  680. 'Reviews with %1$d star: %2$s. Opens in a new tab.',
  681. 'Reviews with %1$d stars: %2$s. Opens in a new tab.',
  682. $key
  683. ),
  684. $key,
  685. number_format_i18n( $ratecount )
  686. )
  687. );
  688. ?>
  689. <div class="counter-container">
  690. <span class="counter-label">
  691. <?php
  692. printf(
  693. '<a href="%s" target="_blank" aria-label="%s">%s</a>',
  694. "https://wordpress.org/support/plugin/{$api->slug}/reviews/?filter={$key}",
  695. $aria_label,
  696. /* translators: %s: Number of stars. */
  697. sprintf( _n( '%d star', '%d stars', $key ), $key )
  698. );
  699. ?>
  700. </span>
  701. <span class="counter-back">
  702. <span class="counter-bar" style="width: <?php echo 92 * $_rating; ?>px;"></span>
  703. </span>
  704. <span class="counter-count" aria-hidden="true"><?php echo number_format_i18n( $ratecount ); ?></span>
  705. </div>
  706. <?php
  707. }
  708. }
  709. if ( ! empty( $api->contributors ) ) {
  710. ?>
  711. <h3><?php _e( 'Contributors' ); ?></h3>
  712. <ul class="contributors">
  713. <?php
  714. foreach ( (array) $api->contributors as $contrib_username => $contrib_details ) {
  715. $contrib_name = $contrib_details['display_name'];
  716. if ( ! $contrib_name ) {
  717. $contrib_name = $contrib_username;
  718. }
  719. $contrib_name = esc_html( $contrib_name );
  720. $contrib_profile = esc_url( $contrib_details['profile'] );
  721. $contrib_avatar = esc_url( add_query_arg( 's', '36', $contrib_details['avatar'] ) );
  722. echo "<li><a href='{$contrib_profile}' target='_blank'><img src='{$contrib_avatar}' width='18' height='18' alt='' />{$contrib_name}</a></li>";
  723. }
  724. ?>
  725. </ul>
  726. <?php if ( ! empty( $api->donate_link ) ) { ?>
  727. <a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a>
  728. <?php } ?>
  729. <?php } ?>
  730. </div>
  731. <div id="section-holder">
  732. <?php
  733. $requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
  734. $requires_wp = isset( $api->requires ) ? $api->requires : null;
  735. $compatible_php = is_php_version_compatible( $requires_php );
  736. $compatible_wp = is_wp_version_compatible( $requires_wp );
  737. $tested_wp = ( empty( $api->tested ) || version_compare( get_bloginfo( 'version' ), $api->tested, '<=' ) );
  738. if ( ! $compatible_php ) {
  739. echo '<div class="notice notice-error notice-alt"><p>';
  740. _e( '<strong>Error:</strong> This plugin <strong>requires a newer version of PHP</strong>.' );
  741. if ( current_user_can( 'update_php' ) ) {
  742. printf(
  743. /* translators: %s: URL to Update PHP page. */
  744. ' ' . __( '<a href="%s" target="_blank">Click here to learn more about updating PHP</a>.' ),
  745. esc_url( wp_get_update_php_url() )
  746. );
  747. wp_update_php_annotation( '</p><p><em>', '</em>' );
  748. } else {
  749. echo '</p>';
  750. }
  751. echo '</div>';
  752. }
  753. if ( ! $tested_wp ) {
  754. echo '<div class="notice notice-warning notice-alt"><p>';
  755. _e( '<strong>Warning:</strong> This plugin <strong>has not been tested</strong> with your current version of WordPress.' );
  756. echo '</p></div>';
  757. } elseif ( ! $compatible_wp ) {
  758. echo '<div class="notice notice-error notice-alt"><p>';
  759. _e( '<strong>Error:</strong> This plugin <strong>requires a newer version of WordPress</strong>.' );
  760. if ( current_user_can( 'update_core' ) ) {
  761. printf(
  762. /* translators: %s: URL to WordPress Updates screen. */
  763. ' ' . __( '<a href="%s" target="_parent">Click here to update WordPress</a>.' ),
  764. self_admin_url( 'update-core.php' )
  765. );
  766. }
  767. echo '</p></div>';
  768. }
  769. foreach ( (array) $api->sections as $section_name => $content ) {
  770. $content = links_add_base_url( $content, 'https://wordpress.org/plugins/' . $api->slug . '/' );
  771. $content = links_add_target( $content, '_blank' );
  772. $san_section = esc_attr( $section_name );
  773. $display = ( $section_name === $section ) ? 'block' : 'none';
  774. echo "\t<div id='section-{$san_section}' class='section' style='display: {$display};'>\n";
  775. echo $content;
  776. echo "\t</div>\n";
  777. }
  778. echo "</div>\n";
  779. echo "</div>\n";
  780. echo "</div>\n"; // #plugin-information-scrollable
  781. echo "<div id='$tab-footer'>\n";
  782. if ( ! empty( $api->download_link ) && ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) ) {
  783. $status = install_plugin_install_status( $api );
  784. switch ( $status['status'] ) {
  785. case 'install':
  786. if ( $status['url'] ) {
  787. if ( $compatible_php && $compatible_wp ) {
  788. echo '<a data-slug="' . esc_attr( $api->slug ) . '" id="plugin_install_from_iframe" class="button button-primary right" href="' . $status['url'] . '" target="_parent">' . __( 'Install Now' ) . '</a>';
  789. } else {
  790. printf(
  791. '<button type="button" class="button button-primary button-disabled right" disabled="disabled">%s</button>',
  792. _x( 'Cannot Install', 'plugin' )
  793. );
  794. }
  795. }
  796. break;
  797. case 'update_available':
  798. if ( $status['url'] ) {
  799. if ( $compatible_php ) {
  800. echo '<a data-slug="' . esc_attr( $api->slug ) . '" data-plugin="' . esc_attr( $status['file'] ) . '" id="plugin_update_from_iframe" class="button button-primary right" href="' . $status['url'] . '" target="_parent">' . __( 'Install Update Now' ) . '</a>';
  801. } else {
  802. printf(
  803. '<button type="button" class="button button-primary button-disabled right" disabled="disabled">%s</button>',
  804. _x( 'Cannot Update', 'plugin' )
  805. );
  806. }
  807. }
  808. break;
  809. case 'newer_installed':
  810. /* translators: %s: Plugin version. */
  811. echo '<a class="button button-primary right disabled">' . sprintf( __( 'Newer Version (%s) Installed' ), $status['version'] ) . '</a>';
  812. break;
  813. case 'latest_installed':
  814. echo '<a class="button button-primary right disabled">' . __( 'Latest Version Installed' ) . '</a>';
  815. break;
  816. }
  817. }
  818. echo "</div>\n";
  819. iframe_footer();
  820. exit;
  821. }