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

/wp-admin/includes/class-wp-themes-list-table.php

http://github.com/wordpress/wordpress
PHP | 361 lines | 236 code | 50 blank | 75 comment | 28 complexity | 6c6de73dbd16e338459282305f79928b MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. /**
  3. * List Table API: WP_Themes_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying installed themes in a list table.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. *
  15. * @see WP_List_Table
  16. */
  17. class WP_Themes_List_Table extends WP_List_Table {
  18. protected $search_terms = array();
  19. public $features = array();
  20. /**
  21. * Constructor.
  22. *
  23. * @since 3.1.0
  24. *
  25. * @see WP_List_Table::__construct() for more information on default arguments.
  26. *
  27. * @param array $args An associative array of arguments.
  28. */
  29. public function __construct( $args = array() ) {
  30. parent::__construct(
  31. array(
  32. 'ajax' => true,
  33. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  34. )
  35. );
  36. }
  37. /**
  38. * @return bool
  39. */
  40. public function ajax_user_can() {
  41. // Do not check edit_theme_options here. Ajax calls for available themes require switch_themes.
  42. return current_user_can( 'switch_themes' );
  43. }
  44. /**
  45. */
  46. public function prepare_items() {
  47. $themes = wp_get_themes( array( 'allowed' => true ) );
  48. if ( ! empty( $_REQUEST['s'] ) ) {
  49. $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
  50. }
  51. if ( ! empty( $_REQUEST['features'] ) ) {
  52. $this->features = $_REQUEST['features'];
  53. }
  54. if ( $this->search_terms || $this->features ) {
  55. foreach ( $themes as $key => $theme ) {
  56. if ( ! $this->search_theme( $theme ) ) {
  57. unset( $themes[ $key ] );
  58. }
  59. }
  60. }
  61. unset( $themes[ get_option( 'stylesheet' ) ] );
  62. WP_Theme::sort_by_name( $themes );
  63. $per_page = 36;
  64. $page = $this->get_pagenum();
  65. $start = ( $page - 1 ) * $per_page;
  66. $this->items = array_slice( $themes, $start, $per_page, true );
  67. $this->set_pagination_args(
  68. array(
  69. 'total_items' => count( $themes ),
  70. 'per_page' => $per_page,
  71. 'infinite_scroll' => true,
  72. )
  73. );
  74. }
  75. /**
  76. */
  77. public function no_items() {
  78. if ( $this->search_terms || $this->features ) {
  79. _e( 'No items found.' );
  80. return;
  81. }
  82. $blog_id = get_current_blog_id();
  83. if ( is_multisite() ) {
  84. if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
  85. printf(
  86. /* translators: 1: URL to Themes tab on Edit Site screen, 2: URL to Add Themes screen. */
  87. __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ),
  88. network_admin_url( 'site-themes.php?id=' . $blog_id ),
  89. network_admin_url( 'theme-install.php' )
  90. );
  91. return;
  92. } elseif ( current_user_can( 'manage_network_themes' ) ) {
  93. printf(
  94. /* translators: %s: URL to Themes tab on Edit Site screen. */
  95. __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%s">enable</a> more themes.' ),
  96. network_admin_url( 'site-themes.php?id=' . $blog_id )
  97. );
  98. return;
  99. }
  100. // Else, fallthrough. install_themes doesn't help if you can't enable it.
  101. } else {
  102. if ( current_user_can( 'install_themes' ) ) {
  103. printf(
  104. /* translators: %s: URL to Add Themes screen. */
  105. __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ),
  106. admin_url( 'theme-install.php' )
  107. );
  108. return;
  109. }
  110. }
  111. // Fallthrough.
  112. printf(
  113. /* translators: %s: Network title. */
  114. __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ),
  115. get_site_option( 'site_name' )
  116. );
  117. }
  118. /**
  119. * @param string $which
  120. */
  121. public function tablenav( $which = 'top' ) {
  122. if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) {
  123. return;
  124. }
  125. ?>
  126. <div class="tablenav themes <?php echo $which; ?>">
  127. <?php $this->pagination( $which ); ?>
  128. <span class="spinner"></span>
  129. <br class="clear" />
  130. </div>
  131. <?php
  132. }
  133. /**
  134. * Displays the themes table.
  135. *
  136. * Overrides the parent display() method to provide a different container.
  137. *
  138. * @since 3.1.0
  139. */
  140. public function display() {
  141. wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
  142. ?>
  143. <?php $this->tablenav( 'top' ); ?>
  144. <div id="availablethemes">
  145. <?php $this->display_rows_or_placeholder(); ?>
  146. </div>
  147. <?php $this->tablenav( 'bottom' ); ?>
  148. <?php
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function get_columns() {
  154. return array();
  155. }
  156. /**
  157. */
  158. public function display_rows_or_placeholder() {
  159. if ( $this->has_items() ) {
  160. $this->display_rows();
  161. } else {
  162. echo '<div class="no-items">';
  163. $this->no_items();
  164. echo '</div>';
  165. }
  166. }
  167. /**
  168. */
  169. public function display_rows() {
  170. $themes = $this->items;
  171. foreach ( $themes as $theme ) :
  172. ?>
  173. <div class="available-theme">
  174. <?php
  175. $template = $theme->get_template();
  176. $stylesheet = $theme->get_stylesheet();
  177. $title = $theme->display( 'Name' );
  178. $version = $theme->display( 'Version' );
  179. $author = $theme->display( 'Author' );
  180. $activate_link = wp_nonce_url( 'themes.php?action=activate&amp;template=' . urlencode( $template ) . '&amp;stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
  181. $actions = array();
  182. $actions['activate'] = sprintf(
  183. '<a href="%s" class="activatelink" title="%s">%s</a>',
  184. $activate_link,
  185. /* translators: %s: Theme name. */
  186. esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ),
  187. __( 'Activate' )
  188. );
  189. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  190. $actions['preview'] .= sprintf(
  191. '<a href="%s" class="load-customize hide-if-no-customize">%s</a>',
  192. wp_customize_url( $stylesheet ),
  193. __( 'Live Preview' )
  194. );
  195. }
  196. if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) {
  197. $actions['delete'] = sprintf(
  198. '<a class="submitdelete deletion" href="%s" onclick="return confirm( \'%s\' );">%s</a>',
  199. wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ),
  200. /* translators: %s: Theme name. */
  201. esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ),
  202. __( 'Delete' )
  203. );
  204. }
  205. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  206. $actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
  207. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  208. $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' );
  209. $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  210. unset( $actions['delete'] );
  211. $screenshot = $theme->get_screenshot();
  212. ?>
  213. <span class="screenshot hide-if-customize">
  214. <?php if ( $screenshot ) : ?>
  215. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  216. <?php endif; ?>
  217. </span>
  218. <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  219. <?php if ( $screenshot ) : ?>
  220. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  221. <?php endif; ?>
  222. </a>
  223. <h3><?php echo $title; ?></h3>
  224. <div class="theme-author">
  225. <?php
  226. /* translators: %s: Theme author. */
  227. printf( __( 'By %s' ), $author );
  228. ?>
  229. </div>
  230. <div class="action-links">
  231. <ul>
  232. <?php foreach ( $actions as $action ) : ?>
  233. <li><?php echo $action; ?></li>
  234. <?php endforeach; ?>
  235. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
  236. </ul>
  237. <?php echo $delete_action; ?>
  238. <?php theme_update_available( $theme ); ?>
  239. </div>
  240. <div class="themedetaildiv hide-if-js">
  241. <p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p>
  242. <p><?php echo $theme->display( 'Description' ); ?></p>
  243. <?php
  244. if ( $theme->parent() ) {
  245. printf(
  246. /* translators: 1: Link to documentation on child themes, 2: Name of parent theme. */
  247. ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
  248. __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
  249. $theme->parent()->display( 'Name' )
  250. );
  251. }
  252. ?>
  253. </div>
  254. </div>
  255. <?php
  256. endforeach;
  257. }
  258. /**
  259. * @param WP_Theme $theme
  260. * @return bool
  261. */
  262. public function search_theme( $theme ) {
  263. // Search the features.
  264. foreach ( $this->features as $word ) {
  265. if ( ! in_array( $word, $theme->get( 'Tags' ), true ) ) {
  266. return false;
  267. }
  268. }
  269. // Match all phrases.
  270. foreach ( $this->search_terms as $word ) {
  271. if ( in_array( $word, $theme->get( 'Tags' ), true ) ) {
  272. continue;
  273. }
  274. foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  275. // Don't mark up; Do translate.
  276. if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
  277. continue 2;
  278. }
  279. }
  280. if ( false !== stripos( $theme->get_stylesheet(), $word ) ) {
  281. continue;
  282. }
  283. if ( false !== stripos( $theme->get_template(), $word ) ) {
  284. continue;
  285. }
  286. return false;
  287. }
  288. return true;
  289. }
  290. /**
  291. * Send required variables to JavaScript land
  292. *
  293. * @since 3.4.0
  294. *
  295. * @param array $extra_args
  296. */
  297. public function _js_vars( $extra_args = array() ) {
  298. $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
  299. $args = array(
  300. 'search' => $search_string,
  301. 'features' => $this->features,
  302. 'paged' => $this->get_pagenum(),
  303. 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  304. );
  305. if ( is_array( $extra_args ) ) {
  306. $args = array_merge( $args, $extra_args );
  307. }
  308. printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
  309. parent::_js_vars();
  310. }
  311. }