]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/theme-editor.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-admin / theme-editor.php
1 <?php
2 /**
3  * Theme editor 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 ( is_multisite() && ! is_network_admin() ) {
13         wp_redirect( network_admin_url( 'theme-editor.php' ) );
14         exit();
15 }
16
17 if ( !current_user_can('edit_themes') )
18         wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site.').'</p>');
19
20 $title = __("Edit Themes");
21 $parent_file = 'themes.php';
22
23 get_current_screen()->add_help_tab( array(
24 'id'            => 'overview',
25 'title'         => __('Overview'),
26 'content'       =>
27         '<p>' . __('You can use the Theme Editor to edit the individual CSS and PHP files which make up your theme.') . '</p>
28         <p>' . __("Begin by choosing a theme to edit from the dropdown menu and clicking Select. A list then appears of the theme's template files. Clicking once on any file name causes the file to appear in the large Editor box.") . '</p>
29         <p>' . __('For PHP files, you can use the Documentation dropdown to select from functions recognized in that file. Look Up takes you to a web page with reference material about that particular function.') . '</p>
30         <p id="newcontent-description">' . __( 'In the editing area the Tab key enters a tab character. To move below this area by pressing Tab, press the Esc key followed by the Tab key. In some cases the Esc key will need to be pressed twice before the Tab key will allow you to continue.' ) . '</p>
31         <p>' . __('After typing in your edits, click Update File.') . '</p>
32         <p>' . __('<strong>Advice:</strong> think very carefully about your site crashing if you are live-editing the theme currently in use.') . '</p>
33         <p>' . sprintf( __('Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s" target="_blank">child theme</a> instead.'), __('https://codex.wordpress.org/Child_Themes') ) . '</p>' .
34         ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
35 ) );
36
37 get_current_screen()->set_help_sidebar(
38         '<p><strong>' . __('For more information:') . '</strong></p>' .
39         '<p>' . __('<a href="https://codex.wordpress.org/Theme_Development" target="_blank">Documentation on Theme Development</a>') . '</p>' .
40         '<p>' . __('<a href="https://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>') . '</p>' .
41         '<p>' . __('<a href="https://codex.wordpress.org/Editing_Files" target="_blank">Documentation on Editing Files</a>') . '</p>' .
42         '<p>' . __('<a href="https://codex.wordpress.org/Template_Tags" target="_blank">Documentation on Template Tags</a>') . '</p>' .
43         '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
44 );
45
46 wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
47
48 if ( $theme ) {
49         $stylesheet = $theme;
50 } else {
51         $stylesheet = get_stylesheet();
52 }
53
54 $theme = wp_get_theme( $stylesheet );
55
56 if ( ! $theme->exists() ) {
57         wp_die( __( 'The requested theme does not exist.' ) );
58 }
59
60 if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() ) {
61         wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
62 }
63
64 $allowed_files = $style_files = array();
65 $has_templates = false;
66 $default_types = array( 'php', 'css' );
67
68 /**
69  * Filter the list of file types allowed for editing in the Theme editor.
70  *
71  * @since 4.4.0
72  *
73  * @param array    $default_types List of file types. Default types include 'php' and 'css'.
74  * @param WP_Theme $theme         The current Theme object.
75  */
76 $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
77
78 // Ensure that default types are still there.
79 $file_types = array_unique( array_merge( $file_types, $default_types ) );
80
81 foreach ( $file_types as $type ) {
82         switch ( $type ) {
83                 case 'php':
84                         $allowed_files += $theme->get_files( 'php', 1 );
85                         $has_templates = ! empty( $allowed_files );
86                         break;
87                 case 'css':
88                         $style_files = $theme->get_files( 'css' );
89                         $allowed_files['style.css'] = $style_files['style.css'];
90                         $allowed_files += $style_files;
91                         break;
92                 default:
93                         $allowed_files += $theme->get_files( $type );
94                         break;
95         }
96 }
97
98 if ( empty( $file ) ) {
99         $relative_file = 'style.css';
100         $file = $allowed_files['style.css'];
101 } else {
102         $relative_file = $file;
103         $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
104 }
105
106 validate_file_to_edit( $file, $allowed_files );
107 $scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0;
108
109 switch( $action ) {
110 case 'update':
111         check_admin_referer( 'edit-theme_' . $file . $stylesheet );
112         $newcontent = wp_unslash( $_POST['newcontent'] );
113         $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto;
114         if ( is_writeable( $file ) ) {
115                 // is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
116                 $f = fopen( $file, 'w+' );
117                 if ( $f !== false ) {
118                         fwrite( $f, $newcontent );
119                         fclose( $f );
120                         $location .= '&updated=true';
121                         $theme->cache_delete();
122                 }
123         }
124         wp_redirect( $location );
125         exit;
126
127 default:
128
129         require_once( ABSPATH . 'wp-admin/admin-header.php' );
130
131         update_recently_edited( $file );
132
133         if ( ! is_file( $file ) )
134                 $error = true;
135
136         $content = '';
137         if ( ! $error && filesize( $file ) > 0 ) {
138                 $f = fopen($file, 'r');
139                 $content = fread($f, filesize($file));
140
141                 if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
142                         $functions = wp_doc_link_parse( $content );
143
144                         $docs_select = '<select name="docs-list" id="docs-list">';
145                         $docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
146                         foreach ( $functions as $function ) {
147                                 $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
148                         }
149                         $docs_select .= '</select>';
150                 }
151
152                 $content = esc_textarea( $content );
153         }
154
155         if ( isset( $_GET['updated'] ) ) : ?>
156  <div id="message" class="updated notice is-dismissible"><p><?php _e( 'File edited successfully.' ) ?></p></div>
157 <?php endif;
158
159 $description = get_file_description( $relative_file );
160 $file_show = array_search( $file, array_filter( $allowed_files ) );
161 if ( $description != $file_show )
162         $description .= ' <span>(' . $file_show . ')</span>';
163 ?>
164 <div class="wrap">
165 <h1><?php echo esc_html( $title ); ?></h1>
166
167 <div class="fileedit-sub">
168 <div class="alignleft">
169 <h2><?php echo $theme->display( 'Name' ); if ( $description ) echo ': ' . $description; ?></h2>
170 </div>
171 <div class="alignright">
172         <form action="theme-editor.php" method="post">
173                 <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
174                 <select name="theme" id="theme">
175 <?php
176 foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
177         if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() )
178                 continue;
179
180         $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
181         echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
182 }
183 ?>
184                 </select>
185                 <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
186         </form>
187 </div>
188 <br class="clear" />
189 </div>
190 <?php
191 if ( $theme->errors() )
192         echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
193 ?>
194         <div id="templateside">
195 <?php
196 if ( $allowed_files ) :
197         $previous_file_type = '';
198
199         foreach ( $allowed_files as $filename => $absolute_filename ) :
200                 $file_type = substr( $filename, strrpos( $filename, '.' ) );
201
202                 if ( $file_type !== $previous_file_type ) {
203                         if ( '' !== $previous_file_type ) {
204                                 echo "\t</ul>\n";
205                         }
206
207                         switch ( $file_type ) {
208                                 case '.php':
209                                         if ( $has_templates || $theme->parent() ) :
210                                                 echo "\t<h2>" . __( 'Templates' ) . "</h2>\n";
211                                                 if ( $theme->parent() ) {
212                                                         echo '<p class="howto">' . sprintf( __( 'This child theme inherits templates from a parent theme, %s.' ),
213                                                                 sprintf( '<a href="%s">%s</a>',
214                                                                         self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
215                                                                         $theme->parent()->display( 'Name' )
216                                                                 )
217                                                         ) . "</p>\n";
218                                                 }
219                                         endif;
220                                         break;
221                                 case '.css':
222                                         echo "\t<h2>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h2>\n";
223                                         break;
224                                 default:
225                                         /* translators: %s: file extension */
226                                         echo "\t<h2>" . sprintf( __( '%s files' ), $file_type ) . "</h2>\n";
227                                         break;
228                         }
229
230                         echo "\t<ul>\n";
231                 }
232                 
233                 $file_description = get_file_description( $filename );
234                 if ( $filename !== basename( $absolute_filename ) || $file_description !== $filename ) {
235                         $file_description .= '<br /><span class="nonessential">(' . $filename . ')</span>';
236                 }
237
238                 if ( $absolute_filename === $file ) {
239                         $file_description = '<span class="highlight">' . $file_description . '</span>';
240                 }
241
242                 $previous_file_type = $file_type;
243 ?>
244                 <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
245 <?php
246         endforeach;
247 ?>
248 </ul>
249 <?php endif; ?>
250 </div>
251 <?php if ( $error ) :
252         echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
253 else : ?>
254         <form name="template" id="template" action="theme-editor.php" method="post">
255         <?php wp_nonce_field( 'edit-theme_' . $file . $stylesheet ); ?>
256                 <div><textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
257                 <input type="hidden" name="action" value="update" />
258                 <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
259                 <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
260                 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
261                 </div>
262         <?php if ( ! empty( $functions ) ) : ?>
263                 <div id="documentation" class="hide-if-no-js">
264                 <label for="docs-list"><?php _e('Documentation:') ?></label>
265                 <?php echo $docs_select; ?>
266                 <input type="button" class="button" value=" <?php esc_attr_e( 'Look Up' ); ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" />
267                 </div>
268         <?php endif; ?>
269
270                 <div>
271                 <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
272                         <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
273                         <?php _e( 'This is a file in your current parent theme.' ); ?></p>
274                 <?php endif; ?>
275 <?php
276         if ( is_writeable( $file ) ) :
277                 submit_button( __( 'Update File' ), 'primary', 'submit', true );
278         else : ?>
279 <p><em><?php _e('You need to make this file writable before you can save your changes. See <a href="https://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.'); ?></em></p>
280 <?php endif; ?>
281                 </div>
282         </form>
283 <?php
284 endif; // $error
285 ?>
286 <br class="clear" />
287 </div>
288 <script type="text/javascript">
289 jQuery(document).ready(function($){
290         $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
291         $('#newcontent').scrollTop( $('#scrollto').val() );
292 });
293 </script>
294 <?php
295 break;
296 }
297
298 include(ABSPATH . 'wp-admin/admin-footer.php' );