WC_Product::build_downloads_map()privateWC 1.0

Takes an array of downloadable file representations and converts it into an array of WC_Product_Download objects, indexed by download ID.

Method of the class: WC_Product{}

No Hooks.

Return

WC_Product_Download[].

Usage

// private - for code of main (parent) class only
$result = $this->build_downloads_map( $downloads ): array;
$downloads(array[]|WC_Product_Download[]) (required)
Download data to be re-mapped.

WC_Product::build_downloads_map() code WC 8.7.0

private function build_downloads_map( array $downloads ): array {
	$downloads_map = array();

	foreach ( $downloads as $download_data ) {
		// If the item is already a WC_Product_Download we can add it to the map and move on.
		if ( is_a( $download_data, 'WC_Product_Download' ) ) {
			$downloads_map[ $download_data->get_id() ] = $download_data;
			continue;
		}

		// If the item is not an array, there is nothing else we can do (bad data).
		if ( ! is_array( $download_data ) ) {
			continue;
		}

		// Otherwise, transform the array to a WC_Product_Download and add to the map.
		$download_object = new WC_Product_Download();

		// If we don't have a previous hash, generate UUID for download.
		if ( empty( $download_data['download_id'] ) ) {
			$download_data['download_id'] = wp_generate_uuid4();
		}

		$download_object->set_id( $download_data['download_id'] );
		$download_object->set_name( $download_data['name'] );
		$download_object->set_file( $download_data['file'] );
		$download_object->set_enabled( isset( $download_data['enabled'] ) ? $download_data['enabled'] : true );

		$downloads_map[ $download_object->get_id() ] = $download_object;
	}

	return $downloads_map;
}