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