Skip to content

Commit

Permalink
Merge pull request #413 from x-team/issue-389
Browse files Browse the repository at this point in the history
Issue 389: Save additional author meta as Stream meta
  • Loading branch information
frankiejarrett committed Apr 17, 2014
2 parents f4684ea + 685ed1a commit 82d5fc2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 31 deletions.
2 changes: 1 addition & 1 deletion includes/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
71 changes: 50 additions & 21 deletions includes/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<a href="%s">%s <span>%s</span></a><br /><small>%s</small>',
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(
'<a href="%s">%s <span>%s</span></a>%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( '<br /><small class="deleted">%s</small>', esc_html__( 'Deleted User', 'stream' ) ) : '',
$author_role ? sprintf( '<br /><small>%s</small>', $author_role ) : ''
);
break;

case 'connector':
Expand Down
35 changes: 26 additions & 9 deletions includes/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions ui/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 82d5fc2

Please sign in to comment.