PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/theme-editor.php

https://gitlab.com/webkod3r/tripolis
PHP | 298 lines | 241 code | 39 blank | 18 comment | 49 complexity | 05eb2dedae5434cbe2949400b3c8dc7d 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( dirname( __FILE__ ) . '/admin.php' );
  10. if ( is_multisite() && ! is_network_admin() ) {
  11. wp_redirect( network_admin_url( 'theme-editor.php' ) );
  12. exit();
  13. }
  14. if ( !current_user_can('edit_themes') )
  15. wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site.').'</p>');
  16. $title = __("Edit Themes");
  17. $parent_file = 'themes.php';
  18. get_current_screen()->add_help_tab( array(
  19. 'id' => 'overview',
  20. 'title' => __('Overview'),
  21. 'content' =>
  22. '<p>' . __('You can use the Theme Editor to edit the individual CSS and PHP files which make up your theme.') . '</p>
  23. <p>' . __("Begin by choosing a theme to edit from the dropdown menu and clicking Select. A list then appears of the theme's template files. Clicking once on any file name causes the file to appear in the large Editor box.") . '</p>
  24. <p>' . __('For PHP files, you can use the Documentation dropdown to select from functions recognized in that file. Look Up takes you to a web page with reference material about that particular function.') . '</p>
  25. <p id="newcontent-description">' . __( 'In the editing area the Tab key enters a tab character. To move below this area by pressing Tab, press the Esc key followed by the Tab key. In some cases the Esc key will need to be pressed twice before the Tab key will allow you to continue.' ) . '</p>
  26. <p>' . __('After typing in your edits, click Update File.') . '</p>
  27. <p>' . __('<strong>Advice:</strong> think very carefully about your site crashing if you are live-editing the theme currently in use.') . '</p>
  28. <p>' . sprintf( __('Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s" target="_blank">child theme</a> instead.'), __('https://codex.wordpress.org/Child_Themes') ) . '</p>' .
  29. ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
  30. ) );
  31. get_current_screen()->set_help_sidebar(
  32. '<p><strong>' . __('For more information:') . '</strong></p>' .
  33. '<p>' . __('<a href="https://codex.wordpress.org/Theme_Development" target="_blank">Documentation on Theme Development</a>') . '</p>' .
  34. '<p>' . __('<a href="https://codex.wordpress.org/Using_Themes" target="_blank">Documentation on Using Themes</a>') . '</p>' .
  35. '<p>' . __('<a href="https://codex.wordpress.org/Editing_Files" target="_blank">Documentation on Editing Files</a>') . '</p>' .
  36. '<p>' . __('<a href="https://codex.wordpress.org/Template_Tags" target="_blank">Documentation on Template Tags</a>') . '</p>' .
  37. '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
  38. );
  39. wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
  40. if ( $theme ) {
  41. $stylesheet = $theme;
  42. } else {
  43. $stylesheet = get_stylesheet();
  44. }
  45. $theme = wp_get_theme( $stylesheet );
  46. if ( ! $theme->exists() ) {
  47. wp_die( __( 'The requested theme does not exist.' ) );
  48. }
  49. if ( $theme->errors() && 'theme_no_stylesheet' == $theme->errors()->get_error_code() ) {
  50. wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
  51. }
  52. $allowed_files = $style_files = array();
  53. $has_templates = false;
  54. $default_types = array( 'php', 'css' );
  55. /**
  56. * Filter the list of file types allowed for editing in the Theme editor.
  57. *
  58. * @since 4.4.0
  59. *
  60. * @param array $default_types List of file types. Default types include 'php' and 'css'.
  61. * @param WP_Theme $theme The current Theme object.
  62. */
  63. $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
  64. // Ensure that default types are still there.
  65. $file_types = array_unique( array_merge( $file_types, $default_types ) );
  66. foreach ( $file_types as $type ) {
  67. switch ( $type ) {
  68. case 'php':
  69. $allowed_files += $theme->get_files( 'php', 1 );
  70. $has_templates = ! empty( $allowed_files );
  71. break;
  72. case 'css':
  73. $style_files = $theme->get_files( 'css' );
  74. $allowed_files['style.css'] = $style_files['style.css'];
  75. $allowed_files += $style_files;
  76. break;
  77. default:
  78. $allowed_files += $theme->get_files( $type );
  79. break;
  80. }
  81. }
  82. if ( empty( $file ) ) {
  83. $relative_file = 'style.css';
  84. $file = $allowed_files['style.css'];
  85. } else {
  86. $relative_file = $file;
  87. $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
  88. }
  89. validate_file_to_edit( $file, $allowed_files );
  90. $scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0;
  91. switch( $action ) {
  92. case 'update':
  93. check_admin_referer( 'edit-theme_' . $file . $stylesheet );
  94. $newcontent = wp_unslash( $_POST['newcontent'] );
  95. $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto;
  96. if ( is_writeable( $file ) ) {
  97. // is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
  98. $f = fopen( $file, 'w+' );
  99. if ( $f !== false ) {
  100. fwrite( $f, $newcontent );
  101. fclose( $f );
  102. $location .= '&updated=true';
  103. $theme->cache_delete();
  104. }
  105. }
  106. wp_redirect( $location );
  107. exit;
  108. default:
  109. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  110. update_recently_edited( $file );
  111. if ( ! is_file( $file ) )
  112. $error = true;
  113. $content = '';
  114. if ( ! $error && filesize( $file ) > 0 ) {
  115. $f = fopen($file, 'r');
  116. $content = fread($f, filesize($file));
  117. if ( '.php' == substr( $file, strrpos( $file, '.' ) ) ) {
  118. $functions = wp_doc_link_parse( $content );
  119. $docs_select = '<select name="docs-list" id="docs-list">';
  120. $docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
  121. foreach ( $functions as $function ) {
  122. $docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
  123. }
  124. $docs_select .= '</select>';
  125. }
  126. $content = esc_textarea( $content );
  127. }
  128. if ( isset( $_GET['updated'] ) ) : ?>
  129. <div id="message" class="updated notice is-dismissible"><p><?php _e( 'File edited successfully.' ) ?></p></div>
  130. <?php endif;
  131. $description = get_file_description( $relative_file );
  132. $file_show = array_search( $file, array_filter( $allowed_files ) );
  133. if ( $description != $file_show )
  134. $description .= ' <span>(' . $file_show . ')</span>';
  135. ?>
  136. <div class="wrap">
  137. <h1><?php echo esc_html( $title ); ?></h1>
  138. <div class="fileedit-sub">
  139. <div class="alignleft">
  140. <h2><?php echo $theme->display( 'Name' ); if ( $description ) echo ': ' . $description; ?></h2>
  141. </div>
  142. <div class="alignright">
  143. <form action="theme-editor.php" method="post">
  144. <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
  145. <select name="theme" id="theme">
  146. <?php
  147. foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
  148. if ( $a_theme->errors() && 'theme_no_stylesheet' == $a_theme->errors()->get_error_code() )
  149. continue;
  150. $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
  151. echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
  152. }
  153. ?>
  154. </select>
  155. <?php submit_button( __( 'Select' ), 'button', 'Submit', false ); ?>
  156. </form>
  157. </div>
  158. <br class="clear" />
  159. </div>
  160. <?php
  161. if ( $theme->errors() )
  162. echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
  163. ?>
  164. <div id="templateside">
  165. <?php
  166. if ( $allowed_files ) :
  167. $previous_file_type = '';
  168. foreach ( $allowed_files as $filename => $absolute_filename ) :
  169. $file_type = substr( $filename, strrpos( $filename, '.' ) );
  170. if ( $file_type !== $previous_file_type ) {
  171. if ( '' !== $previous_file_type ) {
  172. echo "\t</ul>\n";
  173. }
  174. switch ( $file_type ) {
  175. case '.php':
  176. if ( $has_templates || $theme->parent() ) :
  177. echo "\t<h2>" . __( 'Templates' ) . "</h2>\n";
  178. if ( $theme->parent() ) {
  179. echo '<p class="howto">' . sprintf( __( 'This child theme inherits templates from a parent theme, %s.' ),
  180. sprintf( '<a href="%s">%s</a>',
  181. self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
  182. $theme->parent()->display( 'Name' )
  183. )
  184. ) . "</p>\n";
  185. }
  186. endif;
  187. break;
  188. case '.css':
  189. echo "\t<h2>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h2>\n";
  190. break;
  191. default:
  192. /* translators: %s: file extension */
  193. echo "\t<h2>" . sprintf( __( '%s files' ), $file_type ) . "</h2>\n";
  194. break;
  195. }
  196. echo "\t<ul>\n";
  197. }
  198. $file_description = get_file_description( $filename );
  199. if ( $filename !== basename( $absolute_filename ) || $file_description !== $filename ) {
  200. $file_description .= '<br /><span class="nonessential">(' . $filename . ')</span>';
  201. }
  202. if ( $absolute_filename === $file ) {
  203. $file_description = '<span class="highlight">' . $file_description . '</span>';
  204. }
  205. $previous_file_type = $file_type;
  206. ?>
  207. <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
  208. <?php
  209. endforeach;
  210. ?>
  211. </ul>
  212. <?php endif; ?>
  213. </div>
  214. <?php if ( $error ) :
  215. echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
  216. else : ?>
  217. <form name="template" id="template" action="theme-editor.php" method="post">
  218. <?php wp_nonce_field( 'edit-theme_' . $file . $stylesheet ); ?>
  219. <div><textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
  220. <input type="hidden" name="action" value="update" />
  221. <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
  222. <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
  223. <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
  224. </div>
  225. <?php if ( ! empty( $functions ) ) : ?>
  226. <div id="documentation" class="hide-if-no-js">
  227. <label for="docs-list"><?php _e('Documentation:') ?></label>
  228. <?php echo $docs_select; ?>
  229. <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() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" />
  230. </div>
  231. <?php endif; ?>
  232. <div>
  233. <?php if ( is_child_theme() && $theme->get_stylesheet() == get_template() ) : ?>
  234. <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
  235. <?php _e( 'This is a file in your current parent theme.' ); ?></p>
  236. <?php endif; ?>
  237. <?php
  238. if ( is_writeable( $file ) ) :
  239. submit_button( __( 'Update File' ), 'primary', 'submit', true );
  240. else : ?>
  241. <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>
  242. <?php endif; ?>
  243. </div>
  244. </form>
  245. <?php
  246. endif; // $error
  247. ?>
  248. <br class="clear" />
  249. </div>
  250. <script type="text/javascript">
  251. jQuery(document).ready(function($){
  252. $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
  253. $('#newcontent').scrollTop( $('#scrollto').val() );
  254. });
  255. </script>
  256. <?php
  257. break;
  258. }
  259. include(ABSPATH . 'wp-admin/admin-footer.php' );