Keine Beschreibung

theme-editor.php 15KB

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