WP_Upgrader::move_to_temp_backup_dir()publicWP 6.3.0

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

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->move_to_temp_backup_dir( $args );
$args(string[]) (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'.

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 6.3.0 Introduced.

WP_Upgrader::move_to_temp_backup_dir() code WP 6.8

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;
}