PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Quartermain/viettrungtravel_wordpress
PHP | 240 lines | 186 code | 35 blank | 19 comment | 39 complexity | 760a2269e6fdbb93427c253a12369485 MD5 | raw file
  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. var $search = array();
  12. var $features = array();
  13. function ajax_user_can() {
  14. // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
  15. return current_user_can('switch_themes');
  16. }
  17. function prepare_items() {
  18. global $ct;
  19. $ct = current_theme_info();
  20. $themes = get_allowed_themes();
  21. if ( ! empty( $_REQUEST['s'] ) ) {
  22. $search = strtolower( stripslashes( $_REQUEST['s'] ) );
  23. $this->search = array_merge( $this->search, array_filter( array_map( 'trim', explode( ',', $search ) ) ) );
  24. $this->search = array_unique( $this->search );
  25. }
  26. if ( !empty( $_REQUEST['features'] ) ) {
  27. $this->features = $_REQUEST['features'];
  28. $this->features = array_map( 'trim', $this->features );
  29. $this->features = array_map( 'sanitize_title_with_dashes', $this->features );
  30. $this->features = array_unique( $this->features );
  31. }
  32. if ( $this->search || $this->features ) {
  33. foreach ( $themes as $key => $theme ) {
  34. if ( !$this->search_theme( $theme ) )
  35. unset( $themes[ $key ] );
  36. }
  37. }
  38. unset( $themes[$ct->name] );
  39. uksort( $themes, "strnatcasecmp" );
  40. $per_page = 15;
  41. $page = $this->get_pagenum();
  42. $start = ( $page - 1 ) * $per_page;
  43. $this->items = array_slice( $themes, $start, $per_page );
  44. $this->set_pagination_args( array(
  45. 'total_items' => count( $themes ),
  46. 'per_page' => $per_page,
  47. ) );
  48. }
  49. function no_items() {
  50. if ( $this->search || $this->features ) {
  51. _e( 'No items found.' );
  52. return;
  53. }
  54. if ( is_multisite() ) {
  55. if ( current_user_can( 'install_themes' ) && 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> 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' ) );
  57. return;
  58. } elseif ( current_user_can( 'manage_network_themes' ) ) {
  59. 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'] ) );
  60. return;
  61. }
  62. // else, fallthrough. install_themes doesn't help if you can't enable it.
  63. } else {
  64. if ( current_user_can( 'install_themes' ) ) {
  65. 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' ) );
  66. return;
  67. }
  68. }
  69. // Fallthrough.
  70. printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
  71. }
  72. function tablenav( $which = 'top' ) {
  73. if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
  74. return;
  75. ?>
  76. <div class="tablenav <?php echo $which; ?>">
  77. <?php $this->pagination( $which ); ?>
  78. <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading list-ajax-loading" alt="" />
  79. <br class="clear" />
  80. </div>
  81. <?php
  82. }
  83. function display() {
  84. // wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
  85. ?>
  86. <?php $this->tablenav( 'top' ); ?>
  87. <table id="availablethemes" cellspacing="0" cellpadding="0">
  88. <tbody id="the-list" class="list:themes">
  89. <?php $this->display_rows_or_placeholder(); ?>
  90. </tbody>
  91. </table>
  92. <?php $this->tablenav( 'bottom' ); ?>
  93. <?php
  94. }
  95. function get_columns() {
  96. return array();
  97. }
  98. function display_rows() {
  99. $themes = $this->items;
  100. $theme_names = array_keys( $themes );
  101. natcasesort( $theme_names );
  102. $table = array();
  103. $rows = ceil( count( $theme_names ) / 3 );
  104. for ( $row = 1; $row <= $rows; $row++ )
  105. for ( $col = 1; $col <= 3; $col++ )
  106. $table[$row][$col] = array_shift( $theme_names );
  107. foreach ( $table as $row => $cols ) {
  108. ?>
  109. <tr>
  110. <?php
  111. foreach ( $cols as $col => $theme_name ) {
  112. $class = array( 'available-theme' );
  113. if ( $row == 1 ) $class[] = 'top';
  114. if ( $col == 1 ) $class[] = 'left';
  115. if ( $row == $rows ) $class[] = 'bottom';
  116. if ( $col == 3 ) $class[] = 'right';
  117. ?>
  118. <td class="<?php echo join( ' ', $class ); ?>">
  119. <?php if ( !empty( $theme_name ) ) :
  120. $template = $themes[$theme_name]['Template'];
  121. $stylesheet = $themes[$theme_name]['Stylesheet'];
  122. $title = $themes[$theme_name]['Title'];
  123. $version = $themes[$theme_name]['Version'];
  124. $description = $themes[$theme_name]['Description'];
  125. $author = $themes[$theme_name]['Author'];
  126. $screenshot = $themes[$theme_name]['Screenshot'];
  127. $stylesheet_dir = $themes[$theme_name]['Stylesheet Dir'];
  128. $template_dir = $themes[$theme_name]['Template Dir'];
  129. $parent_theme = $themes[$theme_name]['Parent Theme'];
  130. $theme_root = $themes[$theme_name]['Theme Root'];
  131. $theme_root_uri = $themes[$theme_name]['Theme Root URI'];
  132. $preview_link = esc_url( get_option( 'home' ) . '/' );
  133. if ( is_ssl() )
  134. $preview_link = str_replace( 'http://', 'https://', $preview_link );
  135. $preview_link = htmlspecialchars( add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ), $preview_link ) );
  136. $preview_text = esc_attr( sprintf( __( 'Preview of &#8220;%s&#8221;' ), $title ) );
  137. $tags = $themes[$theme_name]['Tags'];
  138. $thickbox_class = 'thickbox thickbox-preview';
  139. $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=".urlencode( $template )."&amp;stylesheet=".urlencode( $stylesheet ), 'switch-theme_' . $template );
  140. $activate_text = esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) );
  141. $actions = array();
  142. $actions[] = '<a href="' . $activate_link . '" class="activatelink" title="' . $activate_text . '">' . __( 'Activate' ) . '</a>';
  143. $actions[] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $theme_name ) ) . '">' . __( 'Preview' ) . '</a>';
  144. if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
  145. $actions[] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;template=$stylesheet", 'delete-theme_' . $stylesheet ) . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $theme_name ) ) . "' );" . '">' . __( 'Delete' ) . '</a>';
  146. $actions = apply_filters( 'theme_action_links', $actions, $themes[$theme_name] );
  147. $actions = implode ( ' | ', $actions );
  148. ?>
  149. <a href="<?php echo $preview_link; ?>" class="<?php echo $thickbox_class; ?> screenshot">
  150. <?php if ( $screenshot ) : ?>
  151. <img src="<?php echo $theme_root_uri . '/' . $stylesheet . '/' . $screenshot; ?>" alt="" />
  152. <?php endif; ?>
  153. </a>
  154. <h3><?php
  155. /* translators: 1: theme title, 2: theme version, 3: theme author */
  156. printf( __( '%1$s %2$s by %3$s' ), $title, $version, $author ) ; ?></h3>
  157. <p class="description"><?php echo $description; ?></p>
  158. <span class='action-links'><?php echo $actions ?></span>
  159. <?php if ( current_user_can( 'edit_themes' ) && $parent_theme ) {
  160. /* translators: 1: theme title, 2: template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
  161. <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.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ), $title, $parent_theme ); ?></p>
  162. <?php } else { ?>
  163. <p><?php printf( __( 'All of this theme&#8217;s files are located in <code>%2$s</code>.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ) ); ?></p>
  164. <?php } ?>
  165. <?php if ( $tags ) : ?>
  166. <p><?php _e( 'Tags:' ); ?> <?php echo join( ', ', $tags ); ?></p>
  167. <?php endif; ?>
  168. <?php theme_update_available( $themes[$theme_name] ); ?>
  169. <?php endif; // end if not empty theme_name ?>
  170. </td>
  171. <?php } // end foreach $cols ?>
  172. </tr>
  173. <?php } // end foreach $table
  174. }
  175. function search_theme( $theme ) {
  176. $matched = 0;
  177. // Match all phrases
  178. if ( count( $this->search ) > 0 ) {
  179. foreach ( $this->search as $word ) {
  180. $matched = 0;
  181. // In a tag?
  182. if ( in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
  183. $matched = 1;
  184. // In one of the fields?
  185. foreach ( array( 'Name', 'Title', 'Description', 'Author', 'Template', 'Stylesheet' ) AS $field ) {
  186. if ( stripos( $theme[$field], $word ) !== false )
  187. $matched++;
  188. }
  189. if ( $matched == 0 )
  190. return false;
  191. }
  192. }
  193. // Now search the features
  194. if ( count( $this->features ) > 0 ) {
  195. foreach ( $this->features as $word ) {
  196. // In a tag?
  197. if ( !in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
  198. return false;
  199. }
  200. }
  201. // Only get here if each word exists in the tags or one of the fields
  202. return true;
  203. }
  204. }
  205. ?>