3
* Edit plugin editor administration panel.
6
* @subpackage Administration
9
/** WordPress Administration Bootstrap */
10
require_once( dirname( __FILE__ ) . '/admin.php' );
12
if ( is_multisite() && ! is_network_admin() ) {
13
wp_redirect( network_admin_url( 'plugin-editor.php' ) );
17
if ( !current_user_can('edit_plugins') )
18
wp_die( __('You do not have sufficient permissions to edit plugins for this site.') );
20
$title = __("Edit Plugins");
21
$parent_file = 'plugins.php';
23
wp_reset_vars( array( 'action', 'error', 'file', 'plugin' ) );
25
$plugins = get_plugins();
27
if ( empty( $plugins ) ) {
28
include( ABSPATH . 'wp-admin/admin-header.php' );
31
<h2><?php echo esc_html( $title ); ?></h2>
32
<div id="message" class="error"><p><?php _e( 'You do not appear to have any plugins available at this time.' ); ?></p></div>
35
include( ABSPATH . 'wp-admin/admin-footer.php' );
41
} elseif ( empty( $plugin ) ) {
42
$plugin = array_keys($plugins);
46
$plugin_files = get_plugin_files($plugin);
49
$file = $plugin_files[0];
51
$file = validate_file_to_edit($file, $plugin_files);
52
$real_file = WP_PLUGIN_DIR . '/' . $file;
53
$scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
59
check_admin_referer('edit-plugin_' . $file);
61
$newcontent = wp_unslash( $_POST['newcontent'] );
62
if ( is_writeable($real_file) ) {
63
$f = fopen($real_file, 'w+');
64
fwrite($f, $newcontent);
67
$network_wide = is_plugin_active_for_network( $file );
69
// 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);
74
if ( ! is_network_admin() )
75
update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
77
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));
80
wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
82
wp_redirect( self_admin_url("plugin-editor.php?file=$file&scrollto=$scrollto") );
88
if ( isset($_GET['liveupdate']) ) {
89
check_admin_referer('edit-plugin-test_' . $file);
91
$error = validate_plugin($file);
92
if ( is_wp_error($error) )
95
if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network($file) ) || ! is_plugin_active($file) )
96
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
98
wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
102
// List of allowable extensions
103
$editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
106
* Filter file type extensions editable in the plugin editor.
110
* @param array $editable_extensions An array of editable plugin file extensions.
112
$editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
114
if ( ! is_file($real_file) ) {
115
wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
117
// Get the extension of the file
118
if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
119
$ext = strtolower($matches[1]);
120
// If extension is not in the acceptable list, skip it
121
if ( !in_array( $ext, $editable_extensions) )
122
wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
126
get_current_screen()->add_help_tab( array(
128
'title' => __('Overview'),
130
'<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>' .
131
'<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’t forget to save your changes (Update File) when you’re finished.') . '</p>' .
132
'<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>' .
133
'<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.') . '</p>' .
134
'<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>' .
135
( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
138
get_current_screen()->set_help_sidebar(
139
'<p><strong>' . __('For more information:') . '</strong></p>' .
140
'<p>' . __('<a href="http://codex.wordpress.org/Plugins_Editor_Screen" target="_blank">Documentation on Editing Plugins</a>') . '</p>' .
141
'<p>' . __('<a href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">Documentation on Writing Plugins</a>') . '</p>' .
142
'<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
145
require_once(ABSPATH . 'wp-admin/admin-header.php');
147
update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
149
$content = file_get_contents( $real_file );
151
if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
152
$functions = wp_doc_link_parse( $content );
154
if ( !empty($functions) ) {
155
$docs_select = '<select name="docs-list" id="docs-list">';
156
$docs_select .= '<option value="">' . __( 'Function Name…' ) . '</option>';
157
foreach ( $functions as $function) {
158
$docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
160
$docs_select .= '</select>';
164
$content = esc_textarea( $content );
166
<?php if (isset($_GET['a'])) : ?>
167
<div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
168
<?php elseif (isset($_GET['phperror'])) : ?>
169
<div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
171
if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $file) ) { ?>
172
<iframe style="border:0" width="100%" height="70px" src="<?php bloginfo('wpurl'); ?>/wp-admin/plugins.php?action=error_scrape&plugin=<?php echo esc_attr($file); ?>&_wpnonce=<?php echo esc_attr($_GET['_error_nonce']); ?>"></iframe>
177
<h2><?php echo esc_html( $title ); ?></h2>
179
<div class="fileedit-sub">
180
<div class="alignleft">
182
if ( is_plugin_active($plugin) ) {
183
if ( is_writeable($real_file) )
184
echo sprintf(__('Editing <strong>%s</strong> (active)'), $file);
186
echo sprintf(__('Browsing <strong>%s</strong> (active)'), $file);
188
if ( is_writeable($real_file) )
189
echo sprintf(__('Editing <strong>%s</strong> (inactive)'), $file);
191
echo sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file);
195
<div class="alignright">
196
<form action="plugin-editor.php" method="post">
197
<strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
198
<select name="plugin" id="plugin">
200
foreach ( $plugins as $plugin_key => $a_plugin ) {
201
$plugin_name = $a_plugin['Name'];
202
if ( $plugin_key == $plugin )
203
$selected = " selected='selected'";
206
$plugin_name = esc_attr($plugin_name);
207
$plugin_key = esc_attr($plugin_key);
208
echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
212
<?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
218
<div id="templateside">
219
<h3><?php _e('Plugin Files'); ?></h3>
223
foreach ( $plugin_files as $plugin_file ) :
224
// Get the extension of the file
225
if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
226
$ext = strtolower($matches[1]);
227
// If extension is not in the acceptable list, skip it
228
if ( !in_array( $ext, $editable_extensions ) )
231
// No extension found
235
<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 $plugin_file ?></a></li>
239
<form name="template" id="template" action="plugin-editor.php" method="post">
240
<?php wp_nonce_field('edit-plugin_' . $file) ?>
241
<div><textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
242
<input type="hidden" name="action" value="update" />
243
<input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
244
<input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
245
<input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
247
<?php if ( !empty( $docs_select ) ) : ?>
248
<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( 'http://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&locale=<?php echo urlencode( get_locale() ) ?>&version=<?php echo urlencode( $wp_version ) ?>&redirect=true'); }" /></div>
250
<?php if ( is_writeable($real_file) ) : ?>
251
<?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
252
<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>
256
if ( isset($_GET['phperror']) ) {
257
echo "<input type='hidden' name='phperror' value='1' />";
258
submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false );
260
submit_button( __( 'Update File' ), 'primary', 'submit', false );
265
<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>
270
<script type="text/javascript">
271
jQuery(document).ready(function($){
272
$('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
273
$('#newcontent').scrollTop( $('#scrollto').val() );
279
include(ABSPATH . "wp-admin/admin-footer.php");