| @@ -275,4 +275,59 @@ | ||
| 275 | 275 | function openstation_agent_face_cleanup( $user_id ) { |
| 276 | 276 | openstation_agent_face_delete( $user_id ); |
| 277 | 277 | } |
| 278 | 278 | add_action( 'openstation_agent_deleted', 'openstation_agent_face_cleanup', 10, 1 ); |
| 279 | + | |
| 280 | +/** | |
| 281 | + * Agents Core is deleting in this request, keyed by user id. | |
| 282 | + * | |
| 283 | + * `openstation_agent_deleted` only fires from `openstation_agent_delete()`, | |
| 284 | + * so an agent deleted from wp-admin → Users (or `wp user delete`) would | |
| 285 | + * leave its files behind. By `deleted_user` the agent marker meta is | |
| 286 | + * already gone, so the id is noted here while the marker still exists. | |
| 287 | + * | |
| 288 | + * @param int $add Agent user id to note; 0 to only read. | |
| 289 | + * @return array<int, true> | |
| 290 | + */ | |
| 291 | +function openstation_agent_face_pending_core_deletes( $add = 0 ) { | |
| 292 | + static $pending = array(); | |
| 293 | + if ( $add > 0 ) { | |
| 294 | + $pending[ (int) $add ] = true; | |
| 295 | + } | |
| 296 | + return $pending; | |
| 297 | +} | |
| 298 | + | |
| 299 | +/** | |
| 300 | + * Note an agent Core is about to delete, while it is still an agent. | |
| 301 | + * | |
| 302 | + * Hooked to `delete_user` (`wp_delete_user()`) and `wpmu_delete_user` | |
| 303 | + * (the network delete, which does not fire `delete_user`). | |
| 304 | + * | |
| 305 | + * @param int $user_id User being deleted. | |
| 306 | + * @return void | |
| 307 | + */ | |
| 308 | +function openstation_agent_face_note_core_delete( $user_id ) { | |
| 309 | + if ( openstation_agent_is_agent( (int) $user_id ) ) { | |
| 310 | + openstation_agent_face_pending_core_deletes( (int) $user_id ); | |
| 311 | + } | |
| 312 | +} | |
| 313 | +add_action( 'delete_user', 'openstation_agent_face_note_core_delete', 10, 1 ); | |
| 314 | +add_action( 'wpmu_delete_user', 'openstation_agent_face_note_core_delete', 10, 1 ); | |
| 315 | + | |
| 316 | +/** | |
| 317 | + * Remove a noted agent's files once Core has deleted the user. | |
| 318 | + * | |
| 319 | + * On multisite `wp_delete_user()` only removes the user from the current | |
| 320 | + * site and the agent lives on, so the files go only when the user row | |
| 321 | + * itself is gone. | |
| 322 | + * | |
| 323 | + * @param int $user_id Deleted user id. | |
| 324 | + * @return void | |
| 325 | + */ | |
| 326 | +function openstation_agent_face_cleanup_after_core_delete( $user_id ) { | |
| 327 | + $pending = openstation_agent_face_pending_core_deletes(); | |
| 328 | + if ( empty( $pending[ (int) $user_id ] ) || get_userdata( (int) $user_id ) ) { | |
| 329 | + return; | |
| 330 | + } | |
| 331 | + openstation_agent_face_delete( (int) $user_id ); | |
| 332 | +} | |
| 333 | +add_action( 'deleted_user', 'openstation_agent_face_cleanup_after_core_delete', 10, 1 ); | |