Skip to content

Commit 399bd6d

Browse files
committed
Fix forms handling when responses aren't saved
This commit addresses issues in the contact form email functionality when the "Save responses" toggle is disabled. The changes ensure that: 1. Method signature enhancement: Added an optional $response parameter to get_compiled_form_for_email() to allow passing response data directly instead of always fetching from database 2. Conditional response fetching: Modified the method to only fetch response from database when not provided as parameter, preventing errors when no feedback ID exists 3. Defensive URL building: Added checks before building dashboard URLs and spam marking URLs to handle cases where $post_id is null (when responses aren't saved) 4. Conditional metadata storage: Added guard clause to prevent storing feedback email metadata when no post ID exists 5. Safe nonce generation: Added null check when generating nonces to avoid issues with missing post IDs 6. Action button handling: Made the "View in dashboard" action button conditional on having a valid dashboard URL These changes ensure the email notification system works properly regardless of whether form responses are being saved to the database or not.
1 parent bda458b commit 399bd6d

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

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

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,15 @@ private static function get_raw_compiled_form_data( $feedback_id, $form = null )
11171117
*
11181118
* @param int $feedback_id - the feedback ID.
11191119
* @param Contact_Form $form - the form.
1120+
* @param Feedback $response - the response.
11201121
*
11211122
* @return array $lines
11221123
*/
1123-
public static function get_compiled_form_for_email( $feedback_id, $form ) {
1124+
public static function get_compiled_form_for_email( $feedback_id, $form, $response = null ) {
11241125
$compiled_form = array();
1125-
$response = Feedback::get( $feedback_id );
1126+
if ( ! $response ) {
1127+
$response = Feedback::get( $feedback_id );
1128+
}
11261129

11271130
if ( $response instanceof Feedback ) {
11281131
// If the response is an instance of Feedback, we can use its method to get compiled fields.
@@ -1890,7 +1893,7 @@ public function process_submission() {
18901893
* @param string the title of the email
18911894
*/
18921895
$title = (string) apply_filters( 'jetpack_forms_response_email_title', '' );
1893-
$message = self::get_compiled_form_for_email( $post_id, $this );
1896+
$message = self::get_compiled_form_for_email( $post_id, $this, $response );
18941897

18951898
if ( is_user_logged_in() ) {
18961899
$sent_by_text = sprintf(
@@ -1925,20 +1928,19 @@ public function process_submission() {
19251928
// Get the status of the feedback
19261929
$status = $is_spam ? 'spam' : 'inbox';
19271930

1928-
// Build the dashboard URL with the status and the feedback's post id
1929-
$dashboard_url = ( new Dashboard_View_Switch() )->get_forms_admin_url( $status, true );
1931+
// Build the dashboard URL with the status and the feedback's post id if we have a post id
1932+
$dashboard_url = '';
1933+
$footer_mark_as_spam_url = '';
19301934
if ( $post_id ) {
1931-
$dashboard_url .= '&r=' . $post_id;
1935+
$dashboard_url = ( new Dashboard_View_Switch() )->get_forms_admin_url( $status, true ) . '&r=' . $post_id;
1936+
$mark_as_spam_url = $dashboard_url . '&mark_as_spam';
1937+
$footer_mark_as_spam_url = sprintf(
1938+
'<a href="%1$s">%2$s</a>',
1939+
esc_url( $mark_as_spam_url ),
1940+
__( 'Mark as spam', 'jetpack-forms' )
1941+
);
19321942
}
19331943

1934-
$mark_as_spam_url = $dashboard_url . '&mark_as_spam';
1935-
1936-
$footer_mark_as_spam_url = $post_id ? sprintf(
1937-
'<a href="%1$s">%2$s</a>',
1938-
esc_url( $mark_as_spam_url ),
1939-
__( 'Mark as spam', 'jetpack-forms' )
1940-
) : '';
1941-
19421944
$footer = implode(
19431945
'',
19441946
/**
@@ -1966,25 +1968,29 @@ public function process_submission() {
19661968
)
19671969
);
19681970

1969-
$actions = sprintf(
1970-
'<table class="button_block" border="0" cellpadding="0" cellspacing="0" role="presentation">
1971-
<tr>
1972-
<td class="pad" align="center">
1973-
<a rel="noopener" target="_blank" href="%1$s" data-tracks-link-desc="">
1974-
<!--[if mso]>
1975-
<i style="mso-text-raise: 30pt;">&nbsp;</i>
1976-
<![endif]-->
1977-
<span>%2$s</span>
1978-
<!--[if mso]>
1979-
<i>&nbsp;</i>
1980-
<![endif]-->
1981-
</a>
1982-
</td>
1983-
</tr>
1984-
</table>',
1985-
esc_url( $dashboard_url ),
1986-
__( 'View in dashboard', 'jetpack-forms' )
1987-
);
1971+
// Build the actions url if we have a dashboard url
1972+
$actions = '';
1973+
if ( $dashboard_url ) {
1974+
$actions = sprintf(
1975+
'<table class="button_block" border="0" cellpadding="0" cellspacing="0" role="presentation">
1976+
<tr>
1977+
<td class="pad" align="center">
1978+
<a rel="noopener" target="_blank" href="%1$s" data-tracks-link-desc="">
1979+
<!--[if mso]>
1980+
<i style="mso-text-raise: 30pt;">&nbsp;</i>
1981+
<![endif]-->
1982+
<span>%2$s</span>
1983+
<!--[if mso]>
1984+
<i>&nbsp;</i>
1985+
<![endif]-->
1986+
</a>
1987+
</td>
1988+
</tr>
1989+
</table>',
1990+
esc_url( $dashboard_url ),
1991+
__( 'View in dashboard', 'jetpack-forms' )
1992+
);
1993+
}
19881994

19891995
/**
19901996
* Filters the message sent via email after a successful form submission.
@@ -2001,7 +2007,9 @@ public function process_submission() {
20012007
// This is called after `contact_form_message`, in order to preserve back-compat
20022008
$message = self::wrap_message_in_html_tags( $title, $message, $footer, $actions );
20032009

2004-
update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( compact( 'to', 'message' ) ) );
2010+
if ( $post_id ) {
2011+
update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( compact( 'to', 'message' ) ) );
2012+
}
20052013

20062014
/**
20072015
* Fires right before the contact form message is sent via email to
@@ -2074,15 +2082,14 @@ public function process_submission() {
20742082
'contact-form-id' => $id,
20752083
'contact-form-sent' => $post_id,
20762084
'contact-form-hash' => $this->hash,
2077-
'_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :( .
2085+
'_wpnonce' => $post_id ? wp_create_nonce( "contact-form-sent-{$post_id}" ) : '', // wp_nonce_url HTMLencodes :( .
20782086
);
20792087

20802088
// If the request accepts JSON, return a JSON response instead of redirecting
20812089
$accepts_json = isset( $_SERVER['HTTP_ACCEPT'] ) && false !== strpos( strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ) ), 'application/json' );
20822090

20832091
if ( $this->is_response_without_reload_enabled && $accepts_json ) {
2084-
$data = array();
2085-
$response = Feedback::get( $post_id );
2092+
$data = array();
20862093
if ( $response instanceof Feedback ) {
20872094
$data = $response->get_compiled_fields( 'ajax', 'label|value' );
20882095
}

0 commit comments

Comments
 (0)