-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #448 from x-team/author-class
Refactor author usage into WP_Stream_Author class; use System user always
- Loading branch information
Showing
6 changed files
with
331 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.