WP_Upgrader::move_to_temp_backup_dir( string[] $args ): bool|WP_Error

In this article

Moves the plugin or theme being updated into a temporary backup directory.

Parameters

$argsstring[]required
Array of data for the temporary backup.
  • slug string
    Plugin or theme slug.
  • src string
    Path to the root directory for plugins or themes.
  • dir string
    Destination subdirectory name. Accepts 'plugins' or 'themes'.

Return

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

Source

public function move_to_temp_backup_dir( $args ) {
	global $wp_filesystem;

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

	/*
	 * Skip any plugin that has "." as its slug.
	 * A slug of "." will result in a `$src` value ending in a period.
	 *
	 * On Windows, this will cause the 'plugins' folder to be moved,
	 * and will cause a failure when attempting to call `mkdir()`.
	 */
	if ( '.' === $args['slug'] ) {
		return false;
	}

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

	$dest_dir = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/';
	$sub_dir  = $dest_dir . $args['dir'] . '/';

	// Create the temporary backup directory if it does not exist.
	if ( ! $wp_filesystem->is_dir( $sub_dir ) ) {
		if ( ! $wp_filesystem->is_dir( $dest_dir ) ) {
			$wp_filesystem->mkdir( $dest_dir, FS_CHMOD_DIR );
		}

		if ( ! $wp_filesystem->mkdir( $sub_dir, FS_CHMOD_DIR ) ) {
			// Could not create the backup directory.
			return new WP_Error( 'fs_temp_backup_mkdir', $this->strings['temp_backup_mkdir_failed'] );
		}
	}

	$src_dir = $wp_filesystem->find_folder( $args['src'] );
	$src     = trailingslashit( $src_dir ) . $args['slug'];
	$dest    = $dest_dir . trailingslashit( $args['dir'] ) . $args['slug'];

	// Delete the temporary backup directory if it already exists.
	if ( $wp_filesystem->is_dir( $dest ) ) {
		$wp_filesystem->delete( $dest, true );
	}

	// Move to the temporary backup directory.
	$result = move_dir( $src, $dest, true );
	if ( is_wp_error( $result ) ) {
		return new WP_Error( 'fs_temp_backup_move', $this->strings['temp_backup_move_failed'] );
	}

	return true;
}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.