~canonical-sysadmins/wordpress/4.9.7

« back to all changes in this revision

Viewing changes to wp-admin/plugin-editor.php

  • Committer: Barry Price
  • Date: 2017-11-17 04:49:02 UTC
  • mfrom: (1.1.30 upstream)
  • Revision ID: barry.price@canonical.com-20171117044902-5frux4ycbq6g9fyf
Merge WP4.9 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
if ( empty( $plugin ) ) {
48
48
        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
                }
50
63
        } else {
51
64
                $plugin = array_keys( $plugins );
52
65
                $plugin = $plugin[0];
55
68
 
56
69
$plugin_files = get_plugin_files($plugin);
57
70
 
58
 
if ( empty($file) )
 
71
if ( empty( $file ) ) {
59
72
        $file = $plugin_files[0];
 
73
}
60
74
 
61
75
$file = validate_file_to_edit($file, $plugin_files);
62
76
$real_file = WP_PLUGIN_DIR . '/' . $file;
63
 
$scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
64
 
 
65
 
if ( isset( $_REQUEST['action'] ) && 'update' === $_REQUEST['action'] ) {
66
 
 
67
 
        check_admin_referer('edit-plugin_' . $file);
68
 
 
69
 
        $newcontent = wp_unslash( $_POST['newcontent'] );
70
 
        if ( is_writeable($real_file) ) {
71
 
                $f = fopen($real_file, 'w+');
72
 
                fwrite($f, $newcontent);
73
 
                fclose($f);
74
 
 
75
 
                $network_wide = is_plugin_active_for_network( $file );
76
 
 
77
 
                // Deactivate so we can test it.
78
 
                if ( is_plugin_active( $plugin ) || isset( $_POST['phperror'] ) ) {
79
 
                        if ( is_plugin_active( $plugin ) ) {
80
 
                                deactivate_plugins( $plugin, true );
81
 
                        }
82
 
 
83
 
                        if ( ! is_network_admin() ) {
84
 
                                update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
85
 
                        } else {
86
 
                                update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
87
 
                        }
88
 
 
89
 
                        wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'edit-plugin-test_' . $file ), "plugin-editor.php?file=$file&plugin=$plugin&liveupdate=1&scrollto=$scrollto&networkwide=" . $network_wide ) );
90
 
                        exit;
 
77
 
 
78
// Handle fallback editing of file when JavaScript is not available.
 
79
$edit_error = null;
 
80
$posted_content = null;
 
81
if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
 
82
        $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
 
83
        if ( is_wp_error( $r ) ) {
 
84
                $edit_error = $r;
 
85
                if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
 
86
                        $posted_content = wp_unslash( $_POST['newcontent'] );
91
87
                }
92
 
                wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&a=te&scrollto=$scrollto" ) );
93
88
        } else {
94
 
                wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&scrollto=$scrollto" ) );
95
 
        }
96
 
        exit;
97
 
 
98
 
} else {
99
 
 
100
 
        if ( isset($_GET['liveupdate']) ) {
101
 
                check_admin_referer('edit-plugin-test_' . $file);
102
 
 
103
 
                $error = validate_plugin( $plugin );
104
 
 
105
 
                if ( is_wp_error( $error ) ) {
106
 
                        wp_die( $error );
107
 
                }
108
 
 
109
 
                if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network( $file ) ) || ! is_plugin_active( $file ) ) {
110
 
                        activate_plugin( $plugin, "plugin-editor.php?file=" . urlencode( $file ) . "&phperror=1", ! empty( $_GET['networkwide'] ) );
111
 
                } // we'll override this later if the plugin can be included without fatal error
112
 
 
113
 
                wp_redirect( self_admin_url( 'plugin-editor.php?file=' . urlencode( $file ) . '&plugin=' . urlencode( $plugin ) . "&a=te&scrollto=$scrollto" ) );
 
89
                wp_redirect( add_query_arg(
 
90
                        array(
 
91
                                'a' => 1, // This means "success" for some reason.
 
92
                                'plugin' => $plugin,
 
93
                                'file' => $file,
 
94
                        ),
 
95
                        admin_url( 'plugin-editor.php' )
 
96
                ) );
114
97
                exit;
115
98
        }
 
99
}
116
100
 
117
101
        // List of allowable extensions
118
 
        $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
119
 
 
120
 
        /**
121
 
         * Filters file type extensions editable in the plugin editor.
122
 
         *
123
 
         * @since 2.8.0
124
 
         *
125
 
         * @param array $editable_extensions An array of editable plugin file extensions.
126
 
         */
