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