Make WordPress Core

Ticket #17552: 17552.5.patch

File 17552.5.patch, 8.0 KB (added by MattyRob, 8 years ago)
  • src/wp-admin/plugin-editor.php

     
    3636        exit;
    3737}
    3838
    39 if ( $file ) {
    40         $plugin = $file;
    41 } elseif ( empty( $plugin ) ) {
    42         $plugin = array_keys($plugins);
    43         $plugin = $plugin[0];
     39if ( empty( $plugin ) ) {
     40        if ( $file ) {
     41                $plugin = $file;
     42        } else {
     43                $plugin = array_keys( $plugins );
     44                $plugin = $plugin[0];
     45        }
    4446}
    4547
    4648$plugin_files = get_plugin_files($plugin);
    4749
    48 if ( empty($file) )
     50if ( empty( $file ) ) {
    4951        $file = $plugin_files[0];
     52}
    5053
    51 $file = validate_file_to_edit($file, $plugin_files);
     54$file = validate_file_to_edit( $file, $plugin_files );
    5255$real_file = WP_PLUGIN_DIR . '/' . $file;
    5356$scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
    5457
     
    5861
    5962        check_admin_referer('edit-plugin_' . $file);
    6063
    61         $newcontent = wp_unslash( $_POST['newcontent'] );
    62         if ( is_writeable($real_file) ) {
    63                 $f = fopen($real_file, 'w+');
    64                 fwrite($f, $newcontent);
    65                 fclose($f);
     64        // Shared redirect arguments.
     65        $redirect_args = array(
     66                'file'     => $file,
     67                'plugin'   => $plugin,
     68                'scrollto' => $scrollto,
     69        );
    6670
    67                 $network_wide = is_plugin_active_for_network( $file );
     71        $network_wide = is_plugin_active_for_network( $file );
     72        if ( is_writeable( $real_file ) ) {
     73                $newcontent = wp_unslash( $_POST['newcontent'] );
    6874
     75                file_put_contents( $real_file, $newcontent );
     76
     77                $network_wide = is_plugin_active_for_network( $plugin );
     78
    6979                // Deactivate so we can test it.
    70                 if ( is_plugin_active($file) || isset($_POST['phperror']) ) {
    71                         if ( is_plugin_active($file) )
    72                                 deactivate_plugins($file, true);
     80                if ( is_plugin_active( $plugin ) || isset( $_POST['phperror'] ) ) {
     81                        if ( is_plugin_active( $plugin ) ) {
     82                                deactivate_plugins( $plugin , true );
     83                        }
    7384
    7485                        if ( ! is_network_admin() ) {
    75                                 update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
     86                                update_option( 'recently_activated', array( $plugin => time() ) + (array) get_option( 'recently_activated' ) );
    7687                        } else {
    77                                 update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
     88                                update_site_option( 'recently_activated', array( $plugin => time() ) + (array) get_site_option( 'recently_activated' ) );
    7889                        }
    7990
    80                         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));
     91                        $redirect_args = array_merge( $redirect_args, array(
     92                                'liveupdate'  => 1,
     93                                'networkwide' => $network_wide,
     94                                '_wpnonce'    => wp_create_nonce( "edit-plugin-test_{$file}" ),
     95                        ) );
     96                        wp_redirect( add_query_arg( $redirect_args, self_admin_url( 'plugin-editor.php?red=1' ) ) );
    8197                        exit;
    8298                }
    83                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
     99                $redirect_args = array_merge( $redirect_args, array( 'updated'  => 1 ) );
     100                wp_redirect( add_query_arg( $redirect_args, self_admin_url( 'plugin-editor.php?red=1' ) ) );
    84101        } else {
    85                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&scrollto=$scrollto") );
     102                wp_redirect( add_query_arg( $redirect_args, self_admin_url( 'plugin-editor.php?red=3' ) ) );
    86103        }
    87104        exit;
    88105
    89106default:
    90107
    91         if ( isset($_GET['liveupdate']) ) {
     108        // Shared redirect arguments.
     109        $redirect_args = array(
     110                'file'     => $file,
     111                'plugin'   => $plugin,
     112        );
     113
     114        if ( isset( $_GET['liveupdate'] ) ) {
     115
    92116                check_admin_referer('edit-plugin-test_' . $file);
    93117
    94                 $error = validate_plugin($file);
    95                 if ( is_wp_error($error) )
     118                $error = validate_plugin( $plugin );
     119
     120                if ( is_wp_error( $error ) )
    96121                        wp_die( $error );
    97122
    98                 if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network($file) ) || ! is_plugin_active($file) )
    99                         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
     123                if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network( $plugin ) ) || ! is_plugin_active( $plugin ) ) {
     124                        // we'll override this later if the plugin can be included without fatal error
     125                        $redirect_args = array_merge( $redirect_args, array( 'phperror' => 1 ) );
     126                        activate_plugin( $plugin, add_query_arg( $redirect_args, self_admin_url( 'plugin-editor.php' ) ), ! empty( $_GET['networkwide'] ) );
     127                }
    100128
    101                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
     129                $redirect_args = array_merge( $redirect_args, array(
     130                        'scrollto' => $scrollto,
     131                        'updated'  => 1
     132                ) );
     133                wp_redirect( add_query_arg( $redirect_args, self_admin_url( 'plugin-editor.php?red=4' ) ) );
    102134                exit;
    103135        }
    104136
     
    115147        $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
    116148
    117149        if ( ! is_file($real_file) ) {
    118                 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
     150                wp_die(  __( 'No such file exists! Double check the name and try again.' ) );
    119151        } else {
    120152                // Get the extension of the file
    121153                if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
     
    122154                        $ext = strtolower($matches[1]);
    123155                        // If extension is not in the acceptable list, skip it
    124156                        if ( !in_array( $ext, $editable_extensions) )
    125                                 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
     157                                wp_die( __( 'Files of this type are not editable.' ) );
    126158                }
    127159        }
    128160
     
    147179
    148180        require_once(ABSPATH . 'wp-admin/admin-header.php');
    149181
    150         update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
     182        update_recently_edited( WP_PLUGIN_DIR . '/' . $plugin );
    151183
    152184        $content = file_get_contents( $real_file );
    153185
     
    166198
    167199        $content = esc_textarea( $content );
    168200        ?>
    169 <?php if (isset($_GET['a'])) : ?>
     201<?php if ( isset( $_GET['updated'] ) ) : ?>
    170202 <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
    171203<?php elseif (isset($_GET['phperror'])) : ?>
    172204 <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
    173205        <?php
    174                 if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
     206                if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $plugin ) ) {
    175207                        $iframe_url = add_query_arg( array(
    176208                                'action'   => 'error_scrape',
    177                                 'plugin'   => urlencode( $file ),
     209                                'plugin'   => urlencode( $plugin ),
    178210                                '_wpnonce' => urlencode( $_GET['_error_nonce'] ),
    179211                        ), admin_url( 'plugins.php' ) );
    180212                        ?>
     
    188220<div class="fileedit-sub">
    189221<div class="alignleft">
    190222<big><?php
    191         if ( is_plugin_active( $plugin ) ) {
     223        if ( is_plugin_active( $plugin ) || is_plugin_active_for_network( $plugin ) ) {
    192224                if ( is_writeable( $real_file ) ) {
    193225                        /* translators: %s: plugin file name */
    194226                        echo sprintf( __( 'Editing %s (active)' ), '<strong>' . $file . '</strong>' );
     
    263295                <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( '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_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" /></div>
    264296                <?php endif; ?>
    265297<?php if ( is_writeable($real_file) ) : ?>
    266         <?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
     298        <?php if ( is_plugin_active( $plugin ) || is_plugin_active_for_network( $plugin ) ) { ?>
    267299                <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>
    268300        <?php } ?>
    269301        <p class="submit">