| 1 |
<?php |
| 2 |
|
| 3 |
class WP_Stream_Author { |
| 4 |
|
| 5 |
/** |
| 6 |
* @var int |
| 7 |
*/ |
| 8 |
public $id; |
| 9 |
|
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
public $meta = array(); |
| 14 |
|
| 15 |
/** |
| 16 |
* @var WP_User |
| 17 |
*/ |
| 18 |
protected $user; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param int $user_id |
| 22 |
* @param array $author_meta |
| 23 |
*/ |
| 24 |
function __construct( $user_id, $author_meta = array() ) { |
| 25 |
$this->id = $user_id; |
| 26 |
$this->meta = $author_meta; |
| 27 |
|
| 28 |
if ( $this->id ) { |
| 29 |
$this->user = new WP_User( $this->id ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param string $name |
| 35 |
* @throws Exception |
| 36 |
* @return string|mixed |
| 37 |
*/ |
| 38 |
function __get( $name ) { |
| 39 |
if ( 'display_name' === $name ) { |
| 40 |
return $this->get_display_name(); |
| 41 |
} elseif ( 'avatar_img' === $name ) { |
| 42 |
return $this->get_avatar_img(); |
| 43 |
} elseif ( 'avatar_src' === $name ) { |
| 44 |
return $this->get_avatar_src(); |
| 45 |
} elseif ( 'role' === $name ) { |
| 46 |
return $this->get_role(); |
| 47 |
} elseif ( 'agent' === $name ) { |
| 48 |
return $this->get_agent(); |
| 49 |
} elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) { |
| 50 |
return $this->user->$name; |
| 51 |
} else { |
| 52 |
throw new Exception( "Unrecognized magic '$name'" ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
function get_display_name() { |
| 60 |
if ( 0 === $this->id ) { |
| 61 |
if ( isset( $this->meta['system_user_name'] ) ) { |
| 62 |
return esc_html( $this->meta['system_user_name'] ); |
| 63 |
} |
| 64 |
return esc_html__( 'N/A', 'stream' ); |
| 65 |
} else { |
| 66 |
if ( $this->is_deleted() ) { |
| 67 |
if ( ! empty( $this->meta['display_name'] ) ) { |
| 68 |
return $this->meta['display_name']; |
| 69 |
} elseif ( ! empty( $this->meta['user_login'] ) ) { |
| 70 |
return $this->meta['user_login']; |
| 71 |
} else { |
| 72 |
return esc_html__( 'N/A', 'stream' ); |
| 73 |
} |
| 74 |
} elseif ( ! empty( $this->user->display_name ) ) { |
| 75 |
return $this->user->display_name; |
| 76 |
} else { |
| 77 |
return $this->user->user_login; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
function get_agent() { |
| 86 |
$agent = ''; |
| 87 |
|
| 88 |
if ( ! empty( $this->meta['agent'] ) ) { |
| 89 |
$agent = $this->meta['agent']; |
| 90 |
} elseif ( ! empty( $this->meta['is_wp_cli'] ) ) { |
| 91 |
$agent = 'wp_cli'; // legacy |
| 92 |
} |
| 93 |
|
| 94 |
return $agent; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return a Gravatar image as an HTML element. |
| 99 |
* |
| 100 |
* This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion. |
| 101 |
* |
| 102 |
* @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 |
| 103 |
* @return string An img HTML element |
| 104 |
*/ |
| 105 |
function get_avatar_img( $size = 80 ) { |
| 106 |
if ( ! get_option( 'show_avatars' ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
if ( 0 === $this->id ) { |
| 111 |
$url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png'; |
| 112 |
$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 ) ); |
| 113 |
} else { |
| 114 |
if ( $this->is_deleted() ) { |
| 115 |
$email = $this->meta['user_email']; |
| 116 |
$avatar = get_avatar( $email, $size ); |
| 117 |
} else { |
| 118 |
$avatar = get_avatar( $this->id, $size ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $avatar; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Return the URL of a Gravatar image. |
| 127 |
* |
| 128 |
* @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 |
| 129 |
* @return string Gravatar image URL |
| 130 |
*/ |
| 131 |
function get_avatar_src( $size = 80 ) { |
| 132 |
$img = $this->get_avatar_img( $size ); |
| 133 |
|
| 134 |
if ( ! $img ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) { |
| 139 |
$src = html_entity_decode( $matches[2] ); |
| 140 |
} else { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
return $src; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Tries to find a label for the record's author_role. |
| 149 |
* |
| 150 |
* If the author_role exists, use the label associated with it. |
| 151 |
* |
| 152 |
* Otherwise, if there is a user role label stored as Stream meta then use that. |
| 153 |
* |
| 154 |
* Otherwise, if the user exists, use the label associated with their current role. |
| 155 |
* |
| 156 |
* Otherwise, use the role slug as the label. |
| 157 |
* |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
function get_role() { |
| 161 |
global $wp_roles; |
| 162 |
|
| 163 |
if ( ! empty( $this->meta['author_role'] ) && isset( $wp_roles->role_names[ $this->meta['author_role'] ] ) ) { |
| 164 |
$author_role = $wp_roles->role_names[ $this->meta['author_role'] ]; |
| 165 |
} elseif ( ! empty( $this->meta['user_role_label'] ) ) { |
| 166 |
$author_role = $this->meta['user_role_label']; |
| 167 |
} elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) { |
| 168 |
$author_role = $wp_roles->role_names[ $this->user->roles[0] ]; |
| 169 |
} else { |
| 170 |
$author_role = ''; |
| 171 |
} |
| 172 |
|
| 173 |
return $author_role; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
function get_records_page_url() { |
| 180 |
$url = add_query_arg( |
| 181 |
array( |
| 182 |
'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG, |
| 183 |
'author' => absint( $this->id ), |
| 184 |
), |
| 185 |
self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) |
| 186 |
); |
| 187 |
|
| 188 |
return $url; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @return bool |
| 193 |
*/ |
| 194 |
function is_deleted() { |
| 195 |
return ( 0 !== $this->id && 0 === $this->user->ID ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
function is_wp_cli() { |
| 202 |
return ( 'wp_cli' === $this->get_agent() ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
function __toString() { |
| 209 |
return $this->get_display_name(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Look at the environment to detect if an agent is being used |
| 214 |
* |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
static function get_current_agent() { |
| 218 |
$agent = ''; |
| 219 |
|
| 220 |
if ( defined( 'WP_CLI' ) ) { |
| 221 |
$agent = 'wp_cli'; |
| 222 |
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 223 |
$agent = 'wp_cron'; |
| 224 |
} |
| 225 |
|
| 226 |
$agent = apply_filters( 'wp_stream_current_agent', $agent ); |
| 227 |
|
| 228 |
return $agent; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @param string $agent |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
static function get_agent_label( $agent ) { |
| 236 |
if ( 'wp_cli' === $agent ) { |
| 237 |
$label = esc_html__( 'via WP-CLI', 'stream' ); |
| 238 |
} elseif ( 'wp_cron' === $agent ) { |
| 239 |
$label = esc_html__( 'during WP Cron', 'stream' ); |
| 240 |
} else { |
| 241 |
$label = ''; |
| 242 |
} |
| 243 |
|
| 244 |
$label = apply_filters( 'wp_stream_agent_label', $label, $agent ); |
| 245 |
|
| 246 |
return $label; |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|