PageRenderTime 27ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-admin/plugin-editor.php

https://github.com/enoex/artisanchairs
PHP | 230 lines | 181 code | 35 blank | 14 comment | 39 complexity | f281fc531319443171cde14b73e8f096 MD5 | raw file
  1. <?php
  2. /**
  3. * Edit plugin editor administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once('admin.php');
  10. $title = __("Edit Plugins");
  11. $parent_file = 'plugins.php';
  12. wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'plugin'));
  13. wp_admin_css( 'theme-editor' );
  14. $plugins = get_plugins();
  15. if ( isset($_REQUEST['file']) )
  16. $plugin = $_REQUEST['file'];
  17. if ( empty($plugin) ) {
  18. $plugin = array_keys($plugins);
  19. $plugin = $plugin[0];
  20. }
  21. $plugin_files = get_plugin_files($plugin);
  22. if ( empty($file) )
  23. $file = $plugin_files[0];
  24. $file = validate_file_to_edit($file, $plugin_files);
  25. $real_file = WP_PLUGIN_DIR . '/' . $file;
  26. switch ( $action ) {
  27. case 'update':
  28. check_admin_referer('edit-plugin_' . $file);
  29. if ( !current_user_can('edit_plugins') )
  30. wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this blog.').'</p>');
  31. $newcontent = stripslashes($_POST['newcontent']);
  32. if ( is_writeable($real_file) ) {
  33. $f = fopen($real_file, 'w+');
  34. fwrite($f, $newcontent);
  35. fclose($f);
  36. // Deactivate so we can test it.
  37. if ( is_plugin_active($file) || isset($_POST['phperror']) ) {
  38. if ( is_plugin_active($file) )
  39. deactivate_plugins($file, true);
  40. wp_redirect(add_query_arg('_wpnonce', wp_create_nonce('edit-plugin-test_' . $file), "plugin-editor.php?file=$file&liveupdate=1"));
  41. exit;
  42. }
  43. wp_redirect("plugin-editor.php?file=$file&a=te");
  44. } else {
  45. wp_redirect("plugin-editor.php?file=$file");
  46. }
  47. exit;
  48. break;
  49. default:
  50. if ( !current_user_can('edit_plugins') )
  51. wp_die('<p>'.__('You do not have sufficient permissions to edit plugins for this blog.').'</p>');
  52. if ( isset($_GET['liveupdate']) ) {
  53. check_admin_referer('edit-plugin-test_' . $file);
  54. $error = validate_plugin($file);
  55. if ( is_wp_error($error) )
  56. wp_die( $error );
  57. if ( ! is_plugin_active($file) )
  58. activate_plugin($file, "plugin-editor.php?file=$file&phperror=1"); // we'll override this later if the plugin can be included without fatal error
  59. wp_redirect("plugin-editor.php?file=$file&a=te");
  60. exit;
  61. }
  62. // List of allowable extensions
  63. $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
  64. $editable_extensions = (array) apply_filters('editable_extensions', $editable_extensions);
  65. if ( ! is_file($real_file) ) {
  66. wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
  67. } else {
  68. // Get the extension of the file
  69. if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
  70. $ext = strtolower($matches[1]);
  71. // If extension is not in the acceptable list, skip it
  72. if ( !in_array( $ext, $editable_extensions) )
  73. wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
  74. }
  75. }
  76. require_once('admin-header.php');
  77. update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
  78. $content = file_get_contents( $real_file );
  79. if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
  80. $functions = wp_doc_link_parse( $content );
  81. if ( !empty($functions) ) {
  82. $docs_select = '<select name="docs-list" id="docs-list">';
  83. $docs_select .= '<option value="">' . __( 'Function Name...' ) . '</option>';
  84. foreach ( $functions as $function) {
  85. $docs_select .= '<option value="' . esc_attr( $function ) . '">' . htmlspecialchars( $function ) . '()</option>';
  86. }
  87. $docs_select .= '</select>';
  88. }
  89. }
  90. $content = htmlspecialchars( $content );
  91. $codepress_lang = codepress_get_lang($real_file);
  92. ?>
  93. <?php if (isset($_GET['a'])) : ?>
  94. <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
  95. <?php elseif (isset($_GET['phperror'])) : ?>
  96. <div id="message" class="updated fade"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
  97. <?php
  98. if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $file) ) { ?>
  99. <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>
  100. <?php } ?>
  101. </div>
  102. <?php endif; ?>
  103. <div class="wrap">
  104. <?php screen_icon(); ?>
  105. <h2><?php echo esc_html( $title ); ?></h2>
  106. <div class="fileedit-sub">
  107. <div class="alignleft">
  108. <big><?php
  109. if ( is_plugin_active($plugin) ) {
  110. if ( is_writeable($real_file) )
  111. echo sprintf(__('Editing <strong>%s</strong> (active)'), $file);
  112. else
  113. echo sprintf(__('Browsing <strong>%s</strong> (active)'), $file);
  114. } else {
  115. if ( is_writeable($real_file) )
  116. echo sprintf(__('Editing <strong>%s</strong> (inactive)'), $file);
  117. else
  118. echo sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file);
  119. }
  120. ?></big>
  121. </div>
  122. <div class="alignright">
  123. <form action="plugin-editor.php" method="post">
  124. <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
  125. <select name="plugin" id="plugin">
  126. <?php
  127. foreach ( $plugins as $plugin_key => $a_plugin ) {
  128. $plugin_name = $a_plugin['Name'];
  129. if ( $plugin_key == $plugin )
  130. $selected = " selected='selected'";
  131. else
  132. $selected = '';
  133. $plugin_name = esc_attr($plugin_name);
  134. $plugin_key = esc_attr($plugin_key);
  135. echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
  136. }
  137. ?>
  138. </select>
  139. <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
  140. </form>
  141. </div>
  142. <br class="clear" />
  143. </div>
  144. <div id="templateside">
  145. <h3><?php _e('Plugin Files'); ?></h3>
  146. <ul>
  147. <?php
  148. foreach ( $plugin_files as $plugin_file ) :
  149. // Get the extension of the file
  150. if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
  151. $ext = strtolower($matches[1]);
  152. // If extension is not in the acceptable list, skip it
  153. if ( !in_array( $ext, $editable_extensions ) )
  154. continue;
  155. } else {
  156. // No extension found
  157. continue;
  158. }
  159. ?>
  160. <li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo $plugin_file; ?>&amp;plugin=<?php echo $plugin; ?>"><?php echo $plugin_file ?></a></li>
  161. <?php endforeach; ?>
  162. </ul>
  163. </div>
  164. <form name="template" id="template" action="plugin-editor.php" method="post">
  165. <?php wp_nonce_field('edit-plugin_' . $file) ?>
  166. <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1" class="codepress <?php echo $codepress_lang ?>"><?php echo $content ?></textarea>
  167. <input type="hidden" name="action" value="update" />
  168. <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
  169. <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
  170. </div>
  171. <?php if ( !empty( $docs_select ) ) : ?>
  172. <div id="documentation"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Lookup' ) ?> " 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>
  173. <?php endif; ?>
  174. <?php if ( is_writeable($real_file) ) : ?>
  175. <?php if ( in_array($file, (array) get_option('active_plugins')) ) { ?>
  176. <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>
  177. <?php } ?>
  178. <p class="submit">
  179. <?php
  180. if ( isset($_GET['phperror']) )
  181. echo "<input type='hidden' name='phperror' value='1' /><input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File and Attempt to Reactivate') . "' tabindex='2' />";
  182. else
  183. echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
  184. ?>
  185. </p>
  186. <?php else : ?>
  187. <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>
  188. <?php endif; ?>
  189. </form>
  190. <br class="clear" />
  191. </div>
  192. <?php
  193. break;
  194. }
  195. include("admin-footer.php");