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