PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 256 lines | 183 code | 49 blank | 24 comment | 27 complexity | e2e9eefaa537befbac3567a8aaaf6def MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.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() {
  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. ?><div class="available-theme"><?php
  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_' . $stylesheet );
  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['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
  108. . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
  109. $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
  110. . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
  111. if ( current_user_can( 'edit_theme_options' ) )
  112. $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
  113. . __( 'Live Preview' ) . '</a>';
  114. if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
  115. $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;stylesheet=$stylesheet", 'delete-theme_' . $stylesheet )
  116. . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
  117. . "' );" . '">' . __( 'Delete' ) . '</a>';
  118. $actions = apply_filters( 'theme_action_links', $actions, $theme );
  119. $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  120. unset( $actions['delete'] );
  121. ?>
  122. <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
  123. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  124. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  125. <?php endif; ?>
  126. </a>
  127. <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  128. <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  129. <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  130. <?php endif; ?>
  131. </a>
  132. <h3><?php echo $title; ?></h3>
  133. <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
  134. <div class="action-links">
  135. <ul>
  136. <?php foreach ( $actions as $action ): ?>
  137. <li><?php echo $action; ?></li>
  138. <?php endforeach; ?>
  139. <li class="hide-if-no-js"><a href="#" class="theme-detail" tabindex='4'><?php _e('Details') ?></a></li>
  140. </ul>
  141. <?php echo $delete_action; ?>
  142. <?php theme_update_available( $theme ); ?>
  143. </div>
  144. <div class="themedetaildiv hide-if-js">
  145. <p><strong><?php _e('Version: '); ?></strong><?php echo $version; ?></p>
  146. <p><?php echo $theme->display('Description'); ?></p>
  147. <?php if ( current_user_can( 'edit_themes' ) && $theme->parent() ) :
  148. /* translators: 1: theme title, 2: template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
  149. <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.' ),
  150. $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>
  151. <?php else :
  152. /* translators: 1: theme title, 2: template dir, 3: stylesheet_dir */ ?>
  153. <p><?php printf( __( 'All of this theme&#8217;s files are located in <code>%2$s</code>.' ),
  154. $title, str_replace( WP_CONTENT_DIR, '', $theme->get_template_directory() ), str_replace( WP_CONTENT_DIR, '', $theme->get_stylesheet_directory() ) ); ?></p>
  155. <?php endif; ?>
  156. </div>
  157. </div>
  158. <?php
  159. endforeach;
  160. }
  161. function search_theme( $theme ) {
  162. // Search the features
  163. foreach ( $this->features as $word ) {
  164. if ( ! in_array( $word, $theme->get('Tags') ) )
  165. return false;
  166. }
  167. // Match all phrases
  168. foreach ( $this->search_terms as $word ) {
  169. if ( in_array( $word, $theme->get('Tags') ) )
  170. continue;
  171. foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  172. // Don't mark up; Do translate.
  173. if ( false !== stripos( $theme->display( $header, false, true ), $word ) )
  174. continue 2;
  175. }
  176. if ( false !== stripos( $theme->get_stylesheet(), $word ) )
  177. continue;
  178. if ( false !== stripos( $theme->get_template(), $word ) )
  179. continue;
  180. return false;
  181. }
  182. return true;
  183. }
  184. /**
  185. * Send required variables to JavaScript land
  186. *
  187. * @since 3.4
  188. * @access private
  189. *
  190. * @uses $this->features Array of all feature search terms.
  191. * @uses get_pagenum()
  192. * @uses _pagination_args['total_pages']
  193. */
  194. function _js_vars( $extra_args = array() ) {
  195. $search_string = isset( $_REQUEST['s'] ) ? esc_attr( stripslashes( $_REQUEST['s'] ) ) : '';
  196. $args = array(
  197. 'search' => $search_string,
  198. 'features' => $this->features,
  199. 'paged' => $this->get_pagenum(),
  200. 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  201. );
  202. if ( is_array( $extra_args ) )
  203. $args = array_merge( $args, $extra_args );
  204. printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", json_encode( $args ) );
  205. parent::_js_vars();
  206. }
  207. }