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