Skip to content

Commit 712fa14

Browse files
zinigormatticbot
authored andcommitted
Forms: Fix warning when post author data returns false. (#42115)
* Fix 'Attempt to read property on bool' warning. * Changelog. * Added a test to cover the failure. Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13633058435 Upstream-Ref: Automattic/jetpack@5964561
1 parent 94c759b commit 712fa14

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This is an alpha version! The changes listed here are not final.
1919
- Forms: use placeholder attribute in editor instead of value
2020

2121
### Fixed
22+
- Fix warnings when post author is not available.
2223
- Forms: Ensure fields that skip rendering (like empty options fields) do not trigger validation or show value in form submission response.
2324
- Forms: Fix 404 error when a user submits an invalid form with JavaScript disabled.
2425
- Forms: fix field name set as label when trying to empty label

src/contact-form/class-contact-form.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public function __construct( $attributes, $content = null ) {
115115
} elseif ( $post ) {
116116
$attributes['id'] = $post->ID;
117117
$post_author = get_userdata( $post->post_author );
118-
$default_to .= $post_author->user_email;
118+
if ( is_a( $post_author, '\WP_User' ) ) {
119+
$default_to .= $post_author->user_email;
120+
} else {
121+
$default_to .= get_option( 'admin_email' );
122+
}
119123
}
120124

121125
if ( ! empty( self::$forms ) ) {
@@ -534,7 +538,8 @@ public static function get_compiled_form( $feedback_id, $form ) {
534538
}
535539
} else {
536540
// The feedback content is stored as the first "half" of post_content
537-
$value = is_a( $feedback, '\WP_Post' ) ? $feedback->post_content : '';
541+
$value = ( is_object( $feedback ) && is_a( $feedback, '\WP_Post' ) ) ?
542+
$feedback->post_content : '';
538543
list( $value ) = explode( '<!--more-->', $value );
539544
$value = trim( $value );
540545
}
@@ -627,7 +632,8 @@ public static function get_compiled_form_for_email( $feedback_id, $form ) {
627632
}
628633
} else {
629634
// The feedback content is stored as the first "half" of post_content
630-
$value = is_a( $feedback, '\WP_Post' ) ? $feedback->post_content : '';
635+
$value = ( is_object( $feedback ) && is_a( $feedback, '\WP_Post' ) ) ?
636+
$feedback->post_content : '';
631637
list( $value ) = explode( '<!--more-->', $value );
632638
$value = trim( $value );
633639
}

0 commit comments

Comments
 (0)