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