PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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