Skip to content

Commit

Permalink
Forms: Add unique IDs when multiple instances. (#40998)
Browse files Browse the repository at this point in the history
  • Loading branch information
lezama authored and coder-karen committed Jan 24, 2025
1 parent a98cc51 commit ff36261
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/update-forms-unique-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Forms: Add unique ids to each form
12 changes: 10 additions & 2 deletions projects/packages/forms/src/contact-form/class-contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class Contact_Form extends Contact_Form_Shortcode {
/**
* The most recent (inclusive) contact-form shortcode processed.
*
* @var Contact_Form
* @var Contact_Form|null
*/
public static $last;

/**
* Form we are currently looking at. If processed, will become $last
*
* @var Contact_Form
* @var Contact_Form|null
*/
public static $current_form;

Expand Down Expand Up @@ -118,6 +118,14 @@ public function __construct( $attributes, $content = null ) {
$default_to .= $post_author->user_email;
}

if ( ! empty( self::$forms ) ) {
// Ensure 'id' exists in $attributes before trying to modify it
if ( ! isset( $attributes['id'] ) ) {
$attributes['id'] = '';
}
$attributes['id'] = $attributes['id'] . '-' . ( count( self::$forms ) + 1 );
}

$this->hash = sha1( wp_json_encode( $attributes ) );
self::$forms[ $this->hash ] = $this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/**
* Unit Tests for Automattic\Jetpack\Forms\Contact_Form.
*
* To run the test visit the packages/forms directory and run composer test-php
*
* @package automattic/jetpack-forms
*/

Expand All @@ -21,6 +23,8 @@ class WP_Test_Contact_Form extends BaseTestCase {

private $post;

private $track_feedback_inserted;

private $plugin;

/**
Expand Down Expand Up @@ -54,7 +58,7 @@ private function set_globals() {
public function set_up_test_case() {
// Avoid actually trying to send any mail.
add_filter( 'pre_wp_mail', '__return_true', PHP_INT_MAX );

$this->track_feedback_inserted = array();
$this->set_globals();

$author_id = wp_insert_user(
Expand Down Expand Up @@ -98,16 +102,28 @@ public function tear_down() {
remove_all_filters( 'wp_mail' );
remove_all_filters( 'grunion_still_email_spam' );
remove_all_filters( 'jetpack_contact_form_is_spam' );

// Reset the forms array
Contact_Form::$forms = array();
Contact_Form::$last = null;
Contact_Form::$current_form = null;
}

/**
* Adds the field values to the global $_POST value.
*
* @param array $values Array of field key value pairs.
* @param array $values Array of form fields and values.
* @param string $form_id Optional form ID. If not provided, will use $this->post->ID.
*/
private function add_field_values( $values ) {
private function add_field_values( $values, $form_id = null ) {
$prefix = $form_id ? $form_id : 'g' . $this->post->ID;
$_POST = array();
foreach ( $values as $key => $val ) {
$_POST[ 'g' . $this->post->ID . '-' . $key ] = $val;
if ( strpos( $key, 'contact-form' ) === 0 || strpos( $key, 'action' ) === 0 ) {
$_POST[ $key ] = $val;
} else {
$_POST[ $prefix . '-' . $key ] = $val;
}
}
}

Expand Down Expand Up @@ -2067,4 +2083,63 @@ public function test_grunion_contact_form_apply_block_attribute() {
Util::grunion_contact_form_apply_block_attribute( $original, array( 'foo' => 'bar' ) )
);
}
/**
* Helper function that tracks the ids of the feedbacks that got created.
*/
public function track_feedback_inserted( $post_id ) {
$this->track_feedback_inserted[] = $post_id;
}
/**
* Tests that multiple instances of the same form work correctly with unique IDs.
*/
public function test_multiple_form_instances_with_unique_ids() {
global $post;

add_action( 'grunion_after_feedback_post_inserted', array( $this, 'track_feedback_inserted' ), 10, 1 );

$this->add_field_values(
array(
'name' => 'First form name 1',
'message' => 'First form message 1',
),
'g' . $post->ID
);

$form1 = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Message' type='textarea' required='1'/]" );
// Submit first form
$result1 = $form1->process_submission();

$this->assertTrue( is_string( $result1 ), 'First form submission should be successful' );

$this->add_field_values(
array(
'name' => 'First form name 2',
'message' => 'First form message 2',
),
'g' . $post->ID . '-2'
);

$form2 = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Message' type='textarea' required='1'/]" );
$result2 = $form2->process_submission();

$this->assertTrue( is_string( $result2 ), 'First form submission should be successful' );

// Verify that the forms have different IDs
$this->assertNotEquals( $form1->get_attribute( 'id' ), $form2->get_attribute( 'id' ), 'Forms should have unique IDs' );

remove_action( 'grunion_after_feedback_post_inserted', array( $this, 'track_feedback_inserted' ), 10 );

$this->assertCount( 2, $this->track_feedback_inserted, 'The number of feedback forms that were inserted does not match! Expected 2.' );

// Add assertion to ensure array is not empty
$this->assertNotEmpty( $this->track_feedback_inserted, 'No feedback forms were inserted' );

$count = 1;
foreach ( $this->track_feedback_inserted as $feedback_id ) {
$feedback = get_post( $feedback_id );
$this->assertStringContainsString( 'First form name ' . $count, $feedback->post_content );
$this->assertStringContainsString( 'First form message ' . $count, $feedback->post_content );
++$count;
}
}
} // end class
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/update-forms-unique-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: bugfix
Comment: Forms: Add unique ids to each form


0 comments on commit ff36261

Please sign in to comment.