Ei kuvausta

class-wp-theme-install-list-table.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * List Table API: WP_Theme_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 themes to install in a list table.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. *
  15. * @see WP_Themes_List_Table
  16. */
  17. class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
  18. public $features = array();
  19. /**
  20. * @return bool
  21. */
  22. public function ajax_user_can() {
  23. return current_user_can( 'install_themes' );
  24. }
  25. /**
  26. * @global array $tabs
  27. * @global string $tab
  28. * @global int $paged
  29. * @global string $type
  30. * @global array $theme_field_defaults
  31. */
  32. public function prepare_items() {
  33. require ABSPATH . 'wp-admin/includes/theme-install.php';
  34. global $tabs, $tab, $paged, $type, $theme_field_defaults;
  35. wp_reset_vars( array( 'tab' ) );
  36. $search_terms = array();
  37. $search_string = '';
  38. if ( ! empty( $_REQUEST['s'] ) ) {
  39. $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
  40. $search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
  41. }
  42. if ( ! empty( $_REQUEST['features'] ) ) {
  43. $this->features = $_REQUEST['features'];
  44. }
  45. $paged = $this->get_pagenum();
  46. $per_page = 36;
  47. // These are the tabs which are shown on the page,
  48. $tabs = array();
  49. $tabs['dashboard'] = __( 'Search' );
  50. if ( 'search' === $tab ) {
  51. $tabs['search'] = __( 'Search Results' );
  52. }
  53. $tabs['upload'] = __( 'Upload' );
  54. $tabs['featured'] = _x( 'Featured', 'themes' );
  55. //$tabs['popular'] = _x( 'Popular', 'themes' );
  56. $tabs['new'] = _x( 'Latest', 'themes' );
  57. $tabs['updated'] = _x( 'Recently Updated', 'themes' );
  58. $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
  59. /** This filter is documented in wp-admin/theme-install.php */
  60. $tabs = apply_filters( 'install_themes_tabs', $tabs );
  61. /**
  62. * Filters tabs not associated with a menu item on the Install Themes screen.
  63. *
  64. * @since 2.8.0
  65. *
  66. * @param string[] $nonmenu_tabs The tabs that don't have a menu item on
  67. * the Install Themes screen.
  68. */
  69. $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
  70. // If a non-valid menu tab has been selected, And it's not a non-menu action.
  71. if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs, true ) ) ) {
  72. $tab = key( $tabs );
  73. }
  74. $args = array(
  75. 'page' => $paged,
  76. 'per_page' => $per_page,
  77. 'fields' => $theme_field_defaults,
  78. );
  79. switch ( $tab ) {
  80. case 'search':
  81. $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
  82. switch ( $type ) {
  83. case 'tag':
  84. $args['tag'] = array_map( 'sanitize_key', $search_terms );
  85. break;
  86. case 'term':
  87. $args['search'] = $search_string;
  88. break;
  89. case 'author':
  90. $args['author'] = $search_string;
  91. break;
  92. }
  93. if ( ! empty( $this->features ) ) {
  94. $args['tag'] = $this->features;
  95. $_REQUEST['s'] = implode( ',', $this->features );
  96. $_REQUEST['type'] = 'tag';
  97. }
  98. add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
  99. break;
  100. case 'featured':
  101. // case 'popular':
  102. case 'new':
  103. case 'updated':
  104. $args['browse'] = $tab;
  105. break;
  106. default:
  107. $args = false;
  108. break;
  109. }
  110. /**
  111. * Filters API request arguments for each Install Themes screen tab.
  112. *
  113. * The dynamic portion of the hook name, `$tab`, refers to the theme install
  114. * tab.
  115. *
  116. * Possible hook names include:
  117. *
  118. * - `install_themes_table_api_args_dashboard`
  119. * - `install_themes_table_api_args_featured`
  120. * - `install_themes_table_api_args_new`
  121. * - `install_themes_table_api_args_search`
  122. * - `install_themes_table_api_args_updated`
  123. * - `install_themes_table_api_args_upload`
  124. *
  125. * @since 3.7.0
  126. *
  127. * @param array|false $args Theme install API arguments.
  128. */
  129. $args = apply_filters( "install_themes_table_api_args_{$tab}", $args );
  130. if ( ! $args ) {
  131. return;
  132. }
  133. $api = themes_api( 'query_themes', $args );
  134. if ( is_wp_error( $api ) ) {
  135. wp_die( '<p>' . $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try Again' ) . '</a></p>' );
  136. }
  137. $this->items = $api->themes;
  138. $this->set_pagination_args(
  139. array(
  140. 'total_items' => $api->info['results'],
  141. 'per_page' => $args['per_page'],
  142. 'infinite_scroll' => true,
  143. )
  144. );
  145. }
  146. /**
  147. */
  148. public function no_items() {
  149. _e( 'No themes match your request.' );
  150. }
  151. /**
  152. * @global array $tabs
  153. * @global string $tab
  154. * @return array
  155. */
  156. protected function get_views() {
  157. global $tabs, $tab;
  158. $display_tabs = array();
  159. foreach ( (array) $tabs as $action => $text ) {
  160. $current_link_attributes = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
  161. $href = self_admin_url( 'theme-install.php?tab=' . $action );
  162. $display_tabs[ 'theme-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
  163. }
  164. return $display_tabs;
  165. }
  166. /**
  167. * Displays the theme install table.
  168. *
  169. * Overrides the parent display() method to provide a different container.
  170. *
  171. * @since 3.1.0
  172. */
  173. public function display() {
  174. wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
  175. ?>
  176. <div class="tablenav top themes">
  177. <div class="alignleft actions">
  178. <?php
  179. /**
  180. * Fires in the Install Themes list table header.
  181. *
  182. * @since 2.8.0
  183. */
  184. do_action( 'install_themes_table_header' );
  185. ?>
  186. </div>
  187. <?php $this->pagination( 'top' ); ?>
  188. <br class="clear" />
  189. </div>
  190. <div id="availablethemes">
  191. <?php $this->display_rows_or_placeholder(); ?>
  192. </div>
  193. <?php
  194. $this->tablenav( 'bottom' );
  195. }
  196. /**
  197. */
  198. public function display_rows() {
  199. $themes = $this->items;
  200. foreach ( $themes as $theme ) {
  201. ?>
  202. <div class="available-theme installable-theme">
  203. <?php
  204. $this->single_row( $theme );
  205. ?>
  206. </div>
  207. <?php
  208. } // End foreach $theme_names.
  209. $this->theme_installer();
  210. }
  211. /**
  212. * Prints a theme from the WordPress.org API.
  213. *
  214. * @since 3.1.0
  215. *
  216. * @global array $themes_allowedtags
  217. *
  218. * @param stdClass $theme {
  219. * An object that contains theme data returned by the WordPress.org API.
  220. *
  221. * @type string $name Theme name, e.g. 'Twenty Twenty-One'.
  222. * @type string $slug Theme slug, e.g. 'twentytwentyone'.
  223. * @type string $version Theme version, e.g. '1.1'.
  224. * @type string $author Theme author username, e.g. 'melchoyce'.
  225. * @type string $preview_url Preview URL, e.g. 'https://2021.wordpress.net/'.
  226. * @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
  227. * @type float $rating Rating score.
  228. * @type int $num_ratings The number of ratings.
  229. * @type string $homepage Theme homepage, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
  230. * @type string $description Theme description.
  231. * @type string $download_link Theme ZIP download URL.
  232. * }
  233. */
  234. public function single_row( $theme ) {
  235. global $themes_allowedtags;
  236. if ( empty( $theme ) ) {
  237. return;
  238. }
  239. $name = wp_kses( $theme->name, $themes_allowedtags );
  240. $author = wp_kses( $theme->author, $themes_allowedtags );
  241. /* translators: %s: Theme name. */
  242. $preview_title = sprintf( __( 'Preview &#8220;%s&#8221;' ), $name );
  243. $preview_url = add_query_arg(
  244. array(
  245. 'tab' => 'theme-information',
  246. 'theme' => $theme->slug,
  247. ),
  248. self_admin_url( 'theme-install.php' )
  249. );
  250. $actions = array();
  251. $install_url = add_query_arg(
  252. array(
  253. 'action' => 'install-theme',
  254. 'theme' => $theme->slug,
  255. ),
  256. self_admin_url( 'update.php' )
  257. );
  258. $update_url = add_query_arg(
  259. array(
  260. 'action' => 'upgrade-theme',
  261. 'theme' => $theme->slug,
  262. ),
  263. self_admin_url( 'update.php' )
  264. );
  265. $status = $this->_get_theme_status( $theme );
  266. switch ( $status ) {
  267. case 'update_available':
  268. $actions[] = sprintf(
  269. '<a class="install-now" href="%s" title="%s">%s</a>',
  270. esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
  271. /* translators: %s: Theme version. */
  272. esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
  273. __( 'Update' )
  274. );
  275. break;
  276. case 'newer_installed':
  277. case 'latest_installed':
  278. $actions[] = sprintf(
  279. '<span class="install-now" title="%s">%s</span>',
  280. esc_attr__( 'This theme is already installed and is up to date' ),
  281. _x( 'Installed', 'theme' )
  282. );
  283. break;
  284. case 'install':
  285. default:
  286. $actions[] = sprintf(
  287. '<a class="install-now" href="%s" title="%s">%s</a>',
  288. esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
  289. /* translators: %s: Theme name. */
  290. esc_attr( sprintf( _x( 'Install %s', 'theme' ), $name ) ),
  291. __( 'Install Now' )
  292. );
  293. break;
  294. }
  295. $actions[] = sprintf(
  296. '<a class="install-theme-preview" href="%s" title="%s">%s</a>',
  297. esc_url( $preview_url ),
  298. /* translators: %s: Theme name. */
  299. esc_attr( sprintf( __( 'Preview %s' ), $name ) ),
  300. __( 'Preview' )
  301. );
  302. /**
  303. * Filters the install action links for a theme in the Install Themes list table.
  304. *
  305. * @since 3.4.0
  306. *
  307. * @param string[] $actions An array of theme action links. Defaults are
  308. * links to Install Now, Preview, and Details.
  309. * @param stdClass $theme An object that contains theme data returned by the
  310. * WordPress.org API.
  311. */
  312. $actions = apply_filters( 'theme_install_actions', $actions, $theme );
  313. ?>
  314. <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
  315. <img src="<?php echo esc_url( $theme->screenshot_url . '?ver=' . $theme->version ); ?>" width="150" alt="" />
  316. </a>
  317. <h3><?php echo $name; ?></h3>
  318. <div class="theme-author">
  319. <?php
  320. /* translators: %s: Theme author. */
  321. printf( __( 'By %s' ), $author );
  322. ?>
  323. </div>
  324. <div class="action-links">
  325. <ul>
  326. <?php foreach ( $actions as $action ) : ?>
  327. <li><?php echo $action; ?></li>
  328. <?php endforeach; ?>
  329. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
  330. </ul>
  331. </div>
  332. <?php
  333. $this->install_theme_info( $theme );
  334. }
  335. /**
  336. * Prints the wrapper for the theme installer.
  337. */
  338. public function theme_installer() {
  339. ?>
  340. <div id="theme-installer" class="wp-full-overlay expanded">
  341. <div class="wp-full-overlay-sidebar">
  342. <div class="wp-full-overlay-header">
  343. <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
  344. <span class="theme-install"></span>
  345. </div>
  346. <div class="wp-full-overlay-sidebar-content">
  347. <div class="install-theme-info"></div>
  348. </div>
  349. <div class="wp-full-overlay-footer">
  350. <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
  351. <span class="collapse-sidebar-arrow"></span>
  352. <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
  353. </button>
  354. </div>
  355. </div>
  356. <div class="wp-full-overlay-main"></div>
  357. </div>
  358. <?php
  359. }
  360. /**
  361. * Prints the wrapper for the theme installer with a provided theme's data.
  362. * Used to make the theme installer work for no-js.
  363. *
  364. * @param stdClass $theme A WordPress.org Theme API object.
  365. */
  366. public function theme_installer_single( $theme ) {
  367. ?>
  368. <div id="theme-installer" class="wp-full-overlay single-theme">
  369. <div class="wp-full-overlay-sidebar">
  370. <?php $this->install_theme_info( $theme ); ?>
  371. </div>
  372. <div class="wp-full-overlay-main">
  373. <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
  374. </div>
  375. </div>
  376. <?php
  377. }
  378. /**
  379. * Prints the info for a theme (to be used in the theme installer modal).
  380. *
  381. * @global array $themes_allowedtags
  382. *
  383. * @param stdClass $theme A WordPress.org Theme API object.
  384. */
  385. public function install_theme_info( $theme ) {
  386. global $themes_allowedtags;
  387. if ( empty( $theme ) ) {
  388. return;
  389. }
  390. $name = wp_kses( $theme->name, $themes_allowedtags );
  391. $author = wp_kses( $theme->author, $themes_allowedtags );
  392. $install_url = add_query_arg(
  393. array(
  394. 'action' => 'install-theme',
  395. 'theme' => $theme->slug,
  396. ),
  397. self_admin_url( 'update.php' )
  398. );
  399. $update_url = add_query_arg(
  400. array(
  401. 'action' => 'upgrade-theme',
  402. 'theme' => $theme->slug,
  403. ),
  404. self_admin_url( 'update.php' )
  405. );
  406. $status = $this->_get_theme_status( $theme );
  407. ?>
  408. <div class="install-theme-info">
  409. <?php
  410. switch ( $status ) {
  411. case 'update_available':
  412. printf(
  413. '<a class="theme-install button button-primary" href="%s" title="%s">%s</a>',
  414. esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
  415. /* translators: %s: Theme version. */
  416. esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
  417. __( 'Update' )
  418. );
  419. break;
  420. case 'newer_installed':
  421. case 'latest_installed':
  422. printf(
  423. '<span class="theme-install" title="%s">%s</span>',
  424. esc_attr__( 'This theme is already installed and is up to date' ),
  425. _x( 'Installed', 'theme' )
  426. );
  427. break;
  428. case 'install':
  429. default:
  430. printf(
  431. '<a class="theme-install button button-primary" href="%s">%s</a>',
  432. esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
  433. __( 'Install' )
  434. );
  435. break;
  436. }
  437. ?>
  438. <h3 class="theme-name"><?php echo $name; ?></h3>
  439. <span class="theme-by">
  440. <?php
  441. /* translators: %s: Theme author. */
  442. printf( __( 'By %s' ), $author );
  443. ?>
  444. </span>
  445. <?php if ( isset( $theme->screenshot_url ) ) : ?>
  446. <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url . '?ver=' . $theme->version ); ?>" alt="" />
  447. <?php endif; ?>
  448. <div class="theme-details">
  449. <?php
  450. wp_star_rating(
  451. array(
  452. 'rating' => $theme->rating,
  453. 'type' => 'percent',
  454. 'number' => $theme->num_ratings,
  455. )
  456. );
  457. ?>
  458. <div class="theme-version">
  459. <strong><?php _e( 'Version:' ); ?> </strong>
  460. <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
  461. </div>
  462. <div class="theme-description">
  463. <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
  464. </div>
  465. </div>
  466. <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
  467. </div>
  468. <?php
  469. }
  470. /**
  471. * Send required variables to JavaScript land
  472. *
  473. * @since 3.4.0
  474. *
  475. * @global string $tab Current tab within Themes->Install screen
  476. * @global string $type Type of search.
  477. *
  478. * @param array $extra_args Unused.
  479. */
  480. public function _js_vars( $extra_args = array() ) {
  481. global $tab, $type;
  482. parent::_js_vars( compact( 'tab', 'type' ) );
  483. }
  484. /**
  485. * Check to see if the theme is already installed.
  486. *
  487. * @since 3.4.0
  488. *
  489. * @param stdClass $theme A WordPress.org Theme API object.
  490. * @return string Theme status.
  491. */
  492. private function _get_theme_status( $theme ) {
  493. $status = 'install';
  494. $installed_theme = wp_get_theme( $theme->slug );
  495. if ( $installed_theme->exists() ) {
  496. if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
  497. $status = 'latest_installed';
  498. } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
  499. $status = 'newer_installed';
  500. } else {
  501. $status = 'update_available';
  502. }
  503. }
  504. return $status;
  505. }
  506. }