-
Notifications
You must be signed in to change notification settings - Fork 116
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de6c60e
Refactor author usage into WP_Stream_Author class; use System user al…
westonruter b1ec007
Fix assignment alignment issue for PHPCS
westonruter 70920fd
Use Unknown user instead of System user
westonruter bb85d70
Fix coding style to use elseif
westonruter ad852eb
Use N/A instead of Unknown
westonruter 6799e80
Remove unused variable
westonruter df48f2f
Introduce author agent concept and logging WP Cron
westonruter d0647bc
Change some variable names
40508d8
Code formatting
frankiejarrett 1435365
Merge branch 'develop' into author-class
frankiejarrett e13aae9
Use esc_html__ in settings strings, add missing textdomain
frankiejarrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter This hook needs a docblock, as well as
wp_stream_agent_label
below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went ahead and merged anyway. Will still need to do this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in b549865