[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/ -> theme-editor.php (source)

   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', 'error', 'file', 'theme' ) );
  46  
  47  if ( $theme )
  48      $stylesheet = urldecode( $theme );
  49  else
  50      $stylesheet = get_stylesheet();
  51  
  52  $theme = wp_get_theme( $stylesheet );
  53  
  54  if ( ! $theme->exists() )
  55      wp_die( __( 'The requested theme does not exist.' ) );
  56  
  57  if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() )
  58      wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
  59  
  60  $allowed_files = $theme->get_files( 'php', 1 );
  61  $has_templates = ! empty( $allowed_files );
  62  $style_files = $theme->get_files( 'css' );
  63  $allowed_files['style.css'] = $style_files['style.css'];
  64  $allowed_files += $style_files;
  65  
  66  if ( empty( $file ) ) {
  67      $relative_file = 'style.css';
  68      $file = $allowed_files['style.css'];
  69  } else {
  70      $relative_file = urldecode( stripslashes( $file ) );
  71      $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
  72  }
  73  
  74  validate_file_to_edit( $file, $allowed_files );
  75  $scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0;
  76  
  77  switch( $action ) {
  78  case 'update':
  79      check_admin_referer( 'edit-theme_' . $file . $stylesheet );
  80      $newcontent = stripslashes( $_POST['newcontent'] );
  81      $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto;
  82      if ( is_writeable( $file ) ) {
  83          //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
  84          $f = fopen( $file, 'w+' );
  85          if ( $f !== false ) {
  86              fwrite( $f, $newcontent );
  87              fclose( $f );
  88              $location .= '&updated=true';
  89              $theme->cache_delete();
  90          }
  91      }
  92      wp_redirect( $location );
  93      exit;
  94  break;
  95  
  96  default:
  97  
  98      require_once ( ABSPATH . 'wp-admin/admin-header.php' );
  99  
 100      update_recently_edited( $file );
 101  
 102      if ( ! is_file( $file ) )
 103          $error = true;
 104  
 105      $content = '';
 106      if ( ! $error && filesize( $file ) > 0 ) {
 107          $f = fopen($file, 'r');
 108          $content = fread($f, filesize($file));
 109  
 110          if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
 111              $functions = wp_doc_link_parse( $content );
 112  
 113              $docs_select = '<select name="docs-list" id="docs-list">';
 114              $docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
 115              foreach ( $functions as $function ) {
 116                  $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
 117              }
 118              $docs_select .= '</select>';
 119          }
 120  
 121          $content = esc_textarea( $content );
 122      }
 123  
 124      ?>
 125  <?php if ( isset( $_GET['updated'] ) ) : ?>
 126   <div id="message" class="updated"><p><?php _e( 'File edited successfully.' ) ?></p></div>
 127  <?php endif;
 128  
 129  $description = get_file_description( $file );
 130  $file_show = array_search( $file, array_filter( $allowed_files ) );
 131  if ( $description != $file_show )
 132      $description .= ' <span>(' . $file_show . ')</span>';
 133  ?>
 134  <div class="wrap">
 135  <?php screen_icon(); ?>
 136  <h2><?php echo esc_html( $title ); ?></h2>
 137  
 138  <div class="fileedit-sub">
 139  <div class="alignleft">
 140  <h3><?php echo $theme->display('Name'); if ( $description ) echo ': ' . $description; ?></h3>
 141  </div>
 142  <div class="alignright">
 143      <form action="theme-editor.php" method="post">
 144          <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
 145          <select name="theme" id="theme">
 146  <?php
 147  foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
 148      if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() )
 149          continue;
 150  
 151      $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
 152      echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
 153  }
 154  ?>
 155          </select>
 156          <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
 157      </form>
 158  </div>
 159  <br class="clear" />
 160  </div>
 161  <?php
 162  if ( $theme->errors() )
 163      echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
 164  ?>
 165      <div id="templateside">
 166  <?php
 167  if ( $allowed_files ) :
 168      if ( $has_templates || $theme->parent() ) :
 169  ?>
 170      <h3><?php _e('Templates'); ?></h3>
 171      <?php if ( $theme->parent() ) : ?>
 172      <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), '<a href="' . self_admin_url('theme-editor.php?theme=' . urlencode( $theme->get_template() ) ) . '">' . $theme->parent()->display('Name') . '</a>' ); ?></p>
 173      <?php endif; ?>
 174      <ul>
 175  <?php
 176      endif;
 177  
 178      foreach ( $allowed_files as $filename => $absolute_filename ) :
 179          if ( 'style.css' == $filename )
 180              echo "\t</ul>\n\t<h3>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h3>\n\t<ul>\n";
 181  
 182          $file_description = get_file_description( $absolute_filename );
 183          if ( $file_description != basename( $filename ) )
 184              $file_description .= '<br /><span class="nonessential">(' . $filename . ')</span>';
 185  
 186          if ( $absolute_filename == $file )
 187              $file_description = '<span class="highlight">' . $file_description . '</span>';
 188  ?>
 189          <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
 190  <?php
 191      endforeach;
 192  ?>
 193  </ul>
 194  <?php endif; ?>
 195  </div>
 196  <?php if ( $error ) :
 197      echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
 198  else : ?>
 199      <form name="template" id="template" action="theme-editor.php" method="post">
 200      <?php wp_nonce_field( 'edit-theme_' . $file . $stylesheet ); ?>
 201           <div><textarea cols="70" rows="30" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
 202           <input type="hidden" name="action" value="update" />
 203           <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
 204           <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
 205           <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
 206           </div>
 207      <?php if ( ! empty( $functions ) ) : ?>
 208          <div id="documentation" class="hide-if-no-js">
 209          <label for="docs-list"><?php _e('Documentation:') ?></label>
 210          <?php echo $docs_select; ?>
 211          <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'); }" />
 212          </div>
 213      <?php endif; ?>
 214  
 215          <div>
 216          <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
 217              <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
 218              <?php _e( 'This is a file in your current parent theme.' ); ?></p>
 219          <?php endif; ?>
 220  <?php
 221      if ( is_writeable( $file ) ) :
 222          submit_button( __( 'Update File' ), 'primary', 'submit', true, array( 'tabindex' => '2' ) );
 223      else : ?>
 224  <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>
 225  <?php endif; ?>
 226          </div>
 227      </form>
 228  <?php
 229  endif; // $error
 230  ?>
 231  <br class="clear" />
 232  </div>
 233  <script type="text/javascript">
 234  /* <![CDATA[ */
 235  jQuery(document).ready(function($){
 236      $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
 237      $('#newcontent').scrollTop( $('#scrollto').val() );
 238  });
 239  /* ]]> */
 240  </script>
 241  <?php
 242  break;
 243  }
 244  
 245  include (ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.