| @@ -1,19 +1,34 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Manages a single user, acting as a model. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace WP_Stream; |
| 3 | 9 | |
| 10 | +/** | |
| 11 | + * Class - Author | |
| 12 | + */ | |
| 4 | 13 | class Author { |
| 5 | 14 | /** |
| 15 | + * Holds User ID. | |
| 16 | + * | |
| 6 | 17 | * @var int |
| 7 | 18 | */ |
| 8 | 19 | public $id; |
| 9 | 20 | |
| 10 | 21 | /** |
| 22 | + * Holds User meta data. | |
| 23 | + * | |
| 11 | 24 | * @var array |
| 12 | 25 | */ |
| 13 | 26 | public $meta = array(); |
| 14 | 27 | |
| 15 | 28 | /** |
| 29 | + * Holds WP user object connected to "$this" instance. | |
| 30 | + * | |
| 16 | 31 | * @var \WP_User |
| 17 | 32 | */ |
| 18 | 33 | protected $user; |
| 19 | 34 | |
| @@ -34,33 +49,40 @@ | ||
| 34 | 49 | |
| 35 | 50 | /** |
| 36 | 51 | * Get various user meta data |
| 37 | 52 | * |
| 38 | - * @param string $name | |
| 53 | + * @todo Make sure this is being covered in the unit tests. | |
| 39 | 54 | * |
| 40 | - * @throws \Exception | |
| 55 | + * @param string $name User meta key. | |
| 41 | 56 | * |
| 57 | + * @throws \Exception Meta not found | User not found. | |
| 58 | + * | |
| 42 | 59 | * @return string |
| 43 | 60 | */ |
| 44 | 61 | public function __get( $name ) { |
| 45 | - if ( 'display_name' === $name ) { | |
| 46 | - return $this->get_display_name(); | |
| 47 | - } elseif ( 'avatar_img' === $name ) { | |
| 48 | - return $this->get_avatar_img(); | |
| 49 | - } elseif ( 'avatar_src' === $name ) { | |
| 50 | - return $this->get_avatar_src(); | |
| 51 | - } elseif ( 'role' === $name ) { | |
| 52 | - return $this->get_role(); | |
| 53 | - } elseif ( 'agent' === $name ) { | |
| 54 | - return $this->get_agent(); | |
| 55 | - } elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) { | |
| 56 | - return $this->user->$name; | |
| 57 | - } else { | |
| 58 | - throw new \Exception( "Unrecognized magic '$name'" ); | |
| 62 | + switch ( $name ) { | |
| 63 | + case 'display_name': | |
| 64 | + case 'avatar_img': | |
| 65 | + case 'avatar_src': | |
| 66 | + case 'role': | |
| 67 | + case 'agent': | |
| 68 | + $getter = "get_{$name}"; | |
| 69 | + return $this->$getter(); | |
| 70 | + default: | |
| 71 | + if ( ! empty( $this->user ) && 0 !== $this->user->ID ) { | |
| 72 | + if ( is_null( $this->user->$name ) ) { | |
| 73 | + throw new \Exception( sprintf( "Unrecognized magic '%s'", esc_attr( $name ) ) ); | |
| 74 | + } | |
| 75 | + return $this->user->$name; | |
| 76 | + } | |
| 77 | + | |
| 78 | + throw new \Exception( 'User not found.' ); | |
| 59 | 79 | } |
| 60 | 80 | } |
| 61 | 81 | |
| 62 | 82 | /** |
| 83 | + * Returns string representation of this object | |
| 84 | + * | |
| 63 | 85 | * @return string |
| 64 | 86 | */ |
| 65 | 87 | public function __toString() { |
| 66 | 88 | return $this->get_display_name(); |
| @@ -75,25 +97,23 @@ | ||
| 75 | 97 | if ( 0 === $this->id ) { |
| 76 | 98 | if ( isset( $this->meta['system_user_name'] ) ) { |
| 77 | 99 | return esc_html( $this->meta['system_user_name'] ); |
| 78 | 100 | } elseif ( 'wp_cli' === $this->get_current_agent() ) { |
| 79 | - return 'WP-CLI'; // No translation needed | |
| 101 | + return 'WP-CLI'; // No translation needed. | |
| 80 | 102 | } |
| 81 | 103 | return esc_html__( 'N/A', 'stream' ); |
| 82 | - } else { | |
| 83 | - if ( $this->is_deleted() ) { | |
| 84 | - if ( ! empty( $this->meta['display_name'] ) ) { | |
| 85 | - return $this->meta['display_name']; | |
| 86 | - } elseif ( ! empty( $this->meta['user_login'] ) ) { | |
| 87 | - return $this->meta['user_login']; | |
| 88 | - } else { | |
| 89 | - return esc_html__( 'N/A', 'stream' ); | |
| 90 | - } | |
| 91 | - } elseif ( ! empty( $this->user->display_name ) ) { | |
| 92 | - return $this->user->display_name; | |
| 104 | + } elseif ( $this->is_deleted() ) { | |
| 105 | + if ( ! empty( $this->meta['display_name'] ) ) { | |
| 106 | + return $this->meta['display_name']; | |
| 107 | + } elseif ( ! empty( $this->meta['user_login'] ) ) { | |
| 108 | + return $this->meta['user_login']; | |
| 93 | 109 | } else { |
| 94 | - return $this->user->user_login; | |
| 110 | + return $this->id; | |
| 95 | 111 | } |
| 112 | + } elseif ( ! empty( $this->user->display_name ) ) { | |
| 113 | + return $this->user->display_name; | |
| 114 | + } else { | |
| 115 | + return $this->user->user_login; | |
| 96 | 116 | } |
| 97 | 117 | } |
| 98 | 118 | |
| 99 | 119 | /** |
| @@ -106,9 +126,9 @@ | ||
| 106 | 126 | |
| 107 | 127 | if ( ! empty( $this->meta['agent'] ) ) { |
| 108 | 128 | $agent = $this->meta['agent']; |
| 109 | 129 | } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) { |
| 110 | - $agent = 'wp_cli'; // legacy | |
| 130 | + $agent = 'wp_cli'; // legacy. | |
| 111 | 131 | } |
| 112 | 132 | |
| 113 | 133 | return $agent; |
| 114 | 134 | } |
| @@ -117,9 +137,9 @@ | ||
| 117 | 137 | * Return a Gravatar image as an HTML element. |
| 118 | 138 | * |
| 119 | 139 | * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion. |
| 120 | 140 | * |
| 121 | - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 | |
| 141 | + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80. | |
| 122 | 142 | * |
| 123 | 143 | * @return string|bool An img HTML element, or false if avatars are disabled |
| 124 | 144 | */ |
| 125 | 145 | public function get_avatar_img( $size = 80 ) { |
| @@ -130,15 +150,13 @@ | ||
| 130 | 150 | if ( 0 === $this->id ) { |
| 131 | 151 | $stream = wp_stream_get_instance(); |
| 132 | 152 | $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png'; |
| 133 | 153 | $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 | - } else { | |
| 135 | - if ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) { | |
| 154 | + } elseif ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) { | |
| 136 | 155 | $email = $this->meta['user_email']; |
| 137 | 156 | $avatar = get_avatar( $email, $size ); |
| 138 | - } else { | |
| 139 | - $avatar = get_avatar( $this->id, $size ); | |
| 140 | - } | |
| 157 | + } else { | |
| 158 | + $avatar = get_avatar( $this->id, $size ); | |
| 141 | 159 | } |
| 142 | 160 | |
| 143 | 161 | return $avatar; |
| 144 | 162 | } |
| @@ -145,9 +163,9 @@ | ||
| 145 | 163 | |
| 146 | 164 | /** |
| 147 | 165 | * Return the URL of a Gravatar image. |
| 148 | 166 | * |
| 149 | - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 | |
| 167 | + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80. | |
| 150 | 168 | * |
| 151 | 169 | * @return string|bool Gravatar image URL, or false on failure |
| 152 | 170 | */ |
| 153 | 171 | public function get_avatar_src( $size = 80 ) { |
| @@ -157,9 +175,9 @@ | ||
| 157 | 175 | return false; |
| 158 | 176 | } |
| 159 | 177 | |
| 160 | 178 | if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) { |
| 161 | - $src = html_entity_decode( $matches[2] ); | |
| 179 | + $src = html_entity_decode( $matches[2], ENT_COMPAT ); | |
| 162 | 180 | } else { |
| 163 | 181 | return false; |
| 164 | 182 | } |
| 165 | 183 | |
| @@ -185,10 +203,18 @@ | ||
| 185 | 203 | if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) { |
| 186 | 204 | $user_role = $wp_roles->role_names[ $this->meta['user_role'] ]; |
| 187 | 205 | } elseif ( ! empty( $this->meta['user_role_label'] ) ) { |
| 188 | 206 | $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] ]; | |
| 207 | + } elseif ( ! empty( $this->user->roles ) ) { | |
| 208 | + $roles = array_map( | |
| 209 | + function ( $role ) use ( $wp_roles ) { | |
| 210 | + return $wp_roles->role_names[ $role ]; | |
| 211 | + }, | |
| 212 | + $this->user->roles | |
| 213 | + ); | |
| 214 | + | |
| 215 | + $separator = apply_filters( 'wp_stream_get_role_list_separator', ' - ' ); | |
| 216 | + $user_role = implode( $separator, $roles ); | |
| 191 | 217 | } elseif ( is_multisite() && is_super_admin( $this->id ) ) { |
| 192 | 218 | $user_role = $wp_roles->role_names['administrator']; |
| 193 | 219 | } |
| 194 | 220 | |
| @@ -213,24 +239,20 @@ | ||
| 213 | 239 | return ( 'wp_cli' === $this->get_agent() ); |
| 214 | 240 | } |
| 215 | 241 | |
| 216 | 242 | /** |
| 217 | - * True if doing WP Cron, otherwise false | |
| 243 | + * Check if the current request is part of a WP cron task. | |
| 218 | 244 | * |
| 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. | |
| 245 | + * Note: This will return true for all manual or custom | |
| 246 | + * cron runs even if the default front-end cron is disabled. | |
| 222 | 247 | * |
| 248 | + * We're not using `wp_doing_cron()` since it was introduced | |
| 249 | + * only in WordPress 4.8.0. | |
| 250 | + * | |
| 223 | 251 | * @return bool |
| 224 | 252 | */ |
| 225 | 253 | public function is_doing_wp_cron() { |
| 226 | - return ( | |
| 227 | - wp_stream_is_cron_enabled() | |
| 228 | - && | |
| 229 | - defined( 'DOING_CRON' ) | |
| 230 | - && | |
| 231 | - DOING_CRON | |
| 232 | - ); | |
| 254 | + return ( defined( 'DOING_CRON' ) && DOING_CRON ); | |
| 233 | 255 | } |
| 234 | 256 | |
| 235 | 257 | /** |
| 236 | 258 | * Look at the environment to detect if an agent is being used |
| @@ -258,9 +280,9 @@ | ||
| 258 | 280 | |
| 259 | 281 | /** |
| 260 | 282 | * Get the agent label |
| 261 | 283 | * |
| 262 | - * @param string $agent | |
| 284 | + * @param string $agent Key representing agent. | |
| 263 | 285 | * |
| 264 | 286 | * @return string |
| 265 | 287 | */ |
| 266 | 288 | public function get_agent_label( $agent ) { |
| @@ -274,9 +296,9 @@ | ||
| 274 | 296 | |
| 275 | 297 | /** |
| 276 | 298 | * Filter agent labels |
| 277 | 299 | * |
| 278 | - * @param string $agent | |
| 300 | + * @param string $agent Key representing agent. | |
| 279 | 301 | * |
| 280 | 302 | * @return string |
| 281 | 303 | */ |
| 282 | 304 | $label = apply_filters( 'wp_stream_agent_label', $label, $agent ); |