Skip to content

Commit 6626ce7

Browse files
committed
Enhance file upload functionality by expanding accepted file types to include various document formats and audio/video types. Refactor related JavaScript and PHP code to improve handling of accepted file types, including the removal of the file type icon feature for a cleaner interface. Update placeholder text and help descriptions for better user guidance on file uploads.
1 parent 1c81ede commit 6626ce7

5 files changed

Lines changed: 74 additions & 113 deletions

File tree

dt-assets/functions/enqueue-scripts.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,16 @@ function dt_site_scripts() {
445445
[
446446
'rest_url' => esc_url_raw( rest_url() ),
447447
'accepted_file_types' => [
448-
'image/png',
449-
'image/gif',
450-
'image/jpeg',
451-
'image/jpg',
448+
'image/*',
449+
'application/pdf',
452450
'audio/*',
453-
'video/*'
451+
'video/*',
452+
'application/msword',
453+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
454+
'application/vnd.ms-excel',
455+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
456+
'text/plain',
457+
'text/markdown',
454458
],
455459
'translations' => [
456460
'modals' => [

dt-core/admin/admin-settings-endpoints.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,9 +1298,15 @@ public static function edit_field( WP_REST_Request $request ) {
12981298
// Upload File Field Types: Field-specific options
12991299
if ( isset( $post_fields[$field_key]['type'] ) && $post_fields[$field_key]['type'] === 'file_upload' ) {
13001300
// Accepted file types
1301-
if ( isset( $post_submission['visibility']['accepted_file_types'] ) && !empty( $post_submission['visibility']['accepted_file_types'] ) ) {
1302-
$types = array_map( 'sanitize_text_field', array_map( 'trim', explode( ',', $post_submission['visibility']['accepted_file_types'] ) ) );
1303-
$custom_field['accepted_file_types'] = $types;
1301+
if ( isset( $post_submission['visibility']['accepted_file_types'] ) ) {
1302+
$raw_accepted = trim( $post_submission['visibility']['accepted_file_types'] );
1303+
if ( $raw_accepted === '' ) {
1304+
// Clear any previously saved override so defaults are used
1305+
unset( $custom_field['accepted_file_types'] );
1306+
} else {
1307+
$types = array_map( 'sanitize_text_field', array_map( 'trim', explode( ',', $raw_accepted ) ) );
1308+
$custom_field['accepted_file_types'] = $types;
1309+
}
13041310
}
13051311

13061312
// Max file size
@@ -1310,16 +1316,6 @@ public static function edit_field( WP_REST_Request $request ) {
13101316
unset( $custom_field['max_file_size'] );
13111317
}
13121318

1313-
// File type icon
1314-
if ( isset( $post_submission['visibility']['file_type_icon'] ) ) {
1315-
$file_type_icon = sanitize_text_field( wp_unslash( $post_submission['visibility']['file_type_icon'] ) );
1316-
if ( !empty( $file_type_icon ) ) {
1317-
$custom_field['file_type_icon'] = $file_type_icon;
1318-
} else {
1319-
unset( $custom_field['file_type_icon'] );
1320-
}
1321-
}
1322-
13231319
// Delete enabled
13241320
if ( isset( $post_submission['visibility']['delete_enabled'] ) ) {
13251321
$custom_field['delete_enabled'] = (bool) $post_submission['visibility']['delete_enabled'];

dt-core/admin/js/dt-settings.js

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,31 +1609,26 @@ jQuery(document).ready(function ($) {
16091609

16101610
// Add file_upload field-specific options
16111611
if (field_type === 'file_upload') {
1612-
let accepted_file_types = field_settings['accepted_file_types'] || [
1613-
'image/*',
1614-
'application/pdf',
1615-
];
1612+
let accepted_file_types =
1613+
field_settings['accepted_file_types'] || [
1614+
'image/*',
1615+
'application/pdf',
1616+
'audio/*',
1617+
'video/*',
1618+
'application/msword',
1619+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
1620+
'application/vnd.ms-excel',
1621+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
1622+
'text/plain',
1623+
'text/markdown',
1624+
];
16161625
let max_file_size = field_settings['max_file_size'] || '';
1617-
let file_type_icon = field_settings['file_type_icon'] || '';
16181626
let delete_enabled = field_settings['delete_enabled'] !== false; // default true
16191627
let display_layout = field_settings['display_layout'] || 'grid';
16201628
let auto_upload = field_settings['auto_upload'] !== false; // default true
16211629
let download_enabled = field_settings['download_enabled'] !== false; // default true
16221630
let rename_enabled = field_settings['rename_enabled'] !== false; // default true
16231631

1624-
let file_type_icon_html = '';
1625-
if (
1626-
file_type_icon &&
1627-
typeof file_type_icon !== 'undefined' &&
1628-
file_type_icon !== 'undefined'
1629-
) {
1630-
if (file_type_icon.trim().toLowerCase().startsWith('mdi')) {
1631-
file_type_icon_html = `<i class="${file_type_icon} field-icon" style="font-size: 20px; vertical-align: middle;"></i>`;
1632-
} else {
1633-
file_type_icon_html = `<img src="${file_type_icon}" class="field-icon" style="vertical-align: middle; max-width: 20px;">`;
1634-
}
1635-
}
1636-
16371632
modal_html_content += `
16381633
<tr>
16391634
<td>
@@ -1642,10 +1637,13 @@ jQuery(document).ready(function ($) {
16421637
<td>
16431638
<input type="text" name="accepted_file_types" id="accepted_file_types"
16441639
value="${accepted_file_types.join(', ')}"
1645-
placeholder="e.g., image/*, application/pdf, .doc, .docx"
1640+
placeholder="e.g., image/*, audio/*, video/*, application/pdf, .docx"
16461641
style="width: 100%;">
16471642
<p style="font-size: 11px; color: #666; margin-top: 5px;">
1648-
Comma-separated list of MIME types or file extensions (e.g., image/*, application/pdf, .doc)
1643+
Optional. Comma-separated list of MIME types or file extensions to override the default set (images, PDFs, audio, video, common documents). Leave empty to use the default types.
1644+
<a href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types" target="_blank" rel="noopener noreferrer">
1645+
View common MIME types.
1646+
</a>
16491647
</p>
16501648
</td>
16511649
</tr>
@@ -1659,23 +1657,6 @@ jQuery(document).ready(function ($) {
16591657
placeholder="Leave empty for no limit">
16601658
</td>
16611659
</tr>
1662-
<tr>
1663-
<td>
1664-
<label for="file_type_icon"><b>File Type Icon</b></label>
1665-
</td>
1666-
<td>
1667-
<div class="input-group">
1668-
${file_type_icon_html}
1669-
<input name="file_type_icon" id="file_type_icon" type="text"
1670-
value="${file_type_icon}" style="vertical-align: middle;">
1671-
<button class="button change-icon-button" style="vertical-align: middle;"
1672-
data-icon-input="file_type_icon">Select Icon</button>
1673-
</div>
1674-
<p style="font-size: 11px; color: #666; margin-top: 5px;">
1675-
Icon to display for non-image files (e.g., PDFs, documents)
1676-
</p>
1677-
</td>
1678-
</tr>
16791660
<tr>
16801661
<td>
16811662
<b>Delete Enabled</b>
@@ -2445,7 +2426,6 @@ jQuery(document).ready(function ($) {
24452426
.val()
24462427
.trim();
24472428
visibility['max_file_size'] = $('#max_file_size').val().trim();
2448-
visibility['file_type_icon'] = $('#file_type_icon').val().trim();
24492429
visibility['delete_enabled'] = $('#delete_enabled').is(':checked');
24502430
visibility['display_layout'] = $('#display_layout').val();
24512431
visibility['auto_upload'] = $('#auto_upload').is(':checked');

dt-core/admin/menu/tabs/tab-custom-fields.php

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,19 @@ private function edit_field( $field_key, $post_type ){
545545
<?php
546546
$custom_fields = dt_get_option( 'dt_field_customizations' );
547547
$custom_field = $custom_fields[ $post_type ][ $field_key ] ?? [];
548-
$accepted_file_types = $custom_field['accepted_file_types'] ?? [ 'image/*', 'application/pdf' ];
548+
$accepted_file_types = $custom_field['accepted_file_types'] ?? [
549+
'image/*',
550+
'application/pdf',
551+
'audio/*',
552+
'video/*',
553+
'application/msword',
554+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
555+
'application/vnd.ms-excel',
556+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
557+
'text/plain',
558+
'text/markdown',
559+
];
549560
$max_file_size = $custom_field['max_file_size'] ?? '';
550-
$file_type_icon = $custom_field['file_type_icon'] ?? '';
551561
$delete_enabled = isset( $custom_field['delete_enabled'] ) ? $custom_field['delete_enabled'] : true;
552562
$display_layout = $custom_field['display_layout'] ?? 'grid';
553563
$auto_upload = isset( $custom_field['auto_upload'] ) ? $custom_field['auto_upload'] : true;
@@ -564,10 +574,13 @@ private function edit_field( $field_key, $post_type ){
564574
<td>
565575
<input type="text" name="accepted_file_types" id="accepted_file_types"
566576
value="<?php echo esc_attr( implode( ', ', $accepted_file_types ) ) ?>"
567-
placeholder="e.g., image/*, application/pdf, .doc, .docx"
577+
placeholder="e.g., image/*, audio/*, video/*, application/pdf, .docx"
568578
style="width: 100%;" />
569579
<p style="font-size: 11px; color: #666; margin-top: 5px;">
570-
<?php esc_html_e( 'Comma-separated list of MIME types or file extensions (e.g., image/*, application/pdf, .doc)', 'disciple_tools' ) ?>
580+
<?php esc_html_e( 'Optional. Comma-separated list of MIME types or file extensions to override the default set (images, PDFs, audio, video, common documents). Leave empty to use the default types.', 'disciple_tools' ); ?>
581+
<a href="<?php echo esc_url( 'https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types' ); ?>" target="_blank" rel="noopener noreferrer">
582+
<?php esc_html_e( 'View common MIME types.', 'disciple_tools' ); ?>
583+
</a>
571584
</p>
572585
</td>
573586
</tr>
@@ -582,41 +595,6 @@ private function edit_field( $field_key, $post_type ){
582595
placeholder="<?php esc_attr_e( 'Leave empty for no limit', 'disciple_tools' ) ?>" />
583596
</td>
584597
</tr>
585-
<tr>
586-
<td style="vertical-align: middle">
587-
<label for="file_type_icon"><b><?php esc_html_e( 'File Type Icon', 'disciple_tools' ) ?></b></label>
588-
</td>
589-
<td>
590-
<table>
591-
<tbody>
592-
<tr>
593-
<td>
594-
<?php if ( !empty( $file_type_icon ) ) : ?>
595-
<?php if ( strpos( $file_type_icon, 'mdi' ) === 0 || strpos( $file_type_icon, 'mdi mdi-' ) === 0 ) : ?>
596-
<i class="<?php echo esc_attr( $file_type_icon ); ?>" style="font-size: 20px; vertical-align: middle;"></i>
597-
<?php else : ?>
598-
<img src="<?php echo esc_attr( $file_type_icon ); ?>" style="width: 20px; vertical-align: middle;">
599-
<?php endif; ?>
600-
<?php endif; ?>
601-
</td>
602-
<td>
603-
<input type="text" name="file_type_icon" id="file_type_icon"
604-
placeholder="<?php esc_attr_e( 'Icon url', 'disciple_tools' ); ?>"
605-
value="<?php echo esc_attr( $file_type_icon ); ?>">
606-
</td>
607-
<td>
608-
<button class="button change-icon-button"
609-
data-form="<?php echo esc_html( $form_name ) ?>"
610-
data-icon-input="file_type_icon"><?php esc_html_e( 'Change Icon', 'disciple_tools' ); ?></button>
611-
</td>
612-
</tr>
613-
</tbody>
614-
</table>
615-
<p style="font-size: 11px; color: #666; margin-top: 5px;">
616-
<?php esc_html_e( 'Icon to display for non-image files (e.g., PDFs, documents)', 'disciple_tools' ) ?>
617-
</p>
618-
</td>
619-
</tr>
620598
<tr>
621599
<td style="vertical-align: middle">
622600
<b><?php esc_html_e( 'Delete Enabled', 'disciple_tools' ) ?></b>
@@ -1069,9 +1047,15 @@ private function process_edit_field( $post_submission ){
10691047
// file_upload field options
10701048
if ( $field['type'] === 'file_upload' ) {
10711049
// Accepted file types
1072-
if ( isset( $post_submission['accepted_file_types'] ) && !empty( $post_submission['accepted_file_types'] ) ) {
1073-
$types = array_map( 'sanitize_text_field', array_map( 'trim', explode( ',', $post_submission['accepted_file_types'] ) ) );
1074-
$custom_field['accepted_file_types'] = $types;
1050+
if ( isset( $post_submission['accepted_file_types'] ) ) {
1051+
$raw_accepted = trim( $post_submission['accepted_file_types'] );
1052+
if ( $raw_accepted === '' ) {
1053+
// Clear any previously saved override so defaults are used
1054+
unset( $custom_field['accepted_file_types'] );
1055+
} else {
1056+
$types = array_map( 'sanitize_text_field', array_map( 'trim', explode( ',', $raw_accepted ) ) );
1057+
$custom_field['accepted_file_types'] = $types;
1058+
}
10751059
}
10761060

10771061
// Max file size
@@ -1081,16 +1065,6 @@ private function process_edit_field( $post_submission ){
10811065
unset( $custom_field['max_file_size'] );
10821066
}
10831067

1084-
// File type icon
1085-
if ( isset( $post_submission['file_type_icon'] ) ) {
1086-
$file_type_icon = sanitize_text_field( wp_unslash( $post_submission['file_type_icon'] ) );
1087-
if ( !empty( $file_type_icon ) ) {
1088-
$custom_field['file_type_icon'] = $file_type_icon;
1089-
} else {
1090-
unset( $custom_field['file_type_icon'] );
1091-
}
1092-
}
1093-
10941068
// Delete enabled
10951069
if ( isset( $post_submission['delete_enabled'] ) ) {
10961070
$custom_field['delete_enabled'] = true;

dt-core/utilities/dt-components.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,21 @@ public static function render_file_upload( $field_key, $fields, $post, $params =
332332
}
333333

334334
// Get field settings for configuration
335-
$accepted_file_types = $fields[ $field_key ]['accepted_file_types'] ?? [ 'image/*', 'application/pdf' ];
335+
$accepted_file_types = $fields[ $field_key ]['accepted_file_types'] ?? [
336+
'image/*',
337+
'application/pdf',
338+
'audio/*',
339+
'video/*',
340+
'application/msword',
341+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
342+
'application/vnd.ms-excel',
343+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
344+
'text/plain',
345+
'text/markdown',
346+
];
336347
$max_file_size = $fields[$field_key]['max_file_size'] ?? null;
337348
$delete_enabled = isset( $fields[$field_key]['delete_enabled'] ) ? $fields[$field_key]['delete_enabled'] : true;
338349
$display_layout = $fields[$field_key]['display_layout'] ?? 'grid';
339-
$file_type_icon = $fields[$field_key]['file_type_icon'] ?? '';
340350
$auto_upload = isset( $fields[$field_key]['auto_upload'] ) ? $fields[$field_key]['auto_upload'] : true;
341351
$download_enabled = isset( $fields[$field_key]['download_enabled'] ) ? $fields[$field_key]['download_enabled'] : true;
342352
$rename_enabled = isset( $fields[$field_key]['rename_enabled'] ) ? $fields[$field_key]['rename_enabled'] : true;
@@ -417,9 +427,6 @@ public static function render_file_upload( $field_key, $fields, $post, $params =
417427
delete-enabled="false"
418428
<?php endif; ?>
419429
display-layout="<?php echo esc_attr( $display_layout ) ?>"
420-
<?php if ( $file_type_icon ): ?>
421-
file-type-icon="<?php echo esc_attr( $file_type_icon ) ?>"
422-
<?php endif; ?>
423430
auto-upload="<?php echo $auto_upload ? 'true' : 'false' ?>"
424431
<?php if ( !$download_enabled ): ?>
425432
download-enabled="false"

0 commit comments

Comments
 (0)