[ 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( dirname( __FILE__ ) . '/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>' . __( 'Sorry, you are not allowed to edit templates for this site.' ) . '</p>' );
  19  }
  20  
  21  $title       = __( 'Edit Themes' );
  22  $parent_file = 'themes.php';
  23  
  24  get_current_screen()->add_help_tab(
  25      array(
  26          'id'      => 'overview',
  27          'title'   => __( 'Overview' ),
  28          'content' =>
  29                  '<p>' . __( 'You can use the theme editor to edit the individual CSS and PHP files which make up your theme.' ) . '</p>' .
  30                  '<p>' . __( 'Begin by choosing a theme to edit from the dropdown menu and clicking the Select button. A list then appears of the theme&#8217;s template files. Clicking once on any file name causes the file to appear in the large Editor box.' ) . '</p>' .
  31                  '<p>' . __( 'For PHP files, you can use the Documentation dropdown to select from functions recognized in that file. Look Up takes you to a web page with reference material about that particular function.' ) . '</p>' .
  32                  '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
  33                  '<ul>' .
  34                  '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
  35                  '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
  36                  '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
  37                  '</ul>' .
  38                  '<p>' . __( 'After typing in your edits, click Update File.' ) . '</p>' .
  39                  '<p>' . __( '<strong>Advice:</strong> Think very carefully about your site crashing if you are live-editing the theme currently in use.' ) . '</p>' .
  40                  '<p>' . sprintf(
  41                      /* translators: %s: link to documentation on child themes */
  42                      __( 'Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s">child theme</a> instead.' ),
  43                      __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' )
  44                  ) . '</p>' .
  45                  ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
  46      )
  47  );
  48  
  49  get_current_screen()->set_help_sidebar(
  50      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  51      '<p>' . __( '<a href="https://codex.wordpress.org/Theme_Development">Documentation on Theme Development</a>' ) . '</p>' .
  52      '<p>' . __( '<a href="https://codex.wordpress.org/Using_Themes">Documentation on Using Themes</a>' ) . '</p>' .
  53      '<p>' . __( '<a href="https://codex.wordpress.org/Editing_Files">Documentation on Editing Files</a>' ) . '</p>' .
  54      '<p>' . __( '<a href="https://codex.wordpress.org/Template_Tags">Documentation on Template Tags</a>' ) . '</p>' .
  55      '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  56  );
  57  
  58  wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
  59  
  60  if ( $theme ) {
  61      $stylesheet = $theme;
  62  } else {
  63      $stylesheet = get_stylesheet();
  64  }
  65  
  66  $theme = wp_get_theme( $stylesheet );
  67  
  68  if ( ! $theme->exists() ) {
  69      wp_die( __( 'The requested theme does not exist.' ) );
  70  }
  71  
  72  if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() ) {
  73      wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
  74  }
  75  
  76  $allowed_files = array();
  77  $style_files   = array();
  78  $has_templates = false;
  79  
  80  $file_types = wp_get_theme_file_editable_extensions( $theme );
  81  
  82  foreach ( $file_types as $type ) {
  83      switch ( $type ) {
  84          case 'php':
  85              $allowed_files += $theme->get_files( 'php', -1 );
  86              $has_templates  = ! empty( $allowed_files );
  87              break;
  88          case 'css':
  89              $style_files                = $theme->get_files( 'css', -1 );
  90              $allowed_files['style.css'] = $style_files['style.css'];
  91              $allowed_files             += $style_files;
  92              break;
  93          default:
  94              $allowed_files += $theme->get_files( $type, -1 );
  95              break;
  96      }
  97  }
  98  
  99  // Move functions.php and style.css to the top.
 100  if ( isset( $allowed_files['functions.php'] ) ) {
 101      $allowed_files = array( 'functions.php' => $allowed_files['functions.php'] ) + $allowed_files;
 102  }
 103  if ( isset( $allowed_files['style.css'] ) ) {
 104      $allowed_files = array( 'style.css' => $allowed_files['style.css'] ) + $allowed_files;
 105  }
 106  
 107  if ( empty( $file ) ) {
 108      $relative_file = 'style.css';
 109      $file          = $allowed_files['style.css'];
 110  } else {
 111      $relative_file = wp_unslash( $file );
 112      $file          = $theme->get_stylesheet_directory() . '/' . $relative_file;
 113  }
 114  
 115  validate_file_to_edit( $file, $allowed_files );
 116  
 117  // Handle fallback editing of file when JavaScript is not available.
 118  $edit_error     = null;
 119  $posted_content = null;
 120  if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
 121      $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
 122      if ( is_wp_error( $r ) ) {
 123          $edit_error = $r;
 124          if ( check_ajax_referer( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
 125              $posted_content = wp_unslash( $_POST['newcontent'] );
 126          }
 127      } else {
 128          wp_redirect(
 129              add_query_arg(
 130                  array(
 131                      'a'     => 1, // This means "success" for some reason.
 132                      'theme' => $stylesheet,
 133                      'file'  => $relative_file,
 134                  ),
 135                  admin_url( 'theme-editor.php' )
 136              )
 137          );
 138          exit;
 139      }
 140  }
 141  
 142      $settings = array(
 143          'codeEditor' => wp_enqueue_code_editor( compact( 'file' ) ),
 144      );
 145      wp_enqueue_script( 'wp-theme-plugin-editor' );
 146      wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
 147      wp_add_inline_script( 'wp-theme-plugin-editor', 'wp.themePluginEditor.themeOrPlugin = "theme";' );
 148  
 149      require_once( ABSPATH . 'wp-admin/admin-header.php' );
 150  
 151      update_recently_edited( $file );
 152  
 153      if ( ! is_file( $file ) ) {
 154          $error = true;
 155      }
 156  
 157      $content = '';
 158      if ( ! empty( $posted_content ) ) {
 159          $content = $posted_content;
 160      } elseif ( ! $error && filesize( $file ) > 0 ) {
 161          $f       = fopen( $file, 'r' );
 162          $content = fread( $f, filesize( $file ) );
 163  
 164          if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
 165              $functions = wp_doc_link_parse( $content );
 166  
 167              $docs_select  = '<select name="docs-list" id="docs-list">';
 168              $docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
 169              foreach ( $functions as $function ) {
 170                  $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
 171              }
 172              $docs_select .= '</select>';
 173          }
 174  
 175          $content = esc_textarea( $content );
 176      }
 177  
 178      $file_description = get_file_description( $relative_file );
 179      $file_show        = array_search( $file, array_filter( $allowed_files ) );
 180      $description      = esc_html( $file_description );
 181      if ( $file_description != $file_show ) {
 182          $description .= ' <span>(' . esc_html( $file_show ) . ')</span>';
 183      }
 184      ?>
 185  <div class="wrap">
 186  <h1><?php echo esc_html( $title ); ?></h1>
 187  
 188  <?php if ( isset( $_GET['a'] ) ) : ?>
 189      <div id="message" class="updated notice is-dismissible">
 190          <p><?php _e( 'File edited successfully.' ); ?></p>
 191      </div>
 192  <?php elseif ( is_wp_error( $edit_error ) ) : ?>
 193      <div id="message" class="notice notice-error">
 194          <p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
 195          <pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
 196      </div>
 197  <?php endif; ?>
 198  <?php if ( preg_match( '/\.css$/', $file ) ) : ?>
 199      <div id="message" class="notice-info notice">
 200          <p><strong><?php _e( 'Did you know?' ); ?></strong></p>
 201          <p>
 202              <?php
 203              echo sprintf(
 204                  /* translators: %s: link to Custom CSS section in the Customizer */
 205                  __( 'There&#8217;s no need to change your CSS here &mdash; you can edit and live preview CSS changes in the <a href="%s">built-in CSS editor</a>.' ),
 206                  esc_url( add_query_arg( 'autofocus[section]', 'custom_css', admin_url( 'customize.php' ) ) )
 207              );
 208              ?>
 209          </p>
 210      </div>
 211  <?php endif; ?>
 212  
 213  <div class="fileedit-sub">
 214  <div class="alignleft">
 215  <h2>
 216  <?php
 217  echo $theme->display( 'Name' );
 218  if ( $description ) {
 219      echo ': ' . $description;}
 220  ?>
 221  </h2>
 222  </div>
 223  <div class="alignright">
 224      <form action="theme-editor.php" method="get">
 225          <strong><label for="theme"><?php _e( 'Select theme to edit:' ); ?> </label></strong>
 226          <select name="theme" id="theme">
 227  <?php
 228  foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
 229      if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() ) {
 230          continue;
 231      }
 232  
 233      $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
 234      echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display( 'Name' ) . '</option>';
 235  }
 236  ?>
 237          </select>
 238          <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
 239      </form>
 240  </div>
 241  <br class="clear" />
 242  </div>
 243  <?php
 244  if ( $theme->errors() ) {
 245      echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
 246  }
 247  ?>
 248  <div id="templateside">
 249      <h2 id="theme-files-label"><?php _e( 'Theme Files' ); ?></h2>
 250      <ul role="tree" aria-labelledby="theme-files-label">
 251          <?php if ( ( $has_templates || $theme->parent() ) && $theme->parent() ) : ?>
 252              <li class="howto">
 253                  <?php
 254                  /* translators: %s: link to edit parent theme */
 255                  echo sprintf(
 256                      __( 'This child theme inherits templates from a parent theme, %s.' ),
 257                      sprintf(
 258                          '<a href="%s">%s</a>',
 259                          self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
 260                          $theme->parent()->display( 'Name' )
 261                      )
 262                  );
 263                  ?>
 264              </li>
 265          <?php endif; ?>
 266          <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
 267              <ul role="group">
 268                  <?php wp_print_theme_file_tree( wp_make_theme_file_tree( $allowed_files ) ); ?>
 269              </ul>
 270          </li>
 271      </ul>
 272  </div>
 273  
 274  <?php
 275  if ( $error ) :
 276      echo '<div class="error"><p>' . __( 'File does not exist! Please double check the name and try again.' ) . '</p></div>';
 277  else :
 278      ?>
 279      <form name="template" id="template" action="theme-editor.php" method="post">
 280          <?php wp_nonce_field( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce' ); ?>
 281          <div>
 282              <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
 283              <textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"><?php echo $content; ?></textarea>
 284              <input type="hidden" name="action" value="update" />
 285              <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
 286              <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
 287          </div>
 288      <?php if ( ! empty( $functions ) ) : ?>
 289          <div id="documentation" class="hide-if-no-js">
 290          <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
 291          <?php echo $docs_select; ?>
 292          <input disabled id="docs-lookup" type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ); ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ); ?>&amp;redirect=true'); }" />
 293          </div>
 294      <?php endif; ?>
 295  
 296      <div>
 297          <div class="editor-notices">
 298              <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
 299                  <div class="notice notice-warning inline">
 300                      <p>
 301                          <?php
 302                          if ( is_writeable( $file ) ) {
 303                              ?>
 304                          <strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
 305                          <?php _e( 'This is a file in your current parent theme.' ); ?>
 306                      </p>
 307                  </div>
 308              <?php endif; ?>
 309          </div>
 310      <?php if ( is_writeable( $file ) ) : ?>
 311          <p class="submit">
 312              <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
 313              <span class="spinner"></span>
 314          </p>
 315      <?php else : ?>
 316          <p><em><?php _e( 'You need to make this file writable before you can save your changes. See <a href="https://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.' ); ?></em></p>
 317      <?php endif; ?>
 318      </div>
 319      <?php wp_print_file_editor_templates(); ?>
 320      </form>
 321      <?php
 322  endif; // $error
 323  ?>
 324  <br class="clear" />
 325  </div>
 326  <?php
 327  $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
 328  if ( ! in_array( 'theme_editor_notice', $dismissed_pointers, true ) ) :
 329      // Get a back URL
 330      $referer                    = wp_get_referer();
 331      $excluded_referer_basenames = array( 'theme-editor.php', 'wp-login.php' );
 332  
 333      if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
 334          $return_url = $referer;
 335      } else {
 336          $return_url = admin_url( '/' );
 337      }
 338      ?>
 339  <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
 340      <div class="notification-dialog-background"></div>
 341      <div class="notification-dialog">
 342          <div class="file-editor-warning-content">
 343              <div class="file-editor-warning-message">
 344                  <h1><?php _e( 'Heads up!' ); ?></h1>
 345                  <p>
 346                      <?php
 347                      _e( 'You appear to be making direct edits to your theme in the WordPress dashboard. We recommend that you don&#8217;t! Editing your theme directly could break your site and your changes may be lost in future updates.' );
 348                      ?>
 349                  </p>
 350                      <?php
 351                      if ( ! $theme->parent() ) {
 352                          echo '<p>';
 353                          echo sprintf(
 354                              /* translators: %s: link to documentation on child themes */
 355                              __( 'If you need to tweak more than your theme&#8217;s CSS, you might want to try <a href="%s">making a child theme</a>.' ),
 356                              esc_url( __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) )
 357                          );
 358                          echo '</p>';
 359                      }
 360                      ?>
 361                  <p><?php _e( 'If you decide to go ahead with direct edits anyway, use a file manager to create a copy with a new name and hang on to the original. That way, you can re-enable a functional version if something goes wrong.' ); ?></p>
 362              </div>
 363              <p>
 364                  <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
 365                  <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
 366              </p>
 367          </div>
 368      </div>
 369  </div>
 370      <?php
 371  endif; // editor warning notice
 372  
 373  include( ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Wed Jul 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1