Skip to content

Commit 656519b

Browse files
authored
Forms: fix create post meta on every admin load (#44332)
* add/extract static methods to get default 'to' and 'subject' * use new methods to simplify defaults on class constructor * create defaults on editor script load without instantiating a Contact_Form * changelogs
1 parent 7efc442 commit 656519b

File tree

5 files changed

+195
-26
lines changed

5 files changed

+195
-26
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: fixed
3+
4+
Forms: prevent post_meta from being created

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ public static function load_editor_styles() {
494494
* Loads scripts
495495
*/
496496
public static function load_editor_scripts() {
497+
global $post;
497498
// Bail early if the user cannot manage the block.
498499
if ( ! self::can_manage_block() ) {
499500
return;
@@ -516,17 +517,15 @@ public static function load_editor_scripts() {
516517

517518
// Create a Contact_Form instance to get the default values
518519
$dashboard_view_switch = new Dashboard_View_Switch();
519-
$contact_form = new Contact_Form( array() );
520-
$defaults = $contact_form->defaults;
521520
$form_responses_url = $dashboard_view_switch->get_forms_admin_url();
522521
$akismet_active_with_key = Jetpack::is_akismet_active();
523522
$akismet_key_url = admin_url( 'admin.php?page=akismet-key-config' );
524523
$preferred_view = $dashboard_view_switch->get_preferred_view();
525524

526525
$data = array(
527526
'defaults' => array(
528-
'to' => $defaults['to'],
529-
'subject' => $defaults['subject'],
527+
'to' => Contact_Form::get_default_to( $post ? $post->post_author : null ),
528+
'subject' => Contact_Form::get_default_subject( array() ),
530529
'formsResponsesUrl' => $form_responses_url,
531530
'akismetActiveWithKey' => $akismet_active_with_key,
532531
'akismetUrl' => $akismet_key_url,

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

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,21 @@ public function __construct( $attributes, $content = null ) {
115115
$this->is_response_without_reload_enabled = apply_filters( 'jetpack_forms_enable_ajax_submission', false );
116116

117117
// Set up the default subject and recipient for this form.
118-
$default_to = '';
119-
$default_subject = '[' . get_option( 'blogname' ) . ']';
118+
$default_to = self::get_default_to( $this->current_post ? $this->current_post->post_author : null );
119+
$default_subject = self::get_default_subject( $attributes );
120120

121121
if ( ! isset( $attributes ) || ! is_array( $attributes ) ) {
122122
$attributes = array();
123123
}
124124

125-
if ( $this->current_post ) {
126-
$default_subject = sprintf(
127-
// translators: the blog name and post title.
128-
_x( '%1$s %2$s', '%1$s = blog name, %2$s = post title', 'jetpack-forms' ),
129-
$default_subject,
130-
Contact_Form_Plugin::strip_tags( $this->current_post->post_title )
131-
);
132-
}
133-
134125
if ( ! empty( $attributes['widget'] ) && $attributes['widget'] ) {
135-
$default_to .= get_option( 'admin_email' );
136126
$attributes['id'] = 'widget-' . $attributes['widget'];
137-
// translators: the blog name (and post name, if applicable).
138-
$default_subject = sprintf( _x( '%1$s Sidebar', '%1$s = blog name', 'jetpack-forms' ), $default_subject );
139127
} elseif ( ! empty( $attributes['block_template'] ) && $attributes['block_template'] ) {
140-
$default_to .= get_option( 'admin_email' );
141128
$attributes['id'] = 'block-template-' . $attributes['block_template'];
142129
} elseif ( ! empty( $attributes['block_template_part'] ) && $attributes['block_template_part'] ) {
143-
$default_to .= get_option( 'admin_email' );
144130
$attributes['id'] = 'block-template-part-' . $attributes['block_template_part'];
145131
} elseif ( $this->current_post ) {
146132
$attributes['id'] = $this->current_post->ID;
147-
$post_author = get_userdata( $this->current_post->post_author );
148-
if ( is_a( $post_author, '\WP_User' ) ) {
149-
$default_to .= $post_author->user_email;
150-
} else {
151-
$default_to .= get_option( 'admin_email' );
152-
}
153133
}
154134

155135
// When using admin-ajax.php, we don't need to add a page number to the id
@@ -229,6 +209,56 @@ public function __construct( $attributes, $content = null ) {
229209
Contact_Form_Plugin::$using_contact_form_field = false;
230210
}
231211

212+
/**
213+
* Get the default recipient email address for the contact form.
214+
*
215+
* @param int|null $post_author_id The ID of the post author. If provided, will return the author's email.
216+
*
217+
* @return string The default recipient email address.
218+
*/
219+
public static function get_default_to( $post_author_id = null ) {
220+
221+
if ( $post_author_id ) {
222+
$post_author = get_userdata( $post_author_id );
223+
if ( ! empty( $post_author->user_email ) ) {
224+
return $post_author->user_email;
225+
}
226+
}
227+
// Get the default recipient email address.
228+
$default_to = get_option( 'admin_email' );
229+
230+
return $default_to;
231+
}
232+
233+
/**
234+
* Get the default subject for the contact form.
235+
*
236+
* @param array $attributes The attributes of the contact form.
237+
*
238+
* @return string The default subject for the contact form.
239+
*/
240+
public static function get_default_subject( $attributes ) {
241+
global $post;
242+
// Get the default subject for the contact form.
243+
$default_subject = '[' . get_option( 'blogname' ) . ']';
244+
245+
if ( $post ) {
246+
$default_subject = sprintf(
247+
// translators: the blog name and post title.
248+
_x( '%1$s %2$s', '%1$s = blog name, %2$s = post title', 'jetpack-forms' ),
249+
$default_subject,
250+
Contact_Form_Plugin::strip_tags( $post->post_title )
251+
);
252+
}
253+
254+
if ( ! empty( $attributes['widget'] ) && $attributes['widget'] ) {
255+
// translators: '%1$s the blog name
256+
$default_subject = sprintf( _x( '%1$s Sidebar', '%1$s = blog name', 'jetpack-forms' ), $default_subject );
257+
}
258+
259+
return $default_subject;
260+
}
261+
232262
/**
233263
* Store shortcode content for recall later
234264
* - used to receate shortcode when user uses do_shortcode

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,4 +2741,136 @@ public function test_form_defaults_to_admin_email_on_no_user_data() {
27412741

27422742
$this->assertEquals( $form1->defaults['to'], get_option( 'admin_email' ), 'The default to address should equal the admin email.' );
27432743
}
2744+
2745+
/**
2746+
* Tests get_default_to method with valid post author.
2747+
*/
2748+
public function test_get_default_to_with_valid_post_author() {
2749+
$author_id = wp_insert_user(
2750+
array(
2751+
'user_email' => '[email protected]',
2752+
'user_login' => 'test_author',
2753+
'user_pass' => 'password123',
2754+
)
2755+
);
2756+
2757+
$result = Contact_Form::get_default_to( $author_id );
2758+
2759+
$this->assertEquals( '[email protected]', $result );
2760+
2761+
wp_delete_user( $author_id );
2762+
}
2763+
2764+
/**
2765+
* Tests get_default_to method with invalid post author ID.
2766+
*/
2767+
public function test_get_default_to_with_invalid_post_author() {
2768+
$result = Contact_Form::get_default_to( 99999 ); // Non-existent user ID
2769+
2770+
$this->assertEquals( get_option( 'admin_email' ), $result );
2771+
}
2772+
2773+
/**
2774+
* Tests get_default_to method with null post author ID.
2775+
*/
2776+
public function test_get_default_to_with_null_post_author() {
2777+
$result = Contact_Form::get_default_to( null );
2778+
2779+
$this->assertEquals( get_option( 'admin_email' ), $result );
2780+
}
2781+
2782+
/**
2783+
* Tests get_default_to method with post author that has empty email.
2784+
*/
2785+
public function test_get_default_to_with_empty_author_email() {
2786+
$author_id = wp_insert_user(
2787+
array(
2788+
'user_email' => '',
2789+
'user_login' => 'test_author_no_email',
2790+
'user_pass' => 'password123',
2791+
)
2792+
);
2793+
2794+
$result = Contact_Form::get_default_to( $author_id );
2795+
2796+
$this->assertEquals( get_option( 'admin_email' ), $result );
2797+
2798+
wp_delete_user( $author_id );
2799+
}
2800+
2801+
/**
2802+
* Tests get_default_subject method with post.
2803+
*/
2804+
public function test_get_default_subject_with_post() {
2805+
global $post;
2806+
2807+
$attributes = array();
2808+
$result = Contact_Form::get_default_subject( $attributes );
2809+
2810+
$expected = '[' . get_option( 'blogname' ) . '] ' . Contact_Form_Plugin::strip_tags( $post->post_title );
2811+
$this->assertEquals( $expected, $result );
2812+
}
2813+
2814+
/**
2815+
* Tests get_default_subject method with widget attribute.
2816+
*/
2817+
public function test_get_default_subject_with_widget() {
2818+
global $post;
2819+
2820+
$attributes = array( 'widget' => true );
2821+
$result = Contact_Form::get_default_subject( $attributes );
2822+
2823+
$blog_name = get_option( 'blogname' );
2824+
$expected = '[' . $blog_name . '] ' . Contact_Form_Plugin::strip_tags( $post->post_title ) . ' Sidebar';
2825+
$this->assertEquals( $expected, $result );
2826+
}
2827+
2828+
/**
2829+
* Tests get_default_subject method with widget attribute set to false.
2830+
*/
2831+
public function test_get_default_subject_with_widget_false() {
2832+
global $post;
2833+
2834+
$attributes = array( 'widget' => false );
2835+
$result = Contact_Form::get_default_subject( $attributes );
2836+
2837+
$expected = '[' . get_option( 'blogname' ) . '] ' . Contact_Form_Plugin::strip_tags( $post->post_title );
2838+
$this->assertEquals( $expected, $result );
2839+
}
2840+
2841+
/**
2842+
* Tests get_default_subject method without post.
2843+
*/
2844+
public function test_get_default_subject_without_post() {
2845+
global $post;
2846+
$original_post = $post;
2847+
$post = null;
2848+
2849+
$attributes = array();
2850+
$result = Contact_Form::get_default_subject( $attributes );
2851+
2852+
$expected = '[' . get_option( 'blogname' ) . ']';
2853+
$this->assertEquals( $expected, $result );
2854+
2855+
// Restore original post
2856+
$post = $original_post;
2857+
}
2858+
2859+
/**
2860+
* Tests get_default_subject method without post but with widget.
2861+
*/
2862+
public function test_get_default_subject_without_post_with_widget() {
2863+
global $post;
2864+
$original_post = $post;
2865+
$post = null;
2866+
2867+
$attributes = array( 'widget' => true );
2868+
$result = Contact_Form::get_default_subject( $attributes );
2869+
2870+
$expected = '[' . get_option( 'blogname' ) . '] Sidebar';
2871+
$this->assertEquals( $expected, $result );
2872+
2873+
// Restore original post
2874+
$post = $original_post;
2875+
}
27442876
} // end class
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: other
3+
4+

0 commit comments

Comments
 (0)