| @@ -2,14 +2,8 @@ | ||
| 2 | 2 | namespace WP_Stream; |
| 3 | 3 | |
| 4 | 4 | class Author { |
| 5 | 5 | /** |
| 6 | - * Hold Plugin class | |
| 7 | - * @var Plugin | |
| 8 | - */ | |
| 9 | - public $plugin; | |
| 10 | - | |
| 11 | - /** | |
| 12 | 6 | * @var int |
| 13 | 7 | */ |
| 14 | 8 | public $id; |
| 15 | 9 | |
| @@ -25,20 +19,18 @@ | ||
| 25 | 19 | |
| 26 | 20 | /** |
| 27 | 21 | * Class constructor. |
| 28 | 22 | * |
| 29 | - * @param int $user_id The user ID. | |
| 30 | - * @param array|string $user_meta The user meta array, or a serialized string of user meta. | |
| 23 | + * @param int $user_id The user ID. | |
| 24 | + * @param array $user_meta The user meta array. | |
| 31 | 25 | */ |
| 32 | - function __construct( $user_id, $user_meta = array() ) { | |
| 26 | + public function __construct( $user_id, $user_meta = array() ) { | |
| 33 | 27 | $this->id = absint( $user_id ); |
| 34 | - $this->meta = maybe_unserialize( $user_meta ); | |
| 28 | + $this->meta = $user_meta; | |
| 35 | 29 | |
| 36 | 30 | if ( $this->id ) { |
| 37 | 31 | $this->user = new \WP_User( $this->id ); |
| 38 | 32 | } |
| 39 | - | |
| 40 | - $this->plugin = wp_stream_get_instance(); | |
| 41 | 33 | } |
| 42 | 34 | |
| 43 | 35 | /** |
| 44 | 36 | * Get various user meta data |
| @@ -48,9 +40,9 @@ | ||
| 48 | 40 | * @throws \Exception |
| 49 | 41 | * |
| 50 | 42 | * @return string |
| 51 | 43 | */ |
| 52 | - function __get( $name ) { | |
| 44 | + public function __get( $name ) { | |
| 53 | 45 | if ( 'display_name' === $name ) { |
| 54 | 46 | return $this->get_display_name(); |
| 55 | 47 | } elseif ( 'avatar_img' === $name ) { |
| 56 | 48 | return $this->get_avatar_img(); |
| @@ -67,13 +59,20 @@ | ||
| 67 | 59 | } |
| 68 | 60 | } |
| 69 | 61 | |
| 70 | 62 | /** |
| 63 | + * @return string | |
| 64 | + */ | |
| 65 | + public function __toString() { | |
| 66 | + return $this->get_display_name(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 71 | 70 | * Get the display name of the user |
| 72 | 71 | * |
| 73 | 72 | * @return string |
| 74 | 73 | */ |
| 75 | - function get_display_name() { | |
| 74 | + public function get_display_name() { | |
| 76 | 75 | if ( 0 === $this->id ) { |
| 77 | 76 | if ( isset( $this->meta['system_user_name'] ) ) { |
| 78 | 77 | return esc_html( $this->meta['system_user_name'] ); |
| 79 | 78 | } elseif ( 'wp_cli' === $this->get_current_agent() ) { |
| @@ -101,9 +100,9 @@ | ||
| 101 | 100 | * Get the agent of the user |
| 102 | 101 | * |
| 103 | 102 | * @return string |
| 104 | 103 | */ |
| 105 | - function get_agent() { | |
| 104 | + public function get_agent() { | |
| 106 | 105 | $agent = ''; |
| 107 | 106 | |
| 108 | 107 | if ( ! empty( $this->meta['agent'] ) ) { |
| 109 | 108 | $agent = $this->meta['agent']; |
| @@ -122,15 +121,16 @@ | ||
| 122 | 121 | * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 |
| 123 | 122 | * |
| 124 | 123 | * @return string|bool An img HTML element, or false if avatars are disabled |
| 125 | 124 | */ |
| 126 | - function get_avatar_img( $size = 80 ) { | |
| 125 | + public function get_avatar_img( $size = 80 ) { | |
| 127 | 126 | if ( ! get_option( 'show_avatars' ) ) { |
| 128 | 127 | return false; |
| 129 | 128 | } |
| 130 | 129 | |
| 131 | 130 | if ( 0 === $this->id ) { |
| 132 | - $url = $this->plugin->locations['url'] . 'ui/stream-icons/wp-cli.png'; | |
| 131 | + $stream = wp_stream_get_instance(); | |
| 132 | + $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png'; | |
| 133 | 133 | $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 ) ); |
| 134 | 134 | } else { |
| 135 | 135 | if ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) { |
| 136 | 136 | $email = $this->meta['user_email']; |
| @@ -149,9 +149,9 @@ | ||
| 149 | 149 | * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 |
| 150 | 150 | * |
| 151 | 151 | * @return string|bool Gravatar image URL, or false on failure |
| 152 | 152 | */ |
| 153 | - function get_avatar_src( $size = 80 ) { | |
| 153 | + public function get_avatar_src( $size = 80 ) { | |
| 154 | 154 | $img = $this->get_avatar_img( $size ); |
| 155 | 155 | |
| 156 | 156 | if ( ! $img ) { |
| 157 | 157 | return false; |
| @@ -176,19 +176,29 @@ | ||
| 176 | 176 | * Otherwise, use the role slug as the label. |
| 177 | 177 | * |
| 178 | 178 | * @return string |
| 179 | 179 | */ |
| 180 | - function get_role() { | |
| 180 | + public function get_role() { | |
| 181 | 181 | global $wp_roles; |
| 182 | 182 | |
| 183 | + $user_role = ''; | |
| 184 | + | |
| 183 | 185 | if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) { |
| 184 | 186 | $user_role = $wp_roles->role_names[ $this->meta['user_role'] ]; |
| 185 | 187 | } elseif ( ! empty( $this->meta['user_role_label'] ) ) { |
| 186 | 188 | $user_role = $this->meta['user_role_label']; |
| 187 | - } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) { | |
| 188 | - $user_role = $wp_roles->role_names[ $this->user->roles[0] ]; | |
| 189 | - } else { | |
| 190 | - $user_role = ''; | |
| 189 | + } elseif ( ! empty( $this->user->roles ) ) { | |
| 190 | + $roles = array_map( | |
| 191 | + function( $role ) use ( $wp_roles ) { | |
| 192 | + return $wp_roles->role_names[ $role ]; | |
| 193 | + }, | |
| 194 | + $this->user->roles | |
| 195 | + ); | |
| 196 | + | |
| 197 | + $separator = apply_filters( 'wp_stream_get_role_list_separator', ' - ' ); | |
| 198 | + $user_role = implode( $separator, $roles ); | |
| 199 | + } elseif ( is_multisite() && is_super_admin( $this->id ) ) { | |
| 200 | + $user_role = $wp_roles->role_names['administrator']; | |
| 191 | 201 | } |
| 192 | 202 | |
| 193 | 203 | return $user_role; |
| 194 | 204 | } |
| @@ -193,30 +203,13 @@ | ||
| 193 | 203 | return $user_role; |
| 194 | 204 | } |
| 195 | 205 | |
| 196 | 206 | /** |
| 197 | - * Construct a URL for viewing user-specific records | |
| 198 | - * | |
| 199 | - * @return string | |
| 200 | - */ | |
| 201 | - function get_records_page_url() { | |
| 202 | - $url = add_query_arg( | |
| 203 | - array( | |
| 204 | - 'page' => $this->plugin->admin->records_page_slug, | |
| 205 | - 'user_id' => absint( $this->id ), | |
| 206 | - ), | |
| 207 | - self_admin_url( $this->plugin->admin->admin_parent_page ) | |
| 208 | - ); | |
| 209 | - | |
| 210 | - return $url; | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | 207 | * True if user no longer exists, otherwise false |
| 215 | 208 | * |
| 216 | 209 | * @return bool |
| 217 | 210 | */ |
| 218 | - function is_deleted() { | |
| 211 | + public function is_deleted() { | |
| 219 | 212 | return ( 0 !== $this->id && 0 === $this->user->ID ); |
| 220 | 213 | } |
| 221 | 214 | |
| 222 | 215 | /** |
| @@ -223,44 +216,33 @@ | ||
| 223 | 216 | * True if user is WP-CLI, otherwise false |
| 224 | 217 | * |
| 225 | 218 | * @return bool |
| 226 | 219 | */ |
| 227 | - function is_wp_cli() { | |
| 220 | + public function is_wp_cli() { | |
| 228 | 221 | return ( 'wp_cli' === $this->get_agent() ); |
| 229 | 222 | } |
| 230 | 223 | |
| 231 | 224 | /** |
| 232 | - * True if doing WP Cron, otherwise false | |
| 225 | + * Check if the current request is part of a WP cron task. | |
| 233 | 226 | * |
| 234 | - * Note: If native WP Cron has been disabled and you are | |
| 235 | - * hitting the cron endpoint with a system cron job, this | |
| 236 | - * method will always return false. | |
| 227 | + * Note: This will return true for all manual or custom | |
| 228 | + * cron runs even if the default front-end cron is disabled. | |
| 237 | 229 | * |
| 230 | + * We're not using `wp_doing_cron()` since it was introduced | |
| 231 | + * only in WordPress 4.8.0. | |
| 232 | + * | |
| 238 | 233 | * @return bool |
| 239 | 234 | */ |
| 240 | - function is_doing_wp_cron() { | |
| 241 | - return ( | |
| 242 | - wp_stream_is_cron_enabled() | |
| 243 | - && | |
| 244 | - defined( 'DOING_CRON' ) | |
| 245 | - && | |
| 246 | - DOING_CRON | |
| 247 | - ); | |
| 235 | + public function is_doing_wp_cron() { | |
| 236 | + return ( defined( 'DOING_CRON' ) && DOING_CRON ); | |
| 248 | 237 | } |
| 249 | 238 | |
| 250 | 239 | /** |
| 251 | - * @return string | |
| 252 | - */ | |
| 253 | - function __toString() { | |
| 254 | - return $this->get_display_name(); | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | 240 | * Look at the environment to detect if an agent is being used |
| 259 | 241 | * |
| 260 | 242 | * @return string |
| 261 | 243 | */ |
| 262 | - function get_current_agent() { | |
| 244 | + public function get_current_agent() { | |
| 263 | 245 | $agent = ''; |
| 264 | 246 | |
| 265 | 247 | if ( defined( '\WP_CLI' ) && \WP_CLI ) { |
| 266 | 248 | $agent = 'wp_cli'; |
| @@ -284,9 +266,9 @@ | ||
| 284 | 266 | * @param string $agent |
| 285 | 267 | * |
| 286 | 268 | * @return string |
| 287 | 269 | */ |
| 288 | - function get_agent_label( $agent ) { | |
| 270 | + public function get_agent_label( $agent ) { | |
| 289 | 271 | if ( 'wp_cli' === $agent ) { |
| 290 | 272 | $label = esc_html__( 'via WP-CLI', 'stream' ); |
| 291 | 273 | } elseif ( 'wp_cron' === $agent ) { |
| 292 | 274 | $label = esc_html__( 'during WP Cron', 'stream' ); |