Нема описа

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 object $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 WP_Theme $theme Theme object.
  310. */
  311. $actions = apply_filters( 'theme_install_actions', $actions, $theme );
  312. ?>
  313. <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
  314. <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" alt="" />
  315. </a>
  316. <h3><?php echo $name; ?></h3>
  317. <div class="theme-author">
  318. <?php
  319. /* translators: %s: Theme author. */
  320. printf( __( 'By %s' ), $author );
  321. ?>
  322. </div>
  323. <div class="action-links">
  324. <ul>
  325. <?php foreach ( $actions as $action ) : ?>
  326. <li><?php echo $action; ?></li>
  327. <?php endforeach; ?>
  328. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
  329. </ul>
  330. </div>
  331. <?php
  332. $this->install_theme_info( $theme );
  333. }
  334. /**
  335. * Prints the wrapper for the theme installer.
  336. */
  337. public function theme_installer() {
  338. ?>
  339. <div id="theme-installer" class="wp-full-overlay expanded">
  340. <div class="wp-full-overlay-sidebar">
  341. <div class="wp-full-overlay-header">
  342. <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
  343. <span class="theme-install"></span>
  344. </div>
  345. <div class="wp-full-overlay-sidebar-content">
  346. <div class="install-theme-info"></div>
  347. </div>
  348. <div class="wp-full-overlay-footer">
  349. <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
  350. <span class="collapse-sidebar-arrow"></span>
  351. <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
  352. </button>
  353. </div>
  354. </div>
  355. <div class="wp-full-overlay-main"></div>
  356. </div>
  357. <?php
  358. }
  359. /**
  360. * Prints the wrapper for the theme installer with a provided theme's data.
  361. * Used to make the theme installer work for no-js.
  362. *
  363. * @param object $theme - A WordPress.org Theme API object.
  364. */
  365. public function theme_installer_single( $theme ) {
  366. ?>
  367. <div id="theme-installer" class="wp-full-overlay single-theme">
  368. <div class="wp-full-overlay-sidebar">
  369. <?php $this->install_theme_info( $theme ); ?>
  370. </div>
  371. <div class="wp-full-overlay-main">
  372. <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
  373. </div>
  374. </div>
  375. <?php
  376. }
  377. /**
  378. * Prints the info for a theme (to be used in the theme installer modal).
  379. *
  380. * @global array $themes_allowedtags
  381. *
  382. * @param object $theme - A WordPress.org Theme API object.
  383. */
  384. public function install_theme_info( $theme ) {
  385. global $themes_allowedtags;
  386. if ( empty( $theme ) ) {
  387. return;
  388. }
  389. $name = wp_kses( $theme->name, $themes_allowedtags );
  390. $author = wp_kses( $theme->author, $themes_allowedtags );
  391. $install_url = add_query_arg(
  392. array(
  393. 'action' => 'install-theme',
  394. 'theme' => $theme->slug,
  395. ),
  396. self_admin_url( 'update.php' )
  397. );
  398. $update_url = add_query_arg(
  399. array(
  400. 'action' => 'upgrade-theme',
  401. 'theme' => $theme->slug,
  402. ),
  403. self_admin_url( 'update.php' )
  404. );
  405. $status = $this->_get_theme_status( $theme );
  406. ?>
  407. <div class="install-theme-info">
  408. <?php
  409. switch ( $status ) {
  410. case 'update_available':
  411. printf(
  412. '<a class="theme-install button button-primary" href="%s" title="%s">%s</a>',
  413. esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
  414. /* translators: %s: Theme version. */
  415. esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
  416. __( 'Update' )
  417. );
  418. break;
  419. case 'newer_installed':
  420. case 'latest_installed':
  421. printf(
  422. '<span class="theme-install" title="%s">%s</span>',
  423. esc_attr__( 'This theme is already installed and is up to date' ),
  424. _x( 'Installed', 'theme' )
  425. );
  426. break;
  427. case 'install':
  428. default:
  429. printf(
  430. '<a class="theme-install button button-primary" href="%s">%s</a>',
  431. esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
  432. __( 'Install' )
  433. );
  434. break;
  435. }
  436. ?>
  437. <h3 class="theme-name"><?php echo $name; ?></h3>
  438. <span class="theme-by">
  439. <?php
  440. /* translators: %s: Theme author. */
  441. printf( __( 'By %s' ), $author );
  442. ?>
  443. </span>
  444. <?php if ( isset( $theme->screenshot_url ) ) : ?>
  445. <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" alt="" />
  446. <?php endif; ?>
  447. <div class="theme-details">
  448. <?php
  449. wp_star_rating(
  450. array(
  451. 'rating' => $theme->rating,
  452. 'type' => 'percent',
  453. 'number' => $theme->num_ratings,
  454. )
  455. );
  456. ?>
  457. <div class="theme-version">
  458. <strong><?php _e( 'Version:' ); ?> </strong>
  459. <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
  460. </div>
  461. <div class="theme-description">
  462. <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
  463. </div>
  464. </div>
  465. <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
  466. </div>
  467. <?php
  468. }
  469. /**
  470. * Send required variables to JavaScript land
  471. *
  472. * @since 3.4.0
  473. *
  474. * @global string $tab Current tab within Themes->Install screen
  475. * @global string $type Type of search.
  476. *
  477. * @param array $extra_args Unused.
  478. */
  479. public function _js_vars( $extra_args = array() ) {
  480. global $tab, $type;
  481. parent::_js_vars( compact( 'tab', 'type' ) );
  482. }
  483. /**
  484. * Check to see if the theme is already installed.
  485. *
  486. * @since 3.4.0
  487. *
  488. * @param object $theme - A WordPress.org Theme API object.
  489. * @return string Theme status.
  490. */
  491. private function _get_theme_status( $theme ) {
  492. $status = 'install';
  493. $installed_theme = wp_get_theme( $theme->slug );
  494. if ( $installed_theme->exists() ) {
  495. if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
  496. $status = 'latest_installed';
  497. } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
  498. $status = 'newer_installed';
  499. } else {
  500. $status = 'update_available';
  501. }
  502. }
  503. return $status;
  504. }
  505. }