WC_Download_Handler::download_file_force
Force download - this is the default method.
Method of the class: WC_Download_Handler{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_Download_Handler::download_file_force( $file_path, $filename );
- $file_path(string) (required)
- File path.
- $filename(string) (required)
- File name.
WC_Download_Handler::download_file_force() WC Download Handler::download file force code WC 11.1.2
public static function download_file_force( $file_path, $filename ) {
// Raise the time limit and release the session before any potentially slow remote I/O below.
self::check_server_config();
$parsed_file_path = self::parse_file_path( $file_path );
$download_range = self::get_download_range( @filesize( $parsed_file_path['file_path'] ) ); // @codingStandardsIgnoreLine.
$start = isset( $download_range['start'] ) ? $download_range['start'] : 0;
$length = isset( $download_range['length'] ) ? $download_range['length'] : 0;
if ( $parsed_file_path['remote_file'] ) {
// Open the remote file before sending our own headers, so the filename announced by
// the remote server in its response headers can be used when the URL contains none.
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- Streaming a remote file needs fopen (WP_Filesystem cannot stream); a false return is handled below.
$handle = @fopen( $parsed_file_path['file_path'], 'r' );
$served = false;
if ( false !== $handle ) {
$response_headers = stream_get_meta_data( $handle )['wrapper_data'] ?? array();
$filename = self::resolve_filename_from_response_headers(
is_array( $response_headers ) ? $response_headers : array(),
$filename,
// A filename customized via the `woocommerce_file_download_filename` filter (or by a
// custom caller of this method) is preserved and only completed with an extension.
self::derive_filename_from_file_path( $file_path ) !== $filename
);
self::download_headers( $parsed_file_path['file_path'], $filename, $download_range, true );
$served = self::readfile_from_handle( $handle, $start, $length );
}
} else {
self::download_headers( $parsed_file_path['file_path'], $filename, $download_range );
$served = self::readfile_chunked( $parsed_file_path['file_path'], $start, $length );
}
if ( ! $served ) {
if ( $parsed_file_path['remote_file'] && 'yes' === get_option( 'woocommerce_downloads_redirect_fallback_allowed' ) ) {
wc_get_logger()->warning(
sprintf(
/* translators: %1$s contains the filepath of the digital asset. */
__( '%1$s could not be served using the Force Download method. A redirect will be used instead.', 'woocommerce' ),
$file_path
)
);
self::download_file_redirect( $file_path );
} else {
self::download_error( __( 'File not found', 'woocommerce' ) );
}
}
exit;
}