ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

theme-editor.php (15040B)


      1 <?php
      2 /**
      3  * Theme editor administration panel.
      4  *
      5  * @package WordPress
      6  * @subpackage Administration
      7  */
      8 
      9 /** WordPress Administration Bootstrap */
     10 require_once __DIR__ . '/admin.php';
     11 
     12 if ( is_multisite() && ! is_network_admin() ) {
     13 	wp_redirect( network_admin_url( 'theme-editor.php' ) );
     14 	exit;
     15 }
     16 
     17 if ( ! current_user_can( 'edit_themes' ) ) {
     18 	wp_die( '<p>' . __( 'Sorry, you are not allowed to edit templates for this site.' ) . '</p>' );
     19 }
     20 
     21 $title       = __( 'Edit Themes' );
     22 $parent_file = 'themes.php';
     23 
     24 get_current_screen()->add_help_tab(
     25 	array(
     26 		'id'      => 'overview',
     27 		'title'   => __( 'Overview' ),
     28 		'content' =>
     29 				'<p>' . __( 'You can use the theme editor to edit the individual CSS and PHP files which make up your theme.' ) . '</p>' .
     30 				'<p>' . __( 'Begin by choosing a theme to edit from the dropdown menu and clicking the Select button. A list then appears of the theme&#8217;s template files. Clicking once on any file name causes the file to appear in the large Editor box.' ) . '</p>' .
     31 				'<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>' .
     32 				'<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' .
     33 				'<ul>' .
     34 				'<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' .
     35 				'<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' .
     36 				'<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' .
     37 				'</ul>' .
     38 				'<p>' . __( 'After typing in your edits, click Update File.' ) . '</p>' .
     39 				'<p>' . __( '<strong>Advice:</strong> Think very carefully about your site crashing if you are live-editing the theme currently in use.' ) . '</p>' .
     40 				'<p>' . sprintf(
     41 					/* translators: %s: Link to documentation on child themes. */
     42 					__( 'Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s">child theme</a> instead.' ),
     43 					__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' )
     44 				) . '</p>' .
     45 				( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ),
     46 	)
     47 );
     48 
     49 get_current_screen()->set_help_sidebar(
     50 	'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
     51 	'<p>' . __( '<a href="https://developer.wordpress.org/themes/">Documentation on Theme Development</a>' ) . '</p>' .
     52 	'<p>' . __( '<a href="https://wordpress.org/support/article/using-themes/">Documentation on Using Themes</a>' ) . '</p>' .
     53 	'<p>' . __( '<a href="https://wordpress.org/support/article/editing-files/">Documentation on Editing Files</a>' ) . '</p>' .
     54 	'<p>' . __( '<a href="https://developer.wordpress.org/themes/basics/template-tags/">Documentation on Template Tags</a>' ) . '</p>' .
     55 	'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
     56 );
     57 
     58 wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
     59 
     60 if ( $theme ) {
     61 	$stylesheet = $theme;
     62 } else {
     63 	$stylesheet = get_stylesheet();
     64 }
     65 
     66 $theme = wp_get_theme( $stylesheet );
     67 
     68 if ( ! $theme->exists() ) {
     69 	wp_die( __( 'The requested theme does not exist.' ) );
     70 }
     71 
     72 if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
     73 	wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
     74 }
     75 
     76 $allowed_files = array();
     77 $style_files   = array();
     78 
     79 $file_types = wp_get_theme_file_editable_extensions( $theme );
     80 
     81 foreach ( $file_types as $type ) {
     82 	switch ( $type ) {
     83 		case 'php':
     84 			$allowed_files += $theme->get_files( 'php', -1 );
     85 			break;
     86 		case 'css':
     87 			$style_files                = $theme->get_files( 'css', -1 );
     88 			$allowed_files['style.css'] = $style_files['style.css'];
     89 			$allowed_files             += $style_files;
     90 			break;
     91 		default:
     92 			$allowed_files += $theme->get_files( $type, -1 );
     93 			break;
     94 	}
     95 }
     96 
     97 // Move functions.php and style.css to the top.
     98 if ( isset( $allowed_files['functions.php'] ) ) {
     99 	$allowed_files = array( 'functions.php' => $allowed_files['functions.php'] ) + $allowed_files;
    100 }
    101 if ( isset( $allowed_files['style.css'] ) ) {
    102 	$allowed_files = array( 'style.css' => $allowed_files['style.css'] ) + $allowed_files;
    103 }
    104 
    105 if ( empty( $file ) ) {
    106 	$relative_file = 'style.css';
    107 	$file          = $allowed_files['style.css'];
    108 } else {
    109 	$relative_file = wp_unslash( $file );
    110 	$file          = $theme->get_stylesheet_directory() . '/' . $relative_file;
    111 }
    112 
    113 validate_file_to_edit( $file, $allowed_files );
    114 
    115 // Handle fallback editing of file when JavaScript is not available.
    116 $edit_error     = null;
    117 $posted_content = null;
    118 
    119 if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
    120 	$r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) );
    121 	if ( is_wp_error( $r ) ) {
    122 		$edit_error = $r;
    123 		if ( check_ajax_referer( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) {
    124 			$posted_content = wp_unslash( $_POST['newcontent'] );
    125 		}
    126 	} else {
    127 		wp_redirect(
    128 			add_query_arg(
    129 				array(
    130 					'a'     => 1, // This means "success" for some reason.
    131 					'theme' => $stylesheet,
    132 					'file'  => $relative_file,
    133 				),
    134 				admin_url( 'theme-editor.php' )
    135 			)
    136 		);
    137 		exit;
    138 	}
    139 }
    140 
    141 $settings = array(
    142 	'codeEditor' => wp_enqueue_code_editor( compact( 'file' ) ),
    143 );
    144 wp_enqueue_script( 'wp-theme-plugin-editor' );
    145 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) );
    146 wp_add_inline_script( 'wp-theme-plugin-editor', 'wp.themePluginEditor.themeOrPlugin = "theme";' );
    147 
    148 require_once ABSPATH . 'wp-admin/admin-header.php';
    149 
    150 update_recently_edited( $file );
    151 
    152 if ( ! is_file( $file ) ) {
    153 	$error = true;
    154 }
    155 
    156 $content = '';
    157 if ( ! empty( $posted_content ) ) {
    158 	$content = $posted_content;
    159 } elseif ( ! $error && filesize( $file ) > 0 ) {
    160 	$f       = fopen( $file, 'r' );
    161 	$content = fread( $f, filesize( $file ) );
    162 
    163 	if ( '.php' === substr( $file, strrpos( $file, '.' ) ) ) {
    164 		$functions = wp_doc_link_parse( $content );
    165 
    166 		$docs_select  = '<select name="docs-list" id="docs-list">';
    167 		$docs_select .= '<option value="">' . esc_attr__( 'Function Name&hellip;' ) . '</option>';
    168 		foreach ( $functions as $function ) {
    169 			$docs_select .= '<option value="' . esc_attr( urlencode( $function ) ) . '">' . htmlspecialchars( $function ) . '()</option>';
    170 		}
    171 		$docs_select .= '</select>';
    172 	}
    173 
    174 	$content = esc_textarea( $content );
    175 }
    176 
    177 $file_description = get_file_description( $relative_file );
    178 $file_show        = array_search( $file, array_filter( $allowed_files ), true );
    179 $description      = esc_html( $file_description );
    180 if ( $file_description !== $file_show ) {
    181 	$description .= ' <span>(' . esc_html( $file_show ) . ')</span>';
    182 }
    183 ?>
    184 <div class="wrap">
    185 <h1><?php echo esc_html( $title ); ?></h1>
    186 
    187 <?php if ( isset( $_GET['a'] ) ) : ?>
    188 	<div id="message" class="updated notice is-dismissible">
    189 		<p><?php _e( 'File edited successfully.' ); ?></p>
    190 	</div>
    191 <?php elseif ( is_wp_error( $edit_error ) ) : ?>
    192 	<div id="message" class="notice notice-error">
    193 		<p><?php _e( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ); ?></p>
    194 		<pre><?php echo esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); ?></pre>
    195 	</div>
    196 <?php endif; ?>
    197 
    198 <?php if ( preg_match( '/\.css$/', $file ) ) : ?>
    199 	<div id="message" class="notice-info notice">
    200 		<p><strong><?php _e( 'Did you know?' ); ?></strong></p>
    201 		<p>
    202 			<?php
    203 			printf(
    204 				/* translators: %s: Link to Custom CSS section in the Customizer. */
    205 				__( 'There&#8217;s no need to change your CSS here &mdash; you can edit and live preview CSS changes in the <a href="%s">built-in CSS editor</a>.' ),
    206 				esc_url( add_query_arg( 'autofocus[section]', 'custom_css', admin_url( 'customize.php' ) ) )
    207 			);
    208 			?>
    209 		</p>
    210 	</div>
    211 <?php endif; ?>
    212 
    213 <div class="fileedit-sub">
    214 <div class="alignleft">
    215 <h2>
    216 	<?php
    217 	echo $theme->display( 'Name' );
    218 	if ( $description ) {
    219 		echo ': ' . $description;}
    220 	?>
    221 </h2>
    222 </div>
    223 <div class="alignright">
    224 	<form action="theme-editor.php" method="get">
    225 		<label for="theme" id="theme-plugin-editor-selector"><?php _e( 'Select theme to edit:' ); ?> </label>
    226 		<select name="theme" id="theme">
    227 		<?php
    228 		foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
    229 			if ( $a_theme->errors() && 'theme_no_stylesheet' === $a_theme->errors()->get_error_code() ) {
    230 				continue;
    231 			}
    232 
    233 			$selected = ( $a_stylesheet === $stylesheet ) ? ' selected="selected"' : '';
    234 			echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display( 'Name' ) . '</option>';
    235 		}
    236 		?>
    237 		</select>
    238 		<?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
    239 	</form>
    240 </div>
    241 <br class="clear" />
    242 </div>
    243 
    244 <?php
    245 if ( $theme->errors() ) {
    246 	echo '<div class="error"><p><strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message() . '</p></div>';
    247 }
    248 ?>
    249 
    250 <div id="templateside">
    251 	<h2 id="theme-files-label"><?php _e( 'Theme Files' ); ?></h2>
    252 	<ul role="tree" aria-labelledby="theme-files-label">
    253 		<?php if ( $theme->parent() ) : ?>
    254 			<li class="howto">
    255 				<?php
    256 				printf(
    257 					/* translators: %s: Link to edit parent theme. */
    258 					__( 'This child theme inherits templates from a parent theme, %s.' ),
    259 					sprintf(
    260 						'<a href="%s">%s</a>',
    261 						self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
    262 						$theme->parent()->display( 'Name' )
    263 					)
    264 				);
    265 				?>
    266 			</li>
    267 		<?php endif; ?>
    268 		<li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1">
    269 			<ul role="group">
    270 				<?php wp_print_theme_file_tree( wp_make_theme_file_tree( $allowed_files ) ); ?>
    271 			</ul>
    272 		</li>
    273 	</ul>
    274 </div>
    275 
    276 <?php
    277 if ( $error ) :
    278 	echo '<div class="error"><p>' . __( 'File does not exist! Please double check the name and try again.' ) . '</p></div>';
    279 else :
    280 	?>
    281 	<form name="template" id="template" action="theme-editor.php" method="post">
    282 		<?php wp_nonce_field( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce' ); ?>
    283 		<div>
    284 			<label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label>
    285 			<textarea cols="70" rows="30" 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>
    286 			<input type="hidden" name="action" value="update" />
    287 			<input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
    288 			<input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
    289 		</div>
    290 
    291 		<?php if ( ! empty( $functions ) ) : ?>
    292 			<div id="documentation" class="hide-if-no-js">
    293 				<label for="docs-list"><?php _e( 'Documentation:' ); ?></label>
    294 				<?php echo $docs_select; ?>
    295 				<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'); }" />
    296 			</div>
    297 		<?php endif; ?>
    298 
    299 		<div>
    300 			<div class="editor-notices">
    301 				<?php if ( is_child_theme() && $theme->get_stylesheet() === get_template() ) : ?>
    302 					<div class="notice notice-warning inline">
    303 						<p>
    304 							<?php if ( is_writable( $file ) ) : ?>
    305 								<strong><?php _e( 'Caution:' ); ?></strong>
    306 							<?php endif; ?>
    307 							<?php _e( 'This is a file in your current parent theme.' ); ?>
    308 						</p>
    309 					</div>
    310 				<?php endif; ?>
    311 			</div>
    312 			<?php if ( is_writable( $file ) ) : ?>
    313 				<p class="submit">
    314 					<?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?>
    315 					<span class="spinner"></span>
    316 				</p>
    317 			<?php else : ?>
    318 				<p>
    319 					<?php
    320 					printf(
    321 						/* translators: %s: Documentation URL. */
    322 						__( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
    323 						__( 'https://wordpress.org/support/article/changing-file-permissions/' )
    324 					);
    325 					?>
    326 				</p>
    327 			<?php endif; ?>
    328 		</div>
    329 
    330 		<?php wp_print_file_editor_templates(); ?>
    331 	</form>
    332 	<?php
    333 endif; // End if $error.
    334 ?>
    335 <br class="clear" />
    336 </div>
    337 <?php
    338 $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
    339 if ( ! in_array( 'theme_editor_notice', $dismissed_pointers, true ) ) :
    340 	// Get a back URL.
    341 	$referer = wp_get_referer();
    342 
    343 	$excluded_referer_basenames = array( 'theme-editor.php', 'wp-login.php' );
    344 
    345 	if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
    346 		$return_url = $referer;
    347 	} else {
    348 		$return_url = admin_url( '/' );
    349 	}
    350 	?>
    351 	<div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden">
    352 		<div class="notification-dialog-background"></div>
    353 		<div class="notification-dialog">
    354 			<div class="file-editor-warning-content">
    355 				<div class="file-editor-warning-message">
    356 					<h1><?php _e( 'Heads up!' ); ?></h1>
    357 					<p>
    358 						<?php
    359 						_e( 'You appear to be making direct edits to your theme in the WordPress dashboard. We recommend that you don&#8217;t! Editing your theme directly could break your site and your changes may be lost in future updates.' );
    360 						?>
    361 					</p>
    362 						<?php
    363 						if ( ! $theme->parent() ) {
    364 							echo '<p>';
    365 							printf(
    366 								/* translators: %s: Link to documentation on child themes. */
    367 								__( 'If you need to tweak more than your theme&#8217;s CSS, you might want to try <a href="%s">making a child theme</a>.' ),
    368 								esc_url( __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) )
    369 							);
    370 							echo '</p>';
    371 						}
    372 						?>
    373 					<p><?php _e( 'If you decide to go ahead with direct edits anyway, 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>
    374 				</div>
    375 				<p>
    376 					<a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a>
    377 					<button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button>
    378 				</p>
    379 			</div>
    380 		</div>
    381 	</div>
    382 	<?php
    383 endif; // Editor warning notice.
    384 
    385 require_once ABSPATH . 'wp-admin/admin-footer.php';