PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/theme-editor.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 256 lines | 211 code | 33 blank | 12 comment | 42 complexity | 169b99b1536890f9bd1453ff8dceace9 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme editor administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once('./admin.php');
  10. if ( !current_user_can('edit_themes') )
  11. wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site.').'</p>');
  12. $title = __("Edit Themes");
  13. $parent_file = 'themes.php';
  14. $help = '<p>' . __('You can use the Theme Editor to edit the individual CSS and PHP files which make up your theme.') . '</p>';
  15. $help .= '<p>' . __('Begin by choosing a theme to edit from the dropdown menu and clicking Select. A list then appears of all the template files. Clicking once on any file name causes the file to appear in the large Editor box.') . '</p>';
  16. $help .= '<p>' . __('For PHP files, you can use the Documentation dropdown to select from functions recognized in that file. Lookup takes you to a web page with reference material about that particular function.') . '</p>';
  17. $help .= '<p>' . __('After typing in your edits, click Update File.') . '</p>';
  18. $help .= '<p>' . __('<strong>Advice:</strong> think very carefully about your site crashing if you are live-editing the theme currently in use.') . '</p>';
  19. $help .= '<p>' . __('Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="http://codex.wordpress.org/Child_Themes">child theme</a> instead.') . '</p>';
  20. $help .= '<p><strong>' . __('For more information:') . '</strong></p>';
  21. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Theme_Development">Documentation on Theme Development</a>') . '</p>';
  22. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Using_Themes">Documentation on Using Themes</a>') . '</p>';
  23. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Editing_Files">Documentation on Editing Files</a>') . '</p>';
  24. $help .= '<p>' . __('<a href="http://codex.wordpress.org/Template_Tags">Documentation on Template Tags</a>') . '</p>';
  25. $help .= '<p>' . __('<a href="http://wordpress.org/support/">Support Forums</a>') . '</p>';
  26. add_contextual_help($current_screen, $help);
  27. wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
  28. wp_admin_css( 'theme-editor' );
  29. $themes = get_themes();
  30. if (empty($theme)) {
  31. $theme = get_current_theme();
  32. } else {
  33. $theme = stripslashes($theme);
  34. }
  35. if ( ! isset($themes[$theme]) )
  36. wp_die(__('The requested theme does not exist.'));
  37. $allowed_files = array_merge($themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files']);
  38. if (empty($file)) {
  39. $file = $allowed_files[0];
  40. } else {
  41. $file = stripslashes($file);
  42. if ( 'theme' == $dir ) {
  43. $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ;
  44. } else if ( 'style' == $dir) {
  45. $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ;
  46. }
  47. }
  48. validate_file_to_edit($file, $allowed_files);
  49. $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
  50. $file_show = basename( $file );
  51. switch($action) {
  52. case 'update':
  53. check_admin_referer('edit-theme_' . $file . $theme);
  54. $newcontent = stripslashes($_POST['newcontent']);
  55. $theme = urlencode($theme);
  56. if (is_writeable($file)) {
  57. //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
  58. $f = fopen($file, 'w+');
  59. if ($f !== FALSE) {
  60. fwrite($f, $newcontent);
  61. fclose($f);
  62. $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
  63. } else {
  64. $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
  65. }
  66. } else {
  67. $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
  68. }
  69. $location = wp_kses_no_null($location);
  70. $strip = array('%0d', '%0a', '%0D', '%0A');
  71. $location = _deep_replace($strip, $location);
  72. header("Location: $location");
  73. exit();
  74. break;
  75. default:
  76. require_once('./admin-header.php');
  77. update_recently_edited($file);
  78. if ( !is_file($file) )
  79. $error = 1;
  80. if ( !$error && filesize($file) > 0 ) {
  81. $f = fopen($file, 'r');
  82. $content = fread($f, filesize($file));
  83. if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
  84. $functions = wp_doc_link_parse( $content );
  85. $docs_select = '<select name="docs-list" id="docs-list">';
  86. $docs_select .= '<option value="">' . esc_attr__( 'Function Name...' ) . '</option>';
  87. foreach ( $functions as $function ) {
  88. $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
  89. }
  90. $docs_select .= '</select>';
  91. }
  92. $content = htmlspecialchars( $content );
  93. }
  94. ?>
  95. <?php if (isset($_GET['a'])) : ?>
  96. <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
  97. <?php endif;
  98. $description = get_file_description($file);
  99. $desc_header = ( $description != $file_show ) ? "<strong>$description</strong> (%s)" : "%s";
  100. ?>
  101. <div class="wrap">
  102. <?php screen_icon(); ?>
  103. <h2><?php echo esc_html( $title ); ?></h2>
  104. <div class="fileedit-sub">
  105. <div class="alignleft">
  106. <big><?php echo sprintf($desc_header, $file_show); ?></big>
  107. </div>
  108. <div class="alignright">
  109. <form action="theme-editor.php" method="post">
  110. <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
  111. <select name="theme" id="theme">
  112. <?php
  113. foreach ($themes as $a_theme) {
  114. $theme_name = $a_theme['Name'];
  115. if ($theme_name == $theme) $selected = " selected='selected'";
  116. else $selected = '';
  117. $theme_name = esc_attr($theme_name);
  118. echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
  119. }
  120. ?>
  121. </select>
  122. <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
  123. </form>
  124. </div>
  125. <br class="clear" />
  126. </div>
  127. <div id="templateside">
  128. <?php
  129. if ($allowed_files) :
  130. ?>
  131. <h3><?php _e('Templates'); ?></h3>
  132. <ul>
  133. <?php
  134. $template_mapping = array();
  135. $template_dir = $themes[$theme]['Template Dir'];
  136. foreach ( $themes[$theme]['Template Files'] as $template_file ) {
  137. $description = trim( get_file_description($template_file) );
  138. $template_show = basename($template_file);
  139. $filedesc = ( $description != $template_file ) ? "$description<br /><span class='nonessential'>($template_show)</span>" : "$description";
  140. $filedesc = ( $template_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($template_show)</span></span>" : $filedesc;
  141. // If we have two files of the same name prefer the one in the Template Directory
  142. // This means that we display the correct files for child themes which overload Templates as well as Styles
  143. if ( array_key_exists($description, $template_mapping ) ) {
  144. if ( false !== strpos( $template_file, $template_dir ) ) {
  145. $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
  146. }
  147. } else {
  148. $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
  149. }
  150. }
  151. ksort( $template_mapping );
  152. while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
  153. ?>
  154. <li><a href="theme-editor.php?file=<?php echo "$template_file"; ?>&amp;theme=<?php echo urlencode($theme) ?>&amp;dir=theme"><?php echo $filedesc ?></a></li>
  155. <?php endwhile; ?>
  156. </ul>
  157. <h3><?php /* translators: Theme stylesheets in theme editor */ _ex('Styles', 'Theme stylesheets in theme editor'); ?></h3>
  158. <ul>
  159. <?php
  160. $template_mapping = array();
  161. $stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
  162. foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
  163. $description = trim( get_file_description($style_file) );
  164. $style_show = basename($style_file);
  165. $filedesc = ( $description != $style_file ) ? "$description<br /><span class='nonessential'>($style_show)</span>" : "$description";
  166. $filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($style_show)</span></span>" : $filedesc;
  167. $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
  168. }
  169. ksort( $template_mapping );
  170. while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
  171. ?>
  172. <li><a href="theme-editor.php?file=<?php echo "$style_file"; ?>&amp;theme=<?php echo urlencode($theme) ?>&amp;dir=style"><?php echo $filedesc ?></a></li>
  173. <?php endwhile; ?>
  174. </ul>
  175. <?php endif; ?>
  176. </div>
  177. <?php if (!$error) { ?>
  178. <form name="template" id="template" action="theme-editor.php" method="post">
  179. <?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
  180. <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
  181. <input type="hidden" name="action" value="update" />
  182. <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
  183. <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
  184. <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
  185. </div>
  186. <?php if ( isset($functions ) && count($functions) ) { ?>
  187. <div id="documentation" class="hide-if-no-js">
  188. <label for="docs-list"><?php _e('Documentation:') ?></label>
  189. <?php echo $docs_select; ?>
  190. <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'); }" />
  191. </div>
  192. <?php } ?>
  193. <div>
  194. <?php if ( is_writeable($file) ) : ?>
  195. <p class="submit">
  196. <?php
  197. echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
  198. ?>
  199. </p>
  200. <?php else : ?>
  201. <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>
  202. <?php endif; ?>
  203. </div>
  204. </form>
  205. <?php
  206. } else {
  207. echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
  208. }
  209. ?>
  210. <br class="clear" />
  211. </div>
  212. <script type="text/javascript">
  213. /* <![CDATA[ */
  214. jQuery(document).ready(function($){
  215. $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
  216. $('#newcontent').scrollTop( $('#scrollto').val() );
  217. });
  218. /* ]]> */
  219. </script>
  220. <?php
  221. break;
  222. }
  223. include("./admin-footer.php");