Skip to content

Commit d49e908

Browse files
enejbCopilot
andauthored
Forms: Fix default author non existing still sends emails to form (page/post author) (#45515)
* Forms: Check that the user is memeber of blog before adding setting them to be the default form author * Forms: Improve default recipient logic in contact form Refactored get_default_to to consider Feedback_Source and post author permissions when determining the default recipient email. Updated related tests to cover new logic and ensure correct behavior for valid and invalid authors. * changelog * minor fix Co-authored-by: Copilot <[email protected]> * Add more tests and improve the order of things for better performance. --------- Co-authored-by: Copilot <[email protected]>
1 parent 1758545 commit d49e908

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Forms: do not send emails to the author of the form if they are no longer able to edit it

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ public function __construct( $attributes, $content = null, $set_id = true ) {
150150

151151
$this->is_response_without_reload_enabled = apply_filters( 'jetpack_forms_enable_ajax_submission', true );
152152

153+
// Initialize the source before setting defaults
154+
if ( ! $this->source ) {
155+
$attributes = is_array( $attributes ) ? $attributes : array();
156+
$this->source = Feedback_Source::get_current( $attributes );
157+
}
158+
153159
// Set up the default subject and recipient for this form.
154160
$post_author_id = self::get_post_property( $this->current_post, 'post_author' );
155-
$default_to = self::get_default_to( $post_author_id );
161+
$default_to = self::get_default_to( $post_author_id, $this->source );
156162
$default_subject = self::get_default_subject( $attributes, $this->current_post );
157163

158164
if ( ! isset( $attributes ) || ! is_array( $attributes ) ) {
@@ -434,19 +440,42 @@ public static function get_forms_context_count( $context ) {
434440
/**
435441
* Get the default recipient email address for the contact form.
436442
*
437-
* @param int|null $post_author_id The ID of the post author. If provided, will return the author's email.
443+
* @param int|null $post_author_id The ID of the post author. If provided, will return the author's email.
444+
* @param Feedback_Source|null $source The source of the feedback entry. Optional, not used currently.
438445
*
439446
* @return string The default recipient email address.
440447
*/
441-
public static function get_default_to( $post_author_id = null ) {
442-
if ( $post_author_id ) {
443-
$post_author = get_userdata( $post_author_id );
444-
if ( ! empty( $post_author->user_email ) ) {
445-
return $post_author->user_email;
446-
}
447-
}
448+
public static function get_default_to( $post_author_id = null, $source = null ) {
448449
// Get the default recipient email address.
449450
$default_to = get_option( 'admin_email' );
451+
// Check that the user has edit permissions for this blog and has an email address
452+
if ( ! $post_author_id ) {
453+
return $default_to;
454+
}
455+
456+
// Check that source is of type Feedback_Source
457+
if ( ! $source instanceof Feedback_Source ) {
458+
return $default_to;
459+
}
460+
461+
if ( absint( $source->get_id() ) === 0 ) {
462+
return $default_to;
463+
}
464+
465+
$post_author = get_userdata( $post_author_id );
466+
if ( empty( $post_author ) || empty( $post_author->user_email ) ) {
467+
return $default_to;
468+
}
469+
470+
// Check that the user is still a member of the blog.
471+
if ( ! is_user_member_of_blog( $post_author_id ) ) {
472+
return $default_to;
473+
}
474+
475+
// Check that the author can still edit the post or page.
476+
if ( user_can( $post_author_id, 'edit_post', $source->get_id() ) ) {
477+
return $post_author->user_email;
478+
}
450479

451480
return $default_to;
452481
}

projects/packages/forms/tests/php/contact-form/Contact_Form_Test.php

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public function set_up_test_case() {
256256
'user_email' => '[email protected]',
257257
'user_login' => 'test_user',
258258
'user_pass' => 'abc123',
259+
'role' => 'author',
259260
)
260261
);
261262

@@ -2507,27 +2508,73 @@ public function test_form_defaults_to_admin_email_on_no_user_data() {
25072508
* Tests get_default_to method with valid post author.
25082509
*/
25092510
public function test_get_default_to_with_valid_post_author() {
2511+
$email = '[email protected]';
25102512
$author_id = wp_insert_user(
25112513
array(
2512-
'user_email' => '[email protected]',
2514+
'user_email' => $email,
25132515
'user_login' => 'test_author',
25142516
'user_pass' => 'password123',
2517+
'role' => 'editor',
25152518
)
25162519
);
2520+
$source = $this->get_source( $author_id );
2521+
$result = Contact_Form::get_default_to( $author_id, $source );
25172522

2518-
$result = Contact_Form::get_default_to( $author_id );
2523+
$this->assertEquals( $email, $result );
25192524

2520-
$this->assertEquals( '[email protected]', $result );
2525+
wp_delete_user( $author_id );
2526+
wp_delete_post( $source->get_id(), true );
2527+
}
2528+
2529+
/**
2530+
* Tests get_default_to method with valid post author.
2531+
*/
2532+
public function test_get_default_to_with_valid_post_author_subscriber() {
2533+
$author_id = wp_insert_user(
2534+
array(
2535+
'user_email' => '[email protected]',
2536+
'user_login' => 'test_author',
2537+
'user_pass' => 'password123',
2538+
'role' => 'subscriber',
2539+
)
2540+
);
2541+
$source = $this->get_source( $author_id );
2542+
$result = Contact_Form::get_default_to( $author_id, $source );
2543+
2544+
$this->assertEquals( get_option( 'admin_email' ), $result );
25212545

25222546
wp_delete_user( $author_id );
2547+
wp_delete_post( $source->get_id(), true );
2548+
}
2549+
/**
2550+
* Helper function to create a Feedback_Source object from a post.
2551+
*/
2552+
public function get_source( $author_id ) {
2553+
$post_id = wp_insert_post(
2554+
array(
2555+
'post_title' => 'Test Post',
2556+
'post_content' => 'This is a test post.',
2557+
'post_status' => 'publish',
2558+
'post_author' => $author_id,
2559+
)
2560+
);
2561+
2562+
return Feedback_Source::from_serialized(
2563+
array(
2564+
'source_id' => $post_id,
2565+
'title' => 'Test Post',
2566+
)
2567+
);
25232568
}
25242569

25252570
/**
25262571
* Tests get_default_to method with invalid post author ID.
25272572
*/
25282573
public function test_get_default_to_with_invalid_post_author() {
2529-
$result = Contact_Form::get_default_to( 99999 ); // Non-existent user ID
2574+
$source = $this->get_source( 99999 );
2575+
$result = Contact_Form::get_default_to( 99999, $source ); // Non-existent user ID
25302576

2577+
wp_delete_post( $source->get_id(), true );
25312578
$this->assertEquals( get_option( 'admin_email' ), $result );
25322579
}
25332580

@@ -2549,14 +2596,18 @@ public function test_get_default_to_with_empty_author_email() {
25492596
'user_email' => '',
25502597
'user_login' => 'test_author_no_email',
25512598
'user_pass' => 'password123',
2599+
'role' => 'editor',
25522600
)
25532601
);
25542602

2555-
$result = Contact_Form::get_default_to( $author_id );
2603+
$source = $this->get_source( $author_id );
2604+
2605+
$result = Contact_Form::get_default_to( $author_id, $source );
25562606

25572607
$this->assertEquals( get_option( 'admin_email' ), $result );
25582608

25592609
wp_delete_user( $author_id );
2610+
wp_delete_post( $source->get_id(), true );
25602611
}
25612612

25622613
/**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: bugfix
3+
4+
Forms: do not send emails to the author of the form if they are no longer able to edit it

0 commit comments

Comments
 (0)