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

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

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