So obviously on_submit is when the button is pressed and is_sent_ok refers to when the email is sent. What I need is a hook that is in between these two.
I want to be able to call some javascript that will trigger DropzoneJS to upload the files when the submit button is pressed and no errors are returned. I want it to then want it to add those submitted files to the email that Contact Form 7 sends out.
At the moment I've got the following: - DropzoneJS upload area is added to form using the cf7 using the tag [dropzone dropzone-randomnumber] - On form submission it triggers the upload (I don't want it to upload before the submit button is pressed), hiding the form - The upload process shows a progress bar of how the upload is going; uploads the files into WordPress Media and returns the absolute url for the uploaded file, which is then added to a hidden div as an input field
Of course doing it this way, cf7 sends out the email before the upload is finished. I want it to send the email as soon as the upload queue is finished.
Here is the code for the above:
Early stages of the Plugin within WordPress:
class Contact_Form_7_DropzoneJS_Admin {
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_action( 'admin_init', array( $this, 'cf7_dropzone_tag_generator' ), 55 );
}
/*
* Generates a new tag
*/
public function cf7_dropzone_tag_generator() {
if ( !function_exists( 'wpcf7_add_tag_generator' ) )
return;
wpcf7_add_tag_generator( 'dropzone', __( 'DropzoneJS', 'contact-form-7' ), 'cf7_dropzone_pane', array( $this, 'cf7_dropzone_pane' ) );
}
/*
* Shows a settings page on Form Builder plugin of Contact Form 7
*
*/
public static function cf7_dropzone_pane( $contact_form ) {
?>
<div id="cf7_dropzone_pane" class="hidden">
<form action="">
<table>
<tr><td><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
</table>
<div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form on the left.", 'contact-form-7' ) ); ?><br /><input type="text" name="dropzonejs" class="tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div>
</form>
</div>
<?php
}
}
Contact_Form_7_DropzoneJS_Admin::get_instance();
}
class Contact_Form_7_DropzoneJS {
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_action( 'wpcf7_init', array( $this, 'add_dropzonejs_shortcode' ) );
}
/*
* Registers New shortcode in Contact Form 7. This is displayed while creating
* a new form in Contact Form 7
*/
public function add_dropzonejs_shortcode() {
wpcf7_add_shortcode( 'dropzonejs', array( 'Contact_Form_7_DropzoneJS_Public', 'dropzonejs_shortcode_handler' ), true );
}
}
Contact_Form_7_DropzoneJS::get_instance();
if ( ! class_exists( 'Contact_Form_7_DropzoneJS_Public' ) ) {
class Contact_Form_7_DropzoneJS_Public {
protected static $instance = null;
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/*private function __construct() {
add_filter( 'wpcf7_validate_recaptcha', array( $this, 'recaptcha_validation_filter' ), 10, 2 );
add_filter( 'wpcf7_messages', array( $this, 'recaptcha_messages' ) );
}*/
public static function dropzonejs_shortcode_handler( $tag ) {
$html = '
<div class="dropzone " drop-zone="" id="file-dropzone">
</div>
<input type="hidden" name="submitted" value="1">
<div class="hidden-files">
</div>
';
return $html;
}
}
Contact_Form_7_DropzoneJS_Public::get_instance();
}
add_action( 'wpcf7_before_send_mail', 'wp');
function wpcf7_disablEmailAndRedirect( $cf7 ) {
// get the contact form object
$wpcf7 = WPCF7_ContactForm::get_current();
// do not send the email
$wpcf7->skip_mail = true;
// redirect after the form has been submitted
$wpcf7->set_properties( array(
'additional_settings' => "on_sent_ok: \"alert('http://example.com//');\"",
) );
}
Here is the JS file that triggers the DropzoneJS stuff:
var js = jQuery.noConflict();
js( document ).ready(function() {
Dropzone.autoDiscover = false;
js('#file-dropzone').dropzone({
url: "upload-multi.php?action=upload",
autoProcessQueue: false,
maxFilesize: 5,
maxFiles: 10,
paramName: "uploadfile",
parallelUploads: 10,
maxThumbnailFilesize: 5,
thumbnailWidth: 150,
thumbnailHeight: 150,
acceptedFiles: "image/*,.doc,.docx,.xls,.xlsx,.pdf, .zip",
init: function() {
var submitButton = document.querySelector(".wpcf7-submit");
myDropzone = this;
test = function() {
//myDropzone.removeElement("<button>Remove file</button>");
if (myDropzone.getQueuedFiles().length > 0) {
myDropzone.processQueue();
//myDropzone.removeAllFiles();
js('#formset').hide();
js('.form-progress').show();
js('.fileupload-process').show();
} else {
js('#formset').hide();
js('.form-progress').show();
js('.form-progress p.progress-message').html('Form submitted successfully.');
}
}
this.on("maxfilesexceeded", function(file){
alert("Max upload limit reached. You can only upload 10 files.");
this.removeFile(file);
});
this.on("addedfile", function(file) {
// Create the remove button
var removeButton = Dropzone.createElement("<button>Remove file</button>");
// Capture the Dropzone instance as closure.
var _this = this;
// Listen to the click event
removeButton.addEventListener("click", function(e) {
// Make sure the button click doesn't submit the form:
e.preventDefault();
e.stopPropagation();
// Remove the file preview.
_this.removeFile(file);
// If you want to the delete the file on the server as well,
// you can do the AJAX request here.
});
// Add the button to the file preview element.
file.previewElement.appendChild(removeButton);
this.on("totaluploadprogress", function(progress, totalBytes, totalBytesSent){
//console.log("totaluploadprogress event: ", progress, totalBytes, totalBytesSent);
document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
var newProgress = Math.round(progress);
js('.progress-bar').html(newProgress+'%');
js('.form-progress p.progress-message').html('Uploading files');
if(progress == 100) {
js('.form-progress p.progress-message').html('Form submitted successfully.');
}
});
});
}
});
});
Here is the Upload-multi code:
include_once($_SERVER["DOCUMENT_ROOT"]. '/wp-load.php'); if($_GET['action'] == 'upload') { function insert_attachment($file_handler) {
/*if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false();
}*/
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $file_handler['uploadfile'], $upload_overrides );
if ( $movefile && !isset( $movefile['error'] ) ) {
//echo "File is valid, and was successfully uploaded.\n";
//var_dump( $movefile);
$attachment = array(
'guid' => $movefile['url'],
'post_mime_type' => $movefile['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $movefile['url'] ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $movefile['url'], 0 );
$attach_data = wp_generate_attachment_metadata( $attach_id, $movefile['url'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
} else {
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
echo $movefile['error'];
}
//$attach_id = media_handle_upload($file_handler['uploadfile'], 0, array(), $upload_overrides);
//set_post_thumbnail( $post_id , $attach_id);
//set post thumbnail if setthumb is 1
//if ($setthumb == 1) update_post_meta($post_id,'_thumbnail_id',$attach_id);
//return $attach_id;
//$file = wp_upload_bits( $_FILES['uploadfile']['name'], null, @file_get_contents( $_FILES['uploadfile']['tmp_name'] ) );
echo $movefile['url'];
}
$newupload = insert_attachment($_FILES);
} elseif($_GET['action'] == 'delete') {
$file = sanitize_file_name($_POST['fileList']);
$upload_dir = wp_upload_dir();
//var_dump($_POST['fileList']);
$file = $upload_dir['url'].'/'.$file;
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
//function get_attachment_id_from_src($image_src) {
include_once($_SERVER["DOCUMENT_ROOT"]. '/wp-includes/wp-db.php');
global $wpdb;
//$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$allmiles = $wpdb->get_var( $wpdb->prepare(
"
SELECT ID
FROM $wpdb->posts
WHERE guid = %s
",
$file
) );
$wpdb->show_errors();
$wpdb->print_error();
//$id = $wpdb->get_var($query);
//echo $id;
//return $id;
var_dump($allmiles);
//}
//$fileID = get_attachment_id_from_src($file);
//var_dump($fileID);
//wp_delete_attachment($fileID);
}
I probably didn't need to add all the code to the post, but if I didn't someone would complain. Hopefully someone can help out.
Aucun commentaire:
Enregistrer un commentaire