diff --git a/includes/db.php b/includes/db.php index 5bd5ace78..5a84b19ed 100644 --- a/includes/db.php +++ b/includes/db.php @@ -71,7 +71,7 @@ public function insert( $recordarr ) { return; } - $fields = array( 'object_id', 'author', 'created', 'summary', 'parent', 'visibility', 'ip' ); + $fields = array( 'object_id', 'author', 'author_role', 'created', 'summary', 'parent', 'visibility', 'ip' ); $data = array_intersect_key( $recordarr, array_flip( $fields ) ); $data = array_filter( $data ); diff --git a/includes/list-table.php b/includes/list-table.php index c644b1368..bb1a2c0d2 100644 --- a/includes/list-table.php +++ b/includes/list-table.php @@ -215,30 +215,59 @@ function column_default( $item, $column_name ) { break; case 'author' : - $user = get_user_by( 'id', $item->author ); + $user = get_user_by( 'id', $item->author ); + $author_meta = wp_stream_get_meta( $item->ID, 'author_meta', true ); + + if ( ! $user && ! is_array( $author_meta ) ) { + $out = __( 'N/A', 'stream' ); + break; + } + + global $wp_roles; + $author_ID = isset( $item->author ) ? $item->author : 0; + $user_deleted = false; + + /** + * Tries to find a label for the record's author_role. + * + * If the author_role exists, use the label associated with it + * Otherwise, if there is a user role label stored as Stream meta then use that + * Otherwise, if the user exists, use the label associated with their current role + * Otherwise, use the role slug as the label + */ + if ( ! empty( $item->author_role ) && isset( $wp_roles->role_names[ $item->author_role ] ) ) { + $author_role = $wp_roles->role_names[ $item->author_role ]; + } elseif ( ! empty( $author_meta['user_role_label'] ) ) { + $author_role = $author_meta['user_role_label']; + } elseif ( isset( $user->roles[0] ) && isset( $wp_roles->role_names[ $user->roles[0] ] ) ) { + $author_role = $wp_roles->role_names[ $user->roles[0] ]; + } else { + $author_role = $item->author_role; + } + if ( $user ) { - global $wp_roles; - - $author_ID = isset( $user->ID ) ? $user->ID : 0; - $author_name = isset( $user->display_name ) ? $user->display_name : null; - $author_role = isset( $user->roles[0] ) ? $wp_roles->role_names[ $user->roles[0] ] : null; - - $out = sprintf( - '%s %s
%s', - add_query_arg( - array( - 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG, - 'author' => absint( $author_ID ), - ), - admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) - ), - get_avatar( $author_ID, 40 ), - $author_name, - $author_role - ); + $author_name = isset( $user->display_name ) ? $user->display_name : $user->user_login; + $author_avatar = get_avatar( $author_ID, 40 ); } else { - $out = __( 'N/A', 'stream' ); + $user_deleted = true; + $author_name = ! empty( $author_meta['display_name'] ) ? $author_meta['display_name'] : $author_meta['user_login']; + $author_avatar = get_avatar( $author_meta['user_email'], 40 ); } + + $out = sprintf( + '%s %s%s%s', + add_query_arg( + array( + 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG, + 'author' => absint( $author_ID ), + ), + admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) + ), + $author_avatar, + $author_name, + $user_deleted ? sprintf( '
%s', esc_html__( 'Deleted User', 'stream' ) ) : '', + $author_role ? sprintf( '
%s', $author_role ) : '' + ); break; case 'connector': diff --git a/includes/log.php b/includes/log.php index 7e6596bd9..518cdb6b5 100644 --- a/includes/log.php +++ b/includes/log.php @@ -61,10 +61,26 @@ public static function get_instance() { * @return int */ public function log( $connector, $message, $args, $object_id, $contexts, $user_id = null ) { + global $wpdb; + if ( is_null( $user_id ) ) { $user_id = get_current_user_id(); } + $user = new WP_User( $user_id ); + $roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' ); + + if ( ! isset( $args['author_meta'] ) ) { + $args['author_meta'] = maybe_serialize( + array( + 'user_email' => $user->user_email, + 'display_name' => $user->display_name, + 'user_login' => $user->user_login, + 'user_role_label' => $roles[ $user->roles[0] ]['name'], + ) + ); + } + // Remove meta with null values from being logged $meta = array_filter( $args, @@ -74,15 +90,16 @@ function ( $var ) { ); $recordarr = array( - 'object_id' => $object_id, - 'author' => $user_id, - 'created' => current_time( 'mysql', 1 ), - 'summary' => vsprintf( $message, $args ), - 'parent' => self::$instance->prev_record, - 'connector' => $connector, - 'contexts' => $contexts, - 'meta' => $meta, - 'ip' => wp_stream_filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP ), + 'object_id' => $object_id, + 'author' => $user_id, + 'author_role' => $user->roles[0], + 'created' => current_time( 'mysql', 1 ), + 'summary' => vsprintf( $message, $args ), + 'parent' => self::$instance->prev_record, + 'connector' => $connector, + 'contexts' => $contexts, + 'meta' => $meta, + 'ip' => wp_stream_filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP ), ); $record_id = WP_Stream_DB::get_instance()->insert( $recordarr ); diff --git a/includes/query.php b/includes/query.php index 04dedf735..e46a4b103 100644 --- a/includes/query.php +++ b/includes/query.php @@ -33,6 +33,7 @@ public function query( $args ) { 'ip' => null, // Author param 'author' => null, + 'author_role' => null, // Date-based filters 'date' => null, 'date_from' => null, diff --git a/ui/admin.css b/ui/admin.css index 6eaf783e8..51c07a57e 100644 --- a/ui/admin.css +++ b/ui/admin.css @@ -173,6 +173,11 @@ margin-top: 1px; } +.toplevel_page_wp_stream .column-author .deleted { + font-style: italic; + color: #aaa; +} + .toplevel_page_wp_stream .filter-date-range { margin-top: -1px; }