WP_Upgrader::restore_temp_backup()publicWP 6.3.0

Restores the plugin or theme from temporary backup.

Method of the class: WP_Upgrader{}

No Hooks.

Return

true|false|WP_Error. True on success, false on early exit, otherwise WP_Error.

Usage

$WP_Upgrader = new WP_Upgrader();
$WP_Upgrader->restore_temp_backup( $temp_backups );
$temp_backups(array[])

An array of temporary backups.

Default: array()

  • ...$0(array)
    Information about the backup.

    • dir(string)
      The temporary backup location in the upgrade-temp-backup directory.

    • slug(string)
      The item's slug.

    • src(string)
      The directory where the original is stored. For example, WP_PLUGIN_DIR.

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 6.3.0 Introduced.
Since 6.6.0 Added the $temp_backups parameter.

WP_Upgrader::restore_temp_backup() code WP 6.8

public function restore_temp_backup( array $temp_backups = array() ) {
	global $wp_filesystem;

	$errors = new WP_Error();

	if ( empty( $temp_backups ) ) {
		$temp_backups = $this->temp_restores;
	}

	foreach ( $temp_backups as $args ) {
		if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {
			return false;
		}

		if ( ! $wp_filesystem->wp_content_dir() ) {
			$errors->add( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
			return $errors;
		}

		$src      = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/' . $args['dir'] . '/' . $args['slug'];
		$dest_dir = $wp_filesystem->find_folder( $args['src'] );
		$dest     = trailingslashit( $dest_dir ) . $args['slug'];

		if ( $wp_filesystem->is_dir( $src ) ) {
			// Cleanup.
			if ( $wp_filesystem->is_dir( $dest ) && ! $wp_filesystem->delete( $dest, true ) ) {
				$errors->add(
					'fs_temp_backup_delete',
					sprintf( $this->strings['temp_backup_restore_failed'], $args['slug'] )
				);
				continue;
			}

			// Move it.
			$result = move_dir( $src, $dest, true );
			if ( is_wp_error( $result ) ) {
				$errors->add(
					'fs_temp_backup_delete',
					sprintf( $this->strings['temp_backup_restore_failed'], $args['slug'] )
				);
				continue;
			}
		}
	}

	return $errors->has_errors() ? $errors : true;
}