/wp-admin/includes/class-wp-theme-install-list-table.php

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