Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor author usage into WP_Stream_Author class; use System user always #448

Merged
merged 11 commits into from
May 6, 2014
34 changes: 7 additions & 27 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,39 +787,19 @@ public static function get_filter_value_by_id() {
}

public static function get_authors_record_meta( $authors ) {
require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php';

$authors_records = array();
foreach ( $authors as $user_id => $author ) {
$icon = '';
$title = '';
if ( 0 === $user_id ) {
$name = 'WP-CLI';
$icon = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png';
$title = 'WP-CLI Operation';
} else {
$user = is_a( $author, 'WP_User' ) ? $author : $author['label']; // @todo hacky. Stop using WP_User as label
$name = $user->display_name;

if ( preg_match( '# src=[\'" ]([^\'" ]*)#', get_avatar( $user->user_email, 32 ), $gravatar_src_match ) ) {
list( $gravatar_src, $gravatar_url ) = $gravatar_src_match;
$icon = $gravatar_url;
}

$title = sprintf(
__( "ID: %d\nUser: %s\nEmail: %s\nRole: %s", 'stream' ),
$user->ID,
$user->user_login,
$user->user_email,
implode( ', ', array_map( 'ucwords', $user->roles ) )
);
}

$author_obj = new WP_Stream_Author( $user_id );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I prefer to not include the variable type in it's name (for example, I wouldn't name a string $example_string). How would you feel about changing this to $author?

WordPress does this with things like $post or $user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised we're already using $author. Should this be changed to $user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But id is not a type. It could be anything. And I want to distinguish between the variable containing a user object vs the underlying user ID.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - I meant the variable $author_obj, where object is the type.

$authors_records[ $user_id ] = array(
'text' => $name,
'text' => $author_obj->get_display_name(),
'id' => $user_id,
'label' => $name,
'icon' => $icon,
'label' => $author_obj->get_display_name(),
'icon' => $author_obj->get_avatar_src( 32 ),
'title' => $title,
'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null,
'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null, // @todo hacky
);
}

Expand Down
165 changes: 165 additions & 0 deletions includes/class-wp-stream-author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

