| @@ -19,14 +19,14 @@ | ||
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Class constructor. |
| 22 | 22 | * |
| 23 | - * @param int $user_id The user ID. | |
| 24 | - * @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. | |
| 25 | 25 | */ |
| 26 | - function __construct( $user_id, $user_meta = array() ) { | |
| 26 | + public function __construct( $user_id, $user_meta = array() ) { | |
| 27 | 27 | $this->id = absint( $user_id ); |
| 28 | - $this->meta = maybe_unserialize( $user_meta ); | |
| 28 | + $this->meta = $user_meta; | |
| 29 | 29 | |
| 30 | 30 | if ( $this->id ) { |
| 31 | 31 | $this->user = new \WP_User( $this->id ); |
| 32 | 32 | } |
| @@ -40,9 +40,9 @@ | ||
| 40 | 40 | * @throws \Exception |
| 41 | 41 | * |
| 42 | 42 | * @return string |
| 43 | 43 | */ |
| 44 | - function __get( $name ) { | |
| 44 | + public function __get( $name ) { | |
| 45 | 45 | if ( 'display_name' === $name ) { |
| 46 | 46 | return $this->get_display_name(); |
| 47 | 47 | } elseif ( 'avatar_img' === $name ) { |
| 48 | 48 | return $this->get_avatar_img(); |
| @@ -61,9 +61,9 @@ | ||
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * @return string |
| 64 | 64 | */ |
| 65 | - function __toString() { | |
| 65 | + public function __toString() { | |
| 66 | 66 | return $this->get_display_name(); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
| @@ -70,9 +70,9 @@ | ||
| 70 | 70 | * Get the display name of the user |
| 71 | 71 | * |
| 72 | 72 | * @return string |
| 73 | 73 | */ |
| 74 | - function get_display_name() { | |
| 74 | + public function get_display_name() { | |
| 75 | 75 | if ( 0 === $this->id ) { |
| 76 | 76 | if ( isset( $this->meta['system_user_name'] ) ) { |
| 77 | 77 | return esc_html( $this->meta['system_user_name'] ); |
| 78 | 78 | } elseif ( 'wp_cli' === $this->get_current_agent() ) { |
| @@ -100,9 +100,9 @@ | ||
| 100 | 100 | * Get the agent of the user |
| 101 | 101 | * |
| 102 | 102 | * @return string |
| 103 | 103 | */ |
| 104 | - function get_agent() { | |
| 104 | + public function get_agent() { | |
| 105 | 105 | $agent = ''; |
| 106 | 106 | |
| 107 | 107 | if ( ! empty( $this->meta['agent'] ) ) { |
| 108 | 108 | $agent = $this->meta['agent']; |
| @@ -121,9 +121,9 @@ | ||
| 121 | 121 | * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 |
| 122 | 122 | * |
| 123 | 123 | * @return string|bool An img HTML element, or false if avatars are disabled |
| 124 | 124 | */ |
| 125 | - function get_avatar_img( $size = 80 ) { | |
| 125 | + public function get_avatar_img( $size = 80 ) { | |
| 126 | 126 | if ( ! get_option( 'show_avatars' ) ) { |
| 127 | 127 | return false; |
| 128 | 128 | } |
| 129 | 129 | |
| @@ -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,9 +176,9 @@ | ||
| 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 | 183 | $user_role = ''; |
| 184 | 184 | |
| @@ -185,10 +185,18 @@ | ||
| 185 | 185 | if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) { |
| 186 | 186 | $user_role = $wp_roles->role_names[ $this->meta['user_role'] ]; |
| 187 | 187 | } elseif ( ! empty( $this->meta['user_role_label'] ) ) { |
| 188 | 188 | $user_role = $this->meta['user_role_label']; |
| 189 | - } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) { | |
| 190 | - $user_role = $wp_roles->role_names[ $this->user->roles[0] ]; | |
| 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 ); | |
| 191 | 199 | } elseif ( is_multisite() && is_super_admin( $this->id ) ) { |
| 192 | 200 | $user_role = $wp_roles->role_names['administrator']; |
| 193 | 201 | } |
| 194 | 202 | |
| @@ -199,9 +207,9 @@ | ||
| 199 | 207 | * True if user no longer exists, otherwise false |
| 200 | 208 | * |
| 201 | 209 | * @return bool |
| 202 | 210 | */ |
| 203 | - function is_deleted() { | |
| 211 | + public function is_deleted() { | |
| 204 | 212 | return ( 0 !== $this->id && 0 === $this->user->ID ); |
| 205 | 213 | } |
| 206 | 214 | |
| 207 | 215 | /** |
| @@ -208,29 +216,25 @@ | ||
| 208 | 216 | * True if user is WP-CLI, otherwise false |
| 209 | 217 | * |
| 210 | 218 | * @return bool |
| 211 | 219 | */ |
| 212 | - function is_wp_cli() { | |
| 220 | + public function is_wp_cli() { | |
| 213 | 221 | return ( 'wp_cli' === $this->get_agent() ); |
| 214 | 222 | } |
| 215 | 223 | |
| 216 | 224 | /** |
| 217 | - * True if doing WP Cron, otherwise false | |
| 225 | + * Check if the current request is part of a WP cron task. | |
| 218 | 226 | * |
| 219 | - * Note: If native WP Cron has been disabled and you are | |
| 220 | - * hitting the cron endpoint with a system cron job, this | |
| 221 | - * 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. | |
| 222 | 229 | * |
| 230 | + * We're not using `wp_doing_cron()` since it was introduced | |
| 231 | + * only in WordPress 4.8.0. | |
| 232 | + * | |
| 223 | 233 | * @return bool |
| 224 | 234 | */ |
| 225 | - function is_doing_wp_cron() { | |
| 226 | - return ( | |
| 227 | - wp_stream_is_cron_enabled() | |
| 228 | - && | |
| 229 | - defined( 'DOING_CRON' ) | |
| 230 | - && | |
| 231 | - DOING_CRON | |
| 232 | - ); | |
| 235 | + public function is_doing_wp_cron() { | |
| 236 | + return ( defined( 'DOING_CRON' ) && DOING_CRON ); | |
| 233 | 237 | } |
| 234 | 238 | |
| 235 | 239 | /** |
| 236 | 240 | * Look at the environment to detect if an agent is being used |
| @@ -236,9 +240,9 @@ | ||
| 236 | 240 | * Look at the environment to detect if an agent is being used |
| 237 | 241 | * |
| 238 | 242 | * @return string |
| 239 | 243 | */ |
| 240 | - function get_current_agent() { | |
| 244 | + public function get_current_agent() { | |
| 241 | 245 | $agent = ''; |
| 242 | 246 | |
| 243 | 247 | if ( defined( '\WP_CLI' ) && \WP_CLI ) { |
| 244 | 248 | $agent = 'wp_cli'; |
| @@ -262,9 +266,9 @@ | ||
| 262 | 266 | * @param string $agent |
| 263 | 267 | * |
| 264 | 268 | * @return string |
| 265 | 269 | */ |
| 266 | - function get_agent_label( $agent ) { | |
| 270 | + public function get_agent_label( $agent ) { | |
| 267 | 271 | if ( 'wp_cli' === $agent ) { |
| 268 | 272 | $label = esc_html__( 'via WP-CLI', 'stream' ); |
| 269 | 273 | } elseif ( 'wp_cron' === $agent ) { |
| 270 | 274 | $label = esc_html__( 'during WP Cron', 'stream' ); |