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