3 * Theme editor administration panel.
6 * @subpackage Administration
9 /** WordPress Administration Bootstrap */
10 require_once('admin.php');
12 if ( !current_user_can('edit_themes') )
13 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this blog.').'</p>');
15 $title = __("Edit Themes");
16 $parent_file = 'themes.php';
18 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
20 wp_admin_css( 'theme-editor' );
22 $themes = get_themes();
25 $theme = get_current_theme();
27 $theme = stripslashes($theme);
30 if ( ! isset($themes[$theme]) )
31 wp_die(__('The requested theme does not exist.'));
33 $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
36 $file = $allowed_files[0];
38 $file = stripslashes($file);
39 if ( 'theme' == $dir ) {
40 $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ;
41 } else if ( 'style' == $dir) {
42 $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ;
46 validate_file_to_edit($file, $allowed_files);
47 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
48 $file_show = basename( $file );
54 check_admin_referer('edit-theme_' . $file . $theme);
56 $newcontent = stripslashes($_POST['newcontent']);
57 $theme = urlencode($theme);
58 if (is_writeable($file)) {
59 //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
60 $f = fopen($file, 'w+');
62 fwrite($f, $newcontent);
64 $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
66 $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
69 $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
72 $location = wp_kses_no_null($location);
73 $strip = array('%0d', '%0a', '%0D', '%0A');
74 $location = _deep_replace($strip, $location);
75 header("Location: $location");
82 require_once('admin-header.php');
84 update_recently_edited($file);
86 if ( !is_file($file) )
89 if ( !$error && filesize($file) > 0 ) {
90 $f = fopen($file, 'r');
91 $content = fread($f, filesize($file));
93 if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
94 $functions = wp_doc_link_parse( $content );
96 $docs_select = '<select name="docs-list" id="docs-list">';
97 $docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
98 foreach ( $functions as $function ) {
99 $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
101 $docs_select .= '</select>';
104 $content = htmlspecialchars( $content );
105 $codepress_lang = codepress_get_lang($file);
109 <?php if (isset($_GET['a'])) : ?>
110 <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
113 $description = get_file_description($file);
114 $desc_header = ( $description != $file_show ) ? "<strong>$description</strong> (%s)" : "%s";
117 <?php screen_icon(); ?>
118 <h2><?php echo esc_html( $title ); ?></h2>
120 <div class="fileedit-sub">
121 <div class="alignleft">
122 <big><?php echo sprintf($desc_header, $file_show); ?></big>
124 <div class="alignright">
125 <form action="theme-editor.php" method="post">
126 <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
127 <select name="theme" id="theme">
129 foreach ($themes as $a_theme) {
130 $theme_name = $a_theme['Name'];
131 if ($theme_name == $theme) $selected = " selected='selected'";
133 $theme_name = esc_attr($theme_name);
134 echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
138 <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
143 <div id="templateside">
144 <h3><?php _e("Theme Files"); ?></h3>
147 if ($allowed_files) :
149 <h4><?php _e('Templates'); ?></h4>
152 $template_mapping = array();
153 $template_dir = $themes[$theme]['Template Dir'];
154 foreach ( $themes[$theme]['Template Files'] as $template_file ) {
155 $description = trim( get_file_description($template_file) );
156 $template_show = basename($template_file);
157 $filedesc = ( $description != $template_file ) ? "$description <span class='nonessential'>($template_show)</span>" : "$description";
158 $filedesc = ( $template_file == $file ) ? "<span class='highlight'>$description <span class='nonessential'>($template_show)</span></span>" : $filedesc;
160 // If we have two files of the same name prefer the one in the Template Directory
161 // This means that we display the correct files for child themes which overload Templates as well as Styles
162 if( array_key_exists($description, $template_mapping ) ) {
163 if ( false !== strpos( $template_file, $template_dir ) ) {
164 $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
167 $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
170 ksort( $template_mapping );
171 while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
173 <li><a href="theme-editor.php?file=<?php echo "$template_file"; ?>&theme=<?php echo urlencode($theme) ?>&dir=theme"><?php echo $filedesc ?></a></li>
176 <h4><?php /* translators: Theme stylesheets in theme editor */ echo _x('Styles', 'Theme stylesheets in theme editor'); ?></h4>
179 $template_mapping = array();
180 $stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
181 foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
182 $description = trim( get_file_description($style_file) );
183 $style_show = basename($style_file);
184 $filedesc = ( $description != $style_file ) ? "$description <span class='nonessential'>($style_show)</span>" : "$description";
185 $filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description <span class='nonessential'>($style_show)</span></span>" : $filedesc;
186 $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
188 ksort( $template_mapping );
189 while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
191 <li><a href="theme-editor.php?file=<?php echo "$style_file"; ?>&theme=<?php echo urlencode($theme) ?>&dir=style"><?php echo $filedesc ?></a></li>
196 <?php if (!$error) { ?>
197 <form name="template" id="template" action="theme-editor.php" method="post">
198 <?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
199 <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1" class="codepress <?php echo $codepress_lang ?>"><?php echo $content ?></textarea>
200 <input type="hidden" name="action" value="update" />
201 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
202 <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
203 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
205 <?php if ( isset($functions ) && count($functions) ) { ?>
206 <div id="documentation">
207 <label for="docs-list"><?php _e('Documentation:') ?></label>
208 <?php echo $docs_select; ?>
209 <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() ) + '&locale=<?php echo urlencode( get_locale() ) ?>&version=<?php echo urlencode( $wp_version ) ?>&redirect=true'); }" />
214 <?php if ( is_writeable($file) ) : ?>
217 echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
221 <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>
227 echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
232 <script type="text/javascript">
234 jQuery(document).ready(function($){
235 $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
236 $('#newcontent').scrollTop( $('#scrollto').val() );
244 include("admin-footer.php");