PageRenderTime 30ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/update-core.php

http://github.com/markjaquith/WordPress
PHP | 952 lines | 724 code | 113 blank | 115 comment | 136 complexity | 28ff1dd72e4745c25dce414a7bdcc3a5 MD5 | raw file
  1<?php
  2/**
  3 * Update Core administration panel.
  4 *
  5 * @package WordPress
  6 * @subpackage Administration
  7 */
  8
  9/** WordPress Administration Bootstrap */
 10require_once __DIR__ . '/admin.php';
 11
 12wp_enqueue_style( 'plugin-install' );
 13wp_enqueue_script( 'plugin-install' );
 14wp_enqueue_script( 'updates' );
 15add_thickbox();
 16
 17if ( is_multisite() && ! is_network_admin() ) {
 18	wp_redirect( network_admin_url( 'update-core.php' ) );
 19	exit();
 20}
 21
 22if ( ! current_user_can( 'update_core' ) && ! current_user_can( 'update_themes' ) && ! current_user_can( 'update_plugins' ) && ! current_user_can( 'update_languages' ) ) {
 23	wp_die( __( 'Sorry, you are not allowed to update this site.' ) );
 24}
 25
 26/**
 27 * Lists available core updates.
 28 *
 29 * @since 2.7.0
 30 *
 31 * @global string $wp_local_package Locale code of the package.
 32 * @global wpdb   $wpdb             WordPress database abstraction object.
 33 *
 34 * @staticvar bool $first_pass
 35 *
 36 * @param object $update
 37 */
 38function list_core_update( $update ) {
 39	global $wp_local_package, $wpdb;
 40	static $first_pass = true;
 41
 42	$wp_version     = get_bloginfo( 'version' );
 43	$version_string = sprintf( '%s&ndash;<strong>%s</strong>', $update->current, $update->locale );
 44
 45	if ( 'en_US' === $update->locale && 'en_US' === get_locale() ) {
 46		$version_string = $update->current;
 47	} elseif ( 'en_US' === $update->locale && $update->packages->partial && $wp_version == $update->partial_version ) {
 48		$updates = get_core_updates();
 49		if ( $updates && 1 == count( $updates ) ) {
 50			// If the only available update is a partial builds, it doesn't need a language-specific version string.
 51			$version_string = $update->current;
 52		}
 53	}
 54
 55	$current = false;
 56	if ( ! isset( $update->response ) || 'latest' === $update->response ) {
 57		$current = true;
 58	}
 59	$submit        = __( 'Update Now' );
 60	$form_action   = 'update-core.php?action=do-core-upgrade';
 61	$php_version   = phpversion();
 62	$mysql_version = $wpdb->db_version();
 63	$show_buttons  = true;
 64	if ( 'development' === $update->response ) {
 65		$message = __( 'You are using a development version of WordPress. You can update to the latest nightly build automatically:' );
 66	} else {
 67		if ( $current ) {
 68			/* translators: %s: WordPress version. */
 69			$message     = sprintf( __( 'If you need to re-install version %s, you can do so here:' ), $version_string );
 70			$submit      = __( 'Re-install Now' );
 71			$form_action = 'update-core.php?action=do-core-reinstall';
 72		} else {
 73			$php_compat = version_compare( $php_version, $update->php_version, '>=' );
 74			if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
 75				$mysql_compat = true;
 76			} else {
 77				$mysql_compat = version_compare( $mysql_version, $update->mysql_version, '>=' );
 78			}
 79
 80			$version_url = sprintf(
 81				/* translators: %s: WordPress version. */
 82				esc_url( __( 'https://wordpress.org/support/wordpress-version/version-%s/' ) ),
 83				sanitize_title( $update->current )
 84			);
 85
 86			$php_update_message = '</p><p>' . sprintf(
 87				/* translators: %s: URL to Update PHP page. */
 88				__( '<a href="%s">Learn more about updating PHP</a>.' ),
 89				esc_url( wp_get_update_php_url() )
 90			);
 91
 92			$annotation = wp_get_update_php_annotation();
 93			if ( $annotation ) {
 94				$php_update_message .= '</p><p><em>' . $annotation . '</em>';
 95			}
 96
 97			if ( ! $mysql_compat && ! $php_compat ) {
 98				$message = sprintf(
 99					/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required PHP version number, 4: Minimum required MySQL version number, 5: Current PHP version number, 6: Current MySQL version number. */
100					__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires PHP version %3$s or higher and MySQL version %4$s or higher. You are running PHP version %5$s and MySQL version %6$s.' ),
101					$version_url,
102					$update->current,
103					$update->php_version,
104					$update->mysql_version,
105					$php_version,
106					$mysql_version
107				) . $php_update_message;
108			} elseif ( ! $php_compat ) {
109				$message = sprintf(
110					/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required PHP version number, 4: Current PHP version number. */
111					__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires PHP version %3$s or higher. You are running version %4$s.' ),
112					$version_url,
113					$update->current,
114					$update->php_version,
115					$php_version
116				) . $php_update_message;
117			} elseif ( ! $mysql_compat ) {
118				$message = sprintf(
119					/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required MySQL version number, 4: Current MySQL version number. */
120					__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires MySQL version %3$s or higher. You are running version %4$s.' ),
121					$version_url,
122					$update->current,
123					$update->mysql_version,
124					$mysql_version
125				);
126			} else {
127				$message = sprintf(
128					/* translators: 1: URL to WordPress release notes, 2: WordPress version number including locale if necessary. */
129					__( 'You can update to <a href="%1$s">WordPress %2$s</a> automatically:' ),
130					$version_url,
131					$version_string
132				);
133			}
134			if ( ! $mysql_compat || ! $php_compat ) {
135				$show_buttons = false;
136			}
137		}
138	}
139
140	echo '<p>';
141	echo $message;
142	echo '</p>';
143	echo '<form method="post" action="' . $form_action . '" name="upgrade" class="upgrade">';
144	wp_nonce_field( 'upgrade-core' );
145	echo '<p>';
146	echo '<input name="version" value="' . esc_attr( $update->current ) . '" type="hidden"/>';
147	echo '<input name="locale" value="' . esc_attr( $update->locale ) . '" type="hidden"/>';
148	if ( $show_buttons ) {
149		if ( $first_pass ) {
150			submit_button( $submit, $current ? '' : 'primary regular', 'upgrade', false );
151			$first_pass = false;
152		} else {
153			submit_button( $submit, '', 'upgrade', false );
154		}
155	}
156	if ( 'en_US' !== $update->locale ) {
157		if ( ! isset( $update->dismissed ) || ! $update->dismissed ) {
158			submit_button( __( 'Hide this update' ), '', 'dismiss', false );
159		} else {
160			submit_button( __( 'Bring back this update' ), '', 'undismiss', false );
161		}
162	}
163	echo '</p>';
164	if ( 'en_US' !== $update->locale && ( ! isset( $wp_local_package ) || $wp_local_package != $update->locale ) ) {
165		echo '<p class="hint">' . __( 'This localized version contains both the translation and various other localization fixes.' ) . '</p>';
166	} elseif ( 'en_US' === $update->locale && 'en_US' !== get_locale() && ( ! $update->packages->partial && $wp_version == $update->partial_version ) ) {
167		// Partial builds don't need language-specific warnings.
168		echo '<p class="hint">' . sprintf(
169			/* translators: %s: WordPress version. */
170			__( 'You are about to install WordPress %s <strong>in English (US).</strong> There is a chance this update will break your translation. You may prefer to wait for the localized version to be released.' ),
171			'development' !== $update->response ? $update->current : ''
172		) . '</p>';
173	}
174	echo '</form>';
175
176}
177
178/**
179 * Display dismissed updates.
180 *
181 * @since 2.7.0
182 */
183function dismissed_updates() {
184	$dismissed = get_core_updates(
185		array(
186			'dismissed' => true,
187			'available' => false,
188		)
189	);
190	if ( $dismissed ) {
191
192		$show_text = esc_js( __( 'Show hidden updates' ) );
193		$hide_text = esc_js( __( 'Hide hidden updates' ) );
194		?>
195	<script type="text/javascript">
196		jQuery(function( $ ) {
197			$( 'dismissed-updates' ).show();
198			$( '#show-dismissed' ).toggle( function() { $( this ).text( '<?php echo $hide_text; ?>' ).attr( 'aria-expanded', 'true' ); }, function() { $( this ).text( '<?php echo $show_text; ?>' ).attr( 'aria-expanded', 'false' ); } );
199			$( '#show-dismissed' ).click( function() { $( '#dismissed-updates' ).toggle( 'fast' ); } );
200		});
201	</script>
202		<?php
203		echo '<p class="hide-if-no-js"><button type="button" class="button" id="show-dismissed" aria-expanded="false">' . __( 'Show hidden updates' ) . '</button></p>';
204		echo '<ul id="dismissed-updates" class="core-updates dismissed">';
205		foreach ( (array) $dismissed as $update ) {
206			echo '<li>';
207			list_core_update( $update );
208			echo '</li>';
209		}
210		echo '</ul>';
211	}
212}
213
214/**
215 * Display upgrade WordPress for downloading latest or upgrading automatically form.
216 *
217 * @since 2.7.0
218 *
219 * @global string $required_php_version   The required PHP version string.
220 * @global string $required_mysql_version The required MySQL version string.
221 */
222function core_upgrade_preamble() {
223	global $required_php_version, $required_mysql_version;
224
225	$wp_version = get_bloginfo( 'version' );
226	$updates    = get_core_updates();
227
228	if ( ! isset( $updates[0]->response ) || 'latest' === $updates[0]->response ) {
229		echo '<h2>';
230		_e( 'You have the latest version of WordPress.' );
231
232		if ( wp_http_supports( array( 'ssl' ) ) ) {
233			require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
234			$upgrader            = new WP_Automatic_Updater;
235			$future_minor_update = (object) array(
236				'current'       => $wp_version . '.1.next.minor',
237				'version'       => $wp_version . '.1.next.minor',
238				'php_version'   => $required_php_version,
239				'mysql_version' => $required_mysql_version,
240			);
241			$should_auto_update  = $upgrader->should_update( 'core', $future_minor_update, ABSPATH );
242			if ( $should_auto_update ) {
243				echo ' ' . __( 'Future security updates will be applied automatically.' );
244			}
245		}
246		echo '</h2>';
247	}
248
249	if ( isset( $updates[0]->version ) && version_compare( $updates[0]->version, $wp_version, '>' ) ) {
250		echo '<div class="notice notice-warning"><p>';
251		_e( '<strong>Important:</strong> Before updating, please <a href="https://wordpress.org/support/article/wordpress-backups/">back up your database and files</a>. For help with updates, visit the <a href="https://wordpress.org/support/article/updating-wordpress/">Updating WordPress</a> documentation page.' );
252		echo '</p></div>';
253
254		echo '<h2 class="response">';
255		_e( 'An updated version of WordPress is available.' );
256		echo '</h2>';
257	}
258
259	if ( isset( $updates[0] ) && 'development' === $updates[0]->response ) {
260		require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
261		$upgrader = new WP_Automatic_Updater;
262		if ( wp_http_supports( 'ssl' ) && $upgrader->should_update( 'core', $updates[0], ABSPATH ) ) {
263			echo '<div class="updated inline"><p>';
264			echo '<strong>' . __( 'BETA TESTERS:' ) . '</strong> ' . __( 'This site is set up to install updates of future beta versions automatically.' );
265			echo '</p></div>';
266		}
267	}
268
269	echo '<ul class="core-updates">';
270	foreach ( (array) $updates as $update ) {
271		echo '<li>';
272		list_core_update( $update );
273		echo '</li>';
274	}
275	echo '</ul>';
276	// Don't show the maintenance mode notice when we are only showing a single re-install option.
277	if ( $updates && ( count( $updates ) > 1 || 'latest' !== $updates[0]->response ) ) {
278		echo '<p>' . __( 'While your site is being updated, it will be in maintenance mode. As soon as your updates are complete, your site will return to normal.' ) . '</p>';
279	} elseif ( ! $updates ) {
280		list( $normalized_version ) = explode( '-', $wp_version );
281		echo '<p>' . sprintf(
282			/* translators: 1: URL to About screen, 2: WordPress version. */
283			__( '<a href="%1$s">Learn more about WordPress %2$s</a>.' ),
284			esc_url( self_admin_url( 'about.php' ) ),
285			$normalized_version
286		) . '</p>';
287	}
288	dismissed_updates();
289}
290
291/**
292 * Display the upgrade plugins form.
293 *
294 * @since 2.9.0
295 */
296function list_plugin_updates() {
297	$wp_version     = get_bloginfo( 'version' );
298	$cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
299
300	require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
301	$plugins = get_plugin_updates();
302	if ( empty( $plugins ) ) {
303		echo '<h2>' . __( 'Plugins' ) . '</h2>';
304		echo '<p>' . __( 'Your plugins are all up to date.' ) . '</p>';
305		return;
306	}
307	$form_action = 'update-core.php?action=do-plugin-upgrade';
308
309	$core_updates = get_core_updates();
310	if ( ! isset( $core_updates[0]->response ) || 'latest' === $core_updates[0]->response || 'development' === $core_updates[0]->response || version_compare( $core_updates[0]->current, $cur_wp_version, '=' ) ) {
311		$core_update_version = false;
312	} else {
313		$core_update_version = $core_updates[0]->current;
314	}
315	?>
316<h2><?php _e( 'Plugins' ); ?></h2>
317<p><?php _e( 'The following plugins have new versions available. Check the ones you want to update and then click &#8220;Update Plugins&#8221;.' ); ?></p>
318<form method="post" action="<?php echo esc_url( $form_action ); ?>" name="upgrade-plugins" class="upgrade">
319	<?php wp_nonce_field( 'upgrade-core' ); ?>
320<p><input id="upgrade-plugins" class="button" type="submit" value="<?php esc_attr_e( 'Update Plugins' ); ?>" name="upgrade" /></p>
321<table class="widefat updates-table" id="update-plugins-table">
322	<thead>
323	<tr>
324		<td class="manage-column check-column"><input type="checkbox" id="plugins-select-all" /></td>
325		<td class="manage-column"><label for="plugins-select-all"><?php _e( 'Select All' ); ?></label></td>
326	</tr>
327	</thead>
328
329	<tbody class="plugins">
330	<?php
331	foreach ( (array) $plugins as $plugin_file => $plugin_data ) {
332		$plugin_data = (object) _get_plugin_data_markup_translate( $plugin_file, (array) $plugin_data, false, true );
333
334		$icon            = '<span class="dashicons dashicons-admin-plugins"></span>';
335		$preferred_icons = array( 'svg', '2x', '1x', 'default' );
336		foreach ( $preferred_icons as $preferred_icon ) {
337			if ( ! empty( $plugin_data->update->icons[ $preferred_icon ] ) ) {
338				$icon = '<img src="' . esc_url( $plugin_data->update->icons[ $preferred_icon ] ) . '" alt="" />';
339				break;
340			}
341		}
342
343		// Get plugin compat for running version of WordPress.
344		if ( isset( $plugin_data->update->tested ) && version_compare( $plugin_data->update->tested, $cur_wp_version, '>=' ) ) {
345			/* translators: %s: WordPress version. */
346			$compat = '<br />' . sprintf( __( 'Compatibility with WordPress %s: 100%% (according to its author)' ), $cur_wp_version );
347		} else {
348			/* translators: %s: WordPress version. */
349			$compat = '<br />' . sprintf( __( 'Compatibility with WordPress %s: Unknown' ), $cur_wp_version );
350		}
351		// Get plugin compat for updated version of WordPress.
352		if ( $core_update_version ) {
353			if ( isset( $plugin_data->update->tested ) && version_compare( $plugin_data->update->tested, $core_update_version, '>=' ) ) {
354				/* translators: %s: WordPress version. */
355				$compat .= '<br />' . sprintf( __( 'Compatibility with WordPress %s: 100%% (according to its author)' ), $core_update_version );
356			} else {
357				/* translators: %s: WordPress version. */
358				$compat .= '<br />' . sprintf( __( 'Compatibility with WordPress %s: Unknown' ), $core_update_version );
359			}
360		}
361
362		$requires_php   = isset( $plugin_data->update->requires_php ) ? $plugin_data->update->requires_php : null;
363		$compatible_php = is_php_version_compatible( $requires_php );
364
365		if ( ! $compatible_php && current_user_can( 'update_php' ) ) {
366			$compat .= '<br>' . __( 'This update doesn&#8217;t work with your version of PHP.' ) . '&nbsp;';
367			$compat .= sprintf(
368				/* translators: %s: URL to Update PHP page. */
369				__( '<a href="%s">Learn more about updating PHP</a>.' ),
370				esc_url( wp_get_update_php_url() )
371			);
372
373			$annotation = wp_get_update_php_annotation();
374
375			if ( $annotation ) {
376				$compat .= '</p><p><em>' . $annotation . '</em>';
377			}
378		}
379
380		// Get the upgrade notice for the new plugin version.
381		if ( isset( $plugin_data->update->upgrade_notice ) ) {
382			$upgrade_notice = '<br />' . strip_tags( $plugin_data->update->upgrade_notice );
383		} else {
384			$upgrade_notice = '';
385		}
386
387		$details_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_data->update->slug . '&section=changelog&TB_iframe=true&width=640&height=662' );
388		$details     = sprintf(
389			'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
390			esc_url( $details_url ),
391			/* translators: 1: Plugin name, 2: Version number. */
392			esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_data->Name, $plugin_data->update->new_version ) ),
393			/* translators: %s: Plugin version. */
394			sprintf( __( 'View version %s details.' ), $plugin_data->update->new_version )
395		);
396
397		$checkbox_id = 'checkbox_' . md5( $plugin_data->Name );
398		?>
399	<tr>
400		<td class="check-column">
401		<?php if ( $compatible_php ) : ?>
402			<input type="checkbox" name="checked[]" id="<?php echo $checkbox_id; ?>" value="<?php echo esc_attr( $plugin_file ); ?>" />
403			<label for="<?php echo $checkbox_id; ?>" class="screen-reader-text">
404				<?php
405				/* translators: %s: Plugin name. */
406				printf( __( 'Select %s' ), $plugin_data->Name );
407				?>
408			</label>
409		<?php endif; ?>
410		</td>
411		<td class="plugin-title"><p>
412			<?php echo $icon; ?>
413			<strong><?php echo $plugin_data->Name; ?></strong>
414			<?php
415			printf(
416				/* translators: 1: Plugin version, 2: New version. */
417				__( 'You have version %1$s installed. Update to %2$s.' ),
418				$plugin_data->Version,
419				$plugin_data->update->new_version
420			);
421			echo ' ' . $details . $compat . $upgrade_notice;
422			?>
423		</p></td>
424	</tr>
425		<?php
426	}
427	?>
428	</tbody>
429
430	<tfoot>
431	<tr>
432		<td class="manage-column check-column"><input type="checkbox" id="plugins-select-all-2" /></td>
433		<td class="manage-column"><label for="plugins-select-all-2"><?php _e( 'Select All' ); ?></label></td>
434	</tr>
435	</tfoot>
436</table>
437<p><input id="upgrade-plugins-2" class="button" type="submit" value="<?php esc_attr_e( 'Update Plugins' ); ?>" name="upgrade" /></p>
438</form>
439	<?php
440}
441
442/**
443 * Display the upgrade themes form.
444 *
445 * @since 2.9.0
446 */
447function list_theme_updates() {
448	$themes = get_theme_updates();
449	if ( empty( $themes ) ) {
450		echo '<h2>' . __( 'Themes' ) . '</h2>';
451		echo '<p>' . __( 'Your themes are all up to date.' ) . '</p>';
452		return;
453	}
454
455	$form_action = 'update-core.php?action=do-theme-upgrade';
456	?>
457<h2><?php _e( 'Themes' ); ?></h2>
458<p><?php _e( 'The following themes have new versions available. Check the ones you want to update and then click &#8220;Update Themes&#8221;.' ); ?></p>
459<p>
460	<?php
461	printf(
462		/* translators: %s: Link to documentation on child themes. */
463		__( '<strong>Please Note:</strong> Any customizations you have made to theme files will be lost. Please consider using <a href="%s">child themes</a> for modifications.' ),
464		__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' )
465	);
466	?>
467</p>
468<form method="post" action="<?php echo esc_url( $form_action ); ?>" name="upgrade-themes" class="upgrade">
469	<?php wp_nonce_field( 'upgrade-core' ); ?>
470<p><input id="upgrade-themes" class="button" type="submit" value="<?php esc_attr_e( 'Update Themes' ); ?>" name="upgrade" /></p>
471<table class="widefat updates-table" id="update-themes-table">
472	<thead>
473	<tr>
474		<td class="manage-column check-column"><input type="checkbox" id="themes-select-all" /></td>
475		<td class="manage-column"><label for="themes-select-all"><?php _e( 'Select All' ); ?></label></td>
476	</tr>
477	</thead>
478
479	<tbody class="plugins">
480	<?php
481	foreach ( $themes as $stylesheet => $theme ) {
482		$checkbox_id = 'checkbox_' . md5( $theme->get( 'Name' ) );
483		?>
484	<tr>
485		<td class="check-column">
486			<input type="checkbox" name="checked[]" id="<?php echo $checkbox_id; ?>" value="<?php echo esc_attr( $stylesheet ); ?>" />
487			<label for="<?php echo $checkbox_id; ?>" class="screen-reader-text">
488				<?php
489				/* translators: %s: Theme name. */
490				printf( __( 'Select %s' ), $theme->display( 'Name' ) );
491				?>
492			</label>
493		</td>
494		<td class="plugin-title"><p>
495			<img src="<?php echo esc_url( $theme->get_screenshot() ); ?>" width="85" height="64" class="updates-table-screenshot" alt="" />
496			<strong><?php echo $theme->display( 'Name' ); ?></strong>
497			<?php
498			printf(
499				/* translators: 1: Theme version, 2: New version. */
500				__( 'You have version %1$s installed. Update to %2$s.' ),
501				$theme->display( 'Version' ),
502				$theme->update['new_version']
503			);
504			?>
505		</p></td>
506	</tr>
507			<?php
508	}
509	?>
510	</tbody>
511
512	<tfoot>
513	<tr>
514		<td class="manage-column check-column"><input type="checkbox" id="themes-select-all-2" /></td>
515		<td class="manage-column"><label for="themes-select-all-2"><?php _e( 'Select All' ); ?></label></td>
516	</tr>
517	</tfoot>
518</table>
519<p><input id="upgrade-themes-2" class="button" type="submit" value="<?php esc_attr_e( 'Update Themes' ); ?>" name="upgrade" /></p>
520</form>
521	<?php
522}
523
524/**
525 * Display the update translations form.
526 *
527 * @since 3.7.0
528 */
529function list_translation_updates() {
530	$updates = wp_get_translation_updates();
531	if ( ! $updates ) {
532		if ( 'en_US' !== get_locale() ) {
533			echo '<h2>' . __( 'Translations' ) . '</h2>';
534			echo '<p>' . __( 'Your translations are all up to date.' ) . '</p>';
535		}
536		return;
537	}
538
539	$form_action = 'update-core.php?action=do-translation-upgrade';
540	?>
541	<h2><?php _e( 'Translations' ); ?></h2>
542	<form method="post" action="<?php echo esc_url( $form_action ); ?>" name="upgrade-translations" class="upgrade">
543		<p><?php _e( 'New translations are available.' ); ?></p>
544		<?php wp_nonce_field( 'upgrade-translations' ); ?>
545		<p><input class="button" type="submit" value="<?php esc_attr_e( 'Update Translations' ); ?>" name="upgrade" /></p>
546	</form>
547	<?php
548}
549
550/**
551 * Upgrade WordPress core display.
552 *
553 * @since 2.7.0
554 *
555 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
556 *
557 * @param bool $reinstall
558 */
559function do_core_upgrade( $reinstall = false ) {
560	global $wp_filesystem;
561
562	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
563
564	if ( $reinstall ) {
565		$url = 'update-core.php?action=do-core-reinstall';
566	} else {
567		$url = 'update-core.php?action=do-core-upgrade';
568	}
569	$url = wp_nonce_url( $url, 'upgrade-core' );
570
571	$version = isset( $_POST['version'] ) ? $_POST['version'] : false;
572	$locale  = isset( $_POST['locale'] ) ? $_POST['locale'] : 'en_US';
573	$update  = find_core_update( $version, $locale );
574	if ( ! $update ) {
575		return;
576	}
577
578	// Allow relaxed file ownership writes for User-initiated upgrades when the API specifies
579	// that it's safe to do so. This only happens when there are no new files to create.
580	$allow_relaxed_file_ownership = ! $reinstall && isset( $update->new_files ) && ! $update->new_files;
581
582	?>
583	<div class="wrap">
584	<h1><?php _e( 'Update WordPress' ); ?></h1>
585	<?php
586
587	$credentials = request_filesystem_credentials( $url, '', false, ABSPATH, array( 'version', 'locale' ), $allow_relaxed_file_ownership );
588	if ( false === $credentials ) {
589		echo '</div>';
590		return;
591	}
592
593	if ( ! WP_Filesystem( $credentials, ABSPATH, $allow_relaxed_file_ownership ) ) {
594		// Failed to connect. Error and request again.
595		request_filesystem_credentials( $url, '', true, ABSPATH, array( 'version', 'locale' ), $allow_relaxed_file_ownership );
596		echo '</div>';
597		return;
598	}
599
600	if ( $wp_filesystem->errors->has_errors() ) {
601		foreach ( $wp_filesystem->errors->get_error_messages() as $message ) {
602			show_message( $message );
603		}
604		echo '</div>';
605		return;
606	}
607
608	if ( $reinstall ) {
609		$update->response = 'reinstall';
610	}
611
612	add_filter( 'update_feedback', 'show_message' );
613
614	$upgrader = new Core_Upgrader();
615	$result   = $upgrader->upgrade(
616		$update,
617		array(
618			'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
619		)
620	);
621
622	if ( is_wp_error( $result ) ) {
623		show_message( $result );
624		if ( 'up_to_date' != $result->get_error_code() && 'locked' != $result->get_error_code() ) {
625			show_message( __( 'Installation Failed' ) );
626		}
627		echo '</div>';
628		return;
629	}
630
631	show_message( __( 'WordPress updated successfully' ) );
632	show_message(
633		'<span class="hide-if-no-js">' . sprintf(
634			/* translators: 1: WordPress version, 2: URL to About screen. */
635			__( 'Welcome to WordPress %1$s. You will be redirected to the About WordPress screen. If not, click <a href="%2$s">here</a>.' ),
636			$result,
637			esc_url( self_admin_url( 'about.php?updated' ) )
638		) . '</span>'
639	);
640	show_message(
641		'<span class="hide-if-js">' . sprintf(
642			/* translators: 1: WordPress version, 2: URL to About screen. */
643			__( 'Welcome to WordPress %1$s. <a href="%2$s">Learn more</a>.' ),
644			$result,
645			esc_url( self_admin_url( 'about.php?updated' ) )
646		) . '</span>'
647	);
648	?>
649	</div>
650	<script type="text/javascript">
651	window.location = '<?php echo self_admin_url( 'about.php?updated' ); ?>';
652	</script>
653	<?php
654}
655
656/**
657 * Dismiss a core update.
658 *
659 * @since 2.7.0
660 */
661function do_dismiss_core_update() {
662	$version = isset( $_POST['version'] ) ? $_POST['version'] : false;
663	$locale  = isset( $_POST['locale'] ) ? $_POST['locale'] : 'en_US';
664	$update  = find_core_update( $version, $locale );
665	if ( ! $update ) {
666		return;
667	}
668	dismiss_core_update( $update );
669	wp_redirect( wp_nonce_url( 'update-core.php?action=upgrade-core', 'upgrade-core' ) );
670	exit;
671}
672
673/**
674 * Undismiss a core update.
675 *
676 * @since 2.7.0
677 */
678function do_undismiss_core_update() {
679	$version = isset( $_POST['version'] ) ? $_POST['version'] : false;
680	$locale  = isset( $_POST['locale'] ) ? $_POST['locale'] : 'en_US';
681	$update  = find_core_update( $version, $locale );
682	if ( ! $update ) {
683		return;
684	}
685	undismiss_core_update( $version, $locale );
686	wp_redirect( wp_nonce_url( 'update-core.php?action=upgrade-core', 'upgrade-core' ) );
687	exit;
688}
689
690$action = isset( $_GET['action'] ) ? $_GET['action'] : 'upgrade-core';
691
692$upgrade_error = false;
693if ( ( 'do-theme-upgrade' == $action || ( 'do-plugin-upgrade' == $action && ! isset( $_GET['plugins'] ) ) )
694	&& ! isset( $_POST['checked'] ) ) {
695	$upgrade_error = 'do-theme-upgrade' == $action ? 'themes' : 'plugins';
696	$action        = 'upgrade-core';
697}
698
699$title       = __( 'WordPress Updates' );
700$parent_file = 'index.php';
701
702$updates_overview  = '<p>' . __( 'On this screen, you can update to the latest version of WordPress, as well as update your themes, plugins, and translations from the WordPress.org repositories.' ) . '</p>';
703$updates_overview .= '<p>' . __( 'If an update is available, you&#8127;ll see a notification appear in the Toolbar and navigation menu.' ) . ' ' . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' ) . '</p>';
704
705get_current_screen()->add_help_tab(
706	array(
707		'id'      => 'overview',
708		'title'   => __( 'Overview' ),
709		'content' => $updates_overview,
710	)
711);
712
713$updates_howto  = '<p>' . __( '<strong>WordPress</strong> &mdash; Updating your WordPress installation is a simple one-click procedure: just <strong>click on the &#8220;Update Now&#8221; button</strong> when you are notified that a new version is available.' ) . ' ' . __( 'In most cases, WordPress will automatically apply maintenance and security updates in the background for you.' ) . '</p>';
714$updates_howto .= '<p>' . __( '<strong>Themes and Plugins</strong> &mdash; To update individual themes or plugins from this screen, use the checkboxes to make your selection, then <strong>click on the appropriate &#8220;Update&#8221; button</strong>. To update all of your themes or plugins at once, you can check the box at the top of the section to select all before clicking the update button.' ) . '</p>';
715
716if ( 'en_US' !== get_locale() ) {
717	$updates_howto .= '<p>' . __( '<strong>Translations</strong> &mdash; The files translating WordPress into your language are updated for you whenever any other updates occur. But if these files are out of date, you can <strong>click the &#8220;Update Translations&#8221;</strong> button.' ) . '</p>';
718}
719
720get_current_screen()->add_help_tab(
721	array(
722		'id'      => 'how-to-update',
723		'title'   => __( 'How to Update' ),
724		'content' => $updates_howto,
725	)
726);
727
728get_current_screen()->set_help_sidebar(
729	'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
730	'<p>' . __( '<a href="https://wordpress.org/support/article/dashboard-updates-screen/">Documentation on Updating WordPress</a>' ) . '</p>' .
731	'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
732);
733
734if ( 'upgrade-core' == $action ) {
735	// Force a update check when requested.
736	$force_check = ! empty( $_GET['force-check'] );
737	wp_version_check( array(), $force_check );
738
739	require_once ABSPATH . 'wp-admin/admin-header.php';
740	?>
741	<div class="wrap">
742	<h1><?php _e( 'WordPress Updates' ); ?></h1>
743	<?php
744	if ( $upgrade_error ) {
745		echo '<div class="error"><p>';
746		if ( 'themes' === $upgrade_error ) {
747			_e( 'Please select one or more themes to update.' );
748		} else {
749			_e( 'Please select one or more plugins to update.' );
750		}
751		echo '</p></div>';
752	}
753
754	$last_update_check = false;
755	$current           = get_site_transient( 'update_core' );
756
757	if ( $current && isset( $current->last_checked ) ) {
758		$last_update_check = $current->last_checked + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
759	}
760
761	echo '<p>';
762	/* translators: 1: Date, 2: Time. */
763	printf( __( 'Last checked on %1$s at %2$s.' ), date_i18n( __( 'F j, Y' ), $last_update_check ), date_i18n( __( 'g:i a' ), $last_update_check ) );
764	echo ' &nbsp; <a class="button" href="' . esc_url( self_admin_url( 'update-core.php?force-check=1' ) ) . '">' . __( 'Check Again' ) . '</a>';
765	echo '</p>';
766
767	if ( current_user_can( 'update_core' ) ) {
768		core_upgrade_preamble();
769	}
770	if ( current_user_can( 'update_plugins' ) ) {
771		list_plugin_updates();
772	}
773	if ( current_user_can( 'update_themes' ) ) {
774		list_theme_updates();
775	}
776	if ( current_user_can( 'update_languages' ) ) {
777		list_translation_updates();
778	}
779
780	/**
781	 * Fires after the core, plugin, and theme update tables.
782	 *
783	 * @since 2.9.0
784	 */
785	do_action( 'core_upgrade_preamble' );
786	echo '</div>';
787
788	wp_localize_script(
789		'updates',
790		'_wpUpdatesItemCounts',
791		array(
792			'totals' => wp_get_update_data(),
793		)
794	);
795
796	require_once ABSPATH . 'wp-admin/admin-footer.php';
797
798} elseif ( 'do-core-upgrade' == $action || 'do-core-reinstall' == $action ) {
799
800	if ( ! current_user_can( 'update_core' ) ) {
801		wp_die( __( 'Sorry, you are not allowed to update this site.' ) );
802	}
803
804	check_admin_referer( 'upgrade-core' );
805
806	// Do the (un)dismiss actions before headers, so that they can redirect.
807	if ( isset( $_POST['dismiss'] ) ) {
808		do_dismiss_core_update();
809	} elseif ( isset( $_POST['undismiss'] ) ) {
810		do_undismiss_core_update();
811	}
812
813	require_once ABSPATH . 'wp-admin/admin-header.php';
814	if ( 'do-core-reinstall' == $action ) {
815		$reinstall = true;
816	} else {
817		$reinstall = false;
818	}
819
820	if ( isset( $_POST['upgrade'] ) ) {
821		do_core_upgrade( $reinstall );
822	}
823
824	wp_localize_script(
825		'updates',
826		'_wpUpdatesItemCounts',
827		array(
828			'totals' => wp_get_update_data(),
829		)
830	);
831
832	require_once ABSPATH . 'wp-admin/admin-footer.php';
833
834} elseif ( 'do-plugin-upgrade' == $action ) {
835
836	if ( ! current_user_can( 'update_plugins' ) ) {
837		wp_die( __( 'Sorry, you are not allowed to update this site.' ) );
838	}
839
840	check_admin_referer( 'upgrade-core' );
841
842	if ( isset( $_GET['plugins'] ) ) {
843		$plugins = explode( ',', $_GET['plugins'] );
844	} elseif ( isset( $_POST['checked'] ) ) {
845		$plugins = (array) $_POST['checked'];
846	} else {
847		wp_redirect( admin_url( 'update-core.php' ) );
848		exit;
849	}
850
851	$url = 'update.php?action=update-selected&plugins=' . urlencode( implode( ',', $plugins ) );
852	$url = wp_nonce_url( $url, 'bulk-update-plugins' );
853
854	$title = __( 'Update Plugins' );
855
856	require_once ABSPATH . 'wp-admin/admin-header.php';
857	echo '<div class="wrap">';
858	echo '<h1>' . __( 'Update Plugins' ) . '</h1>';
859	echo '<iframe src="', $url, '" style="width: 100%; height: 100%; min-height: 750px;" frameborder="0" title="' . esc_attr__( 'Update progress' ) . '"></iframe>';
860	echo '</div>';
861
862	wp_localize_script(
863		'updates',
864		'_wpUpdatesItemCounts',
865		array(
866			'totals' => wp_get_update_data(),
867		)
868	);
869
870	require_once ABSPATH . 'wp-admin/admin-footer.php';
871
872} elseif ( 'do-theme-upgrade' == $action ) {
873
874	if ( ! current_user_can( 'update_themes' ) ) {
875		wp_die( __( 'Sorry, you are not allowed to update this site.' ) );
876	}
877
878	check_admin_referer( 'upgrade-core' );
879
880	if ( isset( $_GET['themes'] ) ) {
881		$themes = explode( ',', $_GET['themes'] );
882	} elseif ( isset( $_POST['checked'] ) ) {
883		$themes = (array) $_POST['checked'];
884	} else {
885		wp_redirect( admin_url( 'update-core.php' ) );
886		exit;
887	}
888
889	$url = 'update.php?action=update-selected-themes&themes=' . urlencode( implode( ',', $themes ) );
890	$url = wp_nonce_url( $url, 'bulk-update-themes' );
891
892	$title = __( 'Update Themes' );
893
894	require_once ABSPATH . 'wp-admin/admin-header.php';
895	?>
896	<div class="wrap">
897		<h1><?php _e( 'Update Themes' ); ?></h1>
898		<iframe src="<?php echo $url; ?>" style="width: 100%; height: 100%; min-height: 750px;" frameborder="0" title="<?php esc_attr_e( 'Update progress' ); ?>"></iframe>
899	</div>
900	<?php
901
902	wp_localize_script(
903		'updates',
904		'_wpUpdatesItemCounts',
905		array(
906			'totals' => wp_get_update_data(),
907		)
908	);
909
910	require_once ABSPATH . 'wp-admin/admin-footer.php';
911
912} elseif ( 'do-translation-upgrade' == $action ) {
913
914	if ( ! current_user_can( 'update_languages' ) ) {
915		wp_die( __( 'Sorry, you are not allowed to update this site.' ) );
916	}
917
918	check_admin_referer( 'upgrade-translations' );
919
920	require_once ABSPATH . 'wp-admin/admin-header.php';
921	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
922
923	$url     = 'update-core.php?action=do-translation-upgrade';
924	$nonce   = 'upgrade-translations';
925	$title   = __( 'Update Translations' );
926	$context = WP_LANG_DIR;
927
928	$upgrader = new Language_Pack_Upgrader( new Language_Pack_Upgrader_Skin( compact( 'url', 'nonce', 'title', 'context' ) ) );
929	$result   = $upgrader->bulk_upgrade();
930
931	wp_localize_script(
932		'updates',
933		'_wpUpdatesItemCounts',
934		array(
935			'totals' => wp_get_update_data(),
936		)
937	);
938
939	require_once ABSPATH . 'wp-admin/admin-footer.php';
940
941} else {
942	/**
943	 * Fires for each custom update action on the WordPress Updates screen.
944	 *
945	 * The dynamic portion of the hook name, `$action`, refers to the
946	 * passed update action. The hook fires in lieu of all available
947	 * default update actions.
948	 *
949	 * @since 3.2.0
950	 */
951	do_action( "update-core-custom_{$action}" );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
952}