PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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