Skip to content

Commit

Permalink
Merge pull request #4098 from impress-org/issue/4097
Browse files Browse the repository at this point in the history
fix: resolve importer failing on setting JSON import #4097
  • Loading branch information
ravinderk committed May 9, 2019
2 parents 434ddcc + f1b11af commit 1ac51dc
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions includes/admin/tools/import/class-give-import-core-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public static function upload_widget_settings_file() {
$upload = wp_handle_upload( $_FILES['json'], array( 'test_form' => false ) );

remove_filter( 'upload_mimes', array( __CLASS__, 'json_upload_mimes' ) );
remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ), 10, 4 );
remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ) );

} else {
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid JSON file.', 'give' ) );
Expand Down Expand Up @@ -498,11 +498,22 @@ public static function json_upload_mimes( $existing_mimes = array() ) {
*/
public static function filetype_mod( $check, $file, $filename, $mimes ) {
if ( empty( $check['ext'] ) && empty( $check['type'] ) ) {
// Allow JSON uploads
$secondary_mime = array( 'json' => 'text/plain' );
// Run another check, but only for our secondary mime and not on core mime types.
$check = wp_check_filetype_and_ext( $file, $filename, $secondary_mime );
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );
finfo_close( $finfo );

if( in_array( $real_mime, array( 'text/plain', 'text/html' ) ) ) {
remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ) );

// Allow JSON uploads
$secondary_mime = array( 'json' => $real_mime );
// Run another check, but only for our secondary mime and not on core mime types.
$check = wp_check_filetype_and_ext( $file, $filename, $secondary_mime );

add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ), 10, 4 );
}
}

return $check;
}
}
Expand Down

0 comments on commit 1ac51dc

Please sign in to comment.