PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/frogdog/noveosdx
PHP | 321 lines | 208 code | 50 blank | 63 comment | 28 complexity | e2e526da09b27269e6b5a162a32b75be MD5 | raw file
Possible License(s): GPL-2.0, MIT, GPL-3.0, 0BSD, BSD-3-Clause, Apache-2.0
  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( __( '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' ) );
  86. return;
  87. } elseif ( current_user_can( 'manage_network_themes' ) ) {
  88. 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 ) );
  89. return;
  90. }
  91. // Else, fallthrough. install_themes doesn't help if you can't enable it.
  92. } else {
  93. if ( current_user_can( 'install_themes' ) ) {
  94. 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' ) );
  95. return;
  96. }
  97. }
  98. // Fallthrough.
  99. printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
  100. }
  101. /**
  102. * @param string $which
  103. */
  104. public function tablenav( $which = 'top' ) {
  105. if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) {
  106. return;
  107. }
  108. ?>
  109. <div class="tablenav themes <?php echo $which; ?>">
  110. <?php $this->pagination( $which ); ?>
  111. <span class="spinner"></span>
  112. <br class="clear" />
  113. </div>
  114. <?php
  115. }
  116. /**
  117. */
  118. public function display() {
  119. wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
  120. ?>
  121. <?php $this->tablenav( 'top' ); ?>
  122. <div id="availablethemes">
  123. <?php $this->display_rows_or_placeholder(); ?>
  124. </div>
  125. <?php $this->tablenav( 'bottom' ); ?>
  126. <?php
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function get_columns() {
  132. return array();
  133. }
  134. /**
  135. */
  136. public function display_rows_or_placeholder() {
  137. if ( $this->has_items() ) {
  138. $this->display_rows();
  139. } else {
  140. echo '<div class="no-items">';
  141. $this->no_items();
  142. echo '</div>';
  143. }
  144. }
  145. /**
  146. */
  147. public function display_rows() {
  148. $themes = $this->items;
  149. foreach ( $themes as $theme ) :
  150. ?>
  151. <div class="available-theme">
  152. <?php
  153. $template = $theme->get_template();
  154. $stylesheet = $theme->get_stylesheet();
  155. $title = $theme->display( 'Name' );
  156. $version = $theme->display( 'Version' );
  157. $author = $theme->display( 'Author' );
  158. $activate_link = wp_nonce_url( 'themes.php?action=activate&amp;template=' . urlencode( $template ) . '&amp;stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
  159. $actions = array();
  160. $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
  161. . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
  162. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  163. $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
  164. . __( 'Live Preview' ) . '</a>';
  165. }
  166. if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) {
  167. $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
  168. . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
  169. . "' );" . '">' . __( 'Delete' ) . '</a>';
  170. }
  171. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  172. $actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
  173. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  174. $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, 'all' );
  175. $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  176. unset( $actions['delete'] );
  177. ?>
  178. <span class="screenshot hide-if-customize">
  179. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  180. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  181. <?php endif; ?>
  182. </span>
  183. <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  184. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  185. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  186. <?php endif; ?>
  187. </a>
  188. <h3><?php echo $title; ?></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. <?php echo $delete_action; ?>
  198. <?php theme_update_available( $theme ); ?>
  199. </div>
  200. <div class="themedetaildiv hide-if-js">
  201. <p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p>
  202. <p><?php echo $theme->display( 'Description' ); ?></p>
  203. <?php
  204. if ( $theme->parent() ) {
  205. printf(
  206. /* translators: %s: link to documentation on child themes */
  207. ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
  208. __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
  209. $theme->parent()->display( 'Name' )
  210. );
  211. }
  212. ?>
  213. </div>
  214. </div>
  215. <?php
  216. endforeach;
  217. }
  218. /**
  219. * @param WP_Theme $theme
  220. * @return bool
  221. */
  222. public function search_theme( $theme ) {
  223. // Search the features
  224. foreach ( $this->features as $word ) {
  225. if ( ! in_array( $word, $theme->get( 'Tags' ) ) ) {
  226. return false;
  227. }
  228. }
  229. // Match all phrases
  230. foreach ( $this->search_terms as $word ) {
  231. if ( in_array( $word, $theme->get( 'Tags' ) ) ) {
  232. continue;
  233. }
  234. foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  235. // Don't mark up; Do translate.
  236. if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
  237. continue 2;
  238. }
  239. }
  240. if ( false !== stripos( $theme->get_stylesheet(), $word ) ) {
  241. continue;
  242. }
  243. if ( false !== stripos( $theme->get_template(), $word ) ) {
  244. continue;
  245. }
  246. return false;
  247. }
  248. return true;
  249. }
  250. /**
  251. * Send required variables to JavaScript land
  252. *
  253. * @since 3.4.0
  254. *
  255. * @param array $extra_args
  256. */
  257. public function _js_vars( $extra_args = array() ) {
  258. $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
  259. $args = array(
  260. 'search' => $search_string,
  261. 'features' => $this->features,
  262. 'paged' => $this->get_pagenum(),
  263. 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  264. );
  265. if ( is_array( $extra_args ) ) {
  266. $args = array_merge( $args, $extra_args );
  267. }
  268. printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
  269. parent::_js_vars();
  270. }
  271. }