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

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

https://github.com/shaiquddin/WordPress
PHP | 253 lines | 180 code | 49 blank | 24 comment | 25 complexity | 4cce743f656b078063b646274add81e8 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Themes List Table class.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. * @access private
  9. */
  10. class WP_Themes_List_Table extends WP_List_Table {
  11. protected $search_terms = array();
  12. var $features = array();
  13. function __construct( $args = array() ) {
  14. parent::__construct( array(
  15. 'ajax' => true,
  16. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  17. ) );
  18. }
  19. function ajax_user_can() {
  20. // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
  21. return current_user_can( 'switch_themes' );
  22. }
  23. function prepare_items() {
  24. $themes = wp_get_themes( array( 'allowed' => true ) );
  25. if ( ! empty( $_REQUEST['s'] ) )
  26. $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( stripslashes( $_REQUEST['s'] ) ) ) ) ) );
  27. if ( ! empty( $_REQUEST['features'] ) )
  28. $this->features = $_REQUEST['features'];
  29. if ( $this->search_terms || $this->features ) {
  30. foreach ( $themes as $key => $theme ) {
  31. if ( ! $this->search_theme( $theme ) )
  32. unset( $themes[ $key ] );
  33. }
  34. }
  35. unset( $themes[ get_option( 'stylesheet' ) ] );
  36. WP_Theme::sort_by_name( $themes );
  37. $per_page = 36;
  38. $page = $this->get_pagenum();
  39. $start = ( $page - 1 ) * $per_page;
  40. $this->items = array_slice( $themes, $start, $per_page, true );
  41. $this->set_pagination_args( array(
  42. 'total_items' => count( $themes ),
  43. 'per_page' => $per_page,
  44. 'infinite_scroll' => true,
  45. ) );
  46. }
  47. function no_items() {
  48. if ( $this->search_terms || $this->features ) {
  49. _e( 'No items found.' );
  50. return;
  51. }
  52. if ( is_multisite() ) {
  53. if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
  54. 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' ) );
  55. return;
  56. } elseif ( current_user_can( 'manage_network_themes' ) ) {
  57. 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'] ) );
  58. return;
  59. }
  60. // else, fallthrough. install_themes doesn't help if you can't enable it.
  61. } else {
  62. if ( current_user_can( 'install_themes' ) ) {
  63. 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' ) );
  64. return;
  65. }
  66. }
  67. // Fallthrough.
  68. printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
  69. }
  70. function tablenav( $which = 'top' ) {
  71. if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
  72. return;
  73. ?>
  74. <div class="tablenav themes <?php echo $which; ?>">
  75. <?php $this->pagination( $which ); ?>
  76. <span class="ajax-loading list-ajax-loading spinner"></span>
  77. <br class="clear" />
  78. </div>
  79. <?php
  80. }
  81. function display() {
  82. wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
  83. ?>
  84. <?php $this->tablenav( 'top' ); ?>
  85. <div id="availablethemes">
  86. <?php $this->display_rows_or_placeholder(); ?>
  87. </div>
  88. <?php $this->tablenav( 'bottom' ); ?>
  89. <?php
  90. }
  91. function get_columns() {
  92. return array();
  93. }
  94. function display_rows() {
  95. $themes = $this->items;
  96. foreach ( $themes as $theme ):
  97. ?><div class="available-theme"><?php
  98. $template = $theme->get_template();
  99. $stylesheet = $theme->get_stylesheet();
  100. $title = $theme->display('Name');
  101. $version = $theme->display('Version');
  102. $author = $theme->display('Author');
  103. $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
  104. $preview_link = esc_url( add_query_arg(
  105. array( 'preview' => 1, 'template' => urlencode( $template ), 'stylesheet' => urlencode( $stylesheet ), 'preview_iframe' => true, 'TB_iframe' => 'true' ),
  106. home_url( '/' ) ) );
  107. $actions = array();
  108. $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
  109. . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
  110. $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
  111. . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
  112. if ( current_user_can( 'edit_theme_options' ) )
  113. $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
  114. . __( 'Live Preview' ) . '</a>';
  115. if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
  116. $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
  117. . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
  118. . "' );" . '">' . __( 'Delete' ) . '</a>';
  119. $actions = apply_filters( 'theme_action_links', $actions, $theme );
  120. $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  121. unset( $actions['delete'] );
  122. ?>
  123. <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
  124. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  125. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  126. <?php endif; ?>
  127. </a>
  128. <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  129. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  130. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  131. <?php endif; ?>
  132. </a>
  133. <h3><?php echo $title; ?></h3>
  134. <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
  135. <div class="action-links">
  136. <ul>
  137. <?php foreach ( $actions as $action ): ?>
  138. <li><?php echo $action; ?></li>
  139. <?php endforeach; ?>
  140. <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
  141. </ul>
  142. <?php echo $delete_action; ?>
  143. <?php theme_update_available( $theme ); ?>
  144. </div>
  145. <div class="themedetaildiv hide-if-js">
  146. <p><strong><?php _e('Version: '); ?></strong><?php echo $version; ?></p>
  147. <p><?php echo $theme->display('Description'); ?></p>
  148. <?php if ( $theme->parent() ) {
  149. printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
  150. __( 'http://codex.wordpress.org/Child_Themes' ),
  151. $theme->parent()->display( 'Name' ) );
  152. } ?>
  153. </div>
  154. </div>
  155. <?php
  156. endforeach;
  157. }
  158. function search_theme( $theme ) {
  159. // Search the features
  160. foreach ( $this->features as $word ) {
  161. if ( ! in_array( $word, $theme->get('Tags') ) )
  162. return false;
  163. }
  164. // Match all phrases
  165. foreach ( $this->search_terms as $word ) {
  166. if ( in_array( $word, $theme->get('Tags') ) )
  167. continue;
  168. foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  169. // Don't mark up; Do translate.
  170. if ( false !== stripos( $theme->display( $header, false, true ), $word ) )
  171. continue 2;
  172. }
  173. if ( false !== stripos( $theme->get_stylesheet(), $word ) )
  174. continue;
  175. if ( false !== stripos( $theme->get_template(), $word ) )
  176. continue;
  177. return false;
  178. }
  179. return true;
  180. }
  181. /**
  182. * Send required variables to JavaScript land
  183. *
  184. * @since 3.4
  185. * @access private
  186. *
  187. * @uses $this->features Array of all feature search terms.
  188. * @uses get_pagenum()
  189. * @uses _pagination_args['total_pages']
  190. */
  191. function _js_vars( $extra_args = array() ) {
  192. $search_string = isset( $_REQUEST['s'] ) ? esc_attr( stripslashes( $_REQUEST['s'] ) ) : '';
  193. $args = array(
  194. 'search' => $search_string,
  195. 'features' => $this->features,
  196. 'paged' => $this->get_pagenum(),
  197. 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  198. );
  199. if ( is_array( $extra_args ) )
  200. $args = array_merge( $args, $extra_args );
  201. printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", json_encode( $args ) );
  202. parent::_js_vars();
  203. }
  204. }