Skip to content

Commit

Permalink
Add original form id and random form id to gform_multiple_instances_s…
Browse files Browse the repository at this point in the history
…trings filter (#23)

* change: add parameters to gform_multiple_instances_strings filter, form_id and random_id, for convenience and less error prone code, avoiding string replacement to determine form ids

* fix: missing line breaks

* fix: change testGformMultipleInstancesStringsFilter test case to reflect new gform_multiple_instances_strings filter arguments, now containing original form id and new random form id

* fix: fix Missing argument 2 for GFMFI_GformGetFormFilterTest::gform_multiple_instances_strings() by adding the correct number of parameters to add_filter function
  • Loading branch information
medfreeman authored and tyxla committed Jan 30, 2017
1 parent d770a97 commit a64541f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -38,6 +38,10 @@ Additionally, the plugin offers the following actions & filters:

##### gform\_multiple\_instances\_strings

**$strings** *(array)*. An array of find => replace pairs. Occurences of "key" will be replaced with the corresponding "value".
**$strings** *(array)*. An array of find => replace pairs. Occurences of "key" will be replaced with the corresponding "value".

**$form_id** *(int)*. The original form id.

**$random_id** *(int)*. The new, randomly generated form id.

This filter allows you to modify the default strings that will be replaced. The keys are the original strings, and the corresponding values are the strings that keys will be replaced with.
2 changes: 1 addition & 1 deletion gravityforms-multiple-form-instances.php
Expand Up @@ -99,7 +99,7 @@ public function gform_get_form_filter( $form_string, $form ) {
);

// allow addons & plugins to add additional find & replace strings
$strings = apply_filters( 'gform_multiple_instances_strings', $strings );
$strings = apply_filters( 'gform_multiple_instances_strings', $strings, $form['id'], $random_id );

// replace all occurences with the new unique ID
foreach ( $strings as $find => $replace ) {
Expand Down
12 changes: 6 additions & 6 deletions tests/unit-tests/GFMFI_GformGetFormFilterTest.php
Expand Up @@ -2,8 +2,8 @@

class GFMFI_GformGetFormFilterTest extends WP_UnitTestCase {

public function gform_multiple_instances_strings( $strings ) {
$strings['fooBar'] = 'barFoo';
public function gform_multiple_instances_strings( $strings, $form_id, $random_id ) {
$strings['foo_' . $form_id . '_'] = 'foo_' . $random_id . '_';
return $strings;
}

Expand Down Expand Up @@ -509,15 +509,15 @@ public function testHiddenFieldAddition() {
* @covers Gravity_Forms_Multiple_Form_Instances::gform_get_form_filter
*/
public function testGformMultipleInstancesStringsFilter() {
add_filter( 'gform_multiple_instances_strings', array($this, 'gform_multiple_instances_strings') );
add_filter( 'gform_multiple_instances_strings', array($this, 'gform_multiple_instances_strings'), 10, 3 );

$input = "fooBar";
$expected = "barFoo";
$input = 'foo_' . $this->form['id'] . '_';
$expected = 'foo_' . $this->randomId . '_';
$actual = $this->gfmfi->gform_get_form_filter( $input, $this->form );

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

remove_filter( 'gform_multiple_instances_strings', array($this, 'gform_multiple_instances_strings') );
remove_filter( 'gform_multiple_instances_strings', array($this, 'gform_multiple_instances_strings'), 10 );
}

}

0 comments on commit a64541f

Please sign in to comment.