WP_Themes_List_Table

Core class used to implement displaying installed themes in a list table.

Defined (1)

The class is defined in the following location(s).

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