]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/plugin-editor.php
WordPress 3.8.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         wp_die( __('There are no plugins installed on this site.') );
29
30 if ( $file ) {
31         $plugin = $file;
32 } elseif ( empty( $plugin ) ) {
33         $plugin = array_keys($plugins);
34         $plugin = $plugin[0];
35 }
36
37 $plugin_files = get_plugin_files($plugin);
38
39 if ( empty($file) )
40         $file = $plugin_files[0];
41
42 $file = validate_file_to_edit($file, $plugin_files);
43 $real_file = WP_PLUGIN_DIR . '/' . $file;
44 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
45
46 switch ( $action ) {
47
48 case 'update':
49
50         check_admin_referer('edit-plugin_' . $file);
51
52         $newcontent = wp_unslash( $_POST['newcontent'] );
53         if ( is_writeable($real_file) ) {
54                 $f = fopen($real_file, 'w+');
55                 fwrite($f, $newcontent);
56                 fclose($f);
57
58                 $network_wide = is_plugin_active_for_network( $file );
59
60                 // Deactivate so we can test it.
61                 if ( is_plugin_active($file) || isset($_POST['phperror']) ) {
62                         if ( is_plugin_active($file) )
63                                 deactivate_plugins($file, true);
64
65                         if ( ! is_network_admin() )
66                                 update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
67
68                         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));
69                         exit;
70                 }
71                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
72         } else {
73                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&scrollto=$scrollto") );
74         }
75         exit;
76
77 break;
78
79 default:
80
81         if ( isset($_GET['liveupdate']) ) {
82                 check_admin_referer('edit-plugin-test_' . $file);
83
84                 $error = validate_plugin($file);
85                 if ( is_wp_error($error) )
86                         wp_die( $error );
87
88                 if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network($file) ) || ! is_plugin_active($file) )
89                         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
90
91                 wp_redirect( self_admin_url("plugin-editor.php?file=$file&a=te&scrollto=$scrollto") );
92                 exit;
93         }
94
95         // List of allowable extensions
96         $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
97
98         /**
99          * Filter file type extensions editable in the plugin editor.
100          *
101          * @since 2.8.0
102          *
103          * @param array $editable_extensions An array of editable plugin file extensions.
104          */
105         $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
106
107         if ( ! is_file($real_file) ) {
108                 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
109         } else {
110                 // Get the extension of the file
111                 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
112                         $ext = strtolower($matches[1]);
113                         // If extension is not in the acceptable list, skip it
114                         if ( !in_array( $ext, $editable_extensions) )
115                                 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
116                 }
117         }
118
119         get_current_screen()->add_help_tab( array(
120         'id'            => 'overview',
121         'title'         => __('Overview'),
122         'content'       =>
123                 '<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>' .
124                 '<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>' .
125                 '<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>' .
126                 '<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>' .
127                 '<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>' .
128                 ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
129         ) );
130
131         get_current_screen()->set_help_sidebar(
132                 '<p><strong>' . __('For more information:') . '</strong></p>' .
133                 '<p>' . __('<a href="http://codex.wordpress.org/Plugins_Editor_Screen" target="_blank">Documentation on Editing Plugins</a>') . '</p>' .
134                 '<p>' . __('<a href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">Documentation on Writing Plugins</a>') . '</p>' .
135                 '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
136         );
137
138         require_once(ABSPATH . 'wp-admin/admin-header.php');
139
140         update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
141
142         $content = file_get_contents( $real_file );
143
144         if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
145                 $functions = wp_doc_link_parse( $content );
146
147                 if ( !empty($functions) ) {
148                         $docs_select = '<select name="docs-list" id="docs-list">';
149                         $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
150                         foreach ( $functions as $function) {
151                                 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
152                         }
153                         $docs_select .= '</select>';
154                 }
155         }
156
157         $content = esc_textarea( $content );
158         ?>
159 <?php if (isset($_GET['a'])) : ?>
160  <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
161 <?php elseif (isset($_GET['phperror'])) : ?>
162  <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
163         <?php
164                 if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $file) ) { ?>
165         <iframe style="border:0" width="100%" height="70px" src="<?php bloginfo('wpurl'); ?>/wp-admin/plugins.php?action=error_scrape&amp;plugin=<?php echo esc_attr($file); ?>&amp;_wpnonce=<?php echo esc_attr($_GET['_error_nonce']); ?>"></iframe>
166         <?php } ?>
167 </div>
168 <?php endif; ?>
169 <div class="wrap">
170 <h2><?php echo esc_html( $title ); ?></h2>
171
172 <div class="fileedit-sub">
173 <div class="alignleft">
174 <big><?php
175         if ( is_plugin_active($plugin) ) {
176                 if ( is_writeable($real_file) )
177                         echo sprintf(__('Editing <strong>%s</strong> (active)'), $file);
178                 else
179                         echo sprintf(__('Browsing <strong>%s</strong> (active)'), $file);
180         } else {
181                 if ( is_writeable($real_file) )
182                         echo sprintf(__('Editing <strong>%s</strong> (inactive)'), $file);
183                 else
184                         echo sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file);
185         }
186         ?></big>
187 </div>
188 <div class="alignright">
189         <form action="plugin-editor.php" method="post">
190                 <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
191                 <select name="plugin" id="plugin">
192 <?php
193         foreach ( $plugins as $plugin_key => $a_plugin ) {
194                 $plugin_name = $a_plugin['Name'];
195                 if ( $plugin_key == $plugin )
196                         $selected = " selected='selected'";
197                 else
198                         $selected = '';
199                 $plugin_name = esc_attr($plugin_name);
200                 $plugin_key = esc_attr($plugin_key);
201                 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
202         }
203 ?>
204                 </select>
205                 <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
206         </form>
207 </div>
208 <br class="clear" />
209 </div>
210
211 <div id="templateside">
212         <h3><?php _e('Plugin Files'); ?></h3>
213
214         <ul>
215 <?php
216 foreach ( $plugin_files as $plugin_file ) :
217         // Get the extension of the file
218         if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
219                 $ext = strtolower($matches[1]);
220                 // If extension is not in the acceptable list, skip it
221                 if ( !in_array( $ext, $editable_extensions ) )
222                         continue;
223         } else {
224                 // No extension found
225                 continue;
226         }
227 ?>
228                 <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>
229 <?php endforeach; ?>
230         </ul>
231 </div>
232 <form name="template" id="template" action="plugin-editor.php" method="post">
233         <?php wp_nonce_field('edit-plugin_' . $file) ?>
234                 <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
235                 <input type="hidden" name="action" value="update" />
236                 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
237                 <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
238                 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
239                 </div>
240                 <?php if ( !empty( $docs_select ) ) : ?>
241                 <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() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" /></div>
242                 <?php endif; ?>
243 <?php if ( is_writeable($real_file) ) : ?>
244         <?php if ( in_array( $file, (array) get_option( 'active_plugins', array() ) ) ) { ?>
245                 <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>
246         <?php } ?>
247         <p class="submit">
248         <?php
249                 if ( isset($_GET['phperror']) ) {
250                         echo "<input type='hidden' name='phperror' value='1' />";
251                         submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false );
252                 } else {
253                         submit_button( __( 'Update File' ), 'primary', 'submit', false );
254                 }
255         ?>
256         </p>
257 <?php else : ?>
258         <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>
259 <?php endif; ?>
260 </form>
261 <br class="clear" />
262 </div>
263 <script type="text/javascript">
264 jQuery(document).ready(function($){
265         $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
266         $('#newcontent').scrollTop( $('#scrollto').val() );
267 });
268 </script>
269 <?php
270         break;
271 }
272 include(ABSPATH . "wp-admin/admin-footer.php");