PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/theme-editor.php

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