Skip to content

Commit b587d52

Browse files
feat: enhance file upload error handling (#578)
Added accepted file type and file size message in the frontend and alerted the correct error message when the uploaded file condition is not met with the configuration.
1 parent 8b56993 commit b587d52

6 files changed

Lines changed: 54 additions & 4 deletions

File tree

classes/frontend-scripts.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ private static function set_localize_data( $handle, $var_name, $product, $js_var
595595
'plupload_runtime' => ( ppom_if_browser_is_ie() ) ? 'html5,html4' : 'html5,silverlight,html4,browserplus,gear',
596596
'ppom_file_upload_nonce' => wp_create_nonce( 'ppom_uploading_file_action' ),
597597
'ppom_file_delete_nonce' => wp_create_nonce( 'ppom_deleting_file_action' ),
598+
'invalid_file_type' => __( 'Invalid file type', 'woocommerce-product-addon' ),
599+
// translators: %s is max file size.
600+
'max_file_size' => __( 'File size must be less than %s', 'woocommerce-product-addon' ),
601+
'duplicate_file' => __( 'You have already selected this file', 'woocommerce-product-addon' ),
598602
);
599603

600604
break;

css/ppom-style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,4 +614,8 @@ span.ppom-tooltip .ppom-tooltip-icon::after {
614614
color: #fff;
615615
line-height: 13px;
616616
font-weight: 700;
617+
}
618+
span.ppom-field-notice {
619+
display: block;
620+
font-size: 14px;
617621
}

js/file-upload.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,15 @@ function ppom_setup_file_upload_input( file_input ) {
861861
},
862862

863863
Error( up, err ) {
864-
//document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
865-
alert( '\nError #' + err.code + ': ' + err.message );
864+
if ( -600 === err.code && file_input.file_size ) {
865+
alert( ppom_file_vars.max_file_size.replace( '%s', file_input.file_size.toUpperCase() ) );
866+
} else if ( -601 === err.code && file_input.file_types ) {
867+
alert( ppom_file_vars.invalid_file_type);
868+
} else if ( -602 === err.code ) {
869+
alert( ppom_file_vars.duplicate_file );
870+
} else {
871+
alert( 'Error #' + err.code + ': ' + err.message );
872+
}
866873
},
867874
},
868875
} );

src/Support/Helpers.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,4 +2581,33 @@ public static function get_image_name( $images ) {
25812581

25822582
return $updated_value;
25832583
}
2584+
2585+
/**
2586+
* Get file uploader notice text based on field meta.
2587+
*
2588+
* @param \PPOM_InputManager $meta_field Meta field object.
2589+
* @return string Notice text to display for file uploader field.
2590+
*/
2591+
public static function get_file_uploader_notice( $meta_field ) {
2592+
$file_types = $meta_field->get_meta_value( 'file_types' );
2593+
$file_size = $meta_field->get_meta_value( 'file_size' );
2594+
2595+
$notice = '';
2596+
if ( $file_types ) {
2597+
$notice = sprintf(
2598+
// translators: %s is the accepted file types.
2599+
__( 'Accepted formats: %s.', 'woocommerce-product-addon' ),
2600+
esc_html( strtoupper( $file_types ) )
2601+
);
2602+
}
2603+
if ( $file_size ) {
2604+
$notice .= ' ' . sprintf(
2605+
// translators: %s is the maximum file size.
2606+
__( 'Max size: %s', 'woocommerce-product-addon' ),
2607+
esc_html( strtoupper( $file_size ) )
2608+
);
2609+
}
2610+
2611+
return $notice;
2612+
}
25842613
}

templates/frontend/inputs/cropper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class="btn btn-primary <?php echo esc_attr( $btn_class ); ?>"
5454
>
5555
<?php echo esc_html( $btn_label ); ?>
5656
</a>
57-
<span class="ppom-dragdrop-text"><?php echo _e( 'Drag file/directory here', 'woocommerce-product-addon' ); ?></span>
57+
<span class="ppom-dragdrop-text"><?php esc_html_e( 'Drag file/directory here', 'woocommerce-product-addon' ); ?></span>
58+
<span class="ppom-field-notice">
59+
<?php echo esc_html( \PPOM\Support\Helpers::get_file_uploader_notice( $fm ) ); ?>
60+
</span>
5861
</div> <!-- ppom-file-container -->
5962

6063
<div id="filelist-<?php echo esc_attr( $fm->data_name() ); ?>" class="filelist"></div>

templates/frontend/inputs/file.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ class="btn btn-primary <?php echo esc_attr( $btn_class ); ?> <?php echo esc_attr
5252
>
5353
<?php echo esc_html( $btn_label ); ?>
5454
</a>
55-
<span class="ppom-dragdrop-text"><?php echo _e( 'Drag File Here', 'woocommerce-product-addon' ); ?></span>
55+
<span class="ppom-dragdrop-text"><?php esc_html_e( 'Drag File Here', 'woocommerce-product-addon' ); ?></span>
56+
<span class="ppom-field-notice">
57+
<?php echo esc_html( \PPOM\Support\Helpers::get_file_uploader_notice( $fm ) ); ?>
58+
</span>
5659
</div> <!-- ppom-file-container -->
5760

5861
<div id="filelist-<?php echo esc_attr( $fm->data_name() ); ?>"

0 commit comments

Comments
 (0)