]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/plugin-editor.php
WordPress 4.5.1
[autoinstalls/wordpress.git] / wp-admin / plugin-editor.php
1 <?php
2 /**
3  * Edit plugin editor administration panel.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
11
12 if ( is_multisite() && ! is_network_admin() ) {
13         wp_redirect( network_admin_url( 'plugin-editor.php' ) );
14         exit();
15 }
16
17 if ( !current_user_can('edit_plugins') )
18         wp_die( __('You do not have sufficient permissions to edit plugins for this site.') );
19
20 $title = __("Edit Plugins");
21 $parent_file = 'plugins.php';
22
23 wp_reset_vars( array( 'action', 'error', 'file', 'plugin' ) );
24
25 $plugins = get_plugins();
26
27 if ( empty( $plugins ) ) {
28         include( ABSPATH . 'wp-admin/admin-header.php' );
29         ?>
30         <div class="wrap">
31                 <h1><?php echo esc_html( $title ); ?></h1>
32                 <div id="message" class="error"><p><?php _e( 'You do not appear to have any plugins available at this time.' ); ?></p></div>
33         </div>
34         <?php
35         include( ABSPATH . 'wp-admin/admin-footer.php' );
36         exit;
37 }
38
39 if ( $file ) {
40         $plugin = $file;
41 } elseif ( empty( $plugin ) ) {
42         $plugin = array_keys($plugins);
43         $plugin = $plugin[0];
44 }
45
46 $plugin_files = get_plugin_files($plugin);
47
48 if ( empty($file) )
49         $file = $plugin_files[0];
50
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;
54
55 switch ( $action ) {
56
57 case 'update':
58
59         check_admin_referer('edit-plugin_' . $file);
60
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);
66
67                 $network_wide = is_plugin_active_for_network( $file );
68
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);
73
74                         if ( ! is_network_admin() ) {
75                                 update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
76                         } else {
77                                 update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
78                         }
79
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));
81                         exit;
82                 }
83                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
84         } else {
85                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&scrollto=$scrollto") );
86         }
87         exit;
88
89 default:
90
91         if ( isset($_GET['liveupdate']) ) {
92                 check_admin_referer('edit-plugin-test_' . $file);
93
94                 $error = validate_plugin($file);
95                 if ( is_wp_error($error) )
96                         wp_die( $error );
97
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
100
101                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
102                 exit;
103         }
104
105         // List of allowable extensions
106         $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
107
108         /**
109          * Filter file type extensions editable in the plugin editor.
110          *
111          * @since 2.8.0
112          *
113          * @param array $editable_extensions An array of editable plugin file extensions.
114          */
115         $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
116
117         if ( ! is_file($real_file) ) {
118                 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
119         } else {
120                 // Get the extension of the file
121                 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
122                         $ext = strtolower($matches[1]);
123                         // If extension is not in the acceptable list, skip it
124                         if ( !in_array( $ext, $editable_extensions) )
125                                 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
126                 }
127         }
128
129         get_current_screen()->add_help_tab( array(
130         'id'            => 'overview',
131         'title'         => __('Overview'),
132         'content'       =>
133                 '<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>' .
134                 '<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&#8217;t forget to save your changes (Update File) when you&#8217;re finished.') . '</p>' .
135                 '<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>' .
136                 '<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>' .
137                 '<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>' .
138                 ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
139         ) );
140
141         get_current_screen()->set_help_sidebar(
142                 '<p><strong>' . __('For more information:') . '</strong></p>' .
143                 '<p>' . __('<a href="https://codex.wordpress.org/Plugins_Editor_Screen" target="_blank">Documentation on Editing Plugins</a>') . '</p>' .
144                 '<p>' . __('<a href="https://codex.wordpress.org/Writing_a_Plugin" target="_blank">Documentation on Writing Plugins</a>') . '</p>' .
145                 '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
146         );
147
148         require_once(ABSPATH . 'wp-admin/admin-header.php');
149
150         update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
151
152         $content = file_get_contents( $real_file );
153
154         if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
155                 $functions = wp_doc_link_parse( $content );
156
157                 if ( !empty($functions) ) {
158                         $docs_select = '<select name="docs-list" id="docs-list">';
159                         $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
160                         foreach ( $functions as $function) {
161                                 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
162                         }
163                         $docs_select .= '</select>';
164                 }
165         }
166
167         $content = esc_textarea( $content );
168         ?>
169 <?php if (isset($_GET['a'])) : ?>
170  <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
171 <?php elseif (isset($_GET['phperror'])) : ?>
172  <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
173         <?php
174                 if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
175                         $iframe_url = add_query_arg( array(
176                                 'action'   => 'error_scrape',
177                                 'plugin'   => urlencode( $file ),
178                                 '_wpnonce' => urlencode( $_GET['_error_nonce'] ),
179                         ), admin_url( 'plugins.php' ) );
180                         ?>
181         <iframe style="border:0" width="100%" height="70px" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
182         <?php } ?>
183 </div>
184 <?php endif; ?>
185 <div class="wrap">
186 <h1><?php echo esc_html( $title ); ?></h1>
187
188 <div class="fileedit-sub">
189 <div class="alignleft">
190 <big><?php
191         if ( is_plugin_active( $plugin ) ) {
192                 if ( is_writeable( $real_file ) ) {
193                         /* translators: %s: plugin file name */
194                         echo sprintf( __( 'Editing %s (active)' ), '<strong>' . $file . '</strong>' );
195                 } else {
196                         /* translators: %s: plugin file name */
197                         echo sprintf( __( 'Browsing %s (active)' ), '<strong>' . $file . '</strong>' );
198                 }
199         } else {
200                 if ( is_writeable( $real_file ) ) {
201                         /* translators: %s: plugin file name */
202                         echo sprintf( __( 'Editing %s (inactive)' ), '<strong>' . $file . '</strong>' );
203                 } else {
204                         /* translators: %s: plugin file name */
205                         echo sprintf( __( 'Browsing %s (inactive)' ), '<strong>' . $file . '</strong>' );
206                 }
207         }
208         ?></big>
209 </div>
210 <div class="alignright">
211         <form action="plugin-editor.php" method="post">
212                 <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
213                 <select name="plugin" id="plugin">
214 <?php
215         foreach ( $plugins as $plugin_key => $a_plugin ) {
216                 $plugin_name = $a_plugin['Name'];
217                 if ( $plugin_key == $plugin )
218                         $selected = " selected='selected'";
219                 else
220                         $selected = '';
221                 $plugin_name = esc_attr($plugin_name);
222                 $plugin_key = esc_attr($plugin_key);
223                 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
224         }
225 ?>
226                 </select>
227                 <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
228         </form>
229 </div>
230 <br class="clear" />
231 </div>
232
233 <div id="templateside">
234         <h2><?php _e( 'Plugin Files' ); ?></h2>
235
236         <ul>
237 <?php
238 foreach ( $plugin_files as $plugin_file ) :
239         // Get the extension of the file
240         if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
241                 $ext = strtolower($matches[1]);
242                 // If extension is not in the acceptable list, skip it
243                 if ( !in_array( $ext, $editable_extensions ) )
244                         continue;
245         } else {
246                 // No extension found
247                 continue;
248         }
249 ?>
250                 <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 $plugin_file ?></a></li>
251 <?php endforeach; ?>
252         </ul>
253 </div>
254 <form name="template" id="template" action="plugin-editor.php" method="post">
255         <?php wp_nonce_field('edit-plugin_' . $file) ?>
256                 <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
257                 <input type="hidden" name="action" value="update" />
258                 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
259                 <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
260                 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
261                 </div>
262                 <?php if ( !empty( $docs_select ) ) : ?>
263                 <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>
264                 <?php endif; ?>
265 <?php if ( is_writeable($real_file) ) : ?>
266         <?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
267                 <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>
268         <?php } ?>
269         <p class="submit">
270         <?php
271                 if ( isset($_GET['phperror']) ) {
272                         echo "<input type='hidden' name='phperror' value='1' />";
273                         submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false );
274                 } else {
275                         submit_button( __( 'Update File' ), 'primary', 'submit', false );
276                 }
277         ?>
278         </p>
279 <?php else : ?>
280         <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>
281 <?php endif; ?>
282 </form>
283 <br class="clear" />
284 </div>
285 <script type="text/javascript">
286 jQuery(document).ready(function($){
287         $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
288         $('#newcontent').scrollTop( $('#scrollto').val() );
289 });
290 </script>
291 <?php
292         break;
293 }
294 include(ABSPATH . "wp-admin/admin-footer.php");