PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/dkrzos/phc
PHP | 394 lines | 270 code | 61 blank | 63 comment | 20 complexity | a4845cc1c2d0b06821b4753ef3800204 MD5 | raw file
Possible License(s): GPL-2.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. var $features = array();
  12. function ajax_user_can() {
  13. return current_user_can( 'install_themes' );
  14. }
  15. 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( stripslashes( $_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','Theme Installer' );
  36. //$tabs['popular'] = _x( 'Popular','Theme Installer' );
  37. $tabs['new'] = _x( 'Newest','Theme Installer' );
  38. $tabs['updated'] = _x( 'Recently Updated','Theme Installer' );
  39. $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
  40. $tabs = apply_filters( 'install_themes_tabs', $tabs );
  41. $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
  42. // If a non-valid menu tab has been selected, And its not a non-menu action.
  43. if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
  44. $tab = key( $tabs );
  45. $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
  46. switch ( $tab ) {
  47. case 'search':
  48. $type = isset( $_REQUEST['type'] ) ? stripslashes( $_REQUEST['type'] ) : 'term';
  49. switch ( $type ) {
  50. case 'tag':
  51. $args['tag'] = array_map( 'sanitize_key', $search_terms );
  52. break;
  53. case 'term':
  54. $args['search'] = $search_string;
  55. break;
  56. case 'author':
  57. $args['author'] = $search_string;
  58. break;
  59. }
  60. if ( ! empty( $this->features ) ) {
  61. $args['tag'] = $this->features;
  62. $_REQUEST['s'] = implode( ',', $this->features );
  63. $_REQUEST['type'] = 'tag';
  64. }
  65. add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
  66. break;
  67. case 'featured':
  68. //case 'popular':
  69. case 'new':
  70. case 'updated':
  71. $args['browse'] = $tab;
  72. break;
  73. default:
  74. $args = false;
  75. }
  76. if ( ! $args )
  77. return;
  78. $api = themes_api( 'query_themes', $args );
  79. if ( is_wp_error( $api ) )
  80. wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
  81. $this->items = $api->themes;
  82. $this->set_pagination_args( array(
  83. 'total_items' => $api->info['results'],
  84. 'per_page' => $per_page,
  85. 'infinite_scroll' => true,
  86. ) );
  87. }
  88. function no_items() {
  89. _e( 'No themes match your request.' );
  90. }
  91. function get_views() {
  92. global $tabs, $tab;
  93. $display_tabs = array();
  94. foreach ( (array) $tabs as $action => $text ) {
  95. $class = ( $action == $tab ) ? ' class="current"' : '';
  96. $href = self_admin_url('theme-install.php?tab=' . $action);
  97. $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
  98. }
  99. return $display_tabs;
  100. }
  101. function display() {
  102. wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
  103. ?>
  104. <div class="tablenav top themes">
  105. <div class="alignleft actions">
  106. <?php do_action( 'install_themes_table_header' ); ?>
  107. </div>
  108. <?php $this->pagination( 'top' ); ?>
  109. <br class="clear" />
  110. </div>
  111. <div id="availablethemes">
  112. <?php $this->display_rows_or_placeholder(); ?>
  113. </div>
  114. <?php
  115. parent::tablenav( 'bottom' );
  116. }
  117. function display_rows() {
  118. $themes = $this->items;
  119. foreach ( $themes as $theme ) {
  120. ?>
  121. <div class="available-theme installable-theme"><?php
  122. $this->single_row( $theme );
  123. ?></div>
  124. <?php } // end foreach $theme_names
  125. $this->theme_installer();
  126. }
  127. /*
  128. * Prints a theme from the WordPress.org API.
  129. *
  130. * @param object $theme An object that contains theme data returned by the WordPress.org API.
  131. *
  132. * Example theme data:
  133. * object(stdClass)[59]
  134. * public 'name' => string 'Magazine Basic' (length=14)
  135. * public 'slug' => string 'magazine-basic' (length=14)
  136. * public 'version' => string '1.1' (length=3)
  137. * public 'author' => string 'tinkerpriest' (length=12)
  138. * public 'preview_url' => string 'http://wp-themes.com/?magazine-basic' (length=36)
  139. * public 'screenshot_url' => string 'http://wp-themes.com/wp-content/themes/magazine-basic/screenshot.png' (length=68)
  140. * public 'rating' => float 80
  141. * public 'num_ratings' => int 1
  142. * public 'homepage' => string 'http://wordpress.org/extend/themes/magazine-basic' (length=49)
  143. * 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>.' (length=214)
  144. * public 'download_link' => string 'http://wordpress.org/extend/themes/download/magazine-basic.1.1.zip' (length=66)
  145. */
  146. function single_row( $theme ) {
  147. global $themes_allowedtags;
  148. if ( empty( $theme ) )
  149. return;
  150. $name = wp_kses( $theme->name, $themes_allowedtags );
  151. $author = wp_kses( $theme->author, $themes_allowedtags );
  152. $preview_title = sprintf( __('Preview &#8220;%s&#8221;'), $name );
  153. $preview_url = add_query_arg( array(
  154. 'tab' => 'theme-information',
  155. 'theme' => $theme->slug,
  156. ) );
  157. $actions = array();
  158. $install_url = add_query_arg( array(
  159. 'action' => 'install-theme',
  160. 'theme' => $theme->slug,
  161. ), self_admin_url( 'update.php' ) );
  162. $update_url = add_query_arg( array(
  163. 'action' => 'upgrade-theme',
  164. 'theme' => $theme->slug,
  165. ), self_admin_url( 'update.php' ) );
  166. $status = $this->_get_theme_status( $theme );
  167. switch ( $status ) {
  168. default:
  169. case 'install':
  170. $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>';
  171. break;
  172. case 'update_available':
  173. $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>';
  174. break;
  175. case 'newer_installed':
  176. case 'latest_installed':
  177. $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
  178. break;
  179. }
  180. $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
  181. $actions = apply_filters( 'theme_install_actions', $actions, $theme );
  182. ?>
  183. <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
  184. <img src='<?php echo esc_url( $theme->screenshot_url ); ?>' width='150' />
  185. </a>
  186. <h3><?php echo $name; ?></h3>
  187. <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
  188. <div class="action-links">
  189. <ul>
  190. <?php foreach ( $actions as $action ): ?>
  191. <li><?php echo $action; ?></li>
  192. <?php endforeach; ?>
  193. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
  194. </ul>
  195. </div>
  196. <?php
  197. $this->install_theme_info( $theme );
  198. }
  199. /*
  200. * Prints the wrapper for the theme installer.
  201. */
  202. function theme_installer() {
  203. ?>
  204. <div id="theme-installer" class="wp-full-overlay expanded">
  205. <div class="wp-full-overlay-sidebar">
  206. <div class="wp-full-overlay-header">
  207. <a href="#" class="close-full-overlay"><?php _e( '&larr; Close' ); ?></a>
  208. </div>
  209. <div class="wp-full-overlay-sidebar-content">
  210. <div class="install-theme-info"></div>
  211. </div>
  212. <div class="wp-full-overlay-footer">
  213. <a href="#" class="collapse-sidebar button-secondary" title="<?php esc_attr_e('Collapse Sidebar'); ?>">
  214. <span class="collapse-sidebar-label"><?php _e('Collapse'); ?></span>
  215. <span class="collapse-sidebar-arrow"></span>
  216. </a>
  217. </div>
  218. </div>
  219. <div class="wp-full-overlay-main"></div>
  220. </div>
  221. <?php
  222. }
  223. /*
  224. * Prints the wrapper for the theme installer with a provided theme's data.
  225. * Used to make the theme installer work for no-js.
  226. *
  227. * @param object $theme - A WordPress.org Theme API object.
  228. */
  229. function theme_installer_single( $theme ) {
  230. ?>
  231. <div id="theme-installer" class="wp-full-overlay single-theme">
  232. <div class="wp-full-overlay-sidebar">
  233. <?php $this->install_theme_info( $theme ); ?>
  234. </div>
  235. <div class="wp-full-overlay-main">
  236. <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
  237. </div>
  238. </div>
  239. <?php
  240. }
  241. /*
  242. * Prints the info for a theme (to be used in the theme installer modal).
  243. *
  244. * @param object $theme - A WordPress.org Theme API object.
  245. */
  246. function install_theme_info( $theme ) {
  247. global $themes_allowedtags;
  248. if ( empty( $theme ) )
  249. return;
  250. $name = wp_kses( $theme->name, $themes_allowedtags );
  251. $author = wp_kses( $theme->author, $themes_allowedtags );
  252. $num_ratings = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
  253. $install_url = add_query_arg( array(
  254. 'action' => 'install-theme',
  255. 'theme' => $theme->slug,
  256. ), self_admin_url( 'update.php' ) );
  257. $update_url = add_query_arg( array(
  258. 'action' => 'upgrade-theme',
  259. 'theme' => $theme->slug,
  260. ), self_admin_url( 'update.php' ) );
  261. $status = $this->_get_theme_status( $theme );
  262. ?>
  263. <div class="install-theme-info"><?php
  264. switch ( $status ) {
  265. default:
  266. case 'install':
  267. echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
  268. break;
  269. case 'update_available':
  270. 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>';
  271. break;
  272. case 'newer_installed':
  273. case 'latest_installed':
  274. echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
  275. break;
  276. } ?>
  277. <h3 class="theme-name"><?php echo $name; ?></h3>
  278. <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
  279. <?php if ( isset( $theme->screenshot_url ) ): ?>
  280. <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" />
  281. <?php endif; ?>
  282. <div class="theme-details">
  283. <div class="star-holder" title="<?php echo esc_attr( $num_ratings ); ?>">
  284. <div class="star-rating" style="width:<?php echo esc_attr( intval( $theme->rating ) . 'px' ); ?>;"></div>
  285. </div>
  286. <div class="theme-version">
  287. <strong><?php _e('Version:') ?> </strong>
  288. <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
  289. </div>
  290. <div class="theme-description">
  291. <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
  292. </div>
  293. </div>
  294. <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
  295. </div>
  296. <?php
  297. }
  298. /**
  299. * Send required variables to JavaScript land
  300. *
  301. * @since 3.4
  302. * @access private
  303. *
  304. * @uses $tab Global; current tab within Themes->Install screen
  305. * @uses $type Global; type of search.
  306. */
  307. function _js_vars() {
  308. global $tab, $type;
  309. parent::_js_vars( compact( 'tab', 'type' ) );
  310. }
  311. /**
  312. * Check to see if the theme is already installed.
  313. *
  314. * @since 3.4
  315. * @access private
  316. *
  317. * @param object $theme - A WordPress.org Theme API object.
  318. * @return string Theme status.
  319. */
  320. private function _get_theme_status( $theme ) {
  321. $status = 'install';
  322. $installed_theme = wp_get_theme( $theme->slug );
  323. if ( $installed_theme->exists() ) {
  324. if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
  325. $status = 'latest_installed';
  326. elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
  327. $status = 'newer_installed';
  328. else
  329. $status = 'update_available';
  330. }
  331. return $status;
  332. }
  333. }