Skip to content

Commit 4950207

Browse files
committed
renaming filter and adding unit testcases
1 parent ea60bd5 commit 4950207

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/wp-admin/network/users.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,29 @@
8383
if ( is_super_admin( $user->ID ) ) {
8484
wp_die(
8585
sprintf(
86-
/* translators: %s: User login. */
86+
/* translators: %s: User login. */
8787
__( 'Warning! User cannot be modified. The user %s is a network administrator.' ),
8888
esc_html( $user->user_login )
89-
)
89+
),
90+
403
9091
);
9192
}
9293

9394
$userfunction = 'all_spam';
94-
$blogs = get_blogs_of_user( $user_id, true );
9595

96-
foreach ( (array) $blogs as $details ) {
97-
if ( ! is_main_site( $details->userblog_id ) ) { // Main site is not a spam!
98-
update_blog_status( $details->userblog_id, 'spam', '1' );
96+
/**
97+
* Filters whether to propagate the blog status when a user is marked as spam.
98+
*
99+
* @since 6.9.0
100+
*
101+
* @param bool $propagate Whether to propagate the blog status. Default false.
102+
* @param int $user_id User ID.
103+
*/
104+
if ( apply_filters( 'propagate_network_user_spam_to_blogs', false, $user_id ) ) {
105+
foreach ( get_blogs_of_user( $user_id, true ) as $details ) {
106+
if ( ! is_main_site( $details->userblog_id ) ) { // Main site is not a spam!
107+
update_blog_status( $details->userblog_id, 'spam', '1' );
108+
}
99109
}
100110
}
101111

@@ -107,12 +117,27 @@
107117

108118
case 'notspam':
109119
$user = get_userdata( $user_id );
120+
if ( is_super_admin( $user->ID ) ) {
121+
wp_die(
122+
sprintf(
123+
/* translators: %s: User login. */
124+
__( 'Warning! User cannot be modified. The user %s is a network administrator.' ),
125+
esc_html( $user->user_login )
126+
),
127+
403
128+
);
129+
}
110130

111131
$userfunction = 'all_notspam';
112132
$blogs = get_blogs_of_user( $user_id, true );
113133

114-
foreach ( (array) $blogs as $details ) {
115-
update_blog_status( $details->userblog_id, 'spam', '0' );
134+
/** This filter is documented in wp-admin/network/users.php */
135+
if ( apply_filters( 'propagate_network_user_spam_to_blogs', false, $user_id ) ) {
136+
foreach ( get_blogs_of_user( $user_id, true ) as $details ) {
137+
if ( ! is_main_site( $details->userblog_id ) && get_current_network_id() === $details->site_id ) { // Main site is never a spam and part of the current network.
138+
update_blog_status( $details->userblog_id, 'spam', '0' );
139+
}
140+
}
116141
}
117142

118143
$user_data = $user->to_array();

tests/phpunit/tests/user.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,54 @@ public function test_wp_update_user_should_not_mark_user_as_spam_on_single_site(
883883
$this->assertSame( 'no_spam', $u->get_error_code() );
884884
}
885885

886+
/**
887+
* Helper to create a user and add them to multiple blogs.
888+
*
889+
* @param int $num_blogs Number of additional blogs to create and add the user to.
890+
* @param bool $include_main_site Whether to add the user to the main site as well.
891+
* @return array Array with 'user_id' and 'blogs' (array of blog IDs).
892+
*/
893+
private function create_user_with_blogs( $num_blogs = 1, $include_main_site = false ) {
894+
$user_id = self::factory()->user->create();
895+
896+
$blogs = array();
897+
if ( $include_main_site ) {
898+
add_user_to_blog( get_main_site_id(), $user_id, 'administrator' );
899+
$blogs[] = get_main_site_id();
900+
}
901+
902+
for ( $i = 0; $i < $num_blogs; $i++ ) {
903+
$blog_id = self::factory()->blog->create( array( 'site_id' => get_current_network_id() ) );
904+
add_user_to_blog( $blog_id, $user_id, 'administrator' );
905+
$blogs[] = $blog_id;
906+
}
907+
908+
return array( 'user_id' => $user_id, 'blogs' => $blogs );
909+
}
910+
911+
/**
912+
* @ticket 61146
913+
*/
914+
public function test_default_do_not_propagate_network_user_spam_to_blogs_on_multisite() {
915+
if ( ! is_multisite() ) {
916+
$this->markTestSkipped( 'This test is for multisite only.' );
917+
}
918+
919+
$data = $this->create_user_with_blogs( 2 );
920+
$user_id = $data['user_id'];
921+
$blogs = $data['blogs'];
922+
923+
// Mark user spam in user record (this alone should not change blog spam states).
924+
$u = wp_update_user( array( 'ID' => $user_id, 'spam' => '1' ) );
925+
$this->assertNotWPError( $u );
926+
$user = get_userdata( $user_id );
927+
$this->assertSame( '1', $user->spam );
928+
929+
foreach ( $blogs as $blog_id ) {
930+
$this->assertNotEquals( '1', get_blog_status( $blog_id, 'spam' ), "Blog {$blog_id} should not be marked spam by default." );
931+
}
932+
}
933+
886934
/**
887935
* @ticket 28315
888936
*/

0 commit comments

Comments
 (0)