angelovcom.net

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

plugin-editor.php (13332B)


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