| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Edit plugin file editor administration panel. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** WordPress Administration Bootstrap */ 10 require_once __DIR__ . '/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 21 // Used in the HTML title tag. 22 $title = __( 'Edit Plugins' ); 23 $parent_file = 'plugins.php'; 24 25 $plugins = get_plugins(); 26 27 if ( empty( $plugins ) ) { 28 require_once ABSPATH . 'wp-admin/admin-header.php'; 29 ?> 30 <div class="wrap"> 31 <h1><?php echo esc_html( $title ); ?></h1> 32 <?php 33 wp_admin_notice( 34 __( 'No plugins are currently available.' ), 35 array( 36 'id' => 'message', 37 'additional_classes' => array( 'error' ), 38 ) 39 ); 40 ?> 41 </div> 42 <?php 43 require_once ABSPATH . 'wp-admin/admin-footer.php'; 44 exit; 45 } 46 47 $file = ''; 48 $plugin = ''; 49 if ( isset( $_REQUEST['file'] ) ) { 50 $file = wp_unslash( $_REQUEST['file'] ); 51 } 52 53 if ( isset( $_REQUEST['plugin'] ) ) { 54 $plugin = wp_unslash( $_REQUEST['plugin'] ); 55 } 56 57 if ( empty( $plugin ) ) { 58 if ( $file ) { 59 60 // Locate the plugin for a given plugin file being edited. 61 $file_dirname = dirname( $file ); 62 foreach ( array_keys( $plugins ) as $plugin_candidate ) { 63 if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) { 64 $plugin = $plugin_candidate; 65 break; 66 } 67 } 68 69 // Fallback to the file as the plugin. 70 if ( empty( $plugin ) ) { 71 $plugin = $file; 72 } 73 } else { 74 $plugin = array_key_first( $plugins ); 75 } 76 } 77 78 $plugin_files = get_plugin_files( $plugin ); 79 80 if ( empty( $file ) ) { 81 $file = $plugin_files[0]; 82 } 83 84 $file = validate_file_to_edit( $file, $plugin_files ); 85 $real_file = WP_PLUGIN_DIR . '/' . $file; 86 87 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_files[0] ); 88 $plugin_name = $plugin_data['Name']; 89 90 // Handle fallback editing of file when JavaScript is not available. 91 $edit_error = null; 92 $posted_content = null; 93 94 if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) { 95 $edit_result = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); 96 97 if ( is_wp_error( $edit_result ) ) { 98 $edit_error = $edit_result; 99 100 if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) { 101 $posted_content = wp_unslash( $_POST['newcontent'] ); 102 } 103 } else { 104 wp_redirect( 105 add_query_arg( 106 array( 107 'a' => 1, // This means "success" for some reason. 108 'plugin' => $plugin, 109 'file' => $file, 110 ), 111 admin_url( 'plugin-editor.php' ) 112 ) 113 ); 114 exit; 115 } 116 } 117 118 // List of allowable extensions. 119 $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin ); 120 121 if ( ! is_file( $real_file ) ) { 122 wp_die( sprintf( '<p>%s</p>', __( 'File does not exist! Please double check the name and try again.' ) ) ); 123 } else { 124 // Get the extension of the file. 125 if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) { 126 $extension = strtolower( $matches[1] ); 127 128 // If extension is not in the acceptable list, skip it. 129 if ( ! in_array( $extension, $editable_extensions, true ) ) { 130 wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) ); 131 } 132 } 133 } 134 135 get_current_screen()->add_help_tab( 136 array( 137 'id' => 'overview', 138 'title' => __( 'Overview' ), 139 'content' => 140 '<p>' . __( 'You can use the plugin file 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>' . 141 '<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. Do not forget to save your changes (Update File) when you are finished.' ) . '</p>' . 142 '<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>' . 143 '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' . 144 '<ul>' . 145 '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' . 146 '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' . 147 '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' . 148 '</ul>' . 149 '<p>' . __( 'If you want to make changes but do not 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 154 get_current_screen()->set_help_sidebar( 155 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . 156 '<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/plugins/editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' . 157 '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' . 158 '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>' 159 ); 160 161 $settings = array( 162 'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ), 163 ); 164 wp_enqueue_script( 'wp-theme-plugin-editor' ); 165 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ); 166 wp_add_inline_script( 'wp-theme-plugin-editor', 'wp.themePluginEditor.themeOrPlugin = "plugin";' ); 167 168 require_once ABSPATH . 'wp-admin/admin-header.php'; 169 170 update_recently_edited( WP_PLUGIN_DIR . '/' . $file ); 171 172 if ( ! empty( $posted_content ) ) { 173 $content = $posted_content; 174 } else { 175 $content = file_get_contents( $real_file ); 176 } 177 178 if ( str_ends_with( $real_file, '.php' ) ) { 179 $functions = wp_doc_link_parse( $content ); 180 181 if ( ! empty( $functions ) ) { 182 $docs_select = '<select name="docs-list" id="docs-list">'; 183 $docs_select .= '<option value="">' . esc_html__( 'Function Name…' ) . '</option>'; 184 185 foreach ( $functions as $function ) { 186 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>'; 187 } 188 189 $docs_select .= '</select>'; 190 } 191 } 192 193 $content = esc_textarea( $content ); 194 ?> 195 <div class="wrap"> 196 <h1><?php echo esc_html( $title ); ?></h1> 197 198 <?php 199 if ( isset( $_GET['a'] ) ) : 200 wp_admin_notice( 201 __( 'File edited successfully.' ), 202 array( 203 'additional_classes' => array( 'updated', 'is-dismissible' ), 204 'id' => 'message', 205 ) 206 ); 207 elseif ( is_wp_error( $edit_error ) ) : 208 $error = esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); 209 $message = '<p>' . __( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ) . '</p> 210 <pre>' . $error . '</pre>'; 211 wp_admin_notice( 212 $message, 213 array( 214 'type' => 'error', 215 'id' => 'message', 216 'paragraph_wrap' => false, 217 ) 218 ); 219 endif; 220 ?> 221 222 <div class="fileedit-sub"> 223 <div class="alignleft"> 224 <h2> 225 <?php 226 if ( is_plugin_active( $plugin ) ) { 227 if ( is_writable( $real_file ) ) { 228 /* translators: %s: Plugin name. */ 229 printf( __( 'Editing %s (active)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' ); 230 } else { 231 /* translators: %s: Plugin name. */ 232 printf( __( 'Browsing %s (active)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' ); 233 } 234 } else { 235 if ( is_writable( $real_file ) ) { 236 /* translators: %s: Plugin name. */ 237 printf( __( 'Editing %s (inactive)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' ); 238 } else { 239 /* translators: %s: Plugin name. */ 240 printf( __( 'Browsing %s (inactive)' ), '<strong>' . esc_html( $plugin_name ) . '</strong>' ); 241 } 242 } 243 ?> 244 </h2> 245 <?php 246 printf( 247 /* translators: %s: File path. */ 248 ' <span><strong>' . __( 'File: %s' ) . '</strong></span>', 249 esc_html( $file ) 250 ); 251 ?> 252 </div> 253 <div class="alignright"> 254 <form action="plugin-editor.php" method="get"> 255 <label for="plugin" id="theme-plugin-editor-selector"><?php _e( 'Select plugin to edit:' ); ?> </label> 256 <select name="plugin" id="plugin"> 257 <?php 258 foreach ( $plugins as $plugin_key => $a_plugin ) { 259 $plugin_name = $a_plugin['Name']; 260 if ( $plugin_key === $plugin ) { 261 $selected = " selected='selected'"; 262 } else { 263 $selected = ''; 264 } 265 $plugin_name = esc_attr( $plugin_name ); 266 $plugin_key = esc_attr( $plugin_key ); 267 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>"; 268 } 269 ?> 270 </select> 271 <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?> 272 </form> 273 </div> 274 <br class="clear" /> 275 </div> 276 277 <div id="templateside"> 278 <h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2> 279 280 <?php 281 $plugin_editable_files = array(); 282 foreach ( $plugin_files as $plugin_file ) { 283 if ( preg_match( '/\.([^.]+)$/', $plugin_file, $matches ) && in_array( $matches[1], $editable_extensions, true ) ) { 284 $plugin_editable_files[] = $plugin_file; 285 } 286 } 287 ?> 288 <ul role="tree" aria-labelledby="plugin-files-label"> 289 <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1"> 290 <ul role="group"> 291 <?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?> 292 </ul> 293 </li> 294 </ul> 295 </div> 296 297 <form name="template" id="template" action="plugin-editor.php" method="post"> 298 <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?> 299 <div> 300 <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label> 301 <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> 302 <input type="hidden" name="action" value="update" /> 303 <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" /> 304 <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" /> 305 </div> 306 307 <?php if ( ! empty( $docs_select ) ) : ?> 308 <div id="documentation" class="hide-if-no-js"> 309 <label for="docs-list"><?php _e( 'Documentation:' ); ?></label> 310 <?php echo $docs_select; ?> 311 <input disabled id="docs-lookup" 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'); }" /> 312 </div> 313 <?php endif; ?> 314 315 <?php if ( is_writable( $real_file ) ) : ?> 316 <div class="editor-notices"> 317 <?php 318 if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) ) { 319 wp_admin_notice( 320 __( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ), 321 array( 322 'type' => 'warning', 323 'additional_classes' => array( 'inline', 'active-plugin-edit-warning' ), 324 ) 325 ); 326 } 327 ?> 328 </div> 329 <p class="submit"> 330 <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?> 331 <span class="spinner"></span> 332 </p> 333 <?php else : ?> 334 <p> 335 <?php 336 printf( 337 /* translators: %s: Documentation URL. */ 338 __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ), 339 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ) 340 ); 341 ?> 342 </p> 343 <?php endif; ?> 344 345 <?php wp_print_file_editor_templates(); ?> 346 </form> 347 <br class="clear" /> 348 </div> 349 <?php 350 $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); 351 if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) : 352 // Get a back URL. 353 $referer = wp_get_referer(); 354 355 $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' ); 356 357 $return_url = admin_url( '/' ); 358 if ( $referer ) { 359 $referer_path = parse_url( $referer, PHP_URL_PATH ); 360 if ( is_string( $referer_path ) && ! in_array( basename( $referer_path ), $excluded_referer_basenames, true ) ) { 361 $return_url = $referer; 362 } 363 } 364 ?> 365 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden"> 366 <div class="notification-dialog-background"></div> 367 <div class="notification-dialog"> 368 <div class="file-editor-warning-content"> 369 <div class="file-editor-warning-message"> 370 <h1><?php _e( 'Heads up!' ); ?></h1> 371 <p><?php _e( 'You appear to be making direct edits to your plugin in the WordPress dashboard. Editing plugins directly is not recommended as it may introduce incompatibilities that break your site and your changes may be lost in future updates.' ); ?></p> 372 <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> 373 </div> 374 <p> 375 <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a> 376 <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button> 377 </p> 378 </div> 379 </div> 380 </div> 381 <?php 382 endif; // Editor warning notice. 383 384 require_once ABSPATH . 'wp-admin/admin-footer.php';
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Sep 13 08:20:28 2026 | Cross-referenced by PHPXref |