* @Description : Plugin main core
* @Package : Drag & Drop Multiple File Upload - Contact Form 7
* @Author : Glen Don L. Mongaya
if ( ! defined( 'ABSPATH' ) || ! defined('dnd_upload_cf7') ) {
* Begin : begin plugin hooks
add_action( 'wpcf7_init', 'dnd_cf7_upload_add_form_tag_file' );
add_action( 'wpcf7_enqueue_scripts', 'dnd_cf7_scripts' );
// Hook on plugins loaded
add_action('plugins_loaded','dnd_cf7_upload_plugins_loaded');
add_action( 'wp_ajax_dnd_codedropz_upload', 'dnd_upload_cf7_upload' );
add_action( 'wp_ajax_nopriv_dnd_codedropz_upload', 'dnd_upload_cf7_upload' );
add_action('wp_ajax_nopriv_dnd_codedropz_upload_delete', 'dnd_codedropz_upload_delete');
add_action('wp_ajax_dnd_codedropz_upload_delete','dnd_codedropz_upload_delete');
add_action('wp_ajax__wpcf7_check_nonce', 'dnd_wpcf7_nonce_check');
add_action('wp_ajax_nopriv__wpcf7_check_nonce', 'dnd_wpcf7_nonce_check');
add_filter('wpcf7_posted_data', 'dnd_wpcf7_posted_data', 10, 1);
add_action('wpcf7_before_send_mail','dnd_cf7_before_send_mail', 30, 1);
add_action('wpcf7_mail_components','dnd_cf7_mail_components', 50, 2);
// Auto clean up dir/files
add_action('template_redirect', 'dnd_cf7_auto_clean_dir', 20, 0 );
add_filter( 'plugin_row_meta', 'dnd_custom_plugin_row_meta', 10, 2 );
add_filter('upload_mimes', 'dnd_extra_mime_types', 1, 1);
add_filter( 'plugin_action_links_' . plugin_basename( dnd_upload_cf7_directory ) .'/drag-n-drop-upload-cf7.php', 'dnd_cf7_upload_links' );
// Add Submenu - Settings
add_action('admin_menu', 'dnd_admin_settings');
// Add custom script in footer
add_action('wp_footer','dnd_custom_scripts');
add_action('before_delete_post', 'dnd_remove_uploaded_files');
function dnd_wpcf7_nonce_check(){
if( ! check_ajax_referer( 'dnd-cf7-security-nonce', false, false ) ){
wp_send_json_success( wp_create_nonce( "dnd-cf7-security-nonce" ) );
function dnd_cf7_upload_links( $actions ) {
$upload_links = array('<a href="' . admin_url( 'admin.php?page=drag-n-drop-upload' ) . '">Settings</a>',);
$actions = array_merge( $upload_links, $actions );
// Load plugin text-domain
function dnd_cf7_upload_plugins_loaded() {
load_plugin_textdomain( 'drag-and-drop-multiple-file-upload-contact-form-7', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages' );
$dir = dnd_get_upload_dir();
if( isset( $dir['upload_dir'] ) && is_dir( $dir['upload_dir'] ) ) {
// Generate .htaccess file`
$htaccess_file = path_join( dirname( $dir['upload_dir'] ), '.htaccess' );
if ( ! file_exists( $htaccess_file ) ) {
if ( $handle = fopen( $htaccess_file, 'w' ) ) {
fwrite( $handle, "Options -Indexes \n <Files *.php> \n deny from all \n </Files>" );
if( get_option('drag_n_drop_fix_spam') == 'yes' ) {
add_filter('wpcf7_spam', function(){
// Remove uploaded files when item is deleted permanently.
function dnd_remove_uploaded_files( $post_id ) {
$post_type = get_post_type( $post_id );
$page = get_post( $post_id );
if( $post_type == 'flamingo_inbound' ) {
preg_match_all( '/(.*?)(\/'.wpcf7_dnd_dir.'\/wpcf7-files\/.*$)/m', $page->post_content, $matches );
if( $matches[0] && count( $matches[0] ) > 0 ) {
foreach( $matches[0] as $files ) {
$new_file = str_replace( site_url().'/', wp_normalize_path( ABSPATH ), $files );
if( file_exists( $new_file ) ) {
wp_delete_file( $new_file );
// Modify contact form posted_data
function dnd_wpcf7_posted_data( $posted_data ){
// Subbmisson instance from CF7
$submission = WPCF7_Submission::get_instance();
// Make sure we have the data
$posted_data = $submission->get_posted_data();
// Scan and get all form tags from cf7 generator
$forms_tags = $submission->get_contact_form();
$uploads_dir = dnd_get_upload_dir();
if( $forms = $forms_tags->scan_form_tags() ) {
foreach( $forms as $field ) {
$field_name = $field->name;
if( $field->basetype == 'mfile' && isset( $posted_data[$field_name] ) && ! empty( $posted_data[$field_name] ) ) {
foreach( $posted_data[$field_name] as $key => $file ) {
$posted_data[$field_name][$key] = trailingslashit( $uploads_dir['upload_url'] ) . wp_basename( $file );
// Hooks for admin settings
function dnd_admin_settings() {
add_submenu_page( 'wpcf7', 'Drag & Drop Uploader - Settings', 'Drag & Drop Upload', 'manage_options', 'drag-n-drop-upload','dnd_upload_admin_settings');
add_action('admin_init','dnd_upload_register_settings');
function dnd_extra_mime_types( $mime_types ){
$mime_types['xls'] = 'application/excel, application/vnd.ms-excel, application/x-excel, application/x-msexcel';
$mime_types['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
function dnd_cf7_error_msg( $error_key ) {
// Array of default error message
'server_limit' => __('The uploaded file exceeds the maximum upload size of your server.','drag-and-drop-multiple-file-upload-contact-form-7'),
'failed_upload' => __('Uploading a file fails for any reason','drag-and-drop-multiple-file-upload-contact-form-7'),
'large_file' => __('Uploaded file is too large','drag-and-drop-multiple-file-upload-contact-form-7'),
'invalid_type' => __('Uploaded file is not allowed for file type','drag-and-drop-multiple-file-upload-contact-form-7'),
'max_file_limit' => __('Note : Some of the files are not uploaded ( Only %count% files allowed )','drag-and-drop-multiple-file-upload-contact-form-7'),
'required' => __('This field is required.', 'drag-and-drop-multiple-file-upload-contact-form-7' ),
'min_file' => __('The minimum file upload is','drag-and-drop-multiple-file-upload-contact-form-7'),
// return error message based on $error_key request
if( isset( $errors[ $error_key ] ) ) {
return $errors[ $error_key ];
function dnd_get_upload_dir() {
$upload = wp_upload_dir();
$uploads_dir = wpcf7_dnd_dir . '/wpcf7-files';
// If save as attachment ( also : Check if upload use year and month folders )
if( get_option('drag_n_drop_mail_attachment') == 'yes' ) {
$uploads_dir = ( get_option('uploads_use_yearmonth_folders') ? wpcf7_dnd_dir . $upload['subdir'] : wpcf7_dnd_dir );
if ( ! is_dir( trailingslashit( $upload['basedir'] ) . $uploads_dir ) ) {
wp_mkdir_p( trailingslashit( $upload['basedir'] ) . $uploads_dir );
// Make sure directory exist before returning
if( file_exists( trailingslashit( $upload['basedir'] ) . $uploads_dir ) ) {
'upload_dir' => trailingslashit( $upload['basedir'] ) . $uploads_dir,
'upload_url' => trailingslashit( $upload['baseurl'] ) . $uploads_dir
return trailingslashit( $upload['basedir'] ) . $uploads_dir;
// Clean up directory - From Contact Form 7
function dnd_cf7_auto_clean_dir( $seconds = 3600, $max = 60 ) {
if ( is_admin() || 'GET' != $_SERVER['REQUEST_METHOD'] || is_robots() || is_feed() || is_trackback() ) {
if( get_option('drag_n_drop_disable_auto_delete') == 'yes' ) {
$upload = wp_upload_dir();
$dir = trailingslashit( $upload['basedir'] ) . wpcf7_dnd_dir . '/wpcf7-files/';
// Make sure dir is readable or writable
if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! wp_is_writable( $dir ) ) {
$seconds = apply_filters( 'dnd_cf7_auto_delete_files', $seconds );
if ( $handle = @opendir( $dir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file == "." || $file == ".." ) {
// Get file time of files OLD files.
$mtime = @filemtime( $dir . $file );
if ( $mtime && time() < $mtime + absint( $seconds ) ) { // less than $seconds old
if( $file != '.htaccess' ) {
wp_delete_file( $dir . $file );
// Hooks before sending the email - ( append links to body email )
function dnd_cf7_before_send_mail( $wpcf7 ){
$upload_path = dnd_get_upload_dir();
// Check If send attachment as link
if( ! get_option('drag_n_drop_mail_attachment') ) {
$submission = WPCF7_Submission::get_instance();
$submitted['posted_data'] = $submission->get_posted_data();
$fields = $wpcf7->scan_form_tags();
$mail = $wpcf7->prop('mail');
$mail_2 = $wpcf7->prop('mail_2');
$simple_path = dirname( $upload_path['upload_url'] ); // dirname - remove duplicate form dir (/wpcf-dnd-uploads/wpcf7-dnd-uploads/example.jpg)
// Loop fields and replace mfile code
foreach( $fields as $field ) {
if( $field->basetype == 'mfile') {
if( isset( $submitted['posted_data'][$field->name] ) && ! empty( $submitted['posted_data'][$field->name] ) ) {
$files = $submitted['posted_data'][$field->name];
$mail_links = dnd_cf7_links( $files, $mail['use_html'] );
$mail['body'] = str_replace( "[$field->name]", "\n" . implode( "\n", $mail_links ), $mail['body'] );
if( $mail_2['active'] ) {
$mail_links_2 = dnd_cf7_links( $files, $mail_2['use_html'] );
$mail_2['body'] = str_replace( "[$field->name]", "\n" . implode( "\n", $mail_links_2 ), $mail_2['body'] );
$wpcf7->set_properties( array("mail" => $mail) );
if( $mail_2['active'] ) {
$wpcf7->set_properties( array("mail_2" => $mail_2) );
function dnd_cf7_links( $files, $use_html = false) {
// check and make sure we have files
foreach( $files as $file ) {
$links[] = ( $use_html ? '<a href="'. esc_url( $file ) .'">'. wp_basename( $file ) .'</a>' : $file );
// Allow other themes/plugin to modify data.
return apply_filters('dndcf7_before_send_files', $links, $files );
function dnd_logs( $message, $email = false ) {
$uploads_dir = dnd_get_upload_dir();
$file = fopen( $uploads_dir['upload_dir']."/logs.txt", "a") or die("Unable to open file!");
fwrite( $file, "\n". ( is_array( $message ) ? print_r( $message, true ) : $message ) );
// hooks - Custom cf7 Mail components ( Attached File on Email )
function dnd_cf7_mail_components( $components, $form ) {
$uploads_dir = dnd_get_upload_dir();
// cf7 - Submission Object
$submission = WPCF7_Submission::get_instance();
$fields = $form->scan_form_tags();
// Send email link as an attachment.
if( get_option('drag_n_drop_mail_attachment') == 'yes' ) {
// Get mail,mail_2 attachment [tags]
$mail = array('mail','mail_2');
foreach( $mail as $single_mail ) {
$props_mail[] = $form->prop( $single_mail );
// Get email attachments (mail, mail_2)
$mail = $props_mail[ $_mail ];
if( $mail['active'] && $mail['attachments'] ) {
// Loop fields get mfile only.
foreach( $fields as $field ) {
// If field type equal to mfile which our default field.
if( $field->basetype == 'mfile') {
// Make sure we have files to attach
if( isset( $_POST[ $field->name ] ) && count( $_POST[ $field->name ] ) > 0 ) {
// Check and make sure [upload-file-xxx] exists in attachments - fields
if ( false !== strpos( $mail['attachments'], "[{$field->name}]" ) ) {
// Loop all the files and attach to cf7 components
foreach( $_POST[ $field->name ] as $_file ) {
// Join dir and a new file name ( get from <input type="hidden" name="upload-file-333"> )
$new_file_name = trailingslashit( $uploads_dir['upload_dir'] ) . wp_basename( $_file );
// Check if submitted and file exists then file is ready.
if ( $submission && file_exists( $new_file_name ) ) {
$components['attachments'][] = $new_file_name;
// Increment mail counter
// Return setup components
function dnd_cf7_scripts() {
$version = dnd_upload_cf7_version;
wp_enqueue_script( 'codedropz-uploader', plugins_url ('/assets/js/codedropz-uploader-min.js', dirname(__FILE__) ), array('jquery'), $version, true );
wp_enqueue_script( 'dnd-upload-cf7', plugins_url ('/assets/js/dnd-upload-cf7.js', dirname(__FILE__) ), array('jquery','codedropz-uploader','contact-form-7'), $version, true );
$data_options = apply_filters('dnd_cf7_data_options',
'tag' => ( get_option('drag_n_drop_heading_tag') ? get_option('drag_n_drop_heading_tag') : 'h3' ),
'text' => ( get_option('drag_n_drop_text') ? get_option('drag_n_drop_text') : __('Drag & Drop Files Here','drag-and-drop-multiple-file-upload-contact-form-7') ),
'or_separator' => ( get_option('drag_n_drop_separator') ? get_option('drag_n_drop_separator') : __('or','drag-and-drop-multiple-file-upload-contact-form-7') ),
'browse' => ( get_option('drag_n_drop_browse_text') ? get_option('drag_n_drop_browse_text') : __('Browse Files','drag-and-drop-multiple-file-upload-contact-form-7') ),
'server_max_error' => ( get_option('drag_n_drop_error_server_limit') ? get_option('drag_n_drop_error_server_limit') : dnd_cf7_error_msg('server_limit') ),
'large_file' => ( get_option('drag_n_drop_error_files_too_large') ? get_option('drag_n_drop_error_files_too_large') : dnd_cf7_error_msg('large_file') ),
'inavalid_type' => ( get_option('drag_n_drop_error_invalid_file') ? get_option('drag_n_drop_error_invalid_file') : dnd_cf7_error_msg('invalid_type') ),
'max_file_limit' => ( get_option('drag_n_drop_error_max_file') ? get_option('drag_n_drop_error_max_file') : dnd_cf7_error_msg('max_file_limit') ),
'required' => dnd_cf7_error_msg('required'),
'text' => __('deleting','drag-and-drop-multiple-file-upload-contact-form-7'),
'title' => __('Remove','drag-and-drop-multiple-file-upload-contact-form-7')
// registered script with data for a JavaScript variable.
wp_localize_script( 'dnd-upload-cf7', 'dnd_cf7_uploader',
'ajax_url' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce( "dnd-cf7-security-nonce" ),
'drag_n_drop_upload' => $data_options,
'dnd_text_counter' => __('of','drag-and-drop-multiple-file-upload-contact-form-7'),
'disable_btn' => ( get_option('drag_n_drop_disable_btn') == 'yes' ? true : false )
wp_enqueue_style( 'dnd-upload-cf7', plugins_url ('/assets/css/dnd-upload-cf7.css', dirname(__FILE__) ), '', $version );
function dnd_cf7_upload_add_form_tag_file() {
wpcf7_add_form_tag( array( 'mfile ', 'mfile*'), 'dnd_cf7_upload_form_tag_handler', array( 'name-attr' => true ) );
// Form tag handler from the tag - callback
function dnd_cf7_upload_form_tag_handler( $tag ) {
// check and make sure tag name is not empty
if ( empty( $tag->name ) ) {
$validation_error = wpcf7_get_validation_error( $tag->name );
$class = wpcf7_form_controls_class( 'drag-n-drop-file d-none' );
// Add not-valid class if there's an error.
if ( $validation_error ) {
$class .= ' wpcf7-not-valid';
// Get current form Object
$form = WPCF7_ContactForm::get_current();