56
69
$plugin_files = get_plugin_files($plugin);
71
if ( empty( $file ) ) {
59
72
$file = $plugin_files[0];
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;
65
if ( isset( $_REQUEST['action'] ) && 'update' === $_REQUEST['action'] ) {
67
check_admin_referer('edit-plugin_' . $file);
69
$newcontent = wp_unslash( $_POST['newcontent'] );
70
if ( is_writeable($real_file) ) {
71
$f = fopen($real_file, 'w+');
72
fwrite($f, $newcontent);
75
$network_wide = is_plugin_active_for_network( $file );
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 );
83
if ( ! is_network_admin() ) {
84
update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
86
update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
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 ) );
78
// Handle fallback editing of file when JavaScript is not available.
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 ) ) {
85
if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
86
$posted_content = wp_unslash( $_POST['newcontent'] );
92
wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&a=te&scrollto=$scrollto" ) );
94
wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&scrollto=$scrollto" ) );
100
if ( isset($_GET['liveupdate']) ) {
101
check_admin_referer('edit-plugin-test_' . $file);
103
$error = validate_plugin( $plugin );
105
if ( is_wp_error( $error ) ) {
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
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(
91
'a' => 1, // This means "success" for some reason.
95
admin_url( 'plugin-editor.php' )
117
101
// List of allowable extensions
118
$editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
121
* Filters file type extensions editable in the plugin editor.
125
* @param array $editable_extensions An array of editable plugin file extensions.
127
$editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
102
$editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
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’ 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’t forget to save your changes (Update File) when you’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>' .
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>' .
149
129
'<p>' . __('If you want to make changes but don’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>' : '' )
157
137
'<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
141
'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
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";' ) );
160
147
require_once(ABSPATH . 'wp-admin/admin-header.php');
162
149
update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
164
$content = file_get_contents( $real_file );
151
if ( ! empty( $posted_content ) ) {
152
$content = $posted_content;
154
$content = file_get_contents( $real_file );
166
157
if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
167
158
$functions = wp_doc_link_parse( $content );
179
170
$content = esc_textarea( $content );
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>
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' ) );
193
<iframe style="border:0" width="100%" height="70px" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
197
172
<div class="wrap">
198
173
<h1><?php echo esc_html( $title ); ?></h1>
175
<?php if ( isset( $_GET['a'] ) ) : ?>
176
<div id="message" class="updated notice is-dismissible">
177
<p><?php _e( 'File edited successfully.' ); ?></p>
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>
200
186
<div class="fileedit-sub">
201
187
<div class="alignleft">
203
190
if ( is_plugin_active( $plugin ) ) {
204
191
if ( is_writeable( $real_file ) ) {
205
192
/* translators: %s: plugin file name */
245
233
<div id="templateside">
246
<h2><?php _e( 'Plugin Files' ); ?></h2>
234
<h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2>
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 ) )
258
// No extension found
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;
262
<li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo urlencode( $plugin_file ) ?>&plugin=<?php echo urlencode( $plugin ) ?>"><?php echo esc_html( $plugin_file ); ?></a></li>
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">
247
<?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?>
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' ); ?>
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 ); ?>" />
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() ) + '&locale=<?php echo urlencode( get_user_locale() ) ?>&version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&redirect=true'); }" /></div>
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>
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>
281
271
<p class="submit">
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 );
287
submit_button( __( 'Update File' ), 'primary', 'submit', false );
272
<?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
273
<span class="spinner"></span>
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>
278
<?php wp_print_file_editor_templates(); ?>
295
280
<br class="clear" />
297
<script type="text/javascript">
298
jQuery(document).ready(function($){
299
$('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
300
$('#newcontent').scrollTop( $('#scrollto').val() );
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 ) ) :
286
$referer = wp_get_referer();
287
$excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
289
if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
290
$return_url = $referer;
292
$return_url = admin_url( '/' );
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’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>
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>
312
endif; // editor warning notice
306
314
include(ABSPATH . "wp-admin/admin-footer.php");