Update comment names when display name is changed

Still doesn't work for emails, though.
This commit is contained in:
Tetrakern 2023-11-21 23:38:14 +01:00
parent f1ba0c4d06
commit 95c871051c
3 changed files with 47 additions and 2 deletions

View File

@ -243,6 +243,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `personal_options_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`
| `pre_get_posts` | `fictioneer_extend_search_query`, `fictioneer_read_others_files`, `fictioneer_read_others_files_list_view`, `fictioneer_filter_chapters_by_story`
| `private_to_draft` | `fictioneer_chapter_to_draft`
| `profile_update` | `fictioneer_on_profile_change`
| `publish_to_draft` | `fictioneer_chapter_to_draft`
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments`
| `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown`

View File

@ -1156,4 +1156,48 @@ function fictioneer_prevent_track_and_ping_updates( $data ) {
}
add_filter( 'wp_insert_post_data', 'fictioneer_prevent_track_and_ping_updates', 1 );
// =============================================================================
// UPDATE ASSOCIATED DATA ON USER PROFILE CHANGE
// =============================================================================
/**
* Trigger delegated functions on user profile update
*
* @since Fictioneer 5.8.0
*
* @param int $user_id The ID of the updated user.
* @param WP_User $old_user_data Object containing user's data prior to update.
*/
function fictioneer_on_profile_change( $user_id, $old_user_data ) {
// Setup
$user_info = get_userdata( $user_id );
// Display name changed?
if ( $old_user_data->display_name !== $user_info->display_name ) {
fictioneer_update_user_comments_nickname( $user_id, $user_info->display_name );
}
}
add_filter( 'profile_update', 'fictioneer_on_profile_change', 10, 2 );
/**
* Updates the display name on all comments for a given user
*
* @since Fictioneer 5.8.0
*
* @global wpdb $wpdb WordPress database object.
*
* @param int $user_id The ID of the user whose comments should be updated.
* @param string $new_nickname The new nickname to be inserted.
*/
function fictioneer_update_user_comments_nickname( $user_id, $new_nickname ) {
global $wpdb;
// Update all comments with the new nickname
$data = array( 'comment_author' => $new_nickname );
$where = array( 'user_id' => $user_id );
$wpdb->update( $wpdb->comments, $data, $where );
}
?>

View File

@ -402,10 +402,10 @@ function fictioneer_update_frontend_profile() {
}
// Nickname?
if ( ! empty( $nickname ) && ! $user->fictioneer_admin_disable_renaming ) {
if ( ! empty( $nickname ) && ! $user->fictioneer_admin_disable_renaming ) {
wp_update_user(
array(
'ID' => $user_id ,
'ID' => $user_id,
'nickname' => $nickname,
'display_name' => $nickname
)