Skip to content

Commit 85f7b97

Browse files
enejblezama
andauthored
Forms: Add Feedback and Feedback_Field classes and start using them to display data (#44121)
* Forms: Adds a new Form_Responses class * Add tests for Form_Response::strip_tags method * Make endpoint rendering work * Make for submission work with the new response data * Add test for get_render_value * Update has_file tests * Add tests for get_render_api_value method * rename for clarity * Simplify things * Fix rendering on submit * Rename Form_Response => Feedback * Rename Response_Field => Feedback_Field * Implement CSV Export * minor update test method name * Minor fixes * Add reprecation * Update the code coverage * minor: test fixes * Introduce Feedback_Entry class * split constructore up, refactor form * Add Feedback_Author Class * Fix phan issue * Add tests for Feedback_Author * Fix tests * minor refactor * Refactor legacy content parsing in Feedback class Modularized the parse_legacy_content method by extracting logic into helper methods for splitting content, extracting values and lines, parsing JSON and array formats, and processing fields. This improves readability, maintainability, and separation of concerns in legacy feedback parsing. * Refactor feedback field rendering and API output Refactored Feedback_Field to support context-aware rendering via get_render_value($context), and made get_render_api_value() private. Updated Feedback to allow flexible compiled field output shapes and context. Adjusted endpoint and tests to use the new API, improving clarity and extensibility for field rendering in different contexts. * Add context csv * Add more context * Fixing php phan issues * Improve test coverage * Fix typo in method name for author info retrieval Renamed get_computer_author_info to get_computed_author_info in Feedback_Author to correct a typo and ensure consistency in method naming. * Rename legacy feedback properties in Feedback class Renamed $feedback_id and $feedback_title to $legacy_feedback_id and $legacy_feedback_title throughout the Feedback class to clarify their legacy status and indicate they are not used in the new feedback system. Updated all references, getters, and related logic accordingly. * Add more Feedback tests * Add form count test * Move the test * More tests and phan fixes * update baseline * Add get_value assertions to Feedback_Field tests Extended the test_get_render_value method to include assertions for the get_value method, ensuring both scalar and array values are correctly returned by Feedback_Field. * Add feedback field tests * remove unused method * Refactor file field type handling in feedback forms Replaces is_file_field() with a more generic is_of_type() method in Feedback_Field, simplifying type checks. Updates legacy file upload handling in Feedback to explicitly set the field type to 'file'. Adjusts and expands related unit tests to cover new logic and ensure correct file field behavior. * Don't reimplement strip_tags method * Export personal Data update to use FeedBack Class * Fix typos in docblocks - Fix "lable" → "label" in Feedback_Field class - Fix "recieved" → "received" in Feedback class - Fix "invividual" → "individual" in Feedback_Field class - Complete incomplete class docblock for Feedback_Field * Making ajax work. * Add a test for label|value format * Add test for get_json_data --------- Co-authored-by: Miguel Lezama <[email protected]>
1 parent 80cf0b3 commit 85f7b97

16 files changed

+4399
-856
lines changed

projects/packages/forms/.phan/baseline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// PhanTypeMismatchArgumentInternal : 6 occurrences
1616
// PhanTypeMismatchArgumentProbablyReal : 6 occurrences
1717
// PhanPluginDuplicateAdjacentStatement : 2 occurrences
18-
// PhanPluginRedundantAssignment : 2 occurrences
1918
// PhanTypeConversionFromArray : 2 occurrences
2019
// PhanTypeMismatchReturn : 2 occurrences
2120
// PhanPluginMixedKeyNoKey : 1 occurrence
21+
// PhanPluginRedundantAssignment : 1 occurrence
2222
// PhanPossiblyNullTypeMismatchProperty : 1 occurrence
2323
// PhanTypeArraySuspiciousNullable : 1 occurrence
2424
// PhanTypeMismatchReturnNullable : 1 occurrence
@@ -28,7 +28,7 @@
2828
'file_suppressions' => [
2929
'src/contact-form/class-admin.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn'],
3030
'src/contact-form/class-contact-form-field.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanPossiblyNullTypeMismatchProperty', 'PhanTypeConversionFromArray', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnProbablyReal'],
31-
'src/contact-form/class-contact-form-plugin.php' => ['PhanPluginDuplicateAdjacentStatement', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginRedundantAssignment', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturnProbablyReal'],
31+
'src/contact-form/class-contact-form-plugin.php' => ['PhanPluginDuplicateAdjacentStatement', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturnProbablyReal'],
3232
'src/contact-form/class-contact-form-shortcode.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchReturnProbablyReal'],
3333
'src/contact-form/class-contact-form.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginRedundantAssignment', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnNullable', 'PhanTypeMismatchReturnProbablyReal'],
3434
'src/dashboard/class-dashboard-view-switch.php' => ['PhanUnreferencedUseNormal'],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Forms: Adds a new Feedback and Feedback_Field class that keeps consistency between DB form response and the one before we submit it.
5+

projects/packages/forms/src/contact-form/class-contact-form-endpoint.php

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -530,100 +530,48 @@ public function prepare_item_for_response( $item, $request ) {
530530
$data = $response->get_data();
531531
$fields = $this->get_fields_for_response( $request );
532532

533-
$has_file = false;
534-
$base_fields = array(
535-
'email_marketing_consent' => '',
536-
'entry_title' => '',
537-
'entry_permalink' => '',
538-
'feedback_id' => '',
539-
);
540-
541-
$data_defaults = array(
542-
'_feedback_author' => '',
543-
'_feedback_author_email' => '',
544-
'_feedback_author_url' => '',
545-
'_feedback_all_fields' => array(),
546-
'_feedback_ip' => '',
547-
'_feedback_subject' => '',
548-
);
549-
550-
$feedback_data = array_merge(
551-
$data_defaults,
552-
\Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin::parse_fields_from_content( $item->ID )
553-
);
554-
555-
$all_fields = array_merge( $base_fields, $feedback_data['_feedback_all_fields'] );
533+
$response = Feedback::get( $item->ID );
556534

557535
$data['date'] = get_the_date( 'c', $data['id'] );
558536
if ( rest_is_field_included( 'uid', $fields ) ) {
559-
$data['uid'] = $all_fields['feedback_id'];
537+
$data['uid'] = $response->get_feedback_id();
560538
}
561539
if ( rest_is_field_included( 'author_name', $fields ) ) {
562-
$data['author_name'] = $feedback_data['_feedback_author'];
540+
$data['author_name'] = $response->get_author();
563541
}
564542
if ( rest_is_field_included( 'author_email', $fields ) ) {
565-
$data['author_email'] = $feedback_data['_feedback_author_email'];
543+
$data['author_email'] = $response->get_author_email();
566544
}
567545
if ( rest_is_field_included( 'author_url', $fields ) ) {
568-
$data['author_url'] = $feedback_data['_feedback_author_url'];
546+
$data['author_url'] = $response->get_author_url();
569547
}
570548
if ( rest_is_field_included( 'author_avatar', $fields ) ) {
571-
$data['author_avatar'] = empty( $feedback_data['_feedback_author_email'] ) ? '' : get_avatar_url( $feedback_data['_feedback_author_email'] );
549+
$data['author_avatar'] = $response->get_author_avatar();
572550
}
573551
if ( rest_is_field_included( 'email_marketing_consent', $fields ) ) {
574-
$data['email_marketing_consent'] = $all_fields['email_marketing_consent'];
552+
$data['email_marketing_consent'] = $response->has_consent() ? __( 'Yes', 'jetpack-forms' ) : __( 'No', 'jetpack-forms' );
575553
}
576554
if ( rest_is_field_included( 'ip', $fields ) ) {
577-
$data['ip'] = $feedback_data['_feedback_ip'];
555+
$data['ip'] = $response->get_ip_address();
578556
}
579557
if ( rest_is_field_included( 'entry_title', $fields ) ) {
580-
$data['entry_title'] = $all_fields['entry_title'];
558+
$data['entry_title'] = $response->get_entry_title();
581559
}
582560
if ( rest_is_field_included( 'entry_permalink', $fields ) ) {
583-
$data['entry_permalink'] = $all_fields['entry_permalink'];
561+
$data['entry_permalink'] = $response->get_entry_permalink();
584562
}
585563
if ( rest_is_field_included( 'subject', $fields ) ) {
586-
$data['subject'] = $feedback_data['_feedback_subject'];
564+
$data['subject'] = $response->get_subject();
587565
}
588566
if ( rest_is_field_included( 'fields', $fields ) ) {
589-
$fields_data = array_diff_key( $all_fields, $base_fields );
590-
591-
foreach ( $fields_data as &$field ) {
592-
if ( Contact_Form::is_file_upload_field( $field ) ) {
593-
594-
foreach ( $field['files'] as &$file ) {
595-
if ( ! isset( $file['size'] ) || ! isset( $file['file_id'] ) ) {
596-
// this shouldn't happen, todo: log this
597-
continue;
598-
}
599-
$file_id = absint( $file['file_id'] );
600-
$file['file_id'] = $file_id;
601-
$file['size'] = size_format( $file['size'] );
602-
$file['url'] = apply_filters( 'jetpack_unauth_file_download_url', '', $file_id );
603-
$file['is_previewable'] = self::is_previewable_file( $file );
604-
$has_file = true;
605-
}
606-
}
607-
}
567+
$data['fields'] = $response->get_compiled_fields( 'api', 'key-value' );
568+
}
608569

609-
$data['fields'] = $fields_data;
610-
$data['has_file'] = $has_file;
570+
if ( rest_is_field_included( 'has_file', $fields ) ) {
571+
$data['has_file'] = $response->has_file();
611572
}
612573
return rest_ensure_response( $data );
613574
}
614-
/**
615-
* Checks if the file is previewable based on its type or extension.
616-
*
617-
* @param array $file File data.
618-
* @return bool True if the file is previewable, false otherwise.
619-
*/
620-
private static function is_previewable_file( $file ) {
621-
$file_type = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );
622-
// Check if the file is previewable based on its type or extension.
623-
// Note: This is a simplified check and does not match if the file is allowed to be uploaded by the server.
624-
$previewable_types = array( 'jpg', 'jpeg', 'png', 'gif', 'webp' );
625-
return in_array( $file_type, $previewable_types, true );
626-
}
627575

628576
/**
629577
* Retrieves the query params for the feedback collection.

0 commit comments

Comments
 (0)