Make WordPress Core


Ignore:
Timestamp:
09/22/2017 01:35:09 AM (7 years ago)
Author:
westonruter
Message:

Plugin Editor: Improve reliability of detecting PHP fatal errors when editing an active plugin.

  • Invalidate PHP opcache after file is updated to ensure include will include the written changes.
  • Define WP_ADMIN when activating plugin in sandbox so plugin code targeting admin will be loaded.
  • Do actions that get triggered when loading the admin to ensure plugin code runs that could cause errors on plugin editor screen (and lock out access).
  • Fix ability to re-activate a plugin after editing a PHP file other than the main plugin file, and ensure PHP fatal error will be displayed in such cases.
  • Consolidate duplicated code into plugin_sandbox_scrape() and re-use in activate_plugin().
  • Show an error notice instead of a success notice when a file is updated but a plugin was deactivated due to a fatal error.
  • Update style of warning when editing an active plugin to be styled as an actual warning notice.

See #12423, #21622.
Fixes #39766.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/plugin-editor.php

    r41399 r41560  
    4747if ( empty( $plugin ) ) {
    4848    if ( $file ) {
    49         $plugin = $file;
     49
     50        // Locate the plugin for a given plugin file being edited.
     51        $file_dirname = dirname( $file );
     52        foreach ( array_keys( $plugins ) as $plugin_candidate ) {
     53            if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) {
     54                $plugin = $plugin_candidate;
     55                break;
     56            }
     57        }
     58
     59        // Fallback to the file as the plugin.
     60        if ( empty( $plugin ) ) {
     61            $plugin = $file;
     62        }
    5063    } else {
    5164        $plugin = array_keys( $plugins );
     
    7285        fwrite($f, $newcontent);
    7386        fclose($f);
     87
     88        if ( preg_match( '/\.php$/', $real_file ) && function_exists( 'opcache_invalidate' ) ) {
     89            opcache_invalidate( $real_file, true );
     90        }
    7491
    7592        $network_wide = is_plugin_active_for_network( $file );
     
    221238 <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
    222239<?php elseif (isset($_GET['phperror'])) : ?>
    223  <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
     240 <div id="message" class="notice notice-error"><p><?php _e( 'This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.' ); ?></p>
    224241    <?php
    225         if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
     242        if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $plugin ) ) {
    226243            $iframe_url = add_query_arg( array(
    227244                'action'   => 'error_scrape',
    228                 'plugin'   => urlencode( $file ),
     245                'plugin'   => urlencode( $plugin ),
    229246                '_wpnonce' => urlencode( $_GET['_error_nonce'] ),
    230247            ), admin_url( 'plugins.php' ) );
     
    316333<?php if ( is_writeable($real_file) ) : ?>
    317334    <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?>
    318         <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>
     335        <div class="notice notice-warning inline active-plugin-edit-warning">
     336            <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>
     337        </div>
    319338    <?php } ?>
    320339    <p class="submit">
Note: See TracChangeset for help on using the changeset viewer.