PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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