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


Generated : Tue Mar 19 08:20:01 2024 Cross-referenced by PHPXref