true, ) ); } function wpcf7_file_form_tag_handler( $tag ) { if ( empty( $tag->name ) ) { return ''; } $validation_error = wpcf7_get_validation_error( $tag->name ); $class = wpcf7_form_controls_class( $tag->type ); if ( $validation_error ) { $class .= ' wpcf7-not-valid'; } $atts = array(); $atts['size'] = $tag->get_size_option( '40' ); $atts['class'] = $tag->get_class_option( $class ); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); $atts['accept'] = wpcf7_acceptable_filetypes( $tag->get_option( 'filetypes' ), 'attr' ); if ( $tag->is_required() ) { $atts['aria-required'] = 'true'; } if ( $validation_error ) { $atts['aria-invalid'] = 'true'; $atts['aria-describedby'] = wpcf7_get_validation_error_reference( $tag->name ); } else { $atts['aria-invalid'] = 'false'; } $atts['type'] = 'file'; $atts['name'] = $tag->name; $atts = wpcf7_format_atts( $atts ); $html = sprintf( '%3$s', sanitize_html_class( $tag->name ), $atts, $validation_error ); return $html; } /* Encode type filter */ add_filter( 'wpcf7_form_enctype', 'wpcf7_file_form_enctype_filter', 10, 1 ); function wpcf7_file_form_enctype_filter( $enctype ) { $multipart = (bool) wpcf7_scan_form_tags( array( 'type' => array( 'file', 'file*' ) ) ); if ( $multipart ) { $enctype = 'multipart/form-data'; } return $enctype; } /* Validation + upload handling filter */ add_filter( 'wpcf7_validate_file', 'wpcf7_file_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_file*', 'wpcf7_file_validation_filter', 10, 2 ); function wpcf7_file_validation_filter( $result, $tag ) { $name = $tag->name; $id = $tag->get_id_option(); $file = isset( $_FILES[$name] ) ? $_FILES[$name] : null; if ( ! empty( $file['error'] ) and UPLOAD_ERR_NO_FILE !== $file['error'] ) { $result->invalidate( $tag, wpcf7_get_message( 'upload_failed_php_error' ) ); return $result; } if ( empty( $file['tmp_name'] ) and $tag->is_required() ) { $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); return $result; } if ( empty( $file['tmp_name'] ) or ! is_uploaded_file( $file['tmp_name'] ) ) { return $result; } /* File type validation */ $file_type_pattern = wpcf7_acceptable_filetypes( $tag->get_option( 'filetypes' ), 'regex' ); $file_type_pattern = '/\.(' . $file_type_pattern . ')$/i'; if ( empty( $file['name'] ) or ! preg_match( $file_type_pattern, $file['name'] ) ) { $result->invalidate( $tag, wpcf7_get_message( 'upload_file_type_invalid' ) ); return $result; } /* File size validation */ $allowed_size = $tag->get_limit_option(); if ( ! empty( $file['size'] ) and $allowed_size < $file['size'] ) { $result->invalidate( $tag, wpcf7_get_message( 'upload_file_too_large' ) ); return $result; } wpcf7_init_uploads(); // Confirm upload dir $uploads_dir = wpcf7_upload_tmp_dir(); $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir ); $filename = $file['name']; $filename = wpcf7_canonicalize( $filename, 'as-is' ); $filename = wpcf7_antiscript_file_name( $filename ); $filename = apply_filters( 'wpcf7_upload_file_name', $filename, $file['name'], $tag ); $filename = wp_unique_filename( $uploads_dir, $filename ); $new_file = path_join( $uploads_dir, $filename ); if ( false === @move_uploaded_file( $file['tmp_name'], $new_file ) ) { $result->invalidate( $tag, wpcf7_get_message( 'upload_failed' ) ); return $result; } // Make sure the uploaded file is only readable for the owner process chmod( $new_file, 0400 ); if ( $submission = WPCF7_Submission::get_instance() ) { $submission->add_uploaded_file( $name, $new_file ); } return $result; } add_filter( 'wpcf7_mail_tag_replaced_file', 'wpcf7_file_mail_tag', 10, 4 ); add_filter( 'wpcf7_mail_tag_replaced_file*', 'wpcf7_file_mail_tag', 10, 4 ); function wpcf7_file_mail_tag( $replaced, $submitted, $html, $mail_tag ) { $submission = WPCF7_Submission::get_instance(); $uploaded_files = $submission->uploaded_files(); $name = $mail_tag->field_name(); if ( ! empty( $uploaded_files[$name] ) ) { $replaced = wp_basename( $uploaded_files[$name] ); } return $replaced; } /* Messages */ add_filter( 'wpcf7_messages', 'wpcf7_file_messages', 10, 1 ); function wpcf7_file_messages( $messages ) { return array_merge( $messages, array( 'upload_failed' => array( 'description' => __( "Uploading a file fails for any reason", 'contact-form-7' ), 'default' => __( "There was an unknown error uploading the file.", 'contact-form-7' ) ), 'upload_file_type_invalid' => array( 'description' => __( "Uploaded file is not allowed for file type", 'contact-form-7' ), 'default' => __( "You are not allowed to upload files of this type.", 'contact-form-7' ) ), 'upload_file_too_large' => array( 'description' => __( "Uploaded file is too large", 'contact-form-7' ), 'default' => __( "The file is too big.", 'contact-form-7' ) ), 'upload_failed_php_error' => array( 'description' => __( "Uploading a file fails for PHP error", 'contact-form-7' ), 'default' => __( "There was an error uploading the file.", 'contact-form-7' ) ) ) ); } /* Tag generator */ add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_file', 50, 0 ); function wpcf7_add_tag_generator_file() { $tag_generator = WPCF7_TagGenerator::get_instance(); $tag_generator->add( 'file', __( 'file', 'contact-form-7' ), 'wpcf7_tag_generator_file' ); } function wpcf7_tag_generator_file( $contact_form, $args = '' ) { $args = wp_parse_args( $args, array() ); $type = 'file'; $description = __( "Generate a form-tag for a file uploading field. For more details, see %s.", 'contact-form-7' ); $desc_link = wpcf7_link( __( 'https://contactform7.com/file-uploading-and-attachment/', 'contact-form-7' ), __( 'File uploading and attachment', 'contact-form-7' ) ); ?>
%s