127
 
        $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
 
102
        $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
128
103
 
129
104
        if ( ! is_file($real_file) ) {
130
105
                wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
145
120
                '<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>' .
146
121
                '<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. Don&#8217;t forget to save your changes (Update File) when you&#8217;re finished.') . '</p>' .
147
122
                '<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>' .
148
 
                '<p id="newcontent-description">' . __( 'In the editing area the Tab key enters a tab character. To move below this area by pressing Tab, press the Esc key followed by the Tab key. In some cases the Esc key will need to be pressed twice before the Tab key will allow you to continue.' ) . '</p>' .
 
123
                '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
 
124
                '<ul>' .
 
125
                '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
 
126
                '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
 
127
                '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
 
128
                '</ul>' .
149
129
                '<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>' .
150
130
                ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
151
131
        ) );
157
137
                '<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
158
138
        );
159
139
 
 
140
        $settings = array(
 
141
                'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
 
142
        );
 
143
        wp_enqueue_script( 'wp-theme-plugin-editor' );
 
144
        wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
 
145
        wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
 
146
 
160
147
        require_once(ABSPATH . 'wp-admin/admin-header.php');
161
148
 
162
149
        update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
163
150
 
164
 
        $content = file_get_contents( $real_file );
 
151
        if ( ! empty( $posted_content ) ) {
 
152
                $content = $posted_content;
 
153
        } else {
 
154
                $content = file_get_contents( $real_file );
 
155
        }
165
156
 
166
157
        if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
167
158
                $functions = wp_doc_link_parse( $content );
178
169
 
179
170
        $content = esc_textarea( $content );
180
171
        ?>
181
 
<?php if (isset($_GET['a'])) : ?>
182
 
 <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
183
 
<?php elseif (isset($_GET['phperror'])) : ?>
184
 
 <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
185
 
        <?php
186
 
                if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
187
 
                        $iframe_url = add_query_arg( array(
188
 
                                'action'   => 'error_scrape',
189
 
                                'plugin'   => urlencode( $file ),
190
 
                                '_wpnonce' => urlencode( $_GET['_error_nonce'] ),
191
 
                        ), admin_url( 'plugins.php' ) );
192
 
                        ?>
193
 
        <iframe style="border:0" width="100%" height="70px" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
194
 
        <?php } ?>
195
 
</div>
196
 
<?php endif; ?>
197
172
<div class="wrap">
198
173
<h1><?php echo esc_html( $title ); ?></h1>
199
174
 
 
175
<?php if ( isset( $_GET['a'] ) ) : ?>
 
176
        <div id="message" class="updated notice is-dismissible">
 
177
                <p><?php _e( 'File edited successfully.' ); ?></p>
 
178
        </div>
 
179
<?php elseif ( is_wp_error( $edit_error ) ) : ?>
 
180
        <div id="message" class="notice notice-error">
 
181
                <p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
 
182
                <pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
 
183
        </div>
 
184
<?php endif; ?>
 
185
 
200
186
<div class="fileedit-sub">
201
187
<div class="alignleft">
202
 
<big><?php
 
188
<h2>
 
189
        <?php
203
190
        if ( is_plugin_active( $plugin ) ) {
204
191
                if ( is_writeable( $real_file ) ) {
205
192
                        /* translators: %s: plugin file name */
217
204
                        echo sprintf( __( 'Browsing %s (inactive)' ), '<strong>' . esc_html( $file ) . '</strong>' );
218
205
                }
219
206
        }
220
 
        ?></big>
 
207
        ?>
 
208
</h2>
221
209
</div>
222
210
<div class="alignright">
223
 
        <form action="plugin-editor.php" method="post">
 
211
        <form action="plugin-editor.php" method="get">
224
212
                <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
225
213
                <select name="plugin" id="plugin">
226
214
<?php
243
231
</div>
244
232
 
245
233
<div id="templateside">
246
 
        <h2><?php _e( 'Plugin Files' ); ?></h2>
 
234
        <h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2>
247
235
 
248
 
        <ul>
249
 
<?php
250
 
foreach ( $plugin_files as $plugin_file ) :
251
 
        // Get the extension of the file
252
 
        if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
253
 
                $ext = strtolower($matches[1]);
254
 
                // If extension is not in the acceptable list, skip it
