From 93c0d3b8404df93d04356412e02921892a764192 Mon Sep 17 00:00:00 2001 From: pedro-mendonca Date: Tue, 22 Feb 2022 17:14:08 +0000 Subject: [PATCH] Set correct mime type and add to allowed mime types --- inc/admin/options.php | 6 ++++-- inc/functions/functions.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/inc/admin/options.php b/inc/admin/options.php index 6630396..391b0c6 100644 --- a/inc/admin/options.php +++ b/inc/admin/options.php @@ -130,7 +130,7 @@ function wpt_customofile_add_rule_validate( $input ) { $input['deactivate_rule'] = isset( $input['deactivate_rule'] ) ? $input['deactivate_rule'] : null; $input['activate_rule'] = isset( $input['activate_rule'] ) ? $input['activate_rule'] : null; $input['delete_rule'] = isset( $input['delete_rule'] ) ? $input['delete_rule'] : null; - $input['mo'] = is_array( $input['mo'] ) ? $input['mo'] : array(); + $input['mo'] = isset( $input['mo'] ) && is_array( $input['mo'] ) ? $input['mo'] : array(); $options = get_option( 'wpt_customofile_options' ); @@ -146,13 +146,15 @@ function wpt_customofile_add_rule_validate( $input ) { if ( $input['wpt-customofile-add-rule'] && isset( $_FILES['wpt_customofile_mo_file']['name'] ) ) { add_filter( 'upload_dir', 'wpt_customofile_filter_upload_dir' ); + add_filter( 'upload_mimes', 'wpt_customofile_filter_upload_mimes' ); $mo_file = wp_handle_upload( $_FILES['wpt_customofile_mo_file'], // phpcs:ignore array( 'test_form' => false, - 'mimes' => array( 'mo' => 'application/octet-stream' ), + 'mimes' => array( 'mo' => 'application/x-gettext-translation' ), ) ); + remove_filter( 'upload_mimes', 'wpt_customofile_filter_upload_mimes' ); remove_filter( 'upload_dir', 'wpt_customofile_filter_upload_dir' ); if ( $mo_file && empty( $mo_file['error'] ) ) { diff --git a/inc/functions/functions.php b/inc/functions/functions.php index 1017129..1f776c6 100644 --- a/inc/functions/functions.php +++ b/inc/functions/functions.php @@ -38,6 +38,24 @@ function wpt_customofile_filter_upload_dir( $upload_dir ) { } +/** + * Filter for upload mime types. + * + * @since 1.2.1 + * + * @param array $wp_get_mime_types Get upload mime types. + * + * @return array Filtered array of upload mime types. + */ +function wpt_customofile_filter_upload_mimes( $wp_get_mime_types ) { + + // Add 'mo' mime type. + $wp_get_mime_types['mo'] = 'application/x-gettext-translation'; + + return $wp_get_mime_types; +} + + /** * Prepare uploads folder. * Check if folder exist and has empty index, create both if don't exist.