PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/theme-editor.php

https://github.com/MikeLockz/lockwitz
PHP | 271 lines | 224 code | 35 blank | 12 comment | 54 complexity | cf091eb3717527c67b38499b36abfe50 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('./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. $help = '<p>' . __('You can use the Theme Editor to edit the individual CSS and PHP files which make up your theme.') . '</p>';
  19. $help .= '<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>';
  20. $help .= '<p>' . __('For PHP files, you can use the Documentation dropdown to select from functions recognized in that file. Lookup takes you to a web page with reference material about that particular function.') . '</p>';
  21. $help .= '<p>' . __('After typing in your edits, click Update File.') . '</p>';
  22. $help .= '<p>' . __('<strong>Advice:</strong> think very carefully about your site crashing if you are live-editing the theme currently in use.') . '</p>';
  23. $help .= '<p>' . __('Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="http://codex.wordpress.org/Child_Themes" target="_blank">child theme</a> instead.') . '</p>';
  24. if ( is_network_admin() )
  25. $help .= '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>';
  26. $help .= '<p><strong>' . __('For more information:') . '</strong></p>';
  27. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Theme_Development" target="_blank">Documentation on Theme Development</a>') . '</p>';
  28. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>') . '</p>';
  29. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Editing_Files" target="_blank">Documentation on Editing Files</a>') . '</p>';
  30. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Template_Tags" target="_blank">Documentation on Template Tags</a>') . '</p>';
  31. $help .= '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>';
  32. add_contextual_help($current_screen, $help);
  33. wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
  34. wp_admin_css( 'theme-editor' );
  35. $themes = get_themes();
  36. if (empty($theme)) {
  37. $theme = get_current_theme();
  38. } else {
  39. $theme = stripslashes($theme);
  40. }
  41. if ( ! isset($themes[$theme]) )
  42. wp_die(__('The requested theme does not exist.'));
  43. $allowed_files = array_merge( $themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files'] );
  44. if ( empty( $file ) ) {
  45. if ( false !== array_search( $themes[$theme]['Stylesheet Dir'] . '/style.css', $allowed_files ) )
  46. $file = $themes[$theme]['Stylesheet Dir'] . '/style.css';
  47. else
  48. $file = $allowed_files[0];
  49. } else {
  50. $file = stripslashes($file);
  51. if ( 'theme' == $dir ) {
  52. $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ;
  53. } else if ( 'style' == $dir) {
  54. $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ;
  55. }
  56. }
  57. validate_file_to_edit($file, $allowed_files);
  58. $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
  59. $file_show = basename( $file );
  60. switch($action) {
  61. case 'update':
  62. check_admin_referer('edit-theme_' . $file . $theme);
  63. $newcontent = stripslashes($_POST['newcontent']);
  64. $theme = urlencode($theme);
  65. if (is_writeable($file)) {
  66. //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
  67. $f = fopen($file, 'w+');
  68. if ($f !== FALSE) {
  69. fwrite($f, $newcontent);
  70. fclose($f);
  71. $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
  72. } else {
  73. $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
  74. }
  75. } else {
  76. $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
  77. }
  78. $location = wp_kses_no_null($location);
  79. $strip = array('%0d', '%0a', '%0D', '%0A');
  80. $location = _deep_replace($strip, $location);
  81. header("Location: $location");
  82. exit();
  83. break;
  84. default:
  85. require_once(ABSPATH . 'wp-admin/admin-header.php');
  86. update_recently_edited($file);
  87. if ( !is_file($file) )
  88. $error = 1;
  89. $content = '';
  90. if ( !$error && filesize($file) > 0 ) {
  91. $f = fopen($file, 'r');
  92. $content = fread($f, filesize($file));
  93. if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
  94. $functions = wp_doc_link_parse( $content );
  95. $docs_select = '<select name="docs-list" id="docs-list">';
  96. $docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
  97. foreach ( $functions as $function ) {
  98. $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
  99. }
  100. $docs_select .= '</select>';
  101. }
  102. $content = esc_textarea( $content );
  103. }
  104. ?>
  105. <?php if (isset($_GET['a'])) : ?>
  106. <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
  107. <?php endif;
  108. $description = get_file_description($file);
  109. $desc_header = ( $description != $file_show ) ? "$description <span>($file_show)</span>" : $file_show;
  110. $is_child_theme = $themes[$theme]['Template'] != $themes[$theme]['Stylesheet'];
  111. ?>
  112. <div class="wrap">
  113. <?php screen_icon(); ?>
  114. <h2><?php echo esc_html( $title ); ?></h2>
  115. <div class="fileedit-sub">
  116. <div class="alignleft">
  117. <h3><?php echo $themes[$theme]['Name'] . ': ' . $desc_header; ?></h3>
  118. </div>
  119. <div class="alignright">
  120. <form action="theme-editor.php" method="post">
  121. <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
  122. <select name="theme" id="theme">
  123. <?php
  124. foreach ($themes as $a_theme) {
  125. $theme_name = $a_theme['Name'];
  126. if ($theme_name == $theme) $selected = " selected='selected'";
  127. else $selected = '';
  128. $theme_name = esc_attr($theme_name);
  129. echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
  130. }
  131. ?>
  132. </select>
  133. <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
  134. </form>
  135. </div>
  136. <br class="clear" />
  137. </div>
  138. <div id="templateside">
  139. <?php
  140. if ($allowed_files) :
  141. ?>
  142. <h3><?php _e('Templates'); ?></h3>
  143. <?php if ( $is_child_theme ) : ?>
  144. <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), $themes[$theme]['Parent Theme'] ); ?></p>
  145. <?php endif; ?>
  146. <ul>
  147. <?php
  148. $template_mapping = array();
  149. $template_dir = $themes[$theme]['Template Dir'];
  150. foreach ( $themes[$theme]['Template Files'] as $template_file ) {
  151. // Don't show parent templates.
  152. if ( $is_child_theme && strpos( $template_file, trailingslashit( $template_dir ) ) === 0 )
  153. continue;
  154. $description = trim( get_file_description($template_file) );
  155. $template_show = basename($template_file);
  156. $filedesc = ( $description != $template_file ) ? "$description<br /><span class='nonessential'>($template_show)</span>" : "$description";
  157. $filedesc = ( $template_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($template_show)</span></span>" : $filedesc;
  158. $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
  159. }
  160. ksort( $template_mapping );
  161. while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
  162. ?>
  163. <li><a href="theme-editor.php?file=<?php echo urlencode( $template_file ) ?>&amp;theme=<?php echo urlencode( $theme ) ?>&amp;dir=theme"><?php echo $filedesc ?></a></li>
  164. <?php endwhile; ?>
  165. </ul>
  166. <h3><?php /* translators: Theme stylesheets in theme editor */ _ex('Styles', 'Theme stylesheets in theme editor'); ?></h3>
  167. <ul>
  168. <?php
  169. $template_mapping = array();
  170. $stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
  171. foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
  172. // Don't show parent styles.
  173. if ( $is_child_theme && strpos( $style_file, trailingslashit( $template_dir ) ) === 0 )
  174. continue;
  175. $description = trim( get_file_description($style_file) );
  176. $style_show = basename($style_file);
  177. $filedesc = ( $description != $style_file ) ? "$description<br /><span class='nonessential'>($style_show)</span>" : "$description";
  178. $filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($style_show)</span></span>" : $filedesc;
  179. $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
  180. }
  181. ksort( $template_mapping );
  182. while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
  183. ?>
  184. <li><a href="theme-editor.php?file=<?php echo urlencode( $style_file ) ?>&amp;theme=<?php echo urlencode($theme) ?>&amp;dir=style"><?php echo $filedesc ?></a></li>
  185. <?php endwhile; ?>
  186. </ul>
  187. <?php endif; ?>
  188. </div>
  189. <?php if (!$error) { ?>
  190. <form name="template" id="template" action="theme-editor.php" method="post">
  191. <?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
  192. <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
  193. <input type="hidden" name="action" value="update" />
  194. <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
  195. <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
  196. <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
  197. </div>
  198. <?php if ( isset($functions ) && count($functions) ) { ?>
  199. <div id="documentation" class="hide-if-no-js">
  200. <label for="docs-list"><?php _e('Documentation:') ?></label>
  201. <?php echo $docs_select; ?>
  202. <input type="button" class="button" value=" <?php esc_attr_e( 'Lookup' ); ?> " 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'); }" />
  203. </div>
  204. <?php } ?>
  205. <div>
  206. <?php if ( is_child_theme() && ! $is_child_theme && $themes[$theme]['Template'] == get_option('template') ) : ?>
  207. <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
  208. <?php _e( 'This is a file in your current parent theme.' ); ?></p>
  209. <?php endif; ?>
  210. <?php
  211. if ( is_writeable( $file ) ) :
  212. submit_button( __( 'Update File' ), 'primary', 'submit', true, array( 'tabindex' => '2' ) );
  213. else : ?>
  214. <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>
  215. <?php endif; ?>
  216. </div>
  217. </form>
  218. <?php
  219. } else {
  220. echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
  221. }
  222. ?>
  223. <br class="clear" />
  224. </div>
  225. <script type="text/javascript">
  226. /* <![CDATA[ */
  227. jQuery(document).ready(function($){
  228. $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
  229. $('#newcontent').scrollTop( $('#scrollto').val() );
  230. });
  231. /* ]]> */
  232. </script>
  233. <?php
  234. break;
  235. }
  236. include(ABSPATH . "wp-admin/admin-footer.php");