PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/plugin-editor.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 342 lines | 284 code | 38 blank | 20 comment | 47 complexity | 5b6697c0f84377dedf0de1aa27a7943b MD5 | raw file
  1. <?php
  2. /**
  3. * Edit plugin file editor administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once __DIR__ . '/admin.php';
  10. if ( is_multisite() && ! is_network_admin() ) {
  11. wp_redirect( network_admin_url( 'plugin-editor.php' ) );
  12. exit;
  13. }
  14. if ( ! current_user_can( 'edit_plugins' ) ) {
  15. wp_die( __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
  16. }
  17. // Used in the HTML title tag.
  18. $title = __( 'Edit Plugins' );
  19. $parent_file = 'plugins.php';
  20. $plugins = get_plugins();
  21. if ( empty( $plugins ) ) {
  22. require_once ABSPATH . 'wp-admin/admin-header.php';
  23. ?>
  24. <div class="wrap">
  25. <h1><?php echo esc_html( $title ); ?></h1>
  26. <div id="message" class="error"><p><?php _e( 'No plugins are currently available.' ); ?></p></div>
  27. </div>
  28. <?php
  29. require_once ABSPATH . 'wp-admin/admin-footer.php';
  30. exit;
  31. }
  32. $file = '';
  33. $plugin = '';
  34. if ( isset( $_REQUEST['file'] ) ) {
  35. $file = wp_unslash( $_REQUEST['file'] );
  36. }
  37. if ( isset( $_REQUEST['plugin'] ) ) {
  38. $plugin = wp_unslash( $_REQUEST['plugin'] );
  39. }
  40. if ( empty( $plugin ) ) {
  41. if ( $file ) {
  42. // Locate the plugin for a given plugin file being edited.
  43. $file_dirname = dirname( $file );
  44. foreach ( array_keys( $plugins ) as $plugin_candidate ) {
  45. if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) {
  46. $plugin = $plugin_candidate;
  47. break;
  48. }
  49. }
  50. // Fallback to the file as the plugin.
  51. if ( empty( $plugin ) ) {
  52. $plugin = $file;
  53. }
  54. } else {
  55. $plugin = array_keys( $plugins );
  56. $plugin = $plugin[0];
  57. }
  58. }
  59. $plugin_files = get_plugin_files( $plugin );
  60. if ( empty( $file ) ) {
  61. $file = $plugin_files[0];
  62. }
  63. $file = validate_file_to_edit( $file, $plugin_files );
  64. $real_file = WP_PLUGIN_DIR . '/' . $file;
  65. // Handle fallback editing of file when JavaScript is not available.
  66. $edit_error = null;
  67. $posted_content = null;
  68. if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
  69. $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
  70. if ( is_wp_error( $r ) ) {
  71. $edit_error = $r;
  72. if ( check_ajax_referer( 'edit-plugin_' . $file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
  73. $posted_content = wp_unslash( $_POST['newcontent'] );
  74. }
  75. } else {
  76. wp_redirect(
  77. add_query_arg(
  78. array(
  79. 'a' => 1, // This means "success" for some reason.
  80. 'plugin' => $plugin,
  81. 'file' => $file,
  82. ),
  83. admin_url( 'plugin-editor.php' )
  84. )
  85. );
  86. exit;
  87. }
  88. }
  89. // List of allowable extensions.
  90. $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
  91. if ( ! is_file( $real_file ) ) {
  92. wp_die( sprintf( '<p>%s</p>', __( 'File does not exist! Please double check the name and try again.' ) ) );
  93. } else {
  94. // Get the extension of the file.
  95. if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
  96. $ext = strtolower( $matches[1] );
  97. // If extension is not in the acceptable list, skip it.
  98. if ( ! in_array( $ext, $editable_extensions, true ) ) {
  99. wp_die( sprintf( '<p>%s</p>', __( 'Files of this type are not editable.' ) ) );
  100. }
  101. }
  102. }
  103. get_current_screen()->add_help_tab(
  104. array(
  105. 'id' => 'overview',
  106. 'title' => __( 'Overview' ),
  107. 'content' =>
  108. '<p>' . __( 'You can use the plugin file 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>' .
  109. '<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>' .
  110. '<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>' .
  111. '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
  112. '<ul>' .
  113. '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
  114. '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
  115. '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
  116. '</ul>' .
  117. '<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>' .
  118. ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
  119. )
  120. );
  121. get_current_screen()->set_help_sidebar(
  122. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  123. '<p>' . __( '<a href="https://wordpress.org/support/article/plugins-editor-screen/">Documentation on Editing Plugins</a>' ) . '</p>' .
  124. '<p>' . __( '<a href="https://developer.wordpress.org/plugins/">Documentation on Writing Plugins</a>' ) . '</p>' .
  125. '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  126. );
  127. $settings = array(
  128. 'codeEditor' => wp_enqueue_code_editor( array( 'file' => $real_file ) ),
  129. );
  130. wp_enqueue_script( 'wp-theme-plugin-editor' );
  131. wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
  132. wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.themeOrPlugin = "plugin";' ) );
  133. require_once ABSPATH . 'wp-admin/admin-header.php';
  134. update_recently_edited( WP_PLUGIN_DIR . '/' . $file );
  135. if ( ! empty( $posted_content ) ) {
  136. $content = $posted_content;
  137. } else {
  138. $content = file_get_contents( $real_file );
  139. }
  140. if ( '.php' === substr( $real_file, strrpos( $real_file, '.' ) ) ) {
  141. $functions = wp_doc_link_parse( $content );
  142. if ( ! empty( $functions ) ) {
  143. $docs_select = '<select name="docs-list" id="docs-list">';
  144. $docs_select .= '<option value="">' . __( 'Function Name&hellip;' ) . '</option>';
  145. foreach ( $functions as $function ) {
  146. $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
  147. }
  148. $docs_select .= '</select>';
  149. }
  150. }
  151. $content = esc_textarea( $content );
  152. ?>
  153. <div class="wrap">
  154. <h1><?php echo esc_html( $title ); ?></h1>
  155. <?php if ( isset( $_GET['a'] ) ) : ?>
  156. <div id="message" class="updated notice is-dismissible">
  157. <p><?php _e( 'File edited successfully.' ); ?></p>
  158. </div>
  159. <?php elseif ( is_wp_error( $edit_error ) ) : ?>
  160. <div id="message" class="notice notice-error">
  161. <p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
  162. <pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
  163. </div>
  164. <?php endif; ?>
  165. <div class="fileedit-sub">
  166. <div class="alignleft">
  167. <h2>
  168. <?php
  169. if ( is_plugin_active( $plugin ) ) {
  170. if ( is_writable( $real_file ) ) {
  171. /* translators: %s: Plugin file name. */
  172. printf( __( 'Editing %s (active)' ), '<strong>' . esc_html( $file ) . '</strong>' );
  173. } else {
  174. /* translators: %s: Plugin file name. */
  175. printf( __( 'Browsing %s (active)' ), '<strong>' . esc_html( $file ) . '</strong>' );
  176. }
  177. } else {
  178. if ( is_writable( $real_file ) ) {
  179. /* translators: %s: Plugin file name. */
  180. printf( __( 'Editing %s (inactive)' ), '<strong>' . esc_html( $file ) . '</strong>' );
  181. } else {
  182. /* translators: %s: Plugin file name. */
  183. printf( __( 'Browsing %s (inactive)' ), '<strong>' . esc_html( $file ) . '</strong>' );
  184. }
  185. }
  186. ?>
  187. </h2>
  188. </div>
  189. <div class="alignright">
  190. <form action="plugin-editor.php" method="get">
  191. <label for="plugin" id="theme-plugin-editor-selector"><?php _e( 'Select plugin to edit:' ); ?> </label>
  192. <select name="plugin" id="plugin">
  193. <?php
  194. foreach ( $plugins as $plugin_key => $a_plugin ) {
  195. $plugin_name = $a_plugin['Name'];
  196. if ( $plugin_key === $plugin ) {
  197. $selected = " selected='selected'";
  198. } else {
  199. $selected = '';
  200. }
  201. $plugin_name = esc_attr( $plugin_name );
  202. $plugin_key = esc_attr( $plugin_key );
  203. echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
  204. }
  205. ?>
  206. </select>
  207. <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
  208. </form>
  209. </div>
  210. <br class="clear" />
  211. </div>
  212. <div id="templateside">
  213. <h2 id="plugin-files-label"><?php _e( 'Plugin Files' ); ?></h2>
  214. <?php
  215. $plugin_editable_files = array();
  216. foreach ( $plugin_files as $plugin_file ) {
  217. if ( preg_match( '/\.([^.]+)$/', $plugin_file, $matches ) && in_array( $matches[1], $editable_extensions, true ) ) {
  218. $plugin_editable_files[] = $plugin_file;
  219. }
  220. }
  221. ?>
  222. <ul role="tree" aria-labelledby="plugin-files-label">
  223. <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
  224. <ul role="group">
  225. <?php wp_print_plugin_file_tree( wp_make_plugin_file_tree( $plugin_editable_files ) ); ?>
  226. </ul>
  227. </ul>
  228. </div>
  229. <form name="template" id="template" action="plugin-editor.php" method="post">
  230. <?php wp_nonce_field( 'edit-plugin_' . $file, 'nonce' ); ?>
  231. <div>
  232. <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
  233. <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>
  234. <input type="hidden" name="action" value="update" />
  235. <input type="hidden" name="file" value="<?php echo esc_attr( $file ); ?>" />
  236. <input type="hidden" name="plugin" value="<?php echo esc_attr( $plugin ); ?>" />
  237. </div>
  238. <?php if ( ! empty( $docs_select ) ) : ?>
  239. <div id="documentation" class="hide-if-no-js">
  240. <label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
  241. <?php echo $docs_select; ?>
  242. <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() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ); ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ); ?>&amp;redirect=true'); }" />
  243. </div>
  244. <?php endif; ?>
  245. <?php if ( is_writable( $real_file ) ) : ?>
  246. <div class="editor-notices">
  247. <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) ) { ?>
  248. <div class="notice notice-warning inline active-plugin-edit-warning">
  249. <p><?php _e( '<strong>Warning:</strong> Making changes to active plugins is not recommended.' ); ?></p>
  250. </div>
  251. <?php } ?>
  252. </div>
  253. <p class="submit">
  254. <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
  255. <span class="spinner"></span>
  256. </p>
  257. <?php else : ?>
  258. <p>
  259. <?php
  260. printf(
  261. /* translators: %s: Documentation URL. */
  262. __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
  263. __( 'https://wordpress.org/support/article/changing-file-permissions/' )
  264. );
  265. ?>
  266. </p>
  267. <?php endif; ?>
  268. <?php wp_print_file_editor_templates(); ?>
  269. </form>
  270. <br class="clear" />
  271. </div>
  272. <?php
  273. $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
  274. if ( ! in_array( 'plugin_editor_notice', $dismissed_pointers, true ) ) :
  275. // Get a back URL.
  276. $referer = wp_get_referer();
  277. $excluded_referer_basenames = array( 'plugin-editor.php', 'wp-login.php' );
  278. $return_url = admin_url( '/' );
  279. if ( $referer ) {
  280. $referer_path = parse_url( $referer, PHP_URL_PATH );
  281. if ( is_string( $referer_path ) && ! in_array( basename( $referer_path ), $excluded_referer_basenames, true ) ) {
  282. $return_url = $referer;
  283. }
  284. }
  285. ?>
  286. <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
  287. <div class="notification-dialog-background"></div>
  288. <div class="notification-dialog">
  289. <div class="file-editor-warning-content">
  290. <div class="file-editor-warning-message">
  291. <h1><?php _e( 'Heads up!' ); ?></h1>
  292. <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>
  293. <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>
  294. </div>
  295. <p>
  296. <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
  297. <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
  298. </p>
  299. </div>
  300. </div>
  301. </div>
  302. <?php
  303. endif; // Editor warning notice.
  304. require_once ABSPATH . 'wp-admin/admin-footer.php';