[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Edit plugin file editor administration panel.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once  __DIR__ . '/admin.php';
  11  
  12  if ( is_multisite() && ! is_network_admin() ) {
  13      wp_redirect( network_admin_url( 'plugin-editor.php' ) );
  14      exit;
  15  }
  16  
  17  if ( ! current_user_can( 'edit_plugins' ) ) {
  18      wp_die( __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
  19  }
  20  
  21  // Used in the HTML title tag.
  22  $title       = __( 'Edit Plugins' );
  23  $parent_file = 'plugins.php';
  24  
  25  $plugins = get_plugins();
  26  
  27  if ( empty( $plugins ) ) {
  28      require_once  ABSPATH . 'wp-admin/admin-header.php';
  29      ?>
  30      <div class="wrap">
  31          <h1><?php echo esc_html( $title ); ?></h1>
  32          <?php
  33          wp_admin_notice(
  34              __( 'No plugins are currently available.' ),
  35              array(
  36                  'id'                 => 'message',
  37                  'additional_classes' => array( 'error' ),
  38              )
  39          );
  40          ?>
  41      </div>
  42      <?php
  43      require_once  ABSPATH . 'wp-admin/admin-footer.php';
  44      exit;
  45  }
  46  
  47  $file   = '';
  48  $plugin = '';
  49  if ( isset( $_REQUEST['file'] ) ) {
  50      $file = wp_unslash( $_REQUEST['file'] );
  51  }
  52  
  53  if ( isset( $_REQUEST['plugin'] ) ) {
  54      $plugin = wp_unslash( $_REQUEST['plugin'] );
  55  }
  56  
  57  if ( empty( $plugin ) ) {
  58      if ( $file ) {
  59  
  60          // Locate the plugin for a given plugin file being edited.
  61          $file_dirname = dirname( $file );
  62          foreach ( array_keys( $plugins ) as $plugin_candidate ) {
  63              if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) {
  64                  $plugin = $plugin_candidate;
  65                  break;
  66              }
  67          }
  68  
  69          // Fallback to the file as the plugin.
  70          if ( empty( $plugin ) ) {
  71              $plugin = $file;
  72          }
  73      } else {
  74          $plugin = array_keys( $plugins );
  75          $plugin = $plugin[0];
  76      }
  77  }
  78  
  79  $plugin_files = get_plugin_files( $plugin );
  80  
  81  if ( empty( $file ) ) {
  82      $file = $plugin_files[0];
  83  }
  84  
  85  $file      = validate_file_to_edit( $file, $plugin_files );
  86  $real_file = WP_PLUGIN_DIR . '/' . $file;
  87  
  88  $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_files[0] );
  89  $plugin_name = $plugin_data['Name'];
  90  
  91  // Handle fallback editing of file when JavaScript is not available.
  92  $edit_error     = null;
  93  $posted_content = null;
  94  
  95  if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
  96      $edit_result = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
  97  
  98      if ( is_wp_error( $edit_result ) ) {
  99          $edit_error = $edit_result;
 100  
 101          if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
 102              $posted_content = wp_unslash( $_POST['newcontent'] );
 103          }
 104      } else {
 105          wp_redirect(
 106              add_query_arg(
 107                  array(
 108                      'a'      => 1, // This means "success" for some reason.
 109                      'plugin' => $plugin,
 110                      'file'   => $file,
 111                  ),
 112                  admin_url( 'plugin-editor.php' )
 113              )
 114          );
 115          exit;
 116      }
 117  }
 118  
 119  // List of allowable extensions.
 120  $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
 121  
 122  if ( ! is_file( $real_file ) ) {
 123      wp_die( sprintf( '<p>%s</p>', __( 'File does not exist! Please double check the name and try again.' ) ) );
 124  } else {
 125      // Get the extension of the file.
 126      if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
 127          $extension = strtolower( $matches[1] );
 128  
 129          // If extension is not in the acceptable list, skip it.
 130          if ( ! in_array( $extension, $editable_extensions, true ) ) {
 131              wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) );
 132          }
 133      }
 134  }
 135  
 136  get_current_screen()->add_help_tab(
 137      array(
 138          'id'      => 'overview',
 139          'title'   => __( 'Overview' ),
 140          'content' =>
 141                  '<p>' . __( 'You can use the plugin file editor to make changes to any of your plugins&#8217; individual PHP files. Be aware that if you make changes, plugins updates will overwrite your customizations.' ) . '</p>' .
 142                  '<p>' . __( 'Choose a plugin to edit from the dropdown menu and click the Select button. Click once on any file name to load it in the editor, and make your changes. Do not forget to save your changes (Update File) when you are finished.' ) . '</p>' .
 143                  '<p>' . __( 'The documentation menu below the editor lists the PHP functions recognized in the plugin file. Clicking Look Up takes you to a web page about that particular function.' ) . '</p>' .
 144                  '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
 145                  '<ul>' .
 146                  '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
 147                  '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
 148                  '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
 149                  '</ul>' .
 150                  '<p>' . __( 'If you want to make changes but do not want them to be overwritten when the plugin is updated, you may be ready to think about writing your own plugin. For information on how to edit plugins, write your own from scratch, or just better understand their anatomy, check out the links below.' ) . '</p>' .
 151                  ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
 152      )
 153  );
 154  
 155  get_current_screen()->set_help_sidebar(
 156      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 157      '<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/plugins/editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' .
 158      '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' .
 159      '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
 160  );
 161  
 162  $settings = array(
 163      'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
 164  );
 165  wp_enqueue_script( 'wp-theme-plugin-editor' );
 166  wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) );
 167  wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
 168  
 169  require_once  ABSPATH . 'wp-admin/admin-header.php';
 170  
 171  update_recently_edited( WP_PLUGIN_DIR . '/' . $file );
 172  
 173  if ( ! empty( $posted_content ) ) {
 174      $content = $posted_content;
 175  } else {
 176      $content = file_get_contents( $real_file );
 177  }
 178  
 179  if ( str_ends_with( $real_file, '.php' ) ) {
 180      $functions = wp_doc_link_parse( $content );
 181  
 182      if ( ! empty( $functions ) ) {
 183          $docs_select  = '<select name="docs-list" id="docs-list">';
 184          $docs_select .= '<option value="">' . esc_html__( 'Function Name&hellip;' ) . '</option>';
 185  
 186          foreach ( $functions as $function ) {
 187              $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
 188          }
 189  
 190          $docs_select .= '</select>';
 191      }
 192  }
 193  
 194  $content = esc_textarea( $content );
 195  ?>
 196  <div class="wrap">
 197  <h1><?php echo esc_html( $title ); ?></h1>
 198  
 199  <?php
 200  if ( isset( $_GET['a'] ) ) :
 201      wp_admin_notice(
 202          __( 'File edited successfully.' ),
 203          array(
 204              'additional_classes' => array( 'updated', 'is-dismissible' ),
 205              'id'                 => 'message',
 206          )
 207      );
 208  elseif ( is_wp_error( $edit_error ) ) :
 209      $error   = esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() );
 210      $message = '<p>' . __( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ) . '</p>
 211      <pre>' . $error . '</pre>';
 212      wp_admin_notice(
 213          $message,
 214          array(
 215              'type'           => 'error',
 216              'id'             => 'message',
 217              'paragraph_wrap' => false,
 218          )
 219      );
 220  endif;
 221  ?>
 222  
 223  <div class="fileedit-sub">
 224  <div class="alignleft">
 225  <h2>
 226      <?php
 227      if ( is_plugin_active( $plugin ) ) {
 228          if ( is_writable( $real_file ) ) {
 229              /* translators: %s: Plugin name. */
 230              printf( __( 'Editing %s (active)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' );
 231          } else {
 232              /* translators: %s: Plugin name. */
 233              printf( __( 'Browsing %s (active)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' );
 234          }
 235      } else {
 236          if ( is_writable( $real_file ) ) {
 237              /* translators: %s: Plugin name. */
 238              printf( __( 'Editing %s (inactive)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' );
 239          } else {
 240              /* translators: %s: Plugin name. */
 241              printf( __( 'Browsing %s (inactive)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' );
 242          }
 243      }
 244      ?>
 245  </h2>
 246  <?php
 247  printf(
 248      /* translators: %s: File path. */
 249      ' <span><strong>' . __( 'File: %s' ) . '</strong></span>',
 250      esc_html( $file )
 251  );
 252  ?>
 253  </div>
 254  <div class="alignright">
 255      <form action="plugin-editor.php" method="get">
 256          <label for="plugin" id="theme-plugin-editor-selector"><?php _e( 'Select plugin to edit:' ); ?> </label>
 257          <select name="plugin" id="plugin">
 258          <?php
 259          foreach ( $plugins as $plugin_key => $a_plugin ) {
 260              $plugin_name = $a_plugin['Name'];
 261              if ( $plugin_key === $plugin ) {
 262                  $selected = " selected='selected'";
 263              } else {
 264                  $selected = '';
 265              }
 266              $plugin_name = esc_attr( $plugin_name );
 267              $plugin_key  = esc_attr( $plugin_key );
 268              echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
 269          }
 270          ?>
 271          </select>
 272          <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
 273      </form>
 274  </div>
 275  <br class="clear" />
 276  </div>
 277  
 278  <div id="templateside">
 279      <h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2>
 280  
 281      <?php
 282      $plugin_editable_files = array();
 283      foreach ( $plugin_files as $plugin_file ) {
 284          if ( preg_match( '/\.([^.]+)$/', $plugin_file, $matches ) && in_array( $matches[1], $editable_extensions, true ) ) {
 285              $plugin_editable_files[] = $plugin_file;
 286          }
 287      }
 288      ?>
 289      <ul role="tree" aria-labelledby="plugin-files-label">
 290      <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
 291          <ul role="group">
 292              <?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?>
 293          </ul>
 294      </li>
 295      </ul>
 296  </div>
 297  
 298  <form name="template" id="template" action="plugin-editor.php" method="post">
 299      <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?>
 300      <div>
 301          <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
 302          <textarea cols="70" rows="25" 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>
 303          <input type="hidden" name="action" value="update" />
 304          <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
 305          <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
 306      </div>
 307  
 308      <?php if ( ! empty( $docs_select ) ) : ?>
 309          <div id="documentation" class="hide-if-no-js">
 310              <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
 311              <?php echo $docs_select; ?>
 312              <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'); }" />
 313          </div>
 314      <?php endif; ?>
 315  
 316      <?php if ( is_writable( $real_file ) ) : ?>
 317          <div class="editor-notices">
 318          <?php
 319          if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) ) {
 320              wp_admin_notice(
 321                  __( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ),
 322                  array(
 323                      'type'               => 'warning',
 324                      'additional_classes' => array( 'inline', 'active-plugin-edit-warning' ),
 325                  )
 326              );
 327          }
 328          ?>
 329          </div>
 330          <p class="submit">
 331              <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
 332              <span class="spinner"></span>
 333          </p>
 334      <?php else : ?>
 335          <p>
 336              <?php
 337              printf(
 338                  /* translators: %s: Documentation URL. */
 339                  __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
 340                  __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' )
 341              );
 342              ?>
 343          </p>
 344      <?php endif; ?>
 345  
 346      <?php wp_print_file_editor_templates(); ?>
 347  </form>
 348  <br class="clear" />
 349  </div>
 350  <?php
 351  $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
 352  if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) :
 353      // Get a back URL.
 354      $referer = wp_get_referer();
 355  
 356      $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
 357  
 358      $return_url = admin_url( '/' );
 359      if ( $referer ) {
 360          $referer_path = parse_url( $referer, PHP_URL_PATH );
 361          if ( is_string( $referer_path ) && ! in_array( basename( $referer_path ), $excluded_referer_basenames, true ) ) {
 362              $return_url = $referer;
 363          }
 364      }
 365      ?>
 366      <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
 367          <div class="notification-dialog-background"></div>
 368          <div class="notification-dialog">
 369              <div class="file-editor-warning-content">
 370                  <div class="file-editor-warning-message">
 371                      <h1><?php _e( 'Heads up!' ); ?></h1>
 372                      <p><?php _e( 'You appear to be making direct edits to your plugin in the WordPress dashboard. Editing plugins directly is not recommended as it may introduce incompatibilities that break your site and your changes may be lost in future updates.' ); ?></p>
 373                      <p><?php _e( 'If you absolutely have to make direct edits to this plugin, 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>
 374                  </div>
 375                  <p>
 376                      <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
 377                      <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
 378                  </p>
 379              </div>
 380          </div>
 381      </div>
 382      <?php
 383  endif; // Editor warning notice.
 384  
 385  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Thu Oct 30 08:20:06 2025 Cross-referenced by PHPXref