You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

597 regels
18 KiB

  1. <?php
  2. /**
  3. * List Table API: WP_Plugin_Install_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying plugins to install in a list table.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. *
  15. * @see WP_List_Table
  16. */
  17. class WP_Plugin_Install_List_Table extends WP_List_Table {
  18. public $order = 'ASC';
  19. public $orderby = null;
  20. public $groups = array();
  21. private $error;
  22. /**
  23. *
  24. * @return bool
  25. */
  26. public function ajax_user_can() {
  27. return current_user_can('install_plugins');
  28. }
  29. /**
  30. * Return a list of slugs of installed plugins, if known.
  31. *
  32. * Uses the transient data from the updates API to determine the slugs of
  33. * known installed plugins. This might be better elsewhere, perhaps even
  34. * within get_plugins().
  35. *
  36. * @since 4.0.0
  37. * @access protected
  38. *
  39. * @return array
  40. */
  41. protected function get_installed_plugin_slugs() {
  42. $slugs = array();
  43. $plugin_info = get_site_transient( 'update_plugins' );
  44. if ( isset( $plugin_info->no_update ) ) {
  45. foreach ( $plugin_info->no_update as $plugin ) {
  46. $slugs[] = $plugin->slug;
  47. }
  48. }
  49. if ( isset( $plugin_info->response ) ) {
  50. foreach ( $plugin_info->response as $plugin ) {
  51. $slugs[] = $plugin->slug;
  52. }
  53. }
  54. return $slugs;
  55. }
  56. /**
  57. *
  58. * @global array $tabs
  59. * @global string $tab
  60. * @global int $paged
  61. * @global string $type
  62. * @global string $term
  63. */
  64. public function prepare_items() {
  65. include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
  66. global $tabs, $tab, $paged, $type, $term;
  67. wp_reset_vars( array( 'tab' ) );
  68. $paged = $this->get_pagenum();
  69. $per_page = 30;
  70. // These are the tabs which are shown on the page
  71. $tabs = array();
  72. if ( 'search' === $tab ) {
  73. $tabs['search'] = __( 'Search Results' );
  74. }
  75. if ( $tab === 'beta' || false !== strpos( get_bloginfo( 'version' ), '-' ) ) {
  76. $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
  77. }
  78. $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
  79. $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
  80. $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
  81. $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
  82. if ( current_user_can( 'upload_plugins' ) ) {
  83. // No longer a real tab. Here for filter compatibility.
  84. // Gets skipped in get_views().
  85. $tabs['upload'] = __( 'Upload Plugin' );
  86. }
  87. $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
  88. /**
  89. * Filters the tabs shown on the Plugin Install screen.
  90. *
  91. * @since 2.7.0
  92. *
  93. * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
  94. * 'recommended', 'favorites', and 'upload'.
  95. */
  96. $tabs = apply_filters( 'install_plugins_tabs', $tabs );
  97. /**
  98. * Filters tabs not associated with a menu item on the Plugin Install screen.
  99. *
  100. * @since 2.7.0
  101. *
  102. * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
  103. */
  104. $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
  105. // If a non-valid menu tab has been selected, And it's not a non-menu action.
  106. if ( empty( $tab ) || ( !isset( $tabs[ $tab ] ) && !in_array( $tab, (array) $nonmenu_tabs ) ) )
  107. $tab = key( $tabs );
  108. $args = array(
  109. 'page' => $paged,
  110. 'per_page' => $per_page,
  111. 'fields' => array(
  112. 'last_updated' => true,
  113. 'icons' => true,
  114. 'active_installs' => true
  115. ),
  116. // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
  117. 'locale' => get_user_locale(),
  118. 'installed_plugins' => $this->get_installed_plugin_slugs(),
  119. );
  120. switch ( $tab ) {
  121. case 'search':
  122. $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
  123. $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
  124. switch ( $type ) {
  125. case 'tag':
  126. $args['tag'] = sanitize_title_with_dashes( $term );
  127. break;
  128. case 'term':
  129. $args['search'] = $term;
  130. break;
  131. case 'author':
  132. $args['author'] = $term;
  133. break;
  134. }
  135. break;
  136. case 'featured':
  137. $args['fields']['group'] = true;
  138. $this->orderby = 'group';
  139. // No break!
  140. case 'popular':
  141. case 'new':
  142. case 'beta':
  143. case 'recommended':
  144. $args['browse'] = $tab;
  145. break;
  146. case 'favorites':
  147. $action = 'save_wporg_username_' . get_current_user_id();
  148. if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action ) ) {
  149. $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
  150. update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
  151. } else {
  152. $user = get_user_option( 'wporg_favorites' );
  153. }
  154. if ( $user )
  155. $args['user'] = $user;
  156. else
  157. $args = false;
  158. add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
  159. break;
  160. default:
  161. $args = false;
  162. break;
  163. }
  164. /**
  165. * Filters API request arguments for each Plugin Install screen tab.
  166. *
  167. * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
  168. * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
  169. *
  170. * @since 3.7.0
  171. *
  172. * @param array|bool $args Plugin Install API arguments.
  173. */
  174. $args = apply_filters( "install_plugins_table_api_args_{$tab}", $args );
  175. if ( !$args )
  176. return;
  177. $api = plugins_api( 'query_plugins', $args );
  178. if ( is_wp_error( $api ) ) {
  179. $this->error = $api;
  180. return;
  181. }
  182. $this->items = $api->plugins;
  183. if ( $this->orderby ) {
  184. uasort( $this->items, array( $this, 'order_callback' ) );
  185. }
  186. $this->set_pagination_args( array(
  187. 'total_items' => $api->info['results'],
  188. 'per_page' => $args['per_page'],
  189. ) );
  190. if ( isset( $api->info['groups'] ) ) {
  191. $this->groups = $api->info['groups'];
  192. }
  193. }
  194. /**
  195. * @access public
  196. */
  197. public function no_items() {
  198. if ( isset( $this->error ) ) {
  199. $message = $this->error->get_error_message() . '<p class="hide-if-no-js"><a href="#" class="button" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a></p>';
  200. } else {
  201. $message = __( 'No plugins match your request.' );
  202. }
  203. echo '<div class="no-plugin-results">' . $message . '</div>';
  204. }
  205. /**
  206. *
  207. * @global array $tabs
  208. * @global string $tab
  209. *
  210. * @return array
  211. */
  212. protected function get_views() {
  213. global $tabs, $tab;
  214. $display_tabs = array();
  215. foreach ( (array) $tabs as $action => $text ) {
  216. $class = ( $action === $tab ) ? ' current' : '';
  217. $href = self_admin_url('plugin-install.php?tab=' . $action);
  218. $display_tabs['plugin-install-'.$action] = "<a href='$href' class='$class'>$text</a>";
  219. }
  220. // No longer a real tab.
  221. unset( $display_tabs['plugin-install-upload'] );
  222. return $display_tabs;
  223. }
  224. /**
  225. * Override parent views so we can use the filter bar display.
  226. */
  227. public function views() {
  228. $views = $this->get_views();
  229. /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
  230. $views = apply_filters( "views_{$this->screen->id}", $views );
  231. $this->screen->render_screen_reader_content( 'heading_views' );
  232. ?>
  233. <div class="wp-filter">
  234. <ul class="filter-links">
  235. <?php
  236. if ( ! empty( $views ) ) {
  237. foreach ( $views as $class => $view ) {
  238. $views[ $class ] = "\t<li class='$class'>$view";
  239. }
  240. echo implode( " </li>\n", $views ) . "</li>\n";
  241. }
  242. ?>
  243. </ul>
  244. <?php install_search_form(); ?>
  245. </div>
  246. <?php
  247. }
  248. /**
  249. * Override the parent display() so we can provide a different container.
  250. */
  251. public function display() {
  252. $singular = $this->_args['singular'];
  253. $data_attr = '';
  254. if ( $singular ) {
  255. $data_attr = " data-wp-lists='list:$singular'";
  256. }
  257. $this->display_tablenav( 'top' );
  258. ?>
  259. <div class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
  260. <?php
  261. $this->screen->render_screen_reader_content( 'heading_list' );
  262. ?>
  263. <div id="the-list"<?php echo $data_attr; ?>>
  264. <?php $this->display_rows_or_placeholder(); ?>
  265. </div>
  266. </div>
  267. <?php
  268. $this->display_tablenav( 'bottom' );
  269. }
  270. /**
  271. * @global string $tab
  272. *
  273. * @param string $which
  274. */
  275. protected function display_tablenav( $which ) {
  276. if ( $GLOBALS['tab'] === 'featured' ) {
  277. return;
  278. }
  279. if ( 'top' === $which ) {
  280. wp_referer_field();
  281. ?>
  282. <div class="tablenav top">
  283. <div class="alignleft actions">
  284. <?php
  285. /**
  286. * Fires before the Plugin Install table header pagination is displayed.
  287. *
  288. * @since 2.7.0
  289. */
  290. do_action( 'install_plugins_table_header' ); ?>
  291. </div>
  292. <?php $this->pagination( $which ); ?>
  293. <br class="clear" />
  294. </div>
  295. <?php } else { ?>
  296. <div class="tablenav bottom">
  297. <?php $this->pagination( $which ); ?>
  298. <br class="clear" />
  299. </div>
  300. <?php
  301. }
  302. }
  303. /**
  304. * @return array
  305. */
  306. protected function get_table_classes() {
  307. return array( 'widefat', $this->_args['plural'] );
  308. }
  309. /**
  310. * @return array
  311. */
  312. public function get_columns() {
  313. return array();
  314. }
  315. /**
  316. * @param object $plugin_a
  317. * @param object $plugin_b
  318. * @return int
  319. */
  320. private function order_callback( $plugin_a, $plugin_b ) {
  321. $orderby = $this->orderby;
  322. if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
  323. return 0;
  324. }
  325. $a = $plugin_a->$orderby;
  326. $b = $plugin_b->$orderby;
  327. if ( $a == $b ) {
  328. return 0;
  329. }
  330. if ( 'DESC' === $this->order ) {
  331. return ( $a < $b ) ? 1 : -1;
  332. } else {
  333. return ( $a < $b ) ? -1 : 1;
  334. }
  335. }
  336. public function display_rows() {
  337. $plugins_allowedtags = array(
  338. 'a' => array( 'href' => array(),'title' => array(), 'target' => array() ),
  339. 'abbr' => array( 'title' => array() ),'acronym' => array( 'title' => array() ),
  340. 'code' => array(), 'pre' => array(), 'em' => array(),'strong' => array(),
  341. 'ul' => array(), 'ol' => array(), 'li' => array(), 'p' => array(), 'br' => array()
  342. );
  343. $plugins_group_titles = array(
  344. 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
  345. 'Social' => _x( 'Social', 'Plugin installer group title' ),
  346. 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
  347. );
  348. $group = null;
  349. foreach ( (array) $this->items as $plugin ) {
  350. if ( is_object( $plugin ) ) {
  351. $plugin = (array) $plugin;
  352. }
  353. // Display the group heading if there is one
  354. if ( isset( $plugin['group'] ) && $plugin['group'] != $group ) {
  355. if ( isset( $this->groups[ $plugin['group'] ] ) ) {
  356. $group_name = $this->groups[ $plugin['group'] ];
  357. if ( isset( $plugins_group_titles[ $group_name ] ) ) {
  358. $group_name = $plugins_group_titles[ $group_name ];
  359. }
  360. } else {
  361. $group_name = $plugin['group'];
  362. }
  363. // Starting a new group, close off the divs of the last one
  364. if ( ! empty( $group ) ) {
  365. echo '</div></div>';
  366. }
  367. echo '<div class="plugin-group"><h3>' . esc_html( $group_name ) . '</h3>';
  368. // needs an extra wrapping div for nth-child selectors to work
  369. echo '<div class="plugin-items">';
  370. $group = $plugin['group'];
  371. }
  372. $title = wp_kses( $plugin['name'], $plugins_allowedtags );
  373. // Remove any HTML from the description.
  374. $description = strip_tags( $plugin['short_description'] );
  375. $version = wp_kses( $plugin['version'], $plugins_allowedtags );
  376. $name = strip_tags( $title . ' ' . $version );
  377. $author = wp_kses( $plugin['author'], $plugins_allowedtags );
  378. if ( ! empty( $author ) ) {
  379. $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
  380. }
  381. $action_links = array();
  382. if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
  383. $status = install_plugin_install_status( $plugin );
  384. switch ( $status['status'] ) {
  385. case 'install':
  386. if ( $status['url'] ) {
  387. /* translators: 1: Plugin name and version. */
  388. $action_links[] = '<a class="install-now button" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Install Now' ) . '</a>';
  389. }
  390. break;
  391. case 'update_available':
  392. if ( $status['url'] ) {
  393. /* translators: 1: Plugin name and version */
  394. $action_links[] = '<a class="update-now button aria-button-if-js" data-plugin="' . esc_attr( $status['file'] ) . '" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Update Now' ) . '</a>';
  395. }
  396. break;
  397. case 'latest_installed':
  398. case 'newer_installed':
  399. if ( is_plugin_active( $status['file'] ) ) {
  400. $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Active', 'plugin' ) . '</button>';
  401. } elseif ( current_user_can( 'activate_plugins' ) ) {
  402. $button_text = __( 'Activate' );
  403. /* translators: %s: Plugin name */
  404. $button_label = _x( 'Activate %s', 'plugin' );
  405. $activate_url = add_query_arg( array(
  406. '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
  407. 'action' => 'activate',
  408. 'plugin' => $status['file'],
  409. ), network_admin_url( 'plugins.php' ) );
  410. if ( is_network_admin() ) {
  411. $button_text = __( 'Network Activate' );
  412. /* translators: %s: Plugin name */
  413. $button_label = _x( 'Network Activate %s', 'plugin' );
  414. $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
  415. }
  416. $action_links[] = sprintf(
  417. '<a href="%1$s" class="button activate-now" aria-label="%2$s">%3$s</a>',
  418. esc_url( $activate_url ),
  419. esc_attr( sprintf( $button_label, $plugin['name'] ) ),
  420. $button_text
  421. );
  422. } else {
  423. $action_links[] = '<button type="button" class="button button-disabled" disabled="disabled">' . _x( 'Installed', 'plugin' ) . '</button>';
  424. }
  425. break;
  426. }
  427. }
  428. $details_link = self_admin_url( 'plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .
  429. '&amp;TB_iframe=true&amp;width=600&amp;height=550' );
  430. /* translators: 1: Plugin name and version. */
  431. $action_links[] = '<a href="' . esc_url( $details_link ) . '" class="thickbox open-plugin-details-modal" aria-label="' . esc_attr( sprintf( __( 'More information about %s' ), $name ) ) . '" data-title="' . esc_attr( $name ) . '">' . __( 'More Details' ) . '</a>';
  432. if ( !empty( $plugin['icons']['svg'] ) ) {
  433. $plugin_icon_url = $plugin['icons']['svg'];
  434. } elseif ( !empty( $plugin['icons']['2x'] ) ) {
  435. $plugin_icon_url = $plugin['icons']['2x'];
  436. } elseif ( !empty( $plugin['icons']['1x'] ) ) {
  437. $plugin_icon_url = $plugin['icons']['1x'];
  438. } else {
  439. $plugin_icon_url = $plugin['icons']['default'];
  440. }
  441. /**
  442. * Filters the install action links for a plugin.
  443. *
  444. * @since 2.7.0
  445. *
  446. * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
  447. * @param array $plugin The plugin currently being listed.
  448. */
  449. $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
  450. $last_updated_timestamp = strtotime( $plugin['last_updated'] );
  451. ?>
  452. <div class="plugin-card plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
  453. <div class="plugin-card-top">
  454. <div class="name column-name">
  455. <h3>
  456. <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal">
  457. <?php echo $title; ?>
  458. <img src="<?php echo esc_attr( $plugin_icon_url ) ?>" class="plugin-icon" alt="">
  459. </a>
  460. </h3>
  461. </div>
  462. <div class="action-links">
  463. <?php
  464. if ( $action_links ) {
  465. echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>';
  466. }
  467. ?>
  468. </div>
  469. <div class="desc column-description">
  470. <p><?php echo $description; ?></p>
  471. <p class="authors"><?php echo $author; ?></p>
  472. </div>
  473. </div>
  474. <div class="plugin-card-bottom">
  475. <div class="vers column-rating">
  476. <?php wp_star_rating( array( 'rating' => $plugin['rating'], 'type' => 'percent', 'number' => $plugin['num_ratings'] ) ); ?>
  477. <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?>)</span>
  478. </div>
  479. <div class="column-updated">
  480. <strong><?php _e( 'Last Updated:' ); ?></strong> <?php printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) ); ?>
  481. </div>
  482. <div class="column-downloaded">
  483. <?php
  484. if ( $plugin['active_installs'] >= 1000000 ) {
  485. $active_installs_text = _x( '1+ Million', 'Active plugin installs' );
  486. } elseif ( 0 == $plugin['active_installs'] ) {
  487. $active_installs_text = _x( 'Less Than 10', 'Active plugin installs' );
  488. } else {
  489. $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
  490. }
  491. printf( __( '%s Active Installs' ), $active_installs_text );
  492. ?>
  493. </div>
  494. <div class="column-compatibility">
  495. <?php
  496. $wp_version = get_bloginfo( 'version' );
  497. if ( ! empty( $plugin['tested'] ) && version_compare( substr( $wp_version, 0, strlen( $plugin['tested'] ) ), $plugin['tested'], '>' ) ) {
  498. echo '<span class="compatibility-untested">' . __( 'Untested with your version of WordPress' ) . '</span>';
  499. } elseif ( ! empty( $plugin['requires'] ) && version_compare( substr( $wp_version, 0, strlen( $plugin['requires'] ) ), $plugin['requires'], '<' ) ) {
  500. echo '<span class="compatibility-incompatible">' . __( '<strong>Incompatible</strong> with your version of WordPress' ) . '</span>';
  501. } else {
  502. echo '<span class="compatibility-compatible">' . __( '<strong>Compatible</strong> with your version of WordPress' ) . '</span>';
  503. }
  504. ?>
  505. </div>
  506. </div>
  507. </div>
  508. <?php
  509. }
  510. // Close off the group divs of the last one
  511. if ( ! empty( $group ) ) {
  512. echo '</div></div>';
  513. }
  514. }
  515. }