]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/theme-editor.php
Wordpress 2.9.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 blog.').'</p>');
14
15 $title = __("Edit Themes");
16 $parent_file = 'themes.php';
17
18 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
19
20 wp_admin_css( 'theme-editor' );
21
22 $themes = get_themes();
23
24 if (empty($theme)) {
25         $theme = get_current_theme();
26 } else {
27         $theme = stripslashes($theme);
28 }
29
30 if ( ! isset($themes[$theme]) )
31         wp_die(__('The requested theme does not exist.'));
32
33 $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
34
35 if (empty($file)) {
36         $file = $allowed_files[0];
37 } else {
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 ; 
43         }
44 }
45
46 validate_file_to_edit($file, $allowed_files);
47 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
48 $file_show = basename( $file );
49
50 switch($action) {
51
52 case 'update':
53
54         check_admin_referer('edit-theme_' . $file . $theme);
55
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+');
61                 if ($f !== FALSE) {
62                         fwrite($f, $newcontent);
63                         fclose($f);
64                         $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
65                 } else {
66                         $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
67                 }
68         } else {
69                 $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
70         }
71
72         $location = wp_kses_no_null($location);
73         $strip = array('%0d', '%0a', '%0D', '%0A');
74         $location = _deep_replace($strip, $location);
75         header("Location: $location");
76         exit();
77
78 break;
79
80 default:
81
82         require_once('admin-header.php');
83
84         update_recently_edited($file);
85
86         if ( !is_file($file) )
87                 $error = 1;
88
89         if ( !$error && filesize($file) > 0 ) {
90                 $f = fopen($file, 'r');
91                 $content = fread($f, filesize($file));
92
93                 if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
94                         $functions = wp_doc_link_parse( $content );
95
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>';
100                         }
101                         $docs_select .= '</select>';
102                 }
103
104                 $content = htmlspecialchars( $content );
105                 $codepress_lang = codepress_get_lang($file);
106         }
107
108         ?>
109 <?php if (isset($_GET['a'])) : ?>
110  <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
111 <?php endif;
112
113 $description = get_file_description($file);
114 $desc_header = ( $description != $file_show ) ? "<strong>$description</strong> (%s)" : "%s";
115 ?>
116 <div class="wrap">
117 <?php screen_icon(); ?>
118 <h2><?php echo esc_html( $title ); ?></h2>
119
120 <div class="fileedit-sub">
121 <div class="alignleft">
122 <big><?php echo sprintf($desc_header, $file_show); ?></big>
123 </div>
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">
128 <?php
129         foreach ($themes as $a_theme) {
130         $theme_name = $a_theme['Name'];
131         if ($theme_name == $theme) $selected = " selected='selected'";
132         else $selected = '';
133         $theme_name = esc_attr($theme_name);
134         echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
135 }
136 ?>
137                 </select>
138                 <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
139         </form>
140 </div>
141 <br class="clear" />
142 </div>
143         <div id="templateside">
144         <h3><?php _e("Theme Files"); ?></h3>
145
146 <?php
147 if ($allowed_files) :
148 ?>
149         <h4><?php _e('Templates'); ?></h4>
150         <ul>
151 <?php
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;
159
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 );
165                         }
166                 } else {
167                         $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
168                 }
169         }
170         ksort( $template_mapping );
171         while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
172         ?>
173                 <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>
174 <?php endwhile; ?>
175         </ul>
176         <h4><?php /* translators: Theme stylesheets in theme editor */ echo _x('Styles', 'Theme stylesheets in theme editor'); ?></h4>
177         <ul>
178 <?php
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 );
187         }
188         ksort( $template_mapping );
189         while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
190                 ?>
191                 <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>
192 <?php endwhile; ?>
193         </ul>
194 <?php endif; ?>
195 </div>
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; ?>" />
204                  </div>
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'); }" />
210                 </div>
211         <?php } ?>
212
213                 <div>
214 <?php if ( is_writeable($file) ) : ?>
215                         <p class="submit">
216 <?php
217         echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
218 ?>
219 </p>
220 <?php else : ?>
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>
222 <?php endif; ?>
223                 </div>
224         </form>
225 <?php
226         } else {
227                 echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
228         }
229 ?>
230 <br class="clear" />
231 </div>
232 <script type="text/javascript">
233 /* <![CDATA[ */
234 jQuery(document).ready(function($){
235         $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
236         $('#newcontent').scrollTop( $('#scrollto').val() );
237 });
238 /* ]]> */
239 </script>
240 <?php
241 break;
242 }
243
244 include("admin-footer.php");