]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/plugin-editor.php
WordPress 4.7-scripts
[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( __('Sorry, you are not allowed to edit plugins for this site.') );
19
20 $title = __("Edit Plugins");
21 $parent_file = 'plugins.php';
22
23 $plugins = get_plugins();
24
25 if ( empty( $plugins ) ) {
26         include( ABSPATH . 'wp-admin/admin-header.php' );
27         ?>
28         <div class="wrap">
29                 <h1><?php echo esc_html( $title ); ?></h1>
30                 <div id="message" class="error"><p><?php _e( 'You do not appear to have any plugins available at this time.' ); ?></p></div>
31         </div>
32         <?php
33         include( ABSPATH . 'wp-admin/admin-footer.php' );
34         exit;
35 }
36
37 $file = '';
38 $plugin = '';
39 if ( isset( $_REQUEST['file'] ) ) {
40         $file = sanitize_text_field( $_REQUEST['file'] );
41 }
42
43 if ( isset( $_REQUEST['plugin'] ) ) {
44         $plugin = sanitize_text_field( $_REQUEST['plugin'] );
45 }
46
47 if ( empty( $plugin ) ) {
48         if ( $file ) {
49                 $plugin = $file;
50         } else {
51                 $plugin = array_keys( $plugins );
52                 $plugin = $plugin[0];
53         }
54 }
55
56 $plugin_files = get_plugin_files($plugin);
57
58 if ( empty($file) )
59         $file = $plugin_files[0];
60
61 $file = validate_file_to_edit($file, $plugin_files);
62 $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;
91                 }
92                 wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&a=te&scrollto=$scrollto" ) );
93         } 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=$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=$file&plugin=$plugin&a=te&scrollto=$scrollto") );
114                 exit;
115         }
116
117         // 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 );
128
129         if ( ! is_file($real_file) ) {
130                 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
131         } else {
132                 // Get the extension of the file
133                 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
134                         $ext = strtolower($matches[1]);
135                         // If extension is not in the acceptable list, skip it
136                         if ( !in_array( $ext, $editable_extensions) )
137                                 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
138                 }
139         }
140
141         get_current_screen()->add_help_tab( array(
142         'id'            => 'overview',
143         'title'         => __('Overview'),
144         'content'       =>
145                 '<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                 '<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                 '<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>' .
149                 '<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                 ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
151         ) );
152
153         get_current_screen()->set_help_sidebar(
154                 '<p><strong>' . __('For more information:') . '</strong></p>' .
155                 '<p>' . __('<a href="https://codex.wordpress.org/Plugins_Editor_Screen">Documentation on Editing Plugins</a>') . '</p>' .
156                 '<p>' . __('<a href="https://codex.wordpress.org/Writing_a_Plugin">Documentation on Writing Plugins</a>') . '</p>' .
157                 '<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
158         );
159
160         require_once(ABSPATH . 'wp-admin/admin-header.php');
161
162         update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
163
164         $content = file_get_contents( $real_file );
165
166         if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
167                 $functions = wp_doc_link_parse( $content );
168
169                 if ( !empty($functions) ) {
170                         $docs_select = '<select name="docs-list" id="docs-list">';
171                         $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
172                         foreach ( $functions as $function) {
173                                 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
174                         }
175                         $docs_select .= '</select>';
176                 }
177         }
178
179         $content = esc_textarea( $content );
180         ?>
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 <div class="wrap">
198 <h1><?php echo esc_html( $title ); ?></h1>
199
200 <div class="fileedit-sub">
201 <div class="alignleft">
202 <big><?php
203         if ( is_plugin_active( $plugin ) ) {
204                 if ( is_writeable( $real_file ) ) {
205                         /* translators: %s: plugin file name */
206                         echo sprintf( __( 'Editing %s (active)' ), '<strong>' . $file . '</strong>' );
207                 } else {
208                         /* translators: %s: plugin file name */
209                         echo sprintf( __( 'Browsing %s (active)' ), '<strong>' . $file . '</strong>' );
210                 }
211         } else {
212                 if ( is_writeable( $real_file ) ) {
213                         /* translators: %s: plugin file name */
214                         echo sprintf( __( 'Editing %s (inactive)' ), '<strong>' . $file . '</strong>' );
215                 } else {
216                         /* translators: %s: plugin file name */
217                         echo sprintf( __( 'Browsing %s (inactive)' ), '<strong>' . $file . '</strong>' );
218                 }
219         }
220         ?></big>
221 </div>
222 <div class="alignright">
223         <form action="plugin-editor.php" method="post">
224                 <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
225                 <select name="plugin" id="plugin">
226 <?php
227         foreach ( $plugins as $plugin_key => $a_plugin ) {
228                 $plugin_name = $a_plugin['Name'];
229                 if ( $plugin_key == $plugin )
230                         $selected = " selected='selected'";
231                 else
232                         $selected = '';
233                 $plugin_name = esc_attr($plugin_name);
234                 $plugin_key = esc_attr($plugin_key);
235                 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
236         }
237 ?>
238                 </select>
239                 <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
240         </form>
241 </div>
242 <br class="clear" />
243 </div>
244
245 <div id="templateside">
246         <h2><?php _e( 'Plugin Files' ); ?></h2>
247
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;
260         }
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 $plugin_file ?></a></li>
263 <?php endforeach; ?>
264         </ul>
265 </div>
266 <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; ?>" />
273                 </div>
274                 <?php if ( !empty( $docs_select ) ) : ?>
275                 <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                 <?php endif; ?>
277 <?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 } ?>
281         <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         ?>
290         </p>
291 <?php else : ?>
292         <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 <?php endif; ?>
294 </form>
295 <br class="clear" />
296 </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 }
305
306 include(ABSPATH . "wp-admin/admin-footer.php");