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