Add inline file delivery to WC_Download_Handler (#31145)

Add inline file delivery to WC_Download_Handler

Create a new setting option for inline file delivery for downloadable
products. In the Download Handler create a new method for the new
setting as well as a method for returning the proper headers to deliver
files inline.

Resolves: #28410

* Update Content Type of inline delivered files

* Update Downloaded file name

* Change content-disposition to be a checkbox

Since inline-delivery works with multiple download methods

* Document  function

* Add new setting for test, see #31145.

* Better setting wording and remove typo comment.

* Address pr feedback.

* Add changelog.

* Rephrase the settings to make it more clear.

* More rephrasing.

Co-authored-by: Matthew Caldwell <caldwellysr@gmail.com>
This commit is contained in:
Vedanshu Jain 2022-08-25 23:33:03 +05:30 committed by GitHub
parent 91072c40e0
commit e8eb000733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: enhancement
Add option to serve downloadable files inline.

View File

@ -421,6 +421,16 @@ class WC_Settings_Products extends WC_Settings_Page {
'autoload' => false, 'autoload' => false,
), ),
array(
'title' => __( 'Open in browser', 'woocommerce' ),
'desc' => __( 'Open downloadable files in the browser, instead of saving them to the device.', 'woocommerce' ),
'id' => 'woocommerce_downloads_deliver_inline',
'type' => 'checkbox',
'default' => false,
'desc_tip' => __( 'Customers can still save the file to their device, but by default file will be opened instead of being downloaded (does not work with redirects).', 'woocommerce' ),
'autoload' => false,
),
array( array(
'title' => __( 'Filename', 'woocommerce' ), 'title' => __( 'Filename', 'woocommerce' ),
'desc' => __( 'Append a unique string to filename for security', 'woocommerce' ), 'desc' => __( 'Append a unique string to filename for security', 'woocommerce' ),

View File

@ -522,7 +522,7 @@ class WC_Download_Handler {
header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'X-Robots-Tag: noindex, nofollow', true );
header( 'Content-Type: ' . self::get_download_content_type( $file_path ) ); header( 'Content-Type: ' . self::get_download_content_type( $file_path ) );
header( 'Content-Description: File Transfer' ); header( 'Content-Description: File Transfer' );
header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); header( 'Content-Disposition: ' . self::get_content_disposition() . '; filename="' . $filename . '";' );
header( 'Content-Transfer-Encoding: binary' ); header( 'Content-Transfer-Encoding: binary' );
$file_size = @filesize( $file_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged $file_size = @filesize( $file_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
@ -578,6 +578,22 @@ class WC_Download_Handler {
} }
} }
/**
*
* Get selected content disposition
*
* Defaults to attachment if `woocommerce_downloads_deliver_inline` setting is not selected.
*
* @return string Content disposition value.
*/
private static function get_content_disposition() : string {
$disposition = 'attachment';
if ( 'yes' === get_option( 'woocommerce_downloads_deliver_inline' ) ) {
$disposition = 'inline';
}
return $disposition;
}
/** /**
* Read file chunked. * Read file chunked.
* *

View File

@ -147,6 +147,7 @@ class WC_Settings_Products_Test extends WC_Settings_Unit_Test_Case {
'woocommerce_downloads_require_login' => 'checkbox', 'woocommerce_downloads_require_login' => 'checkbox',
'woocommerce_downloads_grant_access_after_payment' => 'checkbox', 'woocommerce_downloads_grant_access_after_payment' => 'checkbox',
'woocommerce_downloads_add_hash_to_filename' => 'checkbox', 'woocommerce_downloads_add_hash_to_filename' => 'checkbox',
'woocommerce_downloads_deliver_inline' => 'checkbox',
); );
$this->assertEquals( $expected, $settings_ids_and_types ); $this->assertEquals( $expected, $settings_ids_and_types );