Advertisement
Guest User

theme-editor

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