WordPress 4.5
[autoinstalls/wordpress.git] / wp-admin / themes.php
1 <?php
2 /**
3  * Themes administration panel.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
11
12 if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'edit_theme_options' ) ) {
13         wp_die(
14                 '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
15                 '<p>' . __( 'You are not allowed to edit theme options on this site.' ) . '</p>',
16                 403
17         );
18 }
19
20 if ( current_user_can( 'switch_themes' ) && isset($_GET['action'] ) ) {
21         if ( 'activate' == $_GET['action'] ) {
22                 check_admin_referer('switch-theme_' . $_GET['stylesheet']);
23                 $theme = wp_get_theme( $_GET['stylesheet'] );
24
25                 if ( ! $theme->exists() || ! $theme->is_allowed() ) {
26                         wp_die(
27                                 '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
28                                 '<p>' . __( 'The requested theme does not exist.' ) . '</p>',
29                                 403
30                         );
31                 }
32
33                 switch_theme( $theme->get_stylesheet() );
34                 wp_redirect( admin_url('themes.php?activated=true') );
35                 exit;
36         } elseif ( 'delete' == $_GET['action'] ) {
37                 check_admin_referer('delete-theme_' . $_GET['stylesheet']);
38                 $theme = wp_get_theme( $_GET['stylesheet'] );
39
40                 if ( ! current_user_can( 'delete_themes' ) ) {
41                         wp_die(
42                                 '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
43                                 '<p>' . __( 'You are not allowed to delete this item.' ) . '</p>',
44                                 403
45                         );
46                 }
47
48                 if ( ! $theme->exists() ) {
49                         wp_die(
50                                 '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
51                                 '<p>' . __( 'The requested theme does not exist.' ) . '</p>',
52                                 403
53                         );
54                 }
55
56                 $active = wp_get_theme();
57                 if ( $active->get( 'Template' ) == $_GET['stylesheet'] ) {
58                         wp_redirect( admin_url( 'themes.php?delete-active-child=true' ) );
59                 } else {
60                         delete_theme( $_GET['stylesheet'] );
61                         wp_redirect( admin_url( 'themes.php?deleted=true' ) );
62                 }
63                 exit;
64         }
65 }
66
67 $title = __('Manage Themes');
68 $parent_file = 'themes.php';
69
70 // Help tab: Overview
71 if ( current_user_can( 'switch_themes' ) ) {
72         $help_overview  = '<p>' . __( 'This screen is used for managing your installed themes. Aside from the default theme(s) included with your WordPress installation, themes are designed and developed by third parties.' ) . '</p>' .
73                 '<p>' . __( 'From this screen you can:' ) . '</p>' .
74                 '<ul><li>' . __( 'Hover or tap to see Activate and Live Preview buttons' ) . '</li>' .
75                 '<li>' . __( 'Click on the theme to see the theme name, version, author, description, tags, and the Delete link' ) . '</li>' .
76                 '<li>' . __( 'Click Customize for the current theme or Live Preview for any other theme to see a live preview' ) . '</li></ul>' .
77                 '<p>' . __( 'The current theme is displayed highlighted as the first theme.' ) . '</p>' .
78                 '<p>' . __( 'The search for installed themes will search for terms in their name, description, author, or tag.' ) . ' <span id="live-search-desc">' . __( 'The search results will be updated as you type.' ) . '</span></p>';
79
80         get_current_screen()->add_help_tab( array(
81                 'id'      => 'overview',
82                 'title'   => __( 'Overview' ),
83                 'content' => $help_overview
84         ) );
85 } // switch_themes
86
87 // Help tab: Adding Themes
88 if ( current_user_can( 'install_themes' ) ) {
89         if ( is_multisite() ) {
90                 $help_install = '<p>' . __('Installing themes on Multisite can only be done from the Network Admin section.') . '</p>';
91         } else {
92                 $help_install = '<p>' . sprintf( __('If you would like to see more themes to choose from, click on the &#8220;Add New&#8221; button and you will be able to browse or search for additional themes from the <a href="%s" target="_blank">WordPress.org Theme Directory</a>. Themes in the WordPress.org Theme Directory are designed and developed by third parties, and are compatible with the license WordPress uses. Oh, and they&#8217;re free!'), 'https://wordpress.org/themes/' ) . '</p>';
93         }
94
95         get_current_screen()->add_help_tab( array(
96                 'id'      => 'adding-themes',
97                 'title'   => __('Adding Themes'),
98                 'content' => $help_install
99         ) );
100 } // install_themes
101
102 // Help tab: Previewing and Customizing
103 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
104         $help_customize =
105                 '<p>' . __( 'Tap or hover on any theme then click the Live Preview button to see a live preview of that theme and change theme options in a separate, full-screen view. You can also find a Live Preview button at the bottom of the theme details screen. Any installed theme can be previewed and customized in this way.' ) . '</p>'.
106                 '<p>' . __( 'The theme being previewed is fully interactive &mdash; navigate to different pages to see how the theme handles posts, archives, and other page templates. The settings may differ depending on what theme features the theme being previewed supports. To accept the new settings and activate the theme all in one step, click the Save &amp; Activate button above the menu.' ) . '</p>' .
107                 '<p>' . __( 'When previewing on smaller monitors, you can use the collapse icon at the bottom of the left-hand pane. This will hide the pane, giving you more room to preview your site in the new theme. To bring the pane back, click on the collapse icon again.' ) . '</p>';
108
109         get_current_screen()->add_help_tab( array(
110                 'id'            => 'customize-preview-themes',
111                 'title'         => __( 'Previewing and Customizing' ),
112                 'content'       => $help_customize
113         ) );
114 } // edit_theme_options && customize
115
116 get_current_screen()->set_help_sidebar(
117         '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
118         '<p>' . __( '<a href="https://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>' ) . '</p>' .
119         '<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
120 );
121
122 if ( current_user_can( 'switch_themes' ) ) {
123         $themes = wp_prepare_themes_for_js();
124 } else {
125         $themes = wp_prepare_themes_for_js( array( wp_get_theme() ) );
126 }
127 wp_reset_vars( array( 'theme', 'search' ) );
128
129 wp_localize_script( 'theme', '_wpThemeSettings', array(
130         'themes'   => $themes,
131         'settings' => array(
132                 'canInstall'    => ( ! is_multisite() && current_user_can( 'install_themes' ) ),
133                 'installURI'    => ( ! is_multisite() && current_user_can( 'install_themes' ) ) ? admin_url( 'theme-install.php' ) : null,
134                 'confirmDelete' => __( "Are you sure you want to delete this theme?\n\nClick 'Cancel' to go back, 'OK' to confirm the delete." ),
135                 'adminUrl'      => parse_url( admin_url(), PHP_URL_PATH ),
136         ),
137         'l10n' => array(
138                 'addNew'            => __( 'Add New Theme' ),
139                 'search'            => __( 'Search Installed Themes' ),
140                 'searchPlaceholder' => __( 'Search installed themes...' ), // placeholder (no ellipsis)
141                 'themesFound'       => __( 'Number of Themes found: %d' ),
142                 'noThemesFound'     => __( 'No themes found. Try a different search.' ),
143         ),
144 ) );
145
146 add_thickbox();
147 wp_enqueue_script( 'theme' );
148 wp_enqueue_script( 'customize-loader' );
149
150 require_once( ABSPATH . 'wp-admin/admin-header.php' );
151 ?>
152
153 <div class="wrap">
154         <h1><?php esc_html_e( 'Themes' ); ?>
155                 <span class="title-count theme-count"><?php echo count( $themes ); ?></span>
156         <?php if ( ! is_multisite() && current_user_can( 'install_themes' ) ) : ?>
157                 <a href="<?php echo admin_url( 'theme-install.php' ); ?>" class="hide-if-no-js page-title-action"><?php echo esc_html_x( 'Add New', 'Add new theme' ); ?></a>
158         <?php endif; ?>
159         </h1>
160 <?php
161 if ( ! validate_current_theme() || isset( $_GET['broken'] ) ) : ?>
162 <div id="message1" class="updated notice is-dismissible"><p><?php _e('The active theme is broken. Reverting to the default theme.'); ?></p></div>
163 <?php elseif ( isset($_GET['activated']) ) :
164                 if ( isset( $_GET['previewed'] ) ) { ?>
165                 <div id="message2" class="updated notice is-dismissible"><p><?php _e( 'Settings saved and theme activated.' ); ?> <a href="<?php echo home_url( '/' ); ?>"><?php _e( 'Visit site' ); ?></a></p></div>
166                 <?php } else { ?>
167 <div id="message2" class="updated notice is-dismissible"><p><?php _e( 'New theme activated.' ); ?> <a href="<?php echo home_url( '/' ); ?>"><?php _e( 'Visit site' ); ?></a></p></div><?php
168                 }
169         elseif ( isset($_GET['deleted']) ) : ?>
170 <div id="message3" class="updated notice is-dismissible"><p><?php _e('Theme deleted.') ?></p></div>
171 <?php elseif ( isset( $_GET['delete-active-child'] ) ) : ?>
172         <div id="message4" class="error"><p><?php _e( 'You cannot delete a theme while it has an active child theme.' ); ?></p></div>
173 <?php
174 endif;
175
176 $ct = wp_get_theme();
177
178 if ( $ct->errors() && ( ! is_multisite() || current_user_can( 'manage_network_themes' ) ) ) {
179         echo '<div class="error"><p>' . sprintf( __( 'ERROR: %s' ), $ct->errors()->get_error_message() ) . '</p></div>';
180 }
181
182 /*
183 // Certain error codes are less fatal than others. We can still display theme information in most cases.
184 if ( ! $ct->errors() || ( 1 == count( $ct->errors()->get_error_codes() )
185         && in_array( $ct->errors()->get_error_code(), array( 'theme_no_parent', 'theme_parent_invalid', 'theme_no_index' ) ) ) ) : ?>
186 */
187
188         // Pretend you didn't see this.
189         $current_theme_actions = array();
190         if ( is_array( $submenu ) && isset( $submenu['themes.php'] ) ) {
191                 foreach ( (array) $submenu['themes.php'] as $item) {
192                         $class = '';
193                         if ( 'themes.php' == $item[2] || 'theme-editor.php' == $item[2] || 0 === strpos( $item[2], 'customize.php' ) )
194                                 continue;
195                         // 0 = name, 1 = capability, 2 = file
196                         if ( ( strcmp($self, $item[2]) == 0 && empty($parent_file)) || ($parent_file && ($item[2] == $parent_file)) )
197                                 $class = ' current';
198                         if ( !empty($submenu[$item[2]]) ) {
199                                 $submenu[$item[2]] = array_values($submenu[$item[2]]); // Re-index.
200                                 $menu_hook = get_plugin_page_hook($submenu[$item[2]][0][2], $item[2]);
201                                 if ( file_exists(WP_PLUGIN_DIR . "/{$submenu[$item[2]][0][2]}") || !empty($menu_hook))
202                                         $current_theme_actions[] = "<a class='button button-secondary$class' href='admin.php?page={$submenu[$item[2]][0][2]}'>{$item[0]}</a>";
203                                 else
204                                         $current_theme_actions[] = "<a class='button button-secondary$class' href='{$submenu[$item[2]][0][2]}'>{$item[0]}</a>";
205                         } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
206                                 $menu_file = $item[2];
207
208                                 if ( current_user_can( 'customize' ) ) {
209                                         if ( 'custom-header' === $menu_file ) {
210                                                 $current_theme_actions[] = "<a class='button button-secondary hide-if-no-customize$class' href='customize.php?autofocus[control]=header_image'>{$item[0]}</a>";
211                                         } elseif ( 'custom-background' === $menu_file ) {
212                                                 $current_theme_actions[] = "<a class='button button-secondary hide-if-no-customize$class' href='customize.php?autofocus[control]=background_image'>{$item[0]}</a>";
213                                         }
214                                 }
215
216                                 if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) {
217                                         $menu_file = substr( $menu_file, 0, $pos );
218                                 }
219
220                                 if ( file_exists( ABSPATH . "wp-admin/$menu_file" ) ) {
221                                         $current_theme_actions[] = "<a class='button button-secondary$class' href='{$item[2]}'>{$item[0]}</a>";
222                                 } else {
223                                         $current_theme_actions[] = "<a class='button button-secondary$class' href='themes.php?page={$item[2]}'>{$item[0]}</a>";
224                                 }
225                         }
226                 }
227         }
228
229 ?>
230
231 <div class="theme-browser">
232         <div class="themes wp-clearfix">
233
234 <?php
235 /*
236  * This PHP is synchronized with the tmpl-theme template below!
237  */
238
239 foreach ( $themes as $theme ) :
240         $aria_action = esc_attr( $theme['id'] . '-action' );
241         $aria_name   = esc_attr( $theme['id'] . '-name' );
242         ?>
243 <div class="theme<?php if ( $theme['active'] ) echo ' active'; ?>" tabindex="0" aria-describedby="<?php echo $aria_action . ' ' . $aria_name; ?>">
244         <?php if ( ! empty( $theme['screenshot'][0] ) ) { ?>
245                 <div class="theme-screenshot">
246                         <img src="<?php echo $theme['screenshot'][0]; ?>" alt="" />
247                 </div>
248         <?php } else { ?>
249                 <div class="theme-screenshot blank"></div>
250         <?php } ?>
251         <span class="more-details" id="<?php echo $aria_action; ?>"><?php _e( 'Theme Details' ); ?></span>
252         <div class="theme-author"><?php printf( __( 'By %s' ), $theme['author'] ); ?></div>
253
254         <?php if ( $theme['active'] ) { ?>
255                 <h2 class="theme-name" id="<?php echo $aria_name; ?>">
256                         <?php
257                         /* translators: %s: theme name */
258                         printf( __( '<span>Active:</span> %s' ), $theme['name'] );
259                         ?>
260                 </h2>
261         <?php } else { ?>
262                 <h2 class="theme-name" id="<?php echo $aria_name; ?>"><?php echo $theme['name']; ?></h2>
263         <?php } ?>
264
265         <div class="theme-actions">
266
267         <?php if ( $theme['active'] ) { ?>
268                 <?php if ( $theme['actions']['customize'] && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { ?>
269                         <a class="button button-primary customize load-customize hide-if-no-customize" href="<?php echo $theme['actions']['customize']; ?>"><?php _e( 'Customize' ); ?></a>
270                 <?php } ?>
271         <?php } else { ?>
272                 <a class="button button-secondary activate" href="<?php echo $theme['actions']['activate']; ?>"><?php _e( 'Activate' ); ?></a>
273                 <?php if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { ?>
274                         <a class="button button-primary load-customize hide-if-no-customize" href="<?php echo $theme['actions']['customize']; ?>"><?php _e( 'Live Preview' ); ?></a>
275                 <?php } ?>
276         <?php } ?>
277
278         </div>
279
280         <?php if ( $theme['hasUpdate'] ) { ?>
281                 <div class="theme-update"><?php _e( 'Update Available' ); ?></div>
282         <?php } ?>
283 </div>
284 <?php endforeach; ?>
285         </div>
286 </div>
287 <div class="theme-overlay"></div>
288
289 <p class="no-themes"><?php _e( 'No themes found. Try a different search.' ); ?></p>
290
291 <?php
292 // List broken themes, if any.
293 if ( ! is_multisite() && current_user_can('edit_themes') && $broken_themes = wp_get_themes( array( 'errors' => true ) ) ) {
294 ?>
295
296 <div class="broken-themes">
297 <h3><?php _e('Broken Themes'); ?></h3>
298 <p><?php _e( 'The following themes are installed but incomplete.' ); ?></p>
299
300 <?php
301 $can_delete = current_user_can( 'delete_themes' );
302 $can_install = current_user_can( 'install_themes' );
303 ?>
304 <table>
305         <tr>
306                 <th><?php _ex('Name', 'theme name'); ?></th>
307                 <th><?php _e('Description'); ?></th>
308                 <?php if ( $can_delete ) { ?>
309                         <td></td>
310                 <?php } ?>
311                 <?php if ( $can_install ) { ?>
312                         <td></td>
313                 <?php } ?>
314         </tr>
315         <?php foreach ( $broken_themes as $broken_theme ) : ?>
316                 <tr>
317                         <td><?php echo $broken_theme->get( 'Name' ) ? $broken_theme->display( 'Name' ) : $broken_theme->get_stylesheet(); ?></td>
318                         <td><?php echo $broken_theme->errors()->get_error_message(); ?></td>
319                         <?php
320                         if ( $can_delete ) {
321                                 $stylesheet = $broken_theme->get_stylesheet();
322                                 $delete_url = add_query_arg( array(
323                                         'action'     => 'delete',
324                                         'stylesheet' => urlencode( $stylesheet ),
325                                 ), admin_url( 'themes.php' ) );
326                                 $delete_url = wp_nonce_url( $delete_url, 'delete-theme_' . $stylesheet );
327                                 ?>
328                                 <td><a href="<?php echo esc_url( $delete_url ); ?>" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a></td>
329                                 <?php
330                         }
331
332                         if ( $can_install && 'theme_no_parent' === $broken_theme->errors()->get_error_code() ) {
333                                 $parent_theme_name = $broken_theme->get( 'Template' );
334                                 $parent_theme = themes_api( 'theme_information', array( 'slug' => urlencode( $parent_theme_name ) ) );
335
336                                 if ( ! is_wp_error( $parent_theme ) ) {
337                                         $install_url = add_query_arg( array(
338                                                 'action' => 'install-theme',
339                                                 'theme'  => urlencode( $parent_theme_name ),
340                                         ), admin_url( 'update.php' ) );
341                                         $install_url = wp_nonce_url( $install_url, 'install-theme_' . $parent_theme_name );
342                                         ?>
343                                         <td><a href="<?php echo esc_url( $install_url ); ?>" class="button button-secondary install-theme"><?php _e( 'Install Parent Theme' ); ?></a></td>
344                                         <?php
345                                 }
346                         }
347                         ?>
348                 </tr>
349         <?php endforeach; ?>
350 </table>
351 </div>
352
353 <?php
354 }
355 ?>
356 </div><!-- .wrap -->
357
358 <?php
359 /*
360  * The tmpl-theme template is synchronized with PHP above!
361  */
362 ?>
363 <script id="tmpl-theme" type="text/template">
364         <# if ( data.screenshot[0] ) { #>
365                 <div class="theme-screenshot">
366                         <img src="{{ data.screenshot[0] }}" alt="" />
367                 </div>
368         <# } else { #>
369                 <div class="theme-screenshot blank"></div>
370         <# } #>
371         <span class="more-details" id="{{ data.id }}-action"><?php _e( 'Theme Details' ); ?></span>
372         <div class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.author }}}' ); ?></div>
373
374         <# if ( data.active ) { #>
375                 <h2 class="theme-name" id="{{ data.id }}-name">
376                         <?php
377                         /* translators: %s: theme name */
378                         printf( __( '<span>Active:</span> %s' ), '{{{ data.name }}}' );
379                         ?>
380                 </h2>
381         <# } else { #>
382                 <h2 class="theme-name" id="{{ data.id }}-name">{{{ data.name }}}</h2>
383         <# } #>
384
385         <div class="theme-actions">
386
387         <# if ( data.active ) { #>
388                 <# if ( data.actions.customize ) { #>
389                         <a class="button button-primary customize load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Customize' ); ?></a>
390                 <# } #>
391         <# } else { #>
392                 <a class="button button-secondary activate" href="{{{ data.actions.activate }}}"><?php _e( 'Activate' ); ?></a>
393                 <a class="button button-primary load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Live Preview' ); ?></a>
394         <# } #>
395
396         </div>
397
398         <# if ( data.hasUpdate ) { #>
399                 <div class="theme-update"><?php _e( 'Update Available' ); ?></div>
400         <# } #>
401 </script>
402
403 <script id="tmpl-theme-single" type="text/template">
404         <div class="theme-backdrop"></div>
405         <div class="theme-wrap wp-clearfix">
406                 <div class="theme-header">
407                         <button class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
408                         <button class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
409                         <button class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
410                 </div>
411                 <div class="theme-about wp-clearfix">
412                         <div class="theme-screenshots">
413                         <# if ( data.screenshot[0] ) { #>
414                                 <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
415                         <# } else { #>
416                                 <div class="screenshot blank"></div>
417                         <# } #>
418                         </div>
419
420                         <div class="theme-info">
421                                 <# if ( data.active ) { #>
422                                         <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
423                                 <# } #>
424                                 <h2 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h2>
425                                 <p class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></p>
426
427                                 <# if ( data.hasUpdate ) { #>
428                                 <div class="notice notice-warning notice-alt notice-large">
429                                         <h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
430                                         {{{ data.update }}}
431                                 </div>
432                                 <# } #>
433                                 <p class="theme-description">{{{ data.description }}}</p>
434
435                                 <# if ( data.parent ) { #>
436                                         <p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p>
437                                 <# } #>
438
439                                 <# if ( data.tags ) { #>
440                                         <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
441                                 <# } #>
442                         </div>
443                 </div>
444
445                 <div class="theme-actions">
446                         <div class="active-theme">
447                                 <a href="{{{ data.actions.customize }}}" class="button button-primary customize load-customize hide-if-no-customize"><?php _e( 'Customize' ); ?></a>
448                                 <?php echo implode( ' ', $current_theme_actions ); ?>
449                         </div>
450                         <div class="inactive-theme">
451                                 <# if ( data.actions.activate ) { #>
452                                         <a href="{{{ data.actions.activate }}}" class="button button-secondary activate"><?php _e( 'Activate' ); ?></a>
453                                 <# } #>
454                                 <a href="{{{ data.actions.customize }}}" class="button button-primary load-customize hide-if-no-customize"><?php _e( 'Live Preview' ); ?></a>
455                         </div>
456
457                         <# if ( ! data.active && data.actions['delete'] ) { #>
458                                 <a href="{{{ data.actions['delete'] }}}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
459                         <# } #>
460                 </div>
461         </div>
462 </script>
463
464 <?php require( ABSPATH . 'wp-admin/admin-footer.php' );