]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/theme-editor.php
Wordpress 3.0.2
[autoinstalls/wordpress.git] / wp-admin / theme-editor.php
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 ( !current_user_can('edit_themes') )
13         wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site.').'</p>');
14
15 $title = __("Edit Themes");
16 $parent_file = 'themes.php';
17
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 $help .= '<p><strong>' . __('For more information:') . '</strong></p>';
25 $help .= '<p>' . __('<a href="http://codex.wordpress.org/Theme_Development" target="_blank">Documentation on Theme Development</a>') . '</p>';
26 $help .= '<p>' . __('<a href="http://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>') . '</p>';
27 $help .= '<p>' . __('<a href="http://codex.wordpress.org/Editing_Files" target="_blank">Documentation on Editing Files</a>') . '</p>';
28 $help .= '<p>' . __('<a href="http://codex.wordpress.org/Template_Tags" target="_blank">Documentation on Template Tags</a>') . '</p>';
29 $help .= '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>';
30 add_contextual_help($current_screen, $help);
31
32 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
33
34 wp_admin_css( 'theme-editor' );
35
36 $themes = get_themes();
37
38 if (empty($theme)) {
39         $theme = get_current_theme();
40 } else {
41         $theme = stripslashes($theme);
42 }
43
44 if ( ! isset($themes[$theme]) )
45         wp_die(__('The requested theme does not exist.'));
46
47 $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
48
49 if (empty($file)) {
50         $file = $allowed_files[0];
51 } else {
52         $file = stripslashes($file);
53         if ( 'theme' == $dir ) {
54                 $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ;
55         } else if ( 'style' == $dir) {
56                 $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ;
57         }
58 }
59
60 validate_file_to_edit($file, $allowed_files);
61 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
62 $file_show = basename( $file );
63
64 switch($action) {
65
66 case 'update':
67
68         check_admin_referer('edit-theme_' . $file . $theme);
69
70         $newcontent = stripslashes($_POST['newcontent']);
71         $theme = urlencode($theme);
72         if (is_writeable($file)) {
73                 //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
74                 $f = fopen($file, 'w+');
75                 if ($f !== FALSE) {
76                         fwrite($f, $newcontent);
77                         fclose($f);
78                         $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
79                 } else {
80                         $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
81                 }
82         } else {
83                 $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
84         }
85
86         $location = wp_kses_no_null($location);
87         $strip = array('%0d', '%0a', '%0D', '%0A');
88         $location = _deep_replace($strip, $location);
89         header("Location: $location");
90         exit();
91
92 break;
93
94 default:
95
96         require_once('./admin-header.php');
97
98         update_recently_edited($file);
99
100         if ( !is_file($file) )
101                 $error = 1;
102
103         if ( !$error && filesize($file) > 0 ) {
104                 $f = fopen($file, 'r');
105                 $content = fread($f, filesize($file));
106
107                 if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
108                         $functions = wp_doc_link_parse( $content );
109
110                         $docs_select = '<select name="docs-list" id="docs-list">';
111                         $docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
112                         foreach ( $functions as $function ) {
113                                 $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
114                         }
115                         $docs_select .= '</select>';
116                 }
117
118                 $content = htmlspecialchars( $content );
119         }
120
121         ?>
122 <?php if (isset($_GET['a'])) : ?>
123  <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
124 <?php endif;
125
126 $description = get_file_description($file);
127 $desc_header = ( $description != $file_show ) ? "<strong>$description</strong> (%s)" : "%s";
128 ?>
129 <div class="wrap">
130 <?php screen_icon(); ?>
131 <h2><?php echo esc_html( $title ); ?></h2>
132
133 <div class="fileedit-sub">
134 <div class="alignleft">
135 <big><?php echo sprintf($desc_header, $file_show); ?></big>
136 </div>
137 <div class="alignright">
138         <form action="theme-editor.php" method="post">
139                 <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
140                 <select name="theme" id="theme">
141 <?php
142         foreach ($themes as $a_theme) {
143         $theme_name = $a_theme['Name'];
144         if ($theme_name == $theme) $selected = " selected='selected'";
145         else $selected = '';
146         $theme_name = esc_attr($theme_name);
147         echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
148 }
149 ?>
150                 </select>
151                 <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
152         </form>
153 </div>
154 <br class="clear" />
155 </div>
156         <div id="templateside">
157
158 <?php
159 if ($allowed_files) :
160 ?>
161         <h3><?php _e('Templates'); ?></h3>
162         <ul>
163 <?php
164         $template_mapping = array();
165         $template_dir = $themes[$theme]['Template Dir'];
166         foreach ( $themes[$theme]['Template Files'] as $template_file ) {
167                 $description = trim( get_file_description($template_file) );
168                 $template_show = basename($template_file);
169                 $filedesc = ( $description != $template_file ) ? "$description<br /><span class='nonessential'>($template_show)</span>" : "$description";
170                 $filedesc = ( $template_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($template_show)</span></span>" : $filedesc;
171
172                 // If we have two files of the same name prefer the one in the Template Directory
173                 // This means that we display the correct files for child themes which overload Templates as well as Styles
174                 if ( array_key_exists($description, $template_mapping ) ) {
175                         if ( false !== strpos( $template_file, $template_dir ) )  {
176                                 $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
177                         }
178                 } else {
179                         $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
180                 }
181         }
182         ksort( $template_mapping );
183         while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
184         ?>
185                 <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>
186 <?php endwhile; ?>
187         </ul>
188         <h3><?php /* translators: Theme stylesheets in theme editor */ _ex('Styles', 'Theme stylesheets in theme editor'); ?></h3>
189         <ul>
190 <?php
191         $template_mapping = array();
192         $stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
193         foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
194                 $description = trim( get_file_description($style_file) );
195                 $style_show = basename($style_file);
196                 $filedesc = ( $description != $style_file ) ? "$description<br /><span class='nonessential'>($style_show)</span>" : "$description";
197                 $filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($style_show)</span></span>" : $filedesc;
198                 $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
199         }
200         ksort( $template_mapping );
201         while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
202                 ?>
203                 <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>
204 <?php endwhile; ?>
205         </ul>
206 <?php endif; ?>
207 </div>
208 <?php if (!$error) { ?>
209         <form name="template" id="template" action="theme-editor.php" method="post">
210         <?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
211                  <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
212                  <input type="hidden" name="action" value="update" />
213                  <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
214                  <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
215                  <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
216                  </div>
217         <?php if ( isset($functions ) && count($functions) ) { ?>
218                 <div id="documentation" class="hide-if-no-js">
219                 <label for="docs-list"><?php _e('Documentation:') ?></label>
220                 <?php echo $docs_select; ?>
221                 <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'); }" />
222                 </div>
223         <?php } ?>
224
225                 <div>
226 <?php if ( is_writeable($file) ) : ?>
227                         <p class="submit">
228 <?php
229         echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
230 ?>
231 </p>
232 <?php else : ?>
233 <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>
234 <?php endif; ?>
235                 </div>
236         </form>
237 <?php
238         } else {
239                 echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
240         }
241 ?>
242 <br class="clear" />
243 </div>
244 <script type="text/javascript">
245 /* <![CDATA[ */
246 jQuery(document).ready(function($){
247         $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
248         $('#newcontent').scrollTop( $('#scrollto').val() );
249 });
250 /* ]]> */
251 </script>
252 <?php
253 break;
254 }
255
256 include("./admin-footer.php");