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