PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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