PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/webkod3r/tripolis
PHP | 308 lines | 187 code | 50 blank | 71 comment | 28 complexity | 5e016d657d412a00ef26df0176b5d261 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. if ( is_multisite() ) {
  80. if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
  81. 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' ) );
  82. return;
  83. } elseif ( current_user_can( 'manage_network_themes' ) ) {
  84. 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'] ) );
  85. return;
  86. }
  87. // Else, fallthrough. install_themes doesn't help if you can't enable it.
  88. } else {
  89. if ( current_user_can( 'install_themes' ) ) {
  90. printf( __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress.org Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), admin_url( 'theme-install.php' ) );
  91. return;
  92. }
  93. }
  94. // Fallthrough.
  95. printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
  96. }
  97. /**
  98. * @param string $which
  99. */
  100. public function tablenav( $which = 'top' ) {
  101. if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
  102. return;
  103. ?>
  104. <div class="tablenav themes <?php echo $which; ?>">
  105. <?php $this->pagination( $which ); ?>
  106. <span class="spinner"></span>
  107. <br class="clear" />
  108. </div>
  109. <?php
  110. }
  111. /**
  112. * @access public
  113. */
  114. public function display() {
  115. wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
  116. ?>
  117. <?php $this->tablenav( 'top' ); ?>
  118. <div id="availablethemes">
  119. <?php $this->display_rows_or_placeholder(); ?>
  120. </div>
  121. <?php $this->tablenav( 'bottom' ); ?>
  122. <?php
  123. }
  124. /**
  125. *
  126. * @return array
  127. */
  128. public function get_columns() {
  129. return array();
  130. }
  131. /**
  132. * @access public
  133. */
  134. public function display_rows_or_placeholder() {
  135. if ( $this->has_items() ) {
  136. $this->display_rows();
  137. } else {
  138. echo '<div class="no-items">';
  139. $this->no_items();
  140. echo '</div>';
  141. }
  142. }
  143. /**
  144. * @access public
  145. */
  146. public function display_rows() {
  147. $themes = $this->items;
  148. foreach ( $themes as $theme ):
  149. ?><div class="available-theme"><?php
  150. $template = $theme->get_template();
  151. $stylesheet = $theme->get_stylesheet();
  152. $title = $theme->display('Name');
  153. $version = $theme->display('Version');
  154. $author = $theme->display('Author');
  155. $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
  156. $actions = array();
  157. $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
  158. . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
  159. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  160. $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
  161. . __( 'Live Preview' ) . '</a>';
  162. }
  163. if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
  164. $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
  165. . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
  166. . "' );" . '">' . __( 'Delete' ) . '</a>';
  167. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  168. $actions = apply_filters( 'theme_action_links', $actions, $theme );
  169. /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  170. $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
  171. $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  172. unset( $actions['delete'] );
  173. ?>
  174. <span class="screenshot hide-if-customize">
  175. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  176. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  177. <?php endif; ?>
  178. </span>
  179. <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  180. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  181. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  182. <?php endif; ?>
  183. </a>
  184. <h3><?php echo $title; ?></h3>
  185. <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
  186. <div class="action-links">
  187. <ul>
  188. <?php foreach ( $actions as $action ): ?>
  189. <li><?php echo $action; ?></li>
  190. <?php endforeach; ?>
  191. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
  192. </ul>
  193. <?php echo $delete_action; ?>
  194. <?php theme_update_available( $theme ); ?>
  195. </div>
  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. </div>
  206. <?php
  207. endforeach;
  208. }
  209. /**
  210. * @param WP_Theme $theme
  211. * @return bool
  212. */
  213. public function search_theme( $theme ) {
  214. // Search the features
  215. foreach ( $this->features as $word ) {
  216. if ( ! in_array( $word, $theme->get('Tags') ) )
  217. return false;
  218. }
  219. // Match all phrases
  220. foreach ( $this->search_terms as $word ) {
  221. if ( in_array( $word, $theme->get('Tags') ) )
  222. continue;
  223. foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  224. // Don't mark up; Do translate.
  225. if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
  226. continue 2;
  227. }
  228. }
  229. if ( false !== stripos( $theme->get_stylesheet(), $word ) )
  230. continue;
  231. if ( false !== stripos( $theme->get_template(), $word ) )
  232. continue;
  233. return false;
  234. }
  235. return true;
  236. }
  237. /**
  238. * Send required variables to JavaScript land
  239. *
  240. * @since 3.4.0
  241. * @access public
  242. *
  243. * @param array $extra_args
  244. */
  245. public function _js_vars( $extra_args = array() ) {
  246. $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
  247. $args = array(
  248. 'search' => $search_string,
  249. 'features' => $this->features,
  250. 'paged' => $this->get_pagenum(),
  251. 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  252. );
  253. if ( is_array( $extra_args ) )
  254. $args = array_merge( $args, $extra_args );
  255. printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
  256. parent::_js_vars();
  257. }
  258. }