Make WordPress Core

Ticket #51411: theme-editor.php

File theme-editor.php, 14.8 KB (added by vadatiertebat, 6 years ago)
Line 
1<?php
2/**
3 * Theme editor administration panel.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/** WordPress Administration Bootstrap */
10require_once __DIR__ . '/admin.php';
11
12if ( is_multisite() && ! is_network_admin() ) {
13 wp_redirect( network_admin_url( 'theme-editor.php' ) );
14 exit();
15}
16
17if ( ! current_user_can( 'edit_themes' ) ) {
18 wp_die( '<p>' . __( 'Sorry, you are not allowed to edit templates for this site.' ) . '</p>' );
19}
20
21$title = __( 'Edit Themes' );
22$parent_file = 'themes.php';
23
24get_current_screen()->add_help_tab(
25 array(
26 'id' => 'overview',
27 'title' => __( 'Overview' ),
28 'content' =>
29 '<p>' . __( 'You can use the theme editor to edit the individual CSS and PHP files which make up your theme.' ) . '</p>' .
30 '<p>' . __( 'Begin by choosing a theme to edit from the dropdown menu and clicking the Select button. A list then appears of the theme&#8217;s template files. Clicking once on any file name causes the file to appear in the large Editor box.' ) . '</p>' .
31 '<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>' .
32 '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
33 '<ul>' .
34 '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
35 '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
36 '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
37 '</ul>' .
38 '<p>' . __( 'After typing in your edits, click Update File.' ) . '</p>' .
39 '<p>' . __( '<strong>Advice:</strong> Think very carefully about your site crashing if you are live-editing the theme currently in use.' ) . '</p>' .
40 '<p>' . sprintf(
41 /* translators: %s: Link to documentation on child themes. */
42 __( 'Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s">child theme</a> instead.' ),
43 __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' )
44 ) . '</p>' .
45 ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
46 )
47);
48
49get_current_screen()->set_help_sidebar(
50 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
51 '<p>' . __( '<a href="https://developer.wordpress.org/themes/">Documentation on Theme Development</a>' ) . '</p>' .
52 '<p>' . __( '<a href="https://wordpress.org/support/article/using-themes/">Documentation on Using Themes</a>' ) . '</p>' .
53 '<p>' . __( '<a href="https://wordpress.org/support/article/editing-files/">Documentation on Editing Files</a>' ) . '</p>' .
54 '<p>' . __( '<a href="https://developer.wordpress.org/themes/basics/template-tags/">Documentation on Template Tags</a>' ) . '</p>' .
55 '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
56);
57
58wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
59
60if ( $theme ) {
61 $stylesheet = $theme;
62} else {
63 $stylesheet = get_stylesheet();
64}
65
66$theme = wp_get_theme( $stylesheet );
67
68if ( ! $theme->exists() ) {
69 wp_die( __( 'The requested theme does not exist.' ) );
70}
71
72if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() ) {
73 wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
74}
75
76$allowed_files = array();
77$style_files = array();
78$has_templates = false;
79
80$file_types = wp_get_theme_file_editable_extensions( $theme );
81
82foreach ( $file_types as $type ) {
83 switch ( $type ) {
84 case 'php':
85 $allowed_files += $theme->get_files( 'php', -1 );
86 $has_templates = ! empty( $allowed_files );
87 break;
88 case 'css':
89 $style_files = $theme->get_files( 'css', -1 );
90 $allowed_files['style.css'] = $style_files['style.css'];
91 $allowed_files += $style_files;
92 break;
93 default:
94 $allowed_files += $theme->get_files( $type, -1 );
95 break;
96 }
97}
98
99// Move functions.php and style.css to the top.
100if ( isset( $allowed_files['functions.php'] ) ) {
101 $allowed_files = array( 'functions.php' => $allowed_files['functions.php'] ) + $allowed_files;
102}
103if ( isset( $allowed_files['style.css'] ) ) {
104 $allowed_files = array( 'style.css' => $allowed_files['style.css'] ) + $allowed_files;
105}
106
107if ( empty( $file ) ) {
108 $relative_file = 'style.css';
109 $file = $allowed_files['style.css'];
110} else {
111 $relative_file = wp_unslash( $file );
112 $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
113}
114
115validate_file_to_edit( $file, $allowed_files );
116
117// Handle fallback editing of file when JavaScript is not available.
118$edit_error = null;
119$posted_content = null;
120
121if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
122 $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
123 if ( is_wp_error( $r ) ) {
124 $edit_error = $r;
125 if ( check_ajax_referer( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
126 $posted_content = wp_unslash( $_POST['newcontent'] );
127 }
128 } else {
129 wp_redirect(
130 add_query_arg(
131 array(
132 'a' => 1, // This means "success" for some reason.
133 'theme' => $stylesheet,
134 'file' => $relative_file,
135 ),
136 admin_url( 'theme-editor.php' )
137 )
138 );
139 exit;
140 }
141}
142
143$settings = array(
144 'codeEditor' => wp_enqueue_code_editor( compact( 'file' ) ),
145);
146wp_enqueue_script( 'wp-theme-plugin-editor' );
147wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
148wp_add_inline_script( 'wp-theme-plugin-editor', 'wp.themePluginEditor.themeOrPlugin = "theme";' );
149
150require_once ABSPATH . 'wp-admin/admin-header.php';
151
152update_recently_edited( $file );
153$error = false; // fixed
154if ( ! is_file( $file ) ) {
155 $error = true;
156}
157
158$content = '';
159if ( ! empty( $posted_content ) ) {
160 $content = $posted_content;
161} elseif ( ! $error && filesize( $file ) > 0 ) {
162 $f = fopen( $file, 'r' );
163 $content = fread( $f, filesize( $file ) );
164
165 if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
166 $functions = wp_doc_link_parse( $content );
167
168 $docs_select = '<select name="docs-list" id="docs-list">';
169 $docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
170 foreach ( $functions as $function ) {
171 $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
172 }
173 $docs_select .= '</select>';
174 }
175
176 $content = esc_textarea( $content );
177}
178
179$file_description = get_file_description( $relative_file );
180$file_show = array_search( $file, array_filter( $allowed_files ) );
181$description = esc_html( $file_description );
182if ( $file_description != $file_show ) {
183 $description .= ' <span>(' . esc_html( $file_show ) . ')</span>';
184}
185?>
186<div class="wrap">
187<h1><?php echo esc_html( $title ); ?></h1>
188
189<?php if ( isset( $_GET['a'] ) ) : ?>
190 <div id="message" class="updated notice is-dismissible">
191 <p><?php _e( 'File edited successfully.' ); ?></p>
192 </div>
193<?php elseif ( is_wp_error( $edit_error ) ) : ?>
194 <div id="message" class="notice notice-error">
195 <p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
196 <pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
197 </div>
198<?php endif; ?>
199
200<?php if ( preg_match( '/\.css$/', $file ) ) : ?>
201 <div id="message" class="notice-info notice">
202 <p><strong><?php _e( 'Did you know?' ); ?></strong></p>
203 <p>
204 <?php
205 echo sprintf(
206 /* translators: %s: Link to Custom CSS section in the Customizer. */
207 __( 'There&#8217;s no need to change your CSS here &mdash; you can edit and live preview CSS changes in the <a href="%s">built-in CSS editor</a>.' ),
208 esc_url( add_query_arg( 'autofocus[section]', 'custom_css', admin_url( 'customize.php' ) ) )
209 );
210 ?>
211 </p>
212 </div>
213<?php endif; ?>
214
215<div class="fileedit-sub">
216<div class="alignleft">
217<h2>
218 <?php
219 echo $theme->display( 'Name' );
220 if ( $description ) {
221 echo ': ' . $description;}
222 ?>
223</h2>
224</div>
225<div class="alignright">
226 <form action="theme-editor.php" method="get">
227 <strong><label for="theme"><?php _e( 'Select theme to edit:' ); ?> </label></strong>
228 <select name="theme" id="theme">
229 <?php
230 foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
231 if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() ) {
232 continue;
233 }
234
235 $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
236 echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display( 'Name' ) . '</option>';
237 }
238 ?>
239 </select>
240 <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
241 </form>
242</div>
243<br class="clear" />
244</div>
245
246<?php
247if ( $theme->errors() ) {
248 echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
249}
250?>
251
252<div id="templateside">
253 <h2 id="theme-files-label"><?php _e( 'Theme Files' ); ?></h2>
254 <ul role="tree" aria-labelledby="theme-files-label">
255 <?php if ( ( $has_templates || $theme->parent() ) && $theme->parent() ) : ?>
256 <li class="howto">
257 <?php
258 printf(
259 /* translators: %s: Link to edit parent theme. */
260 __( 'This child theme inherits templates from a parent theme, %s.' ),
261 sprintf(
262 '<a href="%s">%s</a>',
263 self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
264 $theme->parent()->display( 'Name' )
265 )
266 );
267 ?>
268 </li>
269 <?php endif; ?>
270 <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
271 <ul role="group">
272 <?php wp_print_theme_file_tree( wp_make_theme_file_tree( $allowed_files ) ); ?>
273 </ul>
274 </li>
275 </ul>
276</div>
277
278<?php
279if ( $error ) :
280 echo '<div class="error"><p>' . __( 'File does not exist! Please double check the name and try again.' ) . '</p></div>';
281else :
282 ?>
283 <form name="template" id="template" action="theme-editor.php" method="post">
284 <?php wp_nonce_field( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce' ); ?>
285 <div>
286 <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
287 <textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"><?php echo $content; ?></textarea>
288 <input type="hidden" name="action" value="update" />
289 <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
290 <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
291 </div>
292
293 <?php if ( ! empty( $functions ) ) : ?>
294 <div id="documentation" class="hide-if-no-js">
295 <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
296 <?php echo $docs_select; ?>
297 <input disabled id="docs-lookup" 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_user_locale() ); ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ); ?>&amp;redirect=true'); }" />
298 </div>
299 <?php endif; ?>
300
301 <div>
302 <div class="editor-notices">
303 <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
304 <div class="notice notice-warning inline">
305 <p>
306 <?php if ( is_writeable( $file ) ) : ?>
307 <strong><?php _e( 'Caution:' ); ?></strong>
308 <?php endif; ?>
309 <?php _e( 'This is a file in your current parent theme.' ); ?>
310 </p>
311 </div>
312 <?php endif; ?>
313 </div>
314 <?php if ( is_writeable( $file ) ) : ?>
315 <p class="submit">
316 <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
317 <span class="spinner"></span>
318 </p>
319 <?php else : ?>
320 <p><em>
321 <?php
322 printf(
323 /* translators: %s: Documentation URL. */
324 __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
325 __( 'https://wordpress.org/support/article/changing-file-permissions/' )
326 );
327 ?>
328 </em></p>
329 <?php endif; ?>
330 </div>
331
332 <?php wp_print_file_editor_templates(); ?>
333 </form>
334 <?php
335endif; // End if $error.
336?>
337<br class="clear" />
338</div>
339<?php
340$dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
341if ( ! in_array( 'theme_editor_notice', $dismissed_pointers, true ) ) :
342 // Get a back URL.
343 $referer = wp_get_referer();
344
345 $excluded_referer_basenames = array( 'theme-editor.php', 'wp-login.php' );
346
347 if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
348 $return_url = $referer;
349 } else {
350 $return_url = admin_url( '/' );
351 }
352 ?>
353 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
354 <div class="notification-dialog-background"></div>
355 <div class="notification-dialog">
356 <div class="file-editor-warning-content">
357 <div class="file-editor-warning-message">
358 <h1><?php _e( 'Heads up!' ); ?></h1>
359 <p>
360 <?php
361 _e( 'You appear to be making direct edits to your theme in the WordPress dashboard. We recommend that you don&#8217;t! Editing your theme directly could break your site and your changes may be lost in future updates.' );
362 ?>
363 </p>
364 <?php
365 if ( ! $theme->parent() ) {
366 echo '<p>';
367 echo sprintf(
368 /* translators: %s: Link to documentation on child themes. */
369 __( 'If you need to tweak more than your theme&#8217;s CSS, you might want to try <a href="%s">making a child theme</a>.' ),
370 esc_url( __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) )
371 );
372 echo '</p>';
373 }
374 ?>
375 <p><?php _e( 'If you decide to go ahead with direct edits anyway, use a file manager to create a copy with a new name and hang on to the original. That way, you can re-enable a functional version if something goes wrong.' ); ?></p>
376 </div>
377 <p>
378 <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
379 <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
380 </p>
381 </div>
382 </div>
383 </div>
384 <?php
385endif; // Editor warning notice.
386
387require_once ABSPATH . 'wp-admin/admin-footer.php';