From 712fa149dbb5605ad30b81ee0f2364fcbe3cac0a Mon Sep 17 00:00:00 2001 From: zinigor Date: Mon, 3 Mar 2025 14:39:06 +0000 Subject: [PATCH] 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@59645617100d83c7f8322999400d39b71f0016d1 --- CHANGELOG.md | 1 + src/contact-form/class-contact-form.php | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb8a384..a7ee0b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This is an alpha version! The changes listed here are not final. - Forms: use placeholder attribute in editor instead of value ### Fixed +- Fix warnings when post author is not available. - Forms: Ensure fields that skip rendering (like empty options fields) do not trigger validation or show value in form submission response. - Forms: Fix 404 error when a user submits an invalid form with JavaScript disabled. - Forms: fix field name set as label when trying to empty label diff --git a/src/contact-form/class-contact-form.php b/src/contact-form/class-contact-form.php index b5e0d1e..45d1f4e 100644 --- a/src/contact-form/class-contact-form.php +++ b/src/contact-form/class-contact-form.php @@ -115,7 +115,11 @@ public function __construct( $attributes, $content = null ) { } elseif ( $post ) { $attributes['id'] = $post->ID; $post_author = get_userdata( $post->post_author ); - $default_to .= $post_author->user_email; + if ( is_a( $post_author, '\WP_User' ) ) { + $default_to .= $post_author->user_email; + } else { + $default_to .= get_option( 'admin_email' ); + } } if ( ! empty( self::$forms ) ) { @@ -534,7 +538,8 @@ public static function get_compiled_form( $feedback_id, $form ) { } } else { // The feedback content is stored as the first "half" of post_content - $value = is_a( $feedback, '\WP_Post' ) ? $feedback->post_content : ''; + $value = ( is_object( $feedback ) && is_a( $feedback, '\WP_Post' ) ) ? + $feedback->post_content : ''; list( $value ) = explode( '', $value ); $value = trim( $value ); } @@ -627,7 +632,8 @@ public static function get_compiled_form_for_email( $feedback_id, $form ) { } } else { // The feedback content is stored as the first "half" of post_content - $value = is_a( $feedback, '\WP_Post' ) ? $feedback->post_content : ''; + $value = ( is_object( $feedback ) && is_a( $feedback, '\WP_Post' ) ) ? + $feedback->post_content : ''; list( $value ) = explode( '', $value ); $value = trim( $value ); }