[ Index ]

PHP Cross Reference of WordPress

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          <div id="message" class="error"><p><?php _e( 'No plugins are currently available.' ); ?></p></div>
  33      </div>
  34      <?php
  35      require_once ABSPATH . 'wp-admin/admin-footer.php';
  36      exit;
  37  }
  38  
  39  $file   = '';
  40  $plugin = '';
  41  if ( isset( $_REQUEST['file'] ) ) {
  42      $file = wp_unslash( $_REQUEST['file'] );
  43  }
  44  
  45  if ( isset( $_REQUEST['plugin'] ) ) {
  46      $plugin = wp_unslash( $_REQUEST['plugin'] );
  47  }
  48  
  49  if ( empty( $plugin ) ) {
  50      if ( $file ) {
  51  
  52          // Locate the plugin for a given plugin file being edited.
  53          $file_dirname = dirname( $file );
  54          foreach ( array_keys( $plugins ) as $plugin_candidate ) {
  55              if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) {
  56                  $plugin = $plugin_candidate;
  57                  break;
  58              }
  59          }
  60  
  61          // Fallback to the file as the plugin.
  62          if ( empty( $plugin ) ) {
  63              $plugin = $file;
  64          }
  65      } else {
  66          $plugin = array_keys( $plugins );
  67          $plugin = $plugin[0];
  68      }
  69  }
  70  
  71  $plugin_files = get_plugin_files( $plugin );
  72  
  73  if ( empty( $file ) ) {
  74      $file = $plugin_files[0];
  75  }
  76  
  77  $file      = validate_file_to_edit( $file, $plugin_files );
  78  $real_file = WP_PLUGIN_DIR . '/' . $file;
  79  
  80  // Handle fallback editing of file when JavaScript is not available.
  81  $edit_error     = null;
  82  $posted_content = null;
  83  
  84  if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
  85      $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
  86      if ( is_wp_error( $r ) ) {
  87          $edit_error = $r;
  88          if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
  89              $posted_content = wp_unslash( $_POST['newcontent'] );
  90          }
  91      } else {
  92          wp_redirect(
  93              add_query_arg(
  94                  array(
  95                      'a'      => 1, // This means "success" for some reason.
  96                      'plugin' => $plugin,
  97                      'file'   => $file,
  98                  ),
  99                  admin_url( 'plugin-editor.php' )
 100              )
 101          );
 102          exit;
 103      }
 104  }
 105  
 106  // List of allowable extensions.
 107  $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
 108  
 109  if ( ! is_file( $real_file ) ) {
 110      wp_die( sprintf( '<p>%s</p>', __( 'File does not exist! Please double check the name and try again.' ) ) );
 111  } else {
 112      // Get the extension of the file.
 113      if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
 114          $ext = strtolower( $matches[1] );
 115          // If extension is not in the acceptable list, skip it.
 116          if ( ! in_array( $ext, $editable_extensions, true ) ) {
 117              wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) );
 118          }
 119      }
 120  }
 121  
 122  get_current_screen()->add_help_tab(
 123      array(
 124          'id'      => 'overview',
 125          'title'   => __( 'Overview' ),
 126          'content' =>
 127                  '<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>' .
 128                  '<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>' .
 129                  '<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>' .
 130                  '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
 131                  '<ul>' .
 132                  '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
 133                  '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
 134                  '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
 135                  '</ul>' .
 136                  '<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>' .
 137                  ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
 138      )
 139  );
 140  
 141  get_current_screen()->set_help_sidebar(
 142      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 143      '<p>' . __( '<a href="https://wordpress.org/support/article/plugins-editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' .
 144      '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' .
 145      '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 146  );
 147  
 148  $settings = array(
 149      'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
 150  );
 151  wp_enqueue_script( 'wp-theme-plugin-editor' );
 152  wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
 153  wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
 154  
 155  require_once ABSPATH . 'wp-admin/admin-header.php';
 156  
 157  update_recently_edited( WP_PLUGIN_DIR . '/' . $file );
 158  
 159  if ( ! empty( $posted_content ) ) {
 160      $content = $posted_content;
 161  } else {
 162      $content = file_get_contents( $real_file );
 163  }
 164  
 165  if ( '.php' === substr( $real_file, strrpos( $real_file, '.' ) ) ) {
 166      $functions = wp_doc_link_parse( $content );
 167  
 168      if ( ! empty( $functions ) ) {
 169          $docs_select  = '<select name="docs-list" id="docs-list">';
 170          $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
 171          foreach ( $functions as $function ) {
 172              $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
 173          }
 174          $docs_select .= '</select>';
 175      }
 176  }
 177  
 178  $content = esc_textarea( $content );
 179  ?>
 180  <div class="wrap">
 181  <h1><?php echo esc_html( $title ); ?></h1>
 182  
 183  <?php if ( isset( $_GET['a'] ) ) : ?>
 184      <div id="message" class="updated notice is-dismissible">
 185          <p><?php _e( 'File edited successfully.' ); ?></p>
 186      </div>
 187  <?php elseif ( is_wp_error( $edit_error ) ) : ?>
 188      <div id="message" class="notice notice-error">
 189          <p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
 190          <pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
 191      </div>
 192  <?php endif; ?>
 193  
 194  <div class="fileedit-sub">
 195  <div class="alignleft">
 196  <h2>
 197      <?php
 198      if ( is_plugin_active( $plugin ) ) {
 199          if ( is_writable( $real_file ) ) {
 200              /* translators: %s: Plugin file name. */
 201              printf( __( 'Editing %s (active)' ), '<strong>' . esc_html( $file ) . '</strong>' );
 202          } else {
 203              /* translators: %s: Plugin file name. */
 204              printf( __( 'Browsing %s (active)' ), '<strong>' . esc_html( $file ) . '</strong>' );
 205          }
 206      } else {
 207          if ( is_writable( $real_file ) ) {
 208              /* translators: %s: Plugin file name. */
 209              printf( __( 'Editing %s (inactive)' ), '<strong>' . esc_html( $file ) . '</strong>' );
 210          } else {
 211              /* translators: %s: Plugin file name. */
 212              printf( __( 'Browsing %s (inactive)' ), '<strong>' . esc_html( $file ) . '</strong>' );
 213          }
 214      }
 215      ?>
 216  </h2>
 217  </div>
 218  <div class="alignright">
 219      <form action="plugin-editor.php" method="get">
 220          <label for="plugin" id="theme-plugin-editor-selector"><?php _e( 'Select plugin to edit:' ); ?> </label>
 221          <select name="plugin" id="plugin">
 222          <?php
 223          foreach ( $plugins as $plugin_key => $a_plugin ) {
 224              $plugin_name = $a_plugin['Name'];
 225              if ( $plugin_key === $plugin ) {
 226                  $selected = " selected='selected'";
 227              } else {
 228                  $selected = '';
 229              }
 230              $plugin_name = esc_attr( $plugin_name );
 231              $plugin_key  = esc_attr( $plugin_key );
 232              echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
 233          }
 234          ?>
 235          </select>
 236          <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
 237      </form>
 238  </div>
 239  <br class="clear" />
 240  </div>
 241  
 242  <div id="templateside">
 243      <h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2>
 244  
 245      <?php
 246      $plugin_editable_files = array();
 247      foreach ( $plugin_files as $plugin_file ) {
 248          if ( preg_match( '/\.([^.]+)$/', $plugin_file, $matches ) && in_array( $matches[1], $editable_extensions, true ) ) {
 249              $plugin_editable_files[] = $plugin_file;
 250          }
 251      }
 252      ?>
 253      <ul role="tree" aria-labelledby="plugin-files-label">
 254      <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
 255          <ul role="group">
 256              <?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?>
 257          </ul>
 258      </ul>
 259  </div>
 260  
 261  <form name="template" id="template" action="plugin-editor.php" method="post">
 262      <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?>
 263      <div>
 264          <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
 265          <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>
 266          <input type="hidden" name="action" value="update" />
 267          <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
 268          <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
 269      </div>
 270  
 271      <?php if ( ! empty( $docs_select ) ) : ?>
 272          <div id="documentation" class="hide-if-no-js">
 273              <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
 274              <?php echo $docs_select; ?>
 275              <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'); }" />
 276          </div>
 277      <?php endif; ?>
 278  
 279      <?php if ( is_writable( $real_file ) ) : ?>
 280          <div class="editor-notices">
 281          <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) ) { ?>
 282              <div class="notice notice-warning inline active-plugin-edit-warning">
 283                  <p><?php _e( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ); ?></p>
 284              </div>
 285          <?php } ?>
 286          </div>
 287          <p class="submit">
 288              <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
 289              <span class="spinner"></span>
 290          </p>
 291      <?php else : ?>
 292          <p>
 293              <?php
 294              printf(
 295                  /* translators: %s: Documentation URL. */
 296                  __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
 297                  __( 'https://wordpress.org/support/article/changing-file-permissions/' )
 298              );
 299              ?>
 300          </p>
 301      <?php endif; ?>
 302  
 303      <?php wp_print_file_editor_templates(); ?>
 304  </form>
 305  <br class="clear" />
 306  </div>
 307  <?php
 308  $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
 309  if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) :
 310      // Get a back URL.
 311      $referer = wp_get_referer();
 312  
 313      $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
 314  
 315      $return_url = admin_url( '/' );
 316      if ( $referer ) {
 317          $referer_path = parse_url( $referer, PHP_URL_PATH );
 318          if ( is_string( $referer_path ) && ! in_array( basename( $referer_path ), $excluded_referer_basenames, true ) ) {
 319              $return_url = $referer;
 320          }
 321      }
 322      ?>
 323      <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
 324          <div class="notification-dialog-background"></div>
 325          <div class="notification-dialog">
 326              <div class="file-editor-warning-content">
 327                  <div class="file-editor-warning-message">
 328                      <h1><?php _e( 'Heads up!' ); ?></h1>
 329                      <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>
 330                      <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>
 331                  </div>
 332                  <p>
 333                      <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
 334                      <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
 335                  </p>
 336              </div>
 337          </div>
 338      </div>
 339      <?php
 340  endif; // Editor warning notice.
 341  
 342  require_once ABSPATH . 'wp-admin/admin-footer.php';


Generated: Sat Apr 27 01:00:02 2024 Cross-referenced by PHPXref 0.7.1