PageRenderTime 53ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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