Skip to content

Commit 9ecae09

Browse files
GaryJonesclaude
andcommitted
Fix PHP warning when deleting users without co-author terms
Fixes #1165 When deleting WordPress users, a PHP 8.4 warning was occurring: "Attempt to read property 'term_id' on bool" at line 1127 in class-coauthors-plus.php. The issue occurred because `get_author_term()` can return `false` when a user doesn't have a corresponding co-author term (e.g., if the term was manually deleted or the user was created outside the co-authors system). The code attempted to access `$term->term_id` without checking if `$term` was valid. This fix adds a null check before attempting to delete the term, preventing the warning while maintaining the same functional behavior. A new integration test reproduces the bug scenario and verifies the fix. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c245959 commit 9ecae09

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

php/class-coauthors-plus.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,9 @@ public function delete_user_action( $delete_id ): void {
11241124
if ( is_object( $delete_user ) ) {
11251125
// Delete term
11261126
$term = $this->get_author_term( $delete_user );
1127-
wp_delete_term( $term->term_id, $this->coauthor_taxonomy );
1127+
if ( $term ) {
1128+
wp_delete_term( $term->term_id, $this->coauthor_taxonomy );
1129+
}
11281130
}
11291131

11301132
if ( $this->is_guest_authors_enabled() ) {

tests/Integration/CoAuthorsPlusTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,4 +1993,46 @@ public function test_can_filter_supported_post_types(): void {
19931993
remove_filter( 'coauthors_supported_post_types', $callback );
19941994
unregister_post_type( 'foo' );
19951995
}
1996+
1997+
/**
1998+
* Tests that deleting a user without a co-author term doesn't cause PHP warnings.
1999+
*
2000+
* @covers CoAuthors_Plus::delete_user_action()
2001+
*/
2002+
public function test_delete_user_without_coauthor_term_should_not_cause_warning(): void {
2003+
global $coauthors_plus;
2004+
2005+
// Create a user
2006+
$user = $this->create_author( 'test_user_deletion' );
2007+
2008+
// Create a post for the user
2009+
$post_id = $this->factory()->post->create(
2010+
array(
2011+
'post_author' => $user->ID,
2012+
'post_status' => 'publish',
2013+
)
2014+
);
2015+
2016+
// Add the user as a co-author to ensure the term is created
2017+
$coauthors_plus->add_coauthors( $post_id, array( $user->user_nicename ), true );
2018+
2019+
// Verify the term exists
2020+
$term = $coauthors_plus->get_author_term( $user );
2021+
$this->assertNotFalse( $term, 'Co-author term should exist before deletion' );
2022+
2023+
// Manually delete the co-author term to simulate the bug scenario
2024+
// (e.g., term was manually deleted or corrupted)
2025+
wp_delete_term( $term->term_id, $coauthors_plus->coauthor_taxonomy );
2026+
2027+
// Verify the term is gone
2028+
$term_after_deletion = $coauthors_plus->get_author_term( $user );
2029+
$this->assertFalse( $term_after_deletion, 'Co-author term should not exist after manual deletion' );
2030+
2031+
// Now delete the user - this should not cause a PHP warning
2032+
// The bug is that the code tries to access $term->term_id when $term is false
2033+
wp_delete_user( $user->ID );
2034+
2035+
// If we get here without warnings, the test passes
2036+
$this->assertTrue( true, 'User deleted without PHP warnings' );
2037+
}
19962038
}

0 commit comments

Comments
 (0)