class WP_Stream_Author {

/**
* @var int
*/
public $id;

/**
* @var array
*/
public $meta = array();

/**
* @var WP_User
*/
protected $user_obj;

/**
* @param int $user_id
* @param array $author_meta
*/
function __construct( $user_id, $author_meta = array() ) {
$this->id = $user_id;
$this->meta = $author_meta;
if ( $this->id ) {
$this->user_obj = new WP_User( $this->id );
}
}

/**
* @param string $name
* @throws Exception
* @return string|mixed
*/
function __get( $name ) {
if ( 'display_name' === $name ) {
return $this->get_display_name();
} elseif ( 'avatar_img' === $name ) {
return $this->get_avatar_img();
} elseif ( 'avatar_src' === $name ) {
return $this->get_avatar_src();
} elseif ( 'role' === $name ) {
return $this->get_role();
} elseif ( ! empty( $this->user_obj ) && 0 !== $this->user_obj->ID ) {
return $this->user_obj->$name;
} else {
throw new Exception( "Unrecognized magic '$name'" );
}
}

/**
* @return string
*/
function get_display_name() {
if ( 0 === $this->id ) {
return __( 'Unknown', 'stream' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to use the word "System" here. An "Unknown" author sounds like an error, or shortfall in Stream.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does sound better than unknown.

} else {
if ( $this->is_deleted() ) {
if ( ! empty( $this->meta['display_name'] ) ) {
return $this->meta['display_name'];
} elseif ( ! empty( $this->meta['user_login'] ) ) {
return $this->meta['user_login'];
} else {
return __( 'N/A', 'stream' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter Should this also be "Unknown"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_NO._

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 😺

}
} elseif ( ! empty( $this->user_obj->display_name ) ) {
return $this->user_obj->display_name;
} else {
return $this->user_obj->user_login;
}
}
}

/**
* @param int $size
* @return string
*/
function get_avatar_img( $size = 80 ) {
if ( 0 === $this->id ) {
$url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png';
$avatar = sprintf( '<img alt="%1$s" src="%2$s" class="avatar avatar-%3$s photo" height="%3$s" width="%3$s">', esc_attr( $this->get_display_name() ), esc_url( $url ), esc_attr( $size ) );
} else {
if ( $this->is_deleted() ) {
$email = $this->meta['user_email'];
$avatar = get_avatar( $email, $size );
} else {
$avatar = get_avatar( $this->id, $size );
}
}
return $avatar;
}

/**
* @param int $size
* @return string
*/
function get_avatar_src( $size = 80 ) {
$img = $this->get_avatar_img( $size );
assert( preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) );
$src = html_entity_decode( $matches[2] );
return $src;
}

/**
* 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
*
* @return string|null
*/
function get_role() {
global $wp_roles;
if ( ! empty( $this->meta['author_role'] ) && isset( $wp_roles->role_names[ $this->meta['author_role'] ] ) ) {
$author_role = $wp_roles->role_names[ $this->meta['author_role'] ];
} elseif ( ! empty( $this->meta['user_role_label'] ) ) {
$author_role = $this->meta['user_role_label'];
} elseif ( isset( $this->user_obj->roles[0] ) && isset( $wp_roles->role_names[ $this->user_obj->roles[0] ] ) ) {
$author_role = $wp_roles->role_names[ $this->user_obj->roles[0] ];
} else {
$author_role = null;
}
return $author_role;
}

/**
* @return string
*/
function get_records_page_url() {
$url = add_query_arg(
array(
'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG,
'author' => absint( $this->id ),
),
self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE )
);
return $url;
}

/**
* @return bool
*/
function is_deleted() {
return ( 0 !== $this->id && 0 === $this->user_obj->ID );
}

/**
* @return bool
*/
function is_wp_cli() {
return ! empty( $this->meta['is_wp_cli'] );
}

/**
* @return string
*/
function __toString() {
return $this->get_display_name();
}

}
81 changes: 22 additions & 59 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,73 +206,36 @@ public static function stream_activity_options() {
* @return string Contents of new row
*/
public static function widget_row( $item, $i = null ) {
$records_link = add_query_arg(
array( 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG ),
admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE )
);
require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php';

$author_meta = wp_stream_get_meta( $item->ID, 'author_meta', true );
$is_wp_cli = ! empty( $author_meta['is_wp_cli'] );

if ( 0 === (int)$item->author ) {
$author = new WP_User( 0 );
$author_name = $is_wp_cli ? 'WP-CLI' : __( 'N/A', 'stream' );
} else {
$author = get_userdata( $item->author );
$author_name = $author->display_name;
}
$author_link = add_query_arg(
array( 'author' => isset( $author->ID ) ? absint( $author->ID ) : 0 ),
$records_link
$author = new WP_Stream_Author( (int) $item->author, $author_meta );

$time_author = sprintf(
_x(
'%1$s ago by <a href="%2$s">%3$s</a>',
'1: Time, 2: User profile URL, 3: User display name',
'stream'
),
human_time_diff( strtotime( $item->created ) ),
esc_url( $author->get_records_page_url() ),
esc_html( $author->get_display_name() )
);

if ( $author ) {
$time_author = sprintf(
_x(
'%1$s ago by <a href="%2$s">%3$s</a>',
'1: Time, 2: User profile URL, 3: User display name',
'stream'
),
human_time_diff( strtotime( $item->created ) ),
esc_url( $author_link ),
esc_html( $author_name )
);
} else {
$time_author = sprintf(
__( '%s ago', 'stream' ),
human_time_diff( strtotime( $item->created ) )
);
}

$class = '';

if ( isset( $i ) ) {
if ( $i % 2 ) {
$class = 'alternate';
}
if ( $author->is_wp_cli() ) {
$time_author .= __( ' (via WP-CLI)', 'stream' );
}

if ( 0 === $author->ID ) {
if ( $is_wp_cli ) {
$avatar_url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png';
$author_avatar = sprintf( '<img alt="%s" src="%s" class="avatar avatar-72 photo" height="72" width="72">', esc_attr( $author_name ), esc_url( $avatar_url ) );
} else {
$author_avatar = get_avatar( 'system@wp-stream.com', 72, get_option( 'avatar_default' ) ?: 'mystery', $author_name );
$author_avatar = preg_replace( "/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $author_avatar );
}
} else {
$author_avatar = get_avatar( $author->ID, 72 );
}
$class = ( isset( $i ) && $i % 2 ) ? 'alternate' : '';

ob_start()
?><li class="<?php echo esc_html( $class ) ?>" data-id="<?php echo esc_html( $item->ID ) ?>">
<?php if ( $author ) : ?>
<div class="record-avatar">
<a href="<?php echo esc_url( $author_link ) ?>">
<?php echo $author_avatar; // xss ok ?>
</a>
</div>
<?php endif; ?>
<span class="record-meta"><?php echo $time_author // xss ok ?></span>
<div class="record-avatar">
<a href="<?php echo esc_url( $author->get_records_page_url() ) ?>">
<?php echo $author->get_avatar_img( 72 ); // xss ok ?>
</a>
</div>
<span class="record-meta"><?php echo $time_author; // xss ok ?></span>
<br />
<?php echo esc_html( $item->summary ) ?>
</li><?php
Expand Down
Loading