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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Forms: use first/last name for author.
63 changes: 57 additions & 6 deletions projects/packages/forms/src/contact-form/class-feedback-author.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,35 @@ class Feedback_Author {
*/
private $url;

/**
* The first name of the author.
*
* @var string
*/
private $first_name = '';

/**
* The last name of the author.
*
* @var string
*/
private $last_name = '';

/**
* Constructor for Feedback_Author.
*
* @param string $name The name of the author.
* @param string $email The email of the author.
* @param string $url The URL of the author.
* @param string $first_name The first name of the author.
* @param string $last_name The last name of the author.
*/
public function __construct( $name = '', $email = '', $url = '' ) {
$this->name = $name;
$this->email = $email;
$this->url = $url;
public function __construct( $name = '', $email = '', $url = '', $first_name = '', $last_name = '' ) {
$this->name = $name;
$this->email = $email;
$this->url = $url;
$this->first_name = $first_name;
$this->last_name = $last_name;
}

/**
Expand All @@ -56,10 +74,14 @@ public function __construct( $name = '', $email = '', $url = '' ) {
* @return Feedback_Author The Feedback_Author instance.
*/
public static function from_submission( $post_data, $form ) {
$first = isset( $post_data['first-name'] ) ? wp_unslash( $post_data['first-name'] ) : '';
$last = isset( $post_data['last-name'] ) ? wp_unslash( $post_data['last-name'] ) : '';
return new self(
self::get_computed_author_info( $post_data, 'name', 'pre_comment_author_name', $form ),
self::get_computed_author_info( $post_data, 'email', 'pre_comment_author_email', $form ),
self::get_computed_author_info( $post_data, 'url', 'pre_comment_author_url', $form )
self::get_computed_author_info( $post_data, 'url', 'pre_comment_author_url', $form ),
$first,
$last
);
}

Expand Down Expand Up @@ -105,7 +127,8 @@ private static function get_computed_author_info( $post_data, $type, $filter, $f
* @return string The display name of the author.
*/
public function get_display_name(): string {
return empty( $this->name ) ? $this->email : $this->name;
$name = $this->get_name();
return empty( $name ) ? $this->email : $name;
}

/**
Expand All @@ -125,6 +148,16 @@ public function get_avatar_url(): string {
* @return string The name of the author.
*/
public function get_name() {
if ( $this->first_name && $this->last_name ) {
$raw = trim( $this->first_name . ' ' . $this->last_name );
return Contact_Form_Plugin::strip_tags(
stripslashes(
/** This filter is already documented in core/wp-includes/comment-functions.php */
apply_filters( 'pre_comment_author_name', addslashes( $raw ) )
)
);
}
// This name value is filtered upstream when class is instantiated.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feedback_Author->get_name() will now return 'First Last' if available, or fallback to 'Single Name' as before.

I added the filter here to ensure equivalence. We filter the single 'name' field in both places before we instantiate the Feedback_Author class class.

return $this->name;
}

Expand All @@ -145,4 +178,22 @@ public function get_email() {
public function get_url() {
return $this->url;
}

/**
* Get the first name of the author (if provided separately).
*
* @return string
*/
public function get_first_name() {
return $this->first_name;
}

/**
* Get the last name of the author (if provided separately).
*
* @return string
*/
public function get_last_name() {
return $this->last_name;
}
}
22 changes: 21 additions & 1 deletion projects/packages/forms/src/contact-form/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ private function load_from_post( WP_Post $feedback_post ) {
$this->author_data = new Feedback_Author(
$this->get_first_field_of_type( 'name', 'pre_comment_author_name' ),
$this->get_first_field_of_type( 'email', 'pre_comment_author_email' ),
$this->get_first_field_of_type( 'url', 'pre_comment_author_url' )
$this->get_first_field_of_type( 'url', 'pre_comment_author_url' ),
$this->get_field_value_by_form_field_id( 'first-name' ),
Copy link
Member

Choose a reason for hiding this comment

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

Should we also make sure that the field type is "name" ?

$this->get_field_value_by_form_field_id( 'last-name' )
);

$this->comment_content = $this->get_first_field_of_type( 'textarea' );
Expand Down Expand Up @@ -729,6 +731,24 @@ public function get_author_name() {
return $this->author_data->get_name();
}

/**
* Get the author's first name of a feedback entry.
*
* @return string
*/
public function get_author_first_name() {
return $this->author_data->get_first_name();
}

/**
* Get the author's last name of a feedback entry.
*
* @return string
*/
public function get_author_last_name() {
return $this->author_data->get_last_name();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR does not use the two getters above, but I wanted to provide these for future handling, and we'll likely update our integration logic for MailPoet and Hostinger to leverage these methods.


/**
* Get the author email of a feedback entry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@
#[CoversClass( Feedback_Author::class )]
class Feedback_Author_Test extends BaseTestCase {

/**
* Minimal: combining first and last name yields full name in name/display.
*/
public function test_combined_first_last_in_name_and_display() {
$author = new Feedback_Author( '', '[email protected]', '', 'John', 'Doe' );

$this->assertEquals( 'John Doe', $author->get_name() );
$this->assertEquals( 'John Doe', $author->get_display_name() );
$this->assertSame( 'John', $author->get_first_name() );
$this->assertSame( 'Doe', $author->get_last_name() );
}
/**
* Minimal: when only one of first/last is present, fall back to single name.
*/
public function test_partial_first_or_last_falls_back_to_single_name() {
$author = new Feedback_Author( 'Single Name', '[email protected]', '', 'Bob', '' );
$this->assertEquals( 'Single Name', $author->get_name() );
$this->assertEquals( 'Single Name', $author->get_display_name() );
}

/**
* When both first/last are present and differ from single name, combined name takes precedence.
*/
public function test_first_last_override_single_name_when_both_present() {
$author = new Feedback_Author( 'Some Other Name', '[email protected]', '', 'Alice', 'Jones' );
$this->assertEquals( 'Alice Jones', $author->get_name() );
$this->assertEquals( 'Alice Jones', $author->get_display_name() );
}

/**
* When only last name is provided and single name exists, fall back to single name.
*/
public function test_only_lastname_with_single_name_falls_back_to_single() {
$author = new Feedback_Author( 'Single Name', '[email protected]', '', '', 'Jones' );
$this->assertEquals( 'Single Name', $author->get_name() );
$this->assertEquals( 'Single Name', $author->get_display_name() );
}

/**
* When only last name is provided and single name is missing, fall back to email in display.
*/
public function test_only_lastname_without_single_name_falls_back_to_email() {
$author = new Feedback_Author( '', '[email protected]', '', '', 'Jones' );
$this->assertSame( '', $author->get_name() );
$this->assertEquals( '[email protected]', $author->get_display_name() );
}

/**
* Test constructor with all parameters.
*/
Expand Down
29 changes: 29 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 @@ -3140,4 +3140,33 @@ public function test_get_country_flag() {

remove_filter( 'jetpack_get_country_from_ip', $filter_callback, 10 );
}

/**
* Minimal: submission with first-name/last-name sets author name and first/last getters.
*/
public function test_author_first_last_on_submission() {
$form = new Contact_Form(
array(
'title' => 'Test Form',
'description' => 'This is a test form.',
),
"
[contact-field label='First name' type='name' id='first-name'/]
[contact-field label='Last name' type='name' id='last-name'/]
[contact-field label='Email' type='email' id='email'/]
"
);

$post_data = array(
'first-name' => 'Jane',
'last-name' => 'Doe',
'email' => '[email protected]',
);

$response = Feedback::from_submission( $post_data, $form );

$this->assertEquals( 'Jane Doe', $response->get_author_name(), 'Author name should combine first and last' );
$this->assertSame( 'Jane', $response->get_author_first_name(), 'First name getter should return raw first name' );
$this->assertSame( 'Doe', $response->get_author_last_name(), 'Last name getter should return raw last name' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Forms: use first/last name for author.
Loading