Add setting to configure hash appends

This commit is contained in:
vedanshujain 2020-01-17 19:34:16 +05:30
parent 56176e0479
commit ca5fd12103
3 changed files with 19 additions and 2 deletions

View File

@ -838,6 +838,10 @@ class WC_Admin_Post_Types {
return $full_filename;
}
if ( 'no' === get_option( 'woocommerce_downloads_add_hash_to_filename' ) ) {
return $full_filename;
}
return $this->unique_filename( $full_filename, $ext );
}
@ -850,7 +854,7 @@ class WC_Admin_Post_Types {
* @return string Modified filename.
*/
public function unique_filename( $full_filename, $ext ) {
$ideal_random_char_length = 32;
$ideal_random_char_length = 6; // Not going with a larger length because then downloaded filename will not be pretty.
$max_filename_length = 255; // Max file name length for most file systems.
$length_to_prepend = min( $ideal_random_char_length, $max_filename_length - strlen( $full_filename ) - 1 );

View File

@ -260,6 +260,19 @@ class WC_Settings_Products extends WC_Settings_Page {
'autoload' => false,
),
array(
'title' => __( 'Filename', 'woocommerce' ),
'desc' => __( 'Append a unique string to filename for security', 'woocommerce' ),
'id' => 'woocommerce_downloads_add_hash_to_filename',
'type' => 'checkbox',
'default' => 'yes',
'desc_tip' => sprintf(
// translators: Link to WooCommerce Docs.
__( "Not required if you download directory is protected. <a href='%s'>See this guide</a> for more details. Files already uploaded will not be affected.", 'woocommerce' ),
'link to woocommerce docs' // TODO: Change this after updating docs.
),
),
array(
'type' => 'sectionend',
'id' => 'digital_download_options',

View File

@ -33,7 +33,7 @@ class WC_Test_Admin_Post_Types extends WC_Unit_Test_Case {
$ext = '.csv';
$unique_filename = $this->wc_cpt->unique_filename( $full_filename, $ext );
$this->assertEquals( strlen( $full_filename ) + 32 + 1, strlen( $unique_filename ) );
$this->assertEquals( strlen( $full_filename ) + 6 + 1, strlen( $unique_filename ) );
$this->assertEquals( $ext, substr( $unique_filename, -4 ) );
}