Skip to content
Merged
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
5 changes: 4 additions & 1 deletion projects/packages/forms/src/contact-form/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,10 @@ public function get_compiled_fields( $context = 'default', $array_shape = 'all'
$compiled_fields[ $field->get_key() ] = $field->get_render_value( $context );
break;
case 'label-value':
$compiled_fields[ $field->get_label( $context, $count_field_labels[ $label ] ) ] = $field->get_render_value( $context );
$compiled_fields[ $field->get_label( $context, $count_field_labels[ $label ] ) ] = $field->get_render_value( $context );
break;
case 'id-value':
$compiled_fields[ $field->get_form_field_id() ] = $field->get_render_value( $context );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets also add a test for this new shape :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, but had to use custom expected array value since dataprovider doesn't allow to pass down any arguments 🙃

break;
}
}
Expand Down
17 changes: 1 addition & 16 deletions projects/packages/forms/src/service/class-form-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function send_webhooks( $post_id, $fields, $is_spam, $entry_values ) { //
return;
}

$form_data = $this->get_form_data( $feedback );
$form_data = $feedback->get_compiled_fields( 'webhook', 'id-value' );

// Iterate through each webhook and send the request
foreach ( $webhooks as $webhook ) {
Expand Down Expand Up @@ -250,19 +250,4 @@ private function send_webhook( $data, $webhook ) {

return wp_remote_request( $url, $args );
}

/**
* Gather fields key/value pairs from the Feedback object
*
* @param Feedback $feedback The Feedback instance containing submitted form data.
* @return array The form data key/value pairs.
*/
private function get_form_data( $feedback ) {
$fields = array();
foreach ( $feedback->get_fields() as $field ) {
$fields[ $field->get_form_field_id() ] = $field->get_value();
}

return $fields;
}
}
16 changes: 16 additions & 0 deletions projects/packages/forms/tests/php/contact-form/Feedback_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,11 @@ public static function get_compiled_fields_data_provider() {
),
'message' => 'Compiled fields should return only labels as indexed array.',
),
'id-value_format' => array(
'format' => 'id-value',
'expected' => array(), // Rebuilt dynamically in the test with actual form_id
'message' => 'Compiled fields should return field IDs mapped to values.',
),
);
}

Expand Down Expand Up @@ -1906,6 +1911,17 @@ public function test_get_compiled_fields_shapes( $format, $expected, $message )
// Test the specified format
$compiled_fields = $response->get_compiled_fields( 'default', $format );

// For id-value format, rebuild expected with actual form_id, there
// was no way of passing the form_id to the data provider.
if ( 'id-value' === $format ) {
$expected = array(
'g' . $form_id . '-name' => $test_name,
'g' . $form_id . '-email' => $test_email,
'g' . $form_id . '-website' => $test_website,
'g' . $form_id . '-message' => $test_message,
);
}

$this->assertEquals( $expected, $compiled_fields, $message );
}

Expand Down
Loading