plugin-install.php 34 KB

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