web/wp-admin/theme-editor.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
     7  */
     7  */
     8 
     8 
     9 /** WordPress Administration Bootstrap */
     9 /** WordPress Administration Bootstrap */
    10 require_once('admin.php');
    10 require_once('admin.php');
    11 
    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 
    12 $title = __("Edit Themes");
    15 $title = __("Edit Themes");
    13 $parent_file = 'themes.php';
    16 $parent_file = 'themes.php';
    14 
    17 
    15 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme'));
    18 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
    16 
    19 
    17 wp_admin_css( 'theme-editor' );
    20 wp_admin_css( 'theme-editor' );
    18 
    21 
    19 $themes = get_themes();
    22 $themes = get_themes();
    20 
    23 
    29 
    32 
    30 $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
    33 $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
    31 
    34 
    32 if (empty($file)) {
    35 if (empty($file)) {
    33 	$file = $allowed_files[0];
    36 	$file = $allowed_files[0];
    34 }
    37 } else {
    35 
    38 	$file = stripslashes($file);
    36 $file = validate_file_to_edit($file, $allowed_files);
    39 	if ( 'theme' == $dir ) {
    37 $real_file = get_real_file_to_edit($file);
    40 		$file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ; 
    38 
    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;
    39 $file_show = basename( $file );
    48 $file_show = basename( $file );
    40 
    49 
    41 switch($action) {
    50 switch($action) {
    42 
    51 
    43 case 'update':
    52 case 'update':
    44 
    53 
    45 	check_admin_referer('edit-theme_' . $file . $theme);
    54 	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 
    55 
    50 	$newcontent = stripslashes($_POST['newcontent']);
    56 	$newcontent = stripslashes($_POST['newcontent']);
    51 	$theme = urlencode($theme);
    57 	$theme = urlencode($theme);
    52 	if (is_writeable($real_file)) {
    58 	if (is_writeable($file)) {
    53 		//is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
    59 		//is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
    54 		$f = fopen($real_file, 'w+');
    60 		$f = fopen($file, 'w+');
    55 		if ($f !== FALSE) {
    61 		if ($f !== FALSE) {
    56 			fwrite($f, $newcontent);
    62 			fwrite($f, $newcontent);
    57 			fclose($f);
    63 			fclose($f);
    58 			$location = "theme-editor.php?file=$file&theme=$theme&a=te";
    64 			$location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
    59 		} else {
    65 		} else {
    60 			$location = "theme-editor.php?file=$file&theme=$theme";
    66 			$location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
    61 		}
    67 		}
    62 	} else {
    68 	} else {
    63 		$location = "theme-editor.php?file=$file&theme=$theme";
    69 		$location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
    64 	}
    70 	}
    65 
    71 
    66 	$location = wp_kses_no_null($location);
    72 	$location = wp_kses_no_null($location);
    67 	$strip = array('%0d', '%0a', '%0D', '%0A');
    73 	$strip = array('%0d', '%0a', '%0D', '%0A');
    68 	$location = _deep_replace($strip, $location);
    74 	$location = _deep_replace($strip, $location);
    71 
    77 
    72 break;
    78 break;
    73 
    79 
    74 default:
    80 default:
    75 
    81 
    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');
    82 	require_once('admin-header.php');
    80 
    83 
    81 	update_recently_edited($file);
    84 	update_recently_edited($file);
    82 
    85 
    83 	if ( !is_file($real_file) )
    86 	if ( !is_file($file) )
    84 		$error = 1;
    87 		$error = 1;
    85 
    88 
    86 	if ( !$error && filesize($real_file) > 0 ) {
    89 	if ( !$error && filesize($file) > 0 ) {
    87 		$f = fopen($real_file, 'r');
    90 		$f = fopen($file, 'r');
    88 		$content = fread($f, filesize($real_file));
    91 		$content = fread($f, filesize($file));
    89 
    92 
    90 		if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
    93 		if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
    91 			$functions = wp_doc_link_parse( $content );
    94 			$functions = wp_doc_link_parse( $content );
    92 
    95 
    93 			$docs_select = '<select name="docs-list" id="docs-list">';
    96 			$docs_select = '<select name="docs-list" id="docs-list">';
    94 			$docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
    97 			$docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
    95 			foreach ( $functions as $function ) {
    98 			foreach ( $functions as $function ) {
    97 			}
   100 			}
    98 			$docs_select .= '</select>';
   101 			$docs_select .= '</select>';
    99 		}
   102 		}
   100 
   103 
   101 		$content = htmlspecialchars( $content );
   104 		$content = htmlspecialchars( $content );
   102 		$codepress_lang = codepress_get_lang($real_file);
   105 		$codepress_lang = codepress_get_lang($file);
   103 	}
   106 	}
   104 
   107 
   105 	?>
   108 	?>
   106 <?php if (isset($_GET['a'])) : ?>
   109 <?php if (isset($_GET['a'])) : ?>
   107  <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
   110  <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
   135 		<input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
   138 		<input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
   136 	</form>
   139 	</form>
   137 </div>
   140 </div>
   138 <br class="clear" />
   141 <br class="clear" />
   139 </div>
   142 </div>
   140 
   143 	<div id="templateside">
   141 <div id="templateside">
       
   142 	<h3><?php _e("Theme Files"); ?></h3>
   144 	<h3><?php _e("Theme Files"); ?></h3>
   143 
   145 
   144 <?php
   146 <?php
   145 if ($allowed_files) :
   147 if ($allowed_files) :
   146 ?>
   148 ?>
   157 
   159 
   158 		// If we have two files of the same name prefer the one in the Template Directory
   160 		// 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
   161 		// 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 ) ) {
   162 		if( array_key_exists($description, $template_mapping ) ) {
   161 			if ( false !== strpos( $template_file, $template_dir ) )  {
   163 			if ( false !== strpos( $template_file, $template_dir ) )  {
   162 				$template_mapping[ $description ] = array( $template_file, $filedesc );
   164 				$template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
   163 			}
   165 			}
   164 		} else {
   166 		} else {
   165 			$template_mapping[ $description ] = array( $template_file, $filedesc );
   167 			$template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
   166 		}
   168 		}
   167 	}
   169 	}
   168 	ksort( $template_mapping );
   170 	ksort( $template_mapping );
   169 	while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
   171 	while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
   170 	?>
   172 	?>
   171 		<li><a href="theme-editor.php?file=<?php echo "$template_file"; ?>&amp;theme=<?php echo urlencode($theme) ?>"><?php echo $filedesc ?></a></li>
   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>
   172 <?php endwhile; ?>
   174 <?php endwhile; ?>
   173 	</ul>
   175 	</ul>
   174 	<h4><?php /* translators: Theme stylesheets in theme editor */ echo _x('Styles', 'Theme stylesheets in theme editor'); ?></h4>
   176 	<h4><?php /* translators: Theme stylesheets in theme editor */ echo _x('Styles', 'Theme stylesheets in theme editor'); ?></h4>
   175 	<ul>
   177 	<ul>
   176 <?php
   178 <?php
   177 	$template_mapping = array();
   179 	$template_mapping = array();
       
   180 	$stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
   178 	foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
   181 	foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
   179 		$description = trim( get_file_description($style_file) );
   182 		$description = trim( get_file_description($style_file) );
   180 		$style_show = basename($style_file);
   183 		$style_show = basename($style_file);
   181 		$filedesc = ( $description != $style_file ) ? "$description <span class='nonessential'>($style_show)</span>" : "$description";
   184 		$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;
   185 		$filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description <span class='nonessential'>($style_show)</span></span>" : $filedesc;
   183 		$template_mapping[ $description ] = array( $style_file, $filedesc );
   186 		$template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
   184 	}
   187 	}
   185 	ksort( $template_mapping );
   188 	ksort( $template_mapping );
   186 	while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
   189 	while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
   187 		?>
   190 		?>
   188 		<li><a href="theme-editor.php?file=<?php echo "$style_file"; ?>&amp;theme=<?php echo urlencode($theme) ?>"><?php echo $filedesc ?></a></li>
   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>
   189 <?php endwhile; ?>
   192 <?php endwhile; ?>
   190 	</ul>
   193 	</ul>
   191 <?php endif; ?>
   194 <?php endif; ?>
   192 </div>
   195 </div>
   193 
       
   194 <?php if (!$error) { ?>
   196 <?php if (!$error) { ?>
   195 <form name="template" id="template" action="theme-editor.php" method="post">
   197 	<form name="template" id="template" action="theme-editor.php" method="post">
   196 	<?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
   198 	<?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>
   199 		 <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" />
   200 		 <input type="hidden" name="action" value="update" />
   199 		 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
   201 		 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
   200 		 <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
   202 		 <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
       
   203 		 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
   201 		 </div>
   204 		 </div>
   202 	<?php if ( isset($functions ) && count($functions) ) { ?>
   205 	<?php if ( isset($functions ) && count($functions) ) { ?>
   203 		<div id="documentation">
   206 		<div id="documentation">
   204 		<label for="docs-list"><?php _e('Documentation:') ?></label>
   207 		<label for="docs-list"><?php _e('Documentation:') ?></label>
   205 		<?php echo $docs_select; ?>
   208 		<?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'); }" />
   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'); }" />
   207 		</div>
   210 		</div>
   208 	<?php } ?>
   211 	<?php } ?>
   209 
   212 
   210 		<div>
   213 		<div>
   211 <?php if ( is_writeable($real_file) ) : ?>
   214 <?php if ( is_writeable($file) ) : ?>
   212 			<p class="submit">
   215 			<p class="submit">
   213 <?php
   216 <?php
   214 	echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
   217 	echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
   215 ?>
   218 ?>
   216 </p>
   219 </p>
   217 <?php else : ?>
   220 <?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>
   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>
   219 <?php endif; ?>
   222 <?php endif; ?>
   220 		</div>
   223 		</div>
   221 </form>
   224 	</form>
   222 <?php
   225 <?php
   223 	} else {
   226 	} else {
   224 		echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
   227 		echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
   225 	}
   228 	}
   226 ?>
   229 ?>
   227 <br class="clear" />
   230 <br class="clear" />
   228 </div>
   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>
   229 <?php
   240 <?php
   230 break;
   241 break;
   231 }
   242 }
   232 
   243 
   233 include("admin-footer.php");
   244 include("admin-footer.php");