255
 
                if ( !in_array( $ext, $editable_extensions ) )
256
 
                        continue;
257
 
        } else {
258
 
                // No extension found
259
 
                continue;
 
236
        <?php
 
237
        $plugin_editable_files = array();
 
238
        foreach ( $plugin_files as $plugin_file ) {
 
239
                if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches ) && in_array( $matches[1], $editable_extensions ) ) {
 
240
                        $plugin_editable_files[] = $plugin_file;
 
241
                }
260
242
        }
261
 
?>
262
 
                <li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo urlencode( $plugin_file ) ?>&amp;plugin=<?php echo urlencode( $plugin ) ?>"><?php echo esc_html( $plugin_file ); ?></a></li>
263
 
<?php endforeach; ?>
 
243
        ?>
 
244
        <ul role="tree" aria-labelledby="plugin-files-label">
 
245
        <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
 
246
                <ul role="group">
 
247
                        <?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?>
 
248
                </ul>
264
249
        </ul>
265
250
</div>
266
251
<form name="template" id="template" action="plugin-editor.php" method="post">
267
 
        <?php wp_nonce_field('edit-plugin_' . $file) ?>
268
 
                <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
269
 
                <input type="hidden" name="action" value="update" />
270
 
                <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
271
 
                <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
272
 
                <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
 
252
        <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?>
 
253
                <div>
 
254
                        <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
 
255
                        <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>
 
256
                        <input type="hidden" name="action" value="update" />
 
257
                        <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
 
258
                        <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
273
259
                </div>
274
260
                <?php if ( !empty( $docs_select ) ) : ?>
275
261
                <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_user_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" /></div>
276
262
                <?php endif; ?>
277
263
<?php if ( is_writeable($real_file) ) : ?>
278
 
        <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?>
279
 
                <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>
280
 
        <?php } ?>
 
264
        <div class="editor-notices">
 
265
                <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?>
 
266
                        <div class="notice notice-warning inline active-plugin-edit-warning">
 
267
                        <p><?php _e('<strong>Warning:</strong> Making changes to active plugins is not recommended.'); ?></p>
 
268
                </div>
 
269
                <?php } ?>
 
270
        </div>
281
271
        <p class="submit">
282
 
        <?php
283
 
                if ( isset($_GET['phperror']) ) {
284
 
                        echo "<input type='hidden' name='phperror' value='1' />";
285
 
                        submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false );
286
 
                } else {
287
 
                        submit_button( __( 'Update File' ), 'primary', 'submit', false );
288
 
                }
289
 
        ?>
 
272
                <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
 
273
                <span class="spinner"></span>
290
274
        </p>
291
275
<?php else : ?>
292
276
        <p><em><?php _e('You need to make this file writable before you can save your changes. See <a href="https://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.'); ?></em></p>
293
277
<?php endif; ?>
 
278
<?php wp_print_file_editor_templates(); ?>
294
279
</form>
295
280
<br class="clear" />
296
281
</div>
297
 
<script type="text/javascript">
298
 
jQuery(document).ready(function($){
299
 
        $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
300
 
        $('#newcontent').scrollTop( $('#scrollto').val() );
301
 
});
302
 
</script>
303
 
<?php
304
 
}
 
282
<?php
 
283
$dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
 
284
if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) :
 
285
        // Get a back URL
 
286
        $referer = wp_get_referer();
 
287
        $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
 
288
 
 
289
        if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
 
290
                $return_url = $referer;
 
291
        } else {
 
292
                $return_url = admin_url( '/' );
 
293
        }
 
294
?>
 
295
<div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
 
296
        <div class="notification-dialog-background"></div>
 
297
        <div class="notification-dialog">
 
298
                <div class="file-editor-warning-content">
 
299
                        <div class="file-editor-warning-message">
 
300
                                <h1><?php _e( 'Heads up!' ); ?></h1>
 
301
                                <p><?php _e( 'You appear to be making direct edits to your plugin in the WordPress dashboard. We recommend that you don&#8217;t! Editing plugins directly may introduce incompatibilities that break your site and your changes may be lost in future updates.' ); ?></p>
 
302
                                <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>
 
303
                        </div>
 
304
                        <p>
 
305
                                <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
 
306
                                <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
 
307
                        </p>
 
308
                </div>
 
309
        </div>
 
310
</div>
 
311
<?php
 
312
endif; // editor warning notice
305
313
 
306
314
include(ABSPATH . "wp-admin/admin-footer.php");