Skip to content

Fix PHP 8 fatal in avatar cache key when $id_or_email is an object#447

Open
polevaultweb wants to merge 1 commit into
developfrom
fix/avatar-cache-key-php8
Open

Fix PHP 8 fatal in avatar cache key when $id_or_email is an object#447
polevaultweb wants to merge 1 commit into
developfrom
fix/avatar-cache-key-php8

Conversation

@polevaultweb

Copy link
Copy Markdown
Contributor

Summary

  • Fixes PHP 8 fatal error (Object could not be converted to string) in WPUM_Avatars::set_default_avatar() when $id_or_email is a WP_Comment or WP_User object
  • Derives a safe scalar cache key from any input type: comment ID, user ID, email hash, or JSON hash fallback
  • Affects any page with comments or author avatars rendered via get_avatar()

Fixes #443

Test plan

  • Visit a post with comments on PHP 8.0+ — should render without fatal
  • Verify avatar caching still works (transient set/get with the new key format)
  • Check Discussion settings page still works (admin early-return path unchanged)

🤖 Generated with Claude Code

…object

Derives a safe scalar cache key from any input type instead of
concatenating $id_or_email directly, which throws on PHP 8 when
the value is an object without __toString().

Fixes #443

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a PHP 8+ fatal in WPUM_Avatars::set_default_avatar() by ensuring the transient cache key is derived safely when $id_or_email is passed as an object (e.g., WP_Comment / WP_User) via WordPress’ get_avatar_url filter.

Changes:

  • Replaces direct string concatenation of $id_or_email into the transient key with object-aware key derivation.
  • Adds support for deriving the key from comment IDs, user IDs, or email-derived hashes, with a JSON-hash fallback.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +207 to +219
if ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->comment_ID ) ) {
$key_part = 'c' . $id_or_email->comment_ID;
} elseif ( ! empty( $id_or_email->ID ) ) {
$key_part = 'u' . $id_or_email->ID;
} elseif ( ! empty( $id_or_email->user_email ) ) {
$key_part = md5( $id_or_email->user_email );
} else {
$key_part = md5( wp_json_encode( $id_or_email ) );
}
} else {
$key_part = (string) $id_or_email;
}
Comment on lines +207 to +209
if ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->comment_ID ) ) {
$key_part = 'c' . $id_or_email->comment_ID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP 8 fatal in WPUM_Avatars::set_default_avatar() when $id_or_email is WP_Comment or WP_User

2 participants