@@ -50,6 +50,9 @@ class User_Command extends CommandWithDBObject {
5050 'name ' ,
5151 ];
5252
53+ /** Intermediate storage for a BC filter */
54+ private $ value_for_spam ;
55+
5356 public function __construct () {
5457 $ this ->fetcher = new UserFetcher ();
5558 $ this ->sitefetcher = new SiteFetcher ();
@@ -1220,8 +1223,8 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
12201223 }
12211224
12221225 // Make that user's blog as spam too.
1223- $ blogs = get_blogs_of_user ( $ user_id , true );
1224- foreach ( ( array ) $ blogs as $ details ) {
1226+ $ blogs = ( array ) get_blogs_of_user ( $ user_id , true );
1227+ foreach ( $ blogs as $ details ) {
12251228 $ site = $ this ->sitefetcher ->get_check ( $ details ->site_id );
12261229
12271230 // Main blog shouldn't a spam !
@@ -1230,20 +1233,59 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
12301233 }
12311234 }
12321235
1233- // Set status and show message.
1234- wp_update_user (
1235- [
1236- 'ID ' => $ user_id ,
1237- $ pref => $ value ,
1238- ]
1239- );
1236+ if ( Utils \wp_version_compare ( '5.3 ' , '< ' ) ) {
1237+ /*
1238+ * Provide fallback for WP < 5.3 so as to use the preferred
1239+ * method wp_update_user().
1240+ * See https://core.trac.wordpress.org/ticket/45747
1241+ */
1242+ $ this ->value_for_spam = $ value ;
1243+ add_filter ( 'wp_pre_insert_user_data ' , [ $ this , 'set_spam_value ' ], 10 , 3 );
1244+ wp_update_user (
1245+ [
1246+ 'ID ' => $ user_id ,
1247+ $ pref => $ value ,
1248+ ]
1249+ );
1250+ remove_filter ( 'wp_pre_insert_user_data ' , [ $ this , 'set_spam_value ' ], 10 );
1251+ if ( 1 === (int ) $ value ) {
1252+ do_action ( 'make_spam_user ' , $ user_id );
1253+ } else {
1254+ do_action ( 'make_ham_user ' , $ user_id );
1255+ }
1256+ } else {
1257+ wp_update_user (
1258+ [
1259+ 'ID ' => $ user_id ,
1260+ $ pref => $ value ,
1261+ ]
1262+ );
1263+ }
1264+
12401265 WP_CLI ::log ( "User {$ user_id } {$ action }. " );
12411266 $ successes ++;
12421267 }
12431268
12441269 Utils \report_batch_operation_results ( 'user ' , $ verb , count ( $ user_ids ), $ successes , $ errors );
12451270 }
12461271
1272+ /**
1273+ * Filter the user data to provide support spam user data value.
1274+ *
1275+ * This is only needed on WP < 5.3.
1276+ *
1277+ * See https://core.trac.wordpress.org/ticket/45747
1278+ *
1279+ * @param array $data Associative array of user data.
1280+ * @param bool $update Whether this is an update or a new user.
1281+ * @param int|null $id ID of the user in case of an update, null if not.
1282+ * @return array Modified associative array of user data.
1283+ */
1284+ public function set_spam_value ( $ data , $ update , $ id ) {
1285+ $ data ['spam ' ] = $ this ->value_for_spam ;
1286+ return $ data ;
1287+ }
1288+
12471289 /**
12481290 * Checks if a user's password is valid or not.
12491291 *
0 commit comments