WordPress 3.9
[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( wp_unslash( $_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_or_placeholder() {
118                 if ( $this->has_items() ) {
119                         $this->display_rows();
120                 } else {
121                         echo '<div class="no-items">';
122                         $this->no_items();
123                         echo '</div>';
124                 }
125         }
126
127         function display_rows() {
128                 $themes = $this->items;
129
130                 foreach ( $themes as $theme ):
131                         ?><div class="available-theme"><?php
132
133                         $template   = $theme->get_template();
134                         $stylesheet = $theme->get_stylesheet();
135                         $title      = $theme->display('Name');
136                         $version    = $theme->display('Version');
137                         $author     = $theme->display('Author');
138
139                         $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
140
141                         $preview_link = esc_url( add_query_arg(
142                                 array( 'preview' => 1, 'template' => urlencode( $template ), 'stylesheet' => urlencode( $stylesheet ), 'preview_iframe' => true, 'TB_iframe' => 'true' ),
143                                 home_url( '/' ) ) );
144
145                         $actions = array();
146                         $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
147                                 . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
148
149                         $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
150                                 . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
151
152                         if ( current_user_can( 'edit_theme_options' ) )
153                                 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
154                                         . __( 'Live Preview' ) . '</a>';
155
156                         if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
157                                 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
158                                         . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) )
159                                         . "' );" . '">' . __( 'Delete' ) . '</a>';
160
161                         /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
162                         $actions       = apply_filters( 'theme_action_links', $actions, $theme );
163
164                         /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
165                         $actions       = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
166                         $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
167                         unset( $actions['delete'] );
168
169                         ?>
170
171                         <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
172                                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
173                                         <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
174                                 <?php endif; ?>
175                         </a>
176                         <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
177                                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
178                                         <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
179                                 <?php endif; ?>
180                         </a>
181
182                         <h3><?php echo $title; ?></h3>
183                         <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
184                         <div class="action-links">
185                                 <ul>
186                                         <?php foreach ( $actions as $action ): ?>
187                                                 <li><?php echo $action; ?></li>
188                                         <?php endforeach; ?>
189                                         <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
190                                 </ul>
191                                 <?php echo $delete_action; ?>
192
193                                 <?php theme_update_available( $theme ); ?>
194                         </div>
195
196                         <div class="themedetaildiv hide-if-js">
197                                 <p><strong><?php _e('Version: '); ?></strong><?php echo $version; ?></p>
198                                 <p><?php echo $theme->display('Description'); ?></p>
199                                 <?php if ( $theme->parent() ) {
200                                         printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
201                                                 __( 'http://codex.wordpress.org/Child_Themes' ),
202                                                 $theme->parent()->display( 'Name' ) );
203                                 } ?>
204                         </div>
205
206                         </div>
207                 <?php
208                 endforeach;
209         }
210
211         function search_theme( $theme ) {
212                 // Search the features
213                 foreach ( $this->features as $word ) {
214                         if ( ! in_array( $word, $theme->get('Tags') ) )
215                                 return false;
216                 }
217
218                 // Match all phrases
219                 foreach ( $this->search_terms as $word ) {
220                         if ( in_array( $word, $theme->get('Tags') ) )
221                                 continue;
222
223                         foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
224                                 // Don't mark up; Do translate.
225                                 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
226                                         continue 2;
227                                 }
228                         }
229
230                         if ( false !== stripos( $theme->get_stylesheet(), $word ) )
231                                 continue;
232
233                         if ( false !== stripos( $theme->get_template(), $word ) )
234                                 continue;
235
236                         return false;
237                 }
238
239                 return true;
240         }
241
242         /**
243          * Send required variables to JavaScript land
244          *
245          * @since 3.4.0
246          * @access private
247          *
248          * @uses $this->features Array of all feature search terms.
249          * @uses get_pagenum()
250          * @uses _pagination_args['total_pages']
251          */
252          function _js_vars( $extra_args = array() ) {
253                 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
254
255                 $args = array(
256                         'search' => $search_string,
257                         'features' => $this->features,
258                         'paged' => $this->get_pagenum(),
259                         'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
260                 );
261
262                 if ( is_array( $extra_args ) )
263                         $args = array_merge( $args, $extra_args );
264
265                 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", json_encode( $args ) );
266                 parent::_js_vars();
267         }
268 }