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