theme-editor.php 15 KB

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