PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/theme-editor.php

https://gitlab.com/Blueprint-Marketing/WordPress-1
PHP | 245 lines | 203 code | 32 blank | 10 comment | 46 complexity | 9b513b33cb7e1f82554b00dcccf4c986 MD5 | raw file
  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>'.__('You do not have sufficient permissions 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 Select. A list then appears of all the 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="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.') . '</p>
  26. <p>' . __('After typing in your edits, click Update File.') . '</p>
  27. <p>' . __('<strong>Advice:</strong> think very carefully about your site crashing if you are live-editing the theme currently in use.') . '</p>
  28. <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.'), __('http://codex.wordpress.org/Child_Themes') ) . '</p>' .
  29. ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
  30. ) );
  31. get_current_screen()->set_help_sidebar(
  32. '<p><strong>' . __('For more information:') . '</strong></p>' .
  33. '<p>' . __('<a href="http://codex.wordpress.org/Theme_Development" target="_blank">Documentation on Theme Development</a>') . '</p>' .
  34. '<p>' . __('<a href="http://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>') . '</p>' .
  35. '<p>' . __('<a href="http://codex.wordpress.org/Editing_Files" target="_blank">Documentation on Editing Files</a>') . '</p>' .
  36. '<p>' . __('<a href="http://codex.wordpress.org/Template_Tags" target="_blank">Documentation on Template Tags</a>') . '</p>' .
  37. '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
  38. );
  39. wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
  40. if ( $theme )
  41. $stylesheet = $theme;
  42. else
  43. $stylesheet = get_stylesheet();
  44. $theme = wp_get_theme( $stylesheet );
  45. if ( ! $theme->exists() )
  46. wp_die( __( 'The requested theme does not exist.' ) );
  47. if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() )
  48. wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
  49. $allowed_files = $theme->get_files( 'php', 1 );
  50. $has_templates = ! empty( $allowed_files );
  51. $style_files = $theme->get_files( 'css' );
  52. $allowed_files['style.css'] = $style_files['style.css'];
  53. $allowed_files += $style_files;
  54. if ( empty( $file ) ) {
  55. $relative_file = 'style.css';
  56. $file = $allowed_files['style.css'];
  57. } else {
  58. $relative_file = $file;
  59. $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
  60. }
  61. validate_file_to_edit( $file, $allowed_files );
  62. $scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0;
  63. switch( $action ) {
  64. case 'update':
  65. check_admin_referer( 'edit-theme_' . $file . $stylesheet );
  66. $newcontent = wp_unslash( $_POST['newcontent'] );
  67. $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto;
  68. if ( is_writeable( $file ) ) {
  69. //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
  70. $f = fopen( $file, 'w+' );
  71. if ( $f !== false ) {
  72. fwrite( $f, $newcontent );
  73. fclose( $f );
  74. $location .= '&updated=true';
  75. $theme->cache_delete();
  76. }
  77. }
  78. wp_redirect( $location );
  79. exit;
  80. break;
  81. default:
  82. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  83. update_recently_edited( $file );
  84. if ( ! is_file( $file ) )
  85. $error = true;
  86. $content = '';
  87. if ( ! $error && filesize( $file ) > 0 ) {
  88. $f = fopen($file, 'r');
  89. $content = fread($f, filesize($file));
  90. if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
  91. $functions = wp_doc_link_parse( $content );
  92. $docs_select = '<select name="docs-list" id="docs-list">';
  93. $docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
  94. foreach ( $functions as $function ) {
  95. $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
  96. }
  97. $docs_select .= '</select>';
  98. }
  99. $content = esc_textarea( $content );
  100. }
  101. ?>
  102. <?php if ( isset( $_GET['updated'] ) ) : ?>
  103. <div id="message" class="updated"><p><?php _e( 'File edited successfully.' ) ?></p></div>
  104. <?php endif;
  105. $description = get_file_description( $file );
  106. $file_show = array_search( $file, array_filter( $allowed_files ) );
  107. if ( $description != $file_show )
  108. $description .= ' <span>(' . $file_show . ')</span>';
  109. ?>
  110. <div class="wrap">
  111. <h2><?php echo esc_html( $title ); ?></h2>
  112. <div class="fileedit-sub">
  113. <div class="alignleft">
  114. <h3><?php echo $theme->display('Name'); if ( $description ) echo ': ' . $description; ?></h3>
  115. </div>
  116. <div class="alignright">
  117. <form action="theme-editor.php" method="post">
  118. <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
  119. <select name="theme" id="theme">
  120. <?php
  121. foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
  122. if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() )
  123. continue;
  124. $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
  125. echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
  126. }
  127. ?>
  128. </select>
  129. <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
  130. </form>
  131. </div>
  132. <br class="clear" />
  133. </div>
  134. <?php
  135. if ( $theme->errors() )
  136. echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
  137. ?>
  138. <div id="templateside">
  139. <?php
  140. if ( $allowed_files ) :
  141. if ( $has_templates || $theme->parent() ) :
  142. ?>
  143. <h3><?php _e('Templates'); ?></h3>
  144. <?php if ( $theme->parent() ) : ?>
  145. <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), '<a href="' . self_admin_url('theme-editor.php?theme=' . urlencode( $theme->get_template() ) ) . '">' . $theme->parent()->display('Name') . '</a>' ); ?></p>
  146. <?php endif; ?>
  147. <ul>
  148. <?php
  149. endif;
  150. foreach ( $allowed_files as $filename => $absolute_filename ) :
  151. if ( 'style.css' == $filename )
  152. echo "\t</ul>\n\t<h3>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h3>\n\t<ul>\n";
  153. $file_description = get_file_description( $absolute_filename );
  154. if ( $file_description != basename( $filename ) )
  155. $file_description .= '<br /><span class="nonessential">(' . $filename . ')</span>';
  156. if ( $absolute_filename == $file )
  157. $file_description = '<span class="highlight">' . $file_description . '</span>';
  158. ?>
  159. <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
  160. <?php
  161. endforeach;
  162. ?>
  163. </ul>
  164. <?php endif; ?>
  165. </div>
  166. <?php if ( $error ) :
  167. echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
  168. else : ?>
  169. <form name="template" id="template" action="theme-editor.php" method="post">
  170. <?php wp_nonce_field( 'edit-theme_' . $file . $stylesheet ); ?>
  171. <div><textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
  172. <input type="hidden" name="action" value="update" />
  173. <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
  174. <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
  175. <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
  176. </div>
  177. <?php if ( ! empty( $functions ) ) : ?>
  178. <div id="documentation" class="hide-if-no-js">
  179. <label for="docs-list"><?php _e('Documentation:') ?></label>
  180. <?php echo $docs_select; ?>
  181. <input type="button" class="button" value=" <?php esc_attr_e( 'Look Up' ); ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'http://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'); }" />
  182. </div>
  183. <?php endif; ?>
  184. <div>
  185. <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
  186. <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
  187. <?php _e( 'This is a file in your current parent theme.' ); ?></p>
  188. <?php endif; ?>
  189. <?php
  190. if ( is_writeable( $file ) ) :
  191. submit_button( __( 'Update File' ), 'primary', 'submit', true );
  192. else : ?>
  193. <p><em><?php _e('You need to make this file writable before you can save your changes. See <a href="http://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.'); ?></em></p>
  194. <?php endif; ?>
  195. </div>
  196. </form>
  197. <?php
  198. endif; // $error
  199. ?>
  200. <br class="clear" />
  201. </div>
  202. <script type="text/javascript">
  203. /* <![CDATA[ */
  204. jQuery(document).ready(function($){
  205. $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
  206. $('#newcontent').scrollTop( $('#scrollto').val() );
  207. });
  208. /* ]]> */
  209. </script>
  210. <?php
  211. break;
  212. }
  213. include(ABSPATH . 'wp-admin/admin-footer.php' );