64 $plugin = array_keys( $plugins ); |
65 $plugin = array_keys( $plugins ); |
65 $plugin = $plugin[0]; |
66 $plugin = $plugin[0]; |
66 } |
67 } |
67 } |
68 } |
68 |
69 |
69 $plugin_files = get_plugin_files($plugin); |
70 $plugin_files = get_plugin_files( $plugin ); |
70 |
71 |
71 if ( empty( $file ) ) { |
72 if ( empty( $file ) ) { |
72 $file = $plugin_files[0]; |
73 $file = $plugin_files[0]; |
73 } |
74 } |
74 |
75 |
75 $file = validate_file_to_edit($file, $plugin_files); |
76 $file = validate_file_to_edit( $file, $plugin_files ); |
76 $real_file = WP_PLUGIN_DIR . '/' . $file; |
77 $real_file = WP_PLUGIN_DIR . '/' . $file; |
77 |
78 |
78 // Handle fallback editing of file when JavaScript is not available. |
79 // Handle fallback editing of file when JavaScript is not available. |
79 $edit_error = null; |
80 $edit_error = null; |
80 $posted_content = null; |
81 $posted_content = null; |
81 if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
82 if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
82 $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); |
83 $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); |
83 if ( is_wp_error( $r ) ) { |
84 if ( is_wp_error( $r ) ) { |
84 $edit_error = $r; |
85 $edit_error = $r; |
85 if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) { |
86 if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) { |
86 $posted_content = wp_unslash( $_POST['newcontent'] ); |
87 $posted_content = wp_unslash( $_POST['newcontent'] ); |
87 } |
88 } |
88 } else { |
89 } else { |
89 wp_redirect( add_query_arg( |
90 wp_redirect( |
90 array( |
91 add_query_arg( |
91 'a' => 1, // This means "success" for some reason. |
92 array( |
92 'plugin' => $plugin, |
93 'a' => 1, // This means "success" for some reason. |
93 'file' => $file, |
94 'plugin' => $plugin, |
94 ), |
95 'file' => $file, |
95 admin_url( 'plugin-editor.php' ) |
96 ), |
96 ) ); |
97 admin_url( 'plugin-editor.php' ) |
|
98 ) |
|
99 ); |
97 exit; |
100 exit; |
98 } |
101 } |
99 } |
102 } |
100 |
103 |
101 // List of allowable extensions |
104 // List of allowable extensions |
102 $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin ); |
105 $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin ); |
103 |
106 |
104 if ( ! is_file($real_file) ) { |
107 if ( ! is_file( $real_file ) ) { |
105 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.'))); |
108 wp_die( sprintf( '<p>%s</p>', __( 'No such file exists! Double check the name and try again.' ) ) ); |
106 } else { |
109 } else { |
107 // Get the extension of the file |
110 // Get the extension of the file |
108 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) { |
111 if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) { |
109 $ext = strtolower($matches[1]); |
112 $ext = strtolower( $matches[1] ); |
110 // If extension is not in the acceptable list, skip it |
113 // If extension is not in the acceptable list, skip it |
111 if ( !in_array( $ext, $editable_extensions) ) |
114 if ( ! in_array( $ext, $editable_extensions ) ) { |
112 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.'))); |
115 wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) ); |
113 } |
116 } |
114 } |
117 } |
115 |
118 } |
116 get_current_screen()->add_help_tab( array( |
119 |
117 'id' => 'overview', |
120 get_current_screen()->add_help_tab( |
118 'title' => __('Overview'), |
121 array( |
119 'content' => |
122 'id' => 'overview', |
120 '<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>' . |
123 'title' => __( 'Overview' ), |
121 '<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’t forget to save your changes (Update File) when you’re finished.') . '</p>' . |
124 'content' => |
122 '<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>' . |
125 '<p>' . __( 'You can use the plugin 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>' . |
123 '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' . |
126 '<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’t forget to save your changes (Update File) when you’re finished.' ) . '</p>' . |
124 '<ul>' . |
127 '<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>' . |
125 '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' . |
128 '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' . |
126 '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' . |
129 '<ul>' . |
127 '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' . |
130 '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' . |
128 '</ul>' . |
131 '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' . |
129 '<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>' . |
132 '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' . |
130 ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' ) |
133 '</ul>' . |
131 ) ); |
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>' : '' ), |
|
136 ) |
|
137 ); |
132 |
138 |
133 get_current_screen()->set_help_sidebar( |
139 get_current_screen()->set_help_sidebar( |
134 '<p><strong>' . __('For more information:') . '</strong></p>' . |
140 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . |
135 '<p>' . __('<a href="https://codex.wordpress.org/Plugins_Editor_Screen">Documentation on Editing Plugins</a>') . '</p>' . |
141 '<p>' . __( '<a href="https://codex.wordpress.org/Plugins_Editor_Screen">Documentation on Editing Plugins</a>' ) . '</p>' . |
136 '<p>' . __('<a href="https://codex.wordpress.org/Writing_a_Plugin">Documentation on Writing Plugins</a>') . '</p>' . |
142 '<p>' . __( '<a href="https://codex.wordpress.org/Writing_a_Plugin">Documentation on Writing Plugins</a>' ) . '</p>' . |
137 '<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>' |
143 '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>' |
138 ); |
144 ); |
139 |
145 |
140 $settings = array( |
146 $settings = array( |
141 'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ), |
147 'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ), |
142 ); |
148 ); |
143 wp_enqueue_script( 'wp-theme-plugin-editor' ); |
149 wp_enqueue_script( 'wp-theme-plugin-editor' ); |
144 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) ); |
150 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) ); |
145 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) ); |
151 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) ); |
146 |
152 |
147 require_once(ABSPATH . 'wp-admin/admin-header.php'); |
153 require_once( ABSPATH . 'wp-admin/admin-header.php' ); |
148 |
154 |
149 update_recently_edited(WP_PLUGIN_DIR . '/' . $file); |
155 update_recently_edited( WP_PLUGIN_DIR . '/' . $file ); |
150 |
156 |
151 if ( ! empty( $posted_content ) ) { |
157 if ( ! empty( $posted_content ) ) { |
152 $content = $posted_content; |
158 $content = $posted_content; |
153 } else { |
159 } else { |
154 $content = file_get_contents( $real_file ); |
160 $content = file_get_contents( $real_file ); |
155 } |
161 } |
156 |
162 |
157 if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) { |
163 if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) { |
158 $functions = wp_doc_link_parse( $content ); |
164 $functions = wp_doc_link_parse( $content ); |
159 |
165 |
160 if ( !empty($functions) ) { |
166 if ( ! empty( $functions ) ) { |
161 $docs_select = '<select name="docs-list" id="docs-list">'; |
167 $docs_select = '<select name="docs-list" id="docs-list">'; |
162 $docs_select .= '<option value="">' . __( 'Function Name…' ) . '</option>'; |
168 $docs_select .= '<option value="">' . __( 'Function Name…' ) . '</option>'; |
163 foreach ( $functions as $function) { |
169 foreach ( $functions as $function ) { |
164 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>'; |
170 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>'; |
165 } |
171 } |
166 $docs_select .= '</select>'; |
172 $docs_select .= '</select>'; |
167 } |
173 } |
168 } |
174 } |
255 <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> |
262 <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> |
256 <input type="hidden" name="action" value="update" /> |
263 <input type="hidden" name="action" value="update" /> |
257 <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" /> |
264 <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" /> |
258 <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" /> |
265 <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" /> |
259 </div> |
266 </div> |
260 <?php if ( !empty( $docs_select ) ) : ?> |
267 <?php if ( ! empty( $docs_select ) ) : ?> |
261 <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() ) + '&locale=<?php echo urlencode( get_user_locale() ) ?>&version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&redirect=true'); }" /></div> |
268 <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e( 'Documentation:' ); ?></label> <?php echo $docs_select; ?> <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'); }" /></div> |
262 <?php endif; ?> |
269 <?php endif; ?> |
263 <?php if ( is_writeable($real_file) ) : ?> |
270 <?php if ( is_writeable( $real_file ) ) : ?> |
264 <div class="editor-notices"> |
271 <div class="editor-notices"> |
265 <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?> |
272 <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?> |
266 <div class="notice notice-warning inline active-plugin-edit-warning"> |
273 <div class="notice notice-warning inline active-plugin-edit-warning"> |
267 <p><?php _e('<strong>Warning:</strong> Making changes to active plugins is not recommended.'); ?></p> |
274 <p><?php _e( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ); ?></p> |
268 </div> |
275 </div> |
269 <?php } ?> |
276 <?php } ?> |
270 </div> |
277 </div> |
271 <p class="submit"> |
278 <p class="submit"> |
272 <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?> |
279 <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?> |
273 <span class="spinner"></span> |
280 <span class="spinner"></span> |
274 </p> |
281 </p> |
275 <?php else : ?> |
282 <?php else : ?> |
276 <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> |
283 <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> |
277 <?php endif; ?> |
284 <?php endif; ?> |
278 <?php wp_print_file_editor_templates(); ?> |
285 <?php wp_print_file_editor_templates(); ?> |
279 </form> |
286 </form> |
280 <br class="clear" /> |
287 <br class="clear" /> |
281 </div> |
288 </div> |
282 <?php |
289 <?php |
283 $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
290 $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
284 if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) : |
291 if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) : |
285 // Get a back URL |
292 // Get a back URL |
286 $referer = wp_get_referer(); |
293 $referer = wp_get_referer(); |
287 $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' ); |
294 $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' ); |
288 |
295 |
289 if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) { |
296 if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) { |
290 $return_url = $referer; |
297 $return_url = $referer; |
291 } else { |
298 } else { |
292 $return_url = admin_url( '/' ); |
299 $return_url = admin_url( '/' ); |
293 } |
300 } |
294 ?> |
301 ?> |
295 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden"> |
302 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden"> |
296 <div class="notification-dialog-background"></div> |
303 <div class="notification-dialog-background"></div> |
297 <div class="notification-dialog"> |
304 <div class="notification-dialog"> |
298 <div class="file-editor-warning-content"> |
305 <div class="file-editor-warning-content"> |
299 <div class="file-editor-warning-message"> |
306 <div class="file-editor-warning-message"> |