Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: add support for multifile upload fields #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions gravityforms-multiple-form-instances.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Gravity_Forms_Multiple_Form_Instances {
public function __construct() {
// hook the HTML ID string find & replace functionality
add_filter( 'gform_get_form_filter', array( $this, 'gform_get_form_filter' ), 10, 2 );
// hook the multifile upload script replace functionality
add_filter( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

/**
Expand Down Expand Up @@ -95,6 +97,14 @@ public function gform_get_form_filter( $form_string, $form ) {
'GFCalc(' . $form['id'] . ',' => 'GFCalc(' . $random_id . ',',
'gf_global["number_formats"][' . $form['id'] . ']' => 'gf_global["number_formats"][' . $random_id . ']',
'gform_next_button_' . $form['id'] . '_' => 'gform_next_button_' . $random_id . '_',
'multipart_params":{"form_id":' . $form['id'] . ',' => 'multipart_params":{"form_id":' . $random_id . ',"original_id":' . $form['id'] . ',"random_id":' . $random_id . ',',
'gform_multifile_upload_' . $form['id'] . '_' => 'gform_multifile_upload_' . $random_id . '_',
'gform_drag_drop_area_' . $form['id'] . '_' => 'gform_drag_drop_area_' . $random_id . '_',
'gform_browse_button_' . $form['id'] . '_' => 'gform_browse_button_' . $random_id . '_',
'gform_preview_' . $form['id'] . '_' => 'gform_preview_' . $random_id . '_',
'gform_multifile_messages_' . $form['id'] . '_' => 'gform_multifile_messages_' . $random_id . '_',
'gform_uploaded_files_' . $form['id'] => 'gform_uploaded_files_' . $random_id,
'gformDeleteUploadedFile(' . $form['id'] . ',' => 'gformDeleteUploadedFile(' . $random_id . ',',
$hidden_field => "<input type='hidden' name='gform_random_id' value='" . $random_id . "' />" . $hidden_field,
);

Expand All @@ -109,6 +119,17 @@ public function gform_get_form_filter( $form_string, $form ) {
return $form_string;
}

/**
* Enqueues the multifile upload script in wordpress scripts.
*
* @access public
*
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_script( 'gform_mfi_multifile', plugin_dir_url( __FILE__ ) . 'js/gravityforms-mfi-multifile.js', array( 'jquery', 'gform_gravityforms' ) );
}

}

// initialize the plugin
Expand Down
32 changes: 32 additions & 0 deletions js/gravityforms-mfi-multifile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//----------------------------------------
//------ MULTIFILE UPLOAD FUNCTIONS ------
//----------------------------------------

(function (gform, gfMultiFileUploader, $) {
$(document).bind('gform_post_render', function(e, formID){
$("form#gform_" + formID + " .gform_fileupload_multifile").each(function(){
var settings = $(this).data('settings');
var uploader = gfMultiFileUploader.uploaders[settings.container];
uploader.bind('BeforeUpload', function(up, file) {
uploader.settings.multipart_params.form_id = uploader.settings.multipart_params.original_id;
});
});
});

gform.addFilter('gform_file_upload_markup', function(html, file, up, strings, imagesUrl){
var html = '<strong>' + file.name + '</strong>';
var formId = up.settings.multipart_params.random_id;
var fieldId = up.settings.multipart_params.field_id;
html = "<img "
+ "class='gform_delete' "
+ "src='" + imagesUrl + "/delete.png' "
+ "onclick='gformDeleteUploadedFile(" + formId + "," + fieldId + ", this);' "
+ "onkeypress='gformDeleteUploadedFile(" + formId + "," + fieldId + ", this);' "
+ "alt='"+ strings.delete_file + "' "
+ "title='" + strings.delete_file
+ "' /> "
+ html;
return html;
});

}(window.gform = window.gform || {}, window.gfMultiFileUploader = window.gfMultiFileUploader || {}, jQuery));
88 changes: 88 additions & 0 deletions tests/unit-tests/GFMFI_GformGetFormFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,94 @@ public function testGformNextButtonReplacement() {
$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testMultipartParamsAddition() {
$input = 'multipart_params&quot;:{&quot;form_id&quot;:' . $this->form['id'] . ',';
$expected = 'multipart_params&quot;:{&quot;form_id&quot;:' . $this->randomId . ',&quot;original_id&quot;:' . $this->form['id'] . ',&quot;random_id&quot;:' . $this->randomId . ',';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformMultifileUploadReplacement() {
$input = 'gform_multifile_upload_' . $this->form['id'] . '_';
$expected = 'gform_multifile_upload_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformDragDropAreaReplacement() {
$input = 'gform_drag_drop_area_' . $this->form['id'] . '_';
$expected = 'gform_drag_drop_area_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformBrowseButtonReplacement() {
$input = 'gform_browse_button_' . $this->form['id'] . '_';
$expected = 'gform_browse_button_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformPreviewReplacement() {
$input = 'gform_preview_' . $this->form['id'] . '_';
$expected = 'gform_preview_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformMultifileMessagesReplacement() {
$input = 'gform_multifile_messages_' . $this->form['id'] . '_';
$expected = 'gform_multifile_messages_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformUploadedFilesReplacement() {
$input = 'gform_uploaded_files_' . $this->form['id'] . '_';
$expected = 'gform_uploaded_files_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformDeleteUploadedFileReplacement() {
$input = 'gformDeleteUploadedFile(' . $this->form['id'] . ',';
$expected = 'gformDeleteUploadedFile(' . $this->randomId . ',';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

$this->assertSame( $expected, $actual );
}

/**
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
Expand Down