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