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