Skip to content

Commit

Permalink
Merge pull request #448 from x-team/author-class
Browse files Browse the repository at this point in the history
Refactor author usage into WP_Stream_Author class; use System user always
  • Loading branch information
frankiejarrett committed May 6, 2014
2 parents 7270dbb + e13aae9 commit 02056e6
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 210 deletions.
44 changes: 13 additions & 31 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,9 @@ function ( $user ) {

break;
}

echo json_encode( array_values( $results ) );
if ( isset( $results ) ) {
echo json_encode( array_values( $results ) );
}
die();
}

Expand Down Expand Up @@ -887,43 +888,24 @@ public static function get_filter_value_by_id() {
}

public static function get_authors_record_meta( $authors ) {
$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;
require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php';

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

$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 ) )
);
}
foreach ( $authors as $user_id => $args ) {
$author = new WP_Stream_Author( $user_id );
$disabled = isset( $args['disabled'] ) ? $args['disabled'] : null;

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

return $authors_records;
}

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

class WP_Stream_Author {

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

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

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

/**
* @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 = 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 ( 'agent' === $name ) {
return $this->get_agent();
} elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
return $this->user->$name;
} else {
throw new Exception( "Unrecognized magic '$name'" );
}
}

/**
* @return string
*/
function get_display_name() {
if ( 0 === $this->id ) {
return esc_html__( 'N/A', 'stream' );
} 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 esc_html__( 'N/A', 'stream' );
}
} elseif ( ! empty( $this->user->display_name ) ) {
return $this->user->display_name;
} else {
return $this->user->user_login;
}
}
}

/**
* @return string|null
*/
function get_agent() {
$agent = null;

if ( ! empty( $this->meta['agent'] ) ) {
$agent = $this->meta['agent'];
} elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
$agent = 'wp_cli'; // legacy
}

return $agent;
}

/**
* @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->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) {
$author_role = $wp_roles->role_names[ $this->user->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->ID );
}

/**
* @return bool
*/
function is_wp_cli() {
return ( 'wp_cli' === $this->get_agent() );
}

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

/**
* Look at the environment to detect if an agent is being used
*
* @return null|string
*/
static function get_current_agent() {
$agent = null;

if ( defined( 'WP_CLI' ) ) {
$agent = 'wp_cli';
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
$agent = 'wp_cron';
}

$agent = apply_filters( 'wp_stream_current_agent', $agent );

return $agent;
}

/**
* @param string $agent
* @return string
*/
static function get_agent_label( $agent ) {
if ( 'wp_cli' === $agent ) {
$label = esc_html__( 'via WP-CLI', 'stream' );
} elseif ( 'wp_cron' === $agent ) {
$label = esc_html__( 'during WP Cron', 'stream' );
} else {
$label = null;
}

$label = apply_filters( 'wp_stream_agent_label', $label, $agent );

return $label;
}

}
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->get_agent() ) {
$time_author .= sprintf( ' %s', WP_Stream_Author::get_agent_label( $author->get_agent() ) );
}

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

0 comments on commit 02056e6

Please sign in to comment.