[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Edit plugin 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( 'plugin-editor.php' ) );
  14      exit();
  15  }
  16  
  17  if ( !current_user_can('edit_plugins') )
  18      wp_die( __('You do not have sufficient permissions to edit plugins for this site.') );
  19  
  20  $title = __("Edit Plugins");
  21  $parent_file = 'plugins.php';
  22  
  23  wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'plugin'));
  24  
  25  wp_admin_css( 'theme-editor' );
  26  
  27  $plugins = get_plugins();
  28  
  29  if ( empty($plugins) )
  30      wp_die( __('There are no plugins installed on this site.') );
  31  
  32  if ( isset($_REQUEST['file']) )
  33      $plugin = stripslashes($_REQUEST['file']);
  34  
  35  if ( empty($plugin) ) {
  36      $plugin = array_keys($plugins);
  37      $plugin = $plugin[0];
  38  }
  39  
  40  $plugin_files = get_plugin_files($plugin);
  41  
  42  if ( empty($file) )
  43      $file = $plugin_files[0];
  44  else
  45      $file = stripslashes($file);
  46  
  47  $file = validate_file_to_edit($file, $plugin_files);
  48  $real_file = WP_PLUGIN_DIR . '/' . $file;
  49  $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
  50  
  51  switch ( $action ) {
  52  
  53  case 'update':
  54  
  55      check_admin_referer('edit-plugin_' . $file);
  56  
  57      $newcontent = stripslashes($_POST['newcontent']);
  58      if ( is_writeable($real_file) ) {
  59          $f = fopen($real_file, 'w+');
  60          fwrite($f, $newcontent);
  61          fclose($f);
  62  
  63          $network_wide = is_plugin_active_for_network( $file );
  64  
  65          // Deactivate so we can test it.
  66          if ( is_plugin_active($file) || isset($_POST['phperror']) ) {
  67              if ( is_plugin_active($file) )
  68                  deactivate_plugins($file, true);
  69  
  70              update_option('recently_activated', array($file => time()) + (array)get_option('recently_activated'));
  71  
  72              wp_redirect(add_query_arg('_wpnonce', wp_create_nonce('edit-plugin-test_' . $file), "plugin-editor.php?file=$file&liveupdate=1&scrollto=$scrollto&networkwide=" . $network_wide));
  73              exit;
  74          }
  75          wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
  76      } else {
  77          wp_redirect( self_admin_url("plugin-editor.php?file=$file&scrollto=$scrollto") );
  78      }
  79      exit;
  80  
  81  break;
  82  
  83  default:
  84  
  85      if ( isset($_GET['liveupdate']) ) {
  86          check_admin_referer('edit-plugin-test_' . $file);
  87  
  88          $error = validate_plugin($file);
  89          if ( is_wp_error($error) )
  90              wp_die( $error );
  91  
  92          if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network($file) ) || ! is_plugin_active($file) )
  93              activate_plugin($file, "plugin-editor.php?file=$file&phperror=1", ! empty( $_GET['networkwide'] ) ); // we'll override this later if the plugin can be included without fatal error
  94  
  95          wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
  96          exit;
  97      }
  98  
  99      // List of allowable extensions
 100      $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
 101      $editable_extensions = (array) apply_filters('editable_extensions', $editable_extensions);
 102  
 103      if ( ! is_file($real_file) ) {
 104          wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
 105      } else {
 106          // Get the extension of the file
 107          if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
 108              $ext = strtolower($matches[1]);
 109              // If extension is not in the acceptable list, skip it
 110              if ( !in_array( $ext, $editable_extensions) )
 111                  wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
 112          }
 113      }
 114  
 115      add_contextual_help($current_screen,
 116          '<p>' . __('You can use the 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>' .
 117          '<p>' . __('Choose a plugin to edit from the menu in the upper right and click the Select button. Click once on any file name to load it in the editor, and make your changes. Don&#8217;t forget to save your changes (Update File) when you&#8217;re finished.') . '</p>' .
 118          '<p>' . __('The Documentation menu below the editor lists the PHP functions recognized in the plugin file. Clicking Lookup takes you to a web page about that particular function.') . '</p>' .
 119          '<p>' . __('If you want to make changes but don&#8217;t 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>' .
 120          ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' ) .
 121          '<p><strong>' . __('For more information:') . '</strong></p>' .
 122          '<p>' . __('<a href="http://codex.wordpress.org/Plugins_Editor_Screen" target="_blank">Documentation on Editing Plugins</a>') . '</p>' .
 123          '<p>' . __('<a href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">Documentation on Writing Plugins</a>') . '</p>' .
 124          '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
 125      );
 126  
 127      require_once (ABSPATH . 'wp-admin/admin-header.php');
 128  
 129      update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
 130  
 131      $content = file_get_contents( $real_file );
 132  
 133      if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
 134          $functions = wp_doc_link_parse( $content );
 135  
 136          if ( !empty($functions) ) {
 137              $docs_select = '<select name="docs-list" id="docs-list">';
 138              $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
 139              foreach ( $functions as $function) {
 140                  $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
 141              }
 142              $docs_select .= '</select>';
 143          }
 144      }
 145  
 146      $content = esc_textarea( $content );
 147      ?>
 148  <?php if (isset($_GET['a'])) : ?>
 149   <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
 150  <?php elseif (isset($_GET['phperror'])) : ?>
 151   <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
 152      <?php
 153          if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $file) ) { ?>
 154      <iframe style="border:0" width="100%" height="70px" src="<?php bloginfo('wpurl'); ?>/wp-admin/plugins.php?action=error_scrape&amp;plugin=<?php echo esc_attr($file); ?>&amp;_wpnonce=<?php echo esc_attr($_GET['_error_nonce']); ?>"></iframe>
 155      <?php } ?>
 156  </div>
 157  <?php endif; ?>
 158  <div class="wrap">
 159  <?php screen_icon(); ?>
 160  <h2><?php echo esc_html( $title ); ?></h2>
 161  
 162  <div class="fileedit-sub">
 163  <div class="alignleft">
 164  <big><?php
 165      if ( is_plugin_active($plugin) ) {
 166          if ( is_writeable($real_file) )
 167              echo sprintf(__('Editing <strong>%s</strong> (active)'), $file);
 168          else
 169              echo sprintf(__('Browsing <strong>%s</strong> (active)'), $file);
 170      } else {
 171          if ( is_writeable($real_file) )
 172              echo sprintf(__('Editing <strong>%s</strong> (inactive)'), $file);
 173          else
 174              echo sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file);
 175      }
 176      ?></big>
 177  </div>
 178  <div class="alignright">
 179      <form action="plugin-editor.php" method="post">
 180          <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
 181          <select name="plugin" id="plugin">
 182  <?php
 183      foreach ( $plugins as $plugin_key => $a_plugin ) {
 184          $plugin_name = $a_plugin['Name'];
 185          if ( $plugin_key == $plugin )
 186              $selected = " selected='selected'";
 187          else
 188              $selected = '';
 189          $plugin_name = esc_attr($plugin_name);
 190          $plugin_key = esc_attr($plugin_key);
 191          echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
 192      }
 193  ?>
 194          </select>
 195          <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
 196      </form>
 197  </div>
 198  <br class="clear" />
 199  </div>
 200  
 201  <div id="templateside">
 202      <h3><?php _e('Plugin Files'); ?></h3>
 203  
 204      <ul>
 205  <?php
 206  foreach ( $plugin_files as $plugin_file ) :
 207      // Get the extension of the file
 208      if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
 209          $ext = strtolower($matches[1]);
 210          // If extension is not in the acceptable list, skip it
 211          if ( !in_array( $ext, $editable_extensions ) )
 212              continue;
 213      } else {
 214          // No extension found
 215          continue;
 216      }
 217  ?>
 218          <li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo $plugin_file; ?>&amp;plugin=<?php echo $plugin; ?>"><?php echo $plugin_file ?></a></li>
 219  <?php endforeach; ?>
 220      </ul>
 221  </div>
 222  <form name="template" id="template" action="plugin-editor.php" method="post">
 223      <?php wp_nonce_field('edit-plugin_' . $file) ?>
 224          <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
 225          <input type="hidden" name="action" value="update" />
 226          <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
 227          <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
 228          <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
 229          </div>
 230          <?php if ( !empty( $docs_select ) ) : ?>
 231          <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <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'); }" /></div>
 232          <?php endif; ?>
 233  <?php if ( is_writeable($real_file) ) : ?>
 234      <?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
 235          <p><?php _e('<strong>Warning:</strong> Making changes to active plugins is not recommended.  If your changes cause a fatal error, the plugin will be automatically deactivated.'); ?></p>
 236      <?php } ?>
 237      <p class="submit">
 238      <?php
 239          if ( isset($_GET['phperror']) ) {
 240              echo "<input type='hidden' name='phperror' value='1' />";
 241              submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false, array( 'tabindex' => '2' ) );
 242          } else {
 243              submit_button( __( 'Update File' ), 'primary', 'submit', false, array( 'tabindex' => '2' ) );
 244          }
 245      ?>
 246      </p>
 247  <?php else : ?>
 248      <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>
 249  <?php endif; ?>
 250  </form>
 251  <br class="clear" />
 252  </div>
 253  <script type="text/javascript">
 254  /* <![CDATA[ */
 255  jQuery(document).ready(function($){
 256      $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
 257      $('#newcontent').scrollTop( $('#scrollto').val() );
 258  });
 259  /* ]]> */
 260  </script>
 261  <?php
 262      break;
 263  }
 264  include (ABSPATH . "wp-admin/admin-footer.php");


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting