| @@ -1,34 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Manages a single user, acting as a model. | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace WP_Stream; |
| 9 | 3 | |
| 10 | -/** | |
| 11 | - * Class - Author | |
| 12 | - */ | |
| 13 | 4 | class Author { |
| 14 | 5 | /** |
| 15 | - * Holds User ID. | |
| 16 | - * | |
| 17 | 6 | * @var int |
| 18 | 7 | */ |
| 19 | 8 | public $id; |
| 20 | 9 | |
| 21 | 10 | /** |
| 22 | - * Holds User meta data. | |
| 23 | - * | |
| 24 | 11 | * @var array |
| 25 | 12 | */ |
| 26 | 13 | public $meta = array(); |
| 27 | 14 | |
| 28 | 15 | /** |
| 29 | - * Holds WP user object connected to "$this" instance. | |
| 30 | - * | |
| 31 | 16 | * @var \WP_User |
| 32 | 17 | */ |
| 33 | 18 | protected $user; |
| 34 | 19 | |
| @@ -49,40 +34,33 @@ | ||
| 49 | 34 | |
| 50 | 35 | /** |
| 51 | 36 | * Get various user meta data |
| 52 | 37 | * |
| 53 | - * @todo Make sure this is being covered in the unit tests. | |
| 38 | + * @param string $name | |
| 54 | 39 | * |
| 55 | - * @param string $name User meta key. | |
| 40 | + * @throws \Exception | |
| 56 | 41 | * |
| 57 | - * @throws \Exception Meta not found | User not found. | |
| 58 | - * | |
| 59 | 42 | * @return string |
| 60 | 43 | */ |
| 61 | 44 | public function __get( $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.' ); | |
| 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'" ); | |
| 79 | 59 | } |
| 80 | 60 | } |
| 81 | 61 | |
| 82 | 62 | /** |
| 83 | - * Returns string representation of this object | |
| 84 | - * | |
| 85 | 63 | * @return string |
| 86 | 64 | */ |
| 87 | 65 | public function __toString() { |
| 88 | 66 | return $this->get_display_name(); |
| @@ -97,23 +75,25 @@ | ||
| 97 | 75 | if ( 0 === $this->id ) { |
| 98 | 76 | if ( isset( $this->meta['system_user_name'] ) ) { |
| 99 | 77 | return esc_html( $this->meta['system_user_name'] ); |
| 100 | 78 | } elseif ( 'wp_cli' === $this->get_current_agent() ) { |
| 101 | - return 'WP-CLI'; // No translation needed. | |
| 79 | + return 'WP-CLI'; // No translation needed | |
| 102 | 80 | } |
| 103 | 81 | return esc_html__( 'N/A', 'stream' ); |
| 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']; | |
| 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; | |
| 109 | 93 | } else { |
| 110 | - return $this->id; | |
| 94 | + return $this->user->user_login; | |
| 111 | 95 | } |
| 112 | - } elseif ( ! empty( $this->user->display_name ) ) { | |
| 113 | - return $this->user->display_name; | |
| 114 | - } else { | |
| 115 | - return $this->user->user_login; | |
| 116 | 96 | } |
| 117 | 97 | } |
| 118 | 98 | |
| 119 | 99 | /** |
| @@ -126,9 +106,9 @@ | ||
| 126 | 106 | |
| 127 | 107 | if ( ! empty( $this->meta['agent'] ) ) { |
| 128 | 108 | $agent = $this->meta['agent']; |
| 129 | 109 | } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) { |
| 130 | - $agent = 'wp_cli'; // legacy. | |
| 110 | + $agent = 'wp_cli'; // legacy | |
| 131 | 111 | } |
| 132 | 112 | |
| 133 | 113 | return $agent; |
| 134 | 114 | } |
| @@ -137,9 +117,9 @@ | ||
| 137 | 117 | * Return a Gravatar image as an HTML element. |
| 138 | 118 | * |
| 139 | 119 | * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion. |
| 140 | 120 | * |
| 141 | - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80. | |
| 121 | + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 | |
| 142 | 122 | * |
| 143 | 123 | * @return string|bool An img HTML element, or false if avatars are disabled |
| 144 | 124 | */ |
| 145 | 125 | public function get_avatar_img( $size = 80 ) { |
| @@ -148,15 +128,17 @@ | ||
| 148 | 128 | } |
| 149 | 129 | |
| 150 | 130 | if ( 0 === $this->id ) { |
| 151 | 131 | $stream = wp_stream_get_instance(); |
| 152 | - $url = $stream->locations['url'] . 'assets/wp-cli.png'; | |
| 132 | + $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png'; | |
| 153 | 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 ) ); |
| 154 | - } elseif ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) { | |
| 134 | + } else { | |
| 135 | + if ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) { | |
| 155 | 136 | $email = $this->meta['user_email']; |
| 156 | 137 | $avatar = get_avatar( $email, $size ); |
| 157 | - } else { | |
| 158 | - $avatar = get_avatar( $this->id, $size ); | |
| 138 | + } else { | |
| 139 | + $avatar = get_avatar( $this->id, $size ); | |
| 140 | + } | |
| 159 | 141 | } |
| 160 | 142 | |
| 161 | 143 | return $avatar; |
| 162 | 144 | } |
| @@ -163,9 +145,9 @@ | ||
| 163 | 145 | |
| 164 | 146 | /** |
| 165 | 147 | * Return the URL of a Gravatar image. |
| 166 | 148 | * |
| 167 | - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80. | |
| 149 | + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 | |
| 168 | 150 | * |
| 169 | 151 | * @return string|bool Gravatar image URL, or false on failure |
| 170 | 152 | */ |
| 171 | 153 | public function get_avatar_src( $size = 80 ) { |
| @@ -175,9 +157,9 @@ | ||
| 175 | 157 | return false; |
| 176 | 158 | } |
| 177 | 159 | |
| 178 | 160 | if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) { |
| 179 | - $src = html_entity_decode( $matches[2], ENT_COMPAT ); | |
| 161 | + $src = html_entity_decode( $matches[2] ); | |
| 180 | 162 | } else { |
| 181 | 163 | return false; |
| 182 | 164 | } |
| 183 | 165 | |
| @@ -205,9 +187,9 @@ | ||
| 205 | 187 | } elseif ( ! empty( $this->meta['user_role_label'] ) ) { |
| 206 | 188 | $user_role = $this->meta['user_role_label']; |
| 207 | 189 | } elseif ( ! empty( $this->user->roles ) ) { |
| 208 | 190 | $roles = array_map( |
| 209 | - function ( $role ) use ( $wp_roles ) { | |
| 191 | + function( $role ) use ( $wp_roles ) { | |
| 210 | 192 | return $wp_roles->role_names[ $role ]; |
| 211 | 193 | }, |
| 212 | 194 | $this->user->roles |
| 213 | 195 | ); |
| @@ -280,9 +262,9 @@ | ||
| 280 | 262 | |
| 281 | 263 | /** |
| 282 | 264 | * Get the agent label |
| 283 | 265 | * |
| 284 | - * @param string $agent Key representing agent. | |
| 266 | + * @param string $agent | |
| 285 | 267 | * |
| 286 | 268 | * @return string |
| 287 | 269 | */ |
| 288 | 270 | public function get_agent_label( $agent ) { |
| @@ -296,9 +278,9 @@ | ||
| 296 | 278 | |
| 297 | 279 | /** |
| 298 | 280 | * Filter agent labels |
| 299 | 281 | * |
| 300 | - * @param string $agent Key representing agent. | |
| 282 | + * @param string $agent | |
| 301 | 283 | * |
| 302 | 284 | * @return string |
| 303 | 285 | */ |
| 304 | 286 | $label = apply_filters( 'wp_stream_agent_label', $label, $agent ); |