Ticket #41544: 41544.2.diff
File 41544.2.diff, 10.3 KB (added by , 7 years ago) |
---|
-
src/wp-includes/link-template.php
diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php index eb22284..11fd1f9 100644
function get_avatar_data( $id_or_email, $args = null ) { 3877 3877 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 3878 3878 } 3879 3879 3880 $email_hash = '';3881 $user = $email = false;3882 3883 3880 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 3884 3881 $id_or_email = get_comment( $id_or_email ); 3885 3882 } 3886 3883 3887 // Process the user identifier. 3888 if ( is_numeric( $id_or_email ) ) { 3889 $user = get_user_by( 'id', absint( $id_or_email ) ); 3890 } elseif ( is_string( $id_or_email ) ) { 3891 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 3892 // md5 hash 3893 list( $email_hash ) = explode( '@', $id_or_email ); 3894 } else { 3895 // email address 3896 $email = $id_or_email; 3897 } 3898 } elseif ( $id_or_email instanceof WP_User ) { 3899 // User Object 3900 $user = $id_or_email; 3901 } elseif ( $id_or_email instanceof WP_Post ) { 3902 // Post Object 3903 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 3904 } elseif ( $id_or_email instanceof WP_Comment ) { 3905 /** 3906 * Filters the list of allowed comment types for retrieving avatars. 3907 * 3908 * @since 3.0.0 3909 * 3910 * @param array $types An array of content types. Default only contains 'comment'. 3911 */ 3912 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 3913 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 3914 $args['url'] = false; 3915 /** This filter is documented in wp-includes/link-template.php */ 3916 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 3917 } 3918 3919 if ( ! empty( $id_or_email->user_id ) ) { 3920 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 3921 } 3922 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 3923 $email = $id_or_email->comment_author_email; 3924 } 3925 } 3926 3927 if ( ! $email_hash ) { 3928 if ( $user ) { 3929 $email = $user->user_email; 3930 } 3884 $parsed = wp_parse_id_or_email( $id_or_email ); 3931 3885 3932 if ( $email ) { 3933 $email_hash = md5( strtolower( trim( $email ) ) ); 3934 } 3886 if( 'comment-disallowed-type' === $parsed['context'] ) { 3887 $args['url'] = false; 3888 /** This filter is documented in wp-includes/link-template.php */ 3889 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 3935 3890 } 3936 3891 3937 if ( $email_hash) {3892 if ( ! empty( $parsed['email_hash'] ) ) { 3938 3893 $args['found_avatar'] = true; 3939 $gravatar_server = hexdec( $ email_hash[0] ) % 3;3894 $gravatar_server = hexdec( $parsed['email_hash'][0] ) % 3; 3940 3895 } else { 3941 3896 $gravatar_server = rand( 0, 2 ); 3942 3897 } … … function get_avatar_data( $id_or_email, $args = null ) { 3949 3904 ); 3950 3905 3951 3906 if ( is_ssl() ) { 3952 $url = 'https://secure.gravatar.com/avatar/' . $ email_hash;3907 $url = 'https://secure.gravatar.com/avatar/' . $parsed['email_hash']; 3953 3908 } else { 3954 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $ email_hash);3909 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $parsed['email_hash'] ); 3955 3910 } 3956 3911 3957 3912 $url = add_query_arg( … … function get_avatar_data( $id_or_email, $args = null ) { 3984 3939 } 3985 3940 3986 3941 /** 3942 * Parse `$id_or_email` 3943 * 3944 * Parse the generic `$id_or_email` input into user_id, email, email_hash and context. 3945 * 3946 * @since x.x.x 3947 * 3948 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, 3949 * user email, WP_User object, WP_Post object, or WP_Comment object. 3950 * @return array $parsed_args { 3951 * Parsed data from the `$id_or_email` input. 3952 * 3953 * @type int $user_id User ID if we were able to find a user. Default 0. 3954 * @type string $email The email. Default empty. 3955 * @type string $email_hash The hashed email. Default empty. 3956 * @type string $context The input context. Possible values are 'user_id', 3957 * 'email-md5-gravatar', 'email', 'user', 'post', 3958 * 'comment-allowed-type', 'comment-disallowed-type', 3959 * } 3960 */ 3961 function wp_parse_id_or_email( $id_or_email ) { 3962 $email_hash = $email = $context = ''; 3963 $user = null; 3964 3965 // Process the user identifier. 3966 if ( is_numeric( $id_or_email ) ) { 3967 $user = get_user_by( 'id', absint( $id_or_email ) ); 3968 $context = 'user_id'; 3969 } elseif ( is_string( $id_or_email ) ) { 3970 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 3971 // md5 hash 3972 list( $email_hash ) = explode( '@', $id_or_email ); 3973 $context = 'email-md5-gravatar'; 3974 } else { 3975 // email address 3976 $email = $id_or_email; 3977 $context = 'email'; 3978 } 3979 } elseif ( $id_or_email instanceof WP_User ) { 3980 // User Object 3981 $user = $id_or_email; 3982 $context = 'user'; 3983 } elseif ( $id_or_email instanceof WP_Post ) { 3984 // Post Object 3985 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 3986 $context = 'post'; 3987 } elseif ( $id_or_email instanceof WP_Comment ) { 3988 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 3989 $id_or_email = get_comment( $id_or_email ); 3990 } 3991 3992 /** 3993 * Filters the list of allowed comment types for retrieving avatars. 3994 * 3995 * @since 3.0.0 3996 * 3997 * @param array $types An array of content types. Default only contains 'comment'. 3998 */ 3999 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 4000 4001 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 4002 return array( 4003 'user_id' => 0, 4004 'email' => '', 4005 'email_hash' => '', 4006 'context' => 'comment-disallowed-type', 4007 ); 4008 } 4009 4010 if ( ! empty( $id_or_email->user_id ) ) { 4011 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 4012 } 4013 4014 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 4015 $email = $id_or_email->comment_author_email; 4016 } 4017 4018 $context = 'comment-allowed-type'; 4019 } 4020 4021 if ( empty( $email_hash ) ) { 4022 if ( $user instanceof WP_User ) { 4023 $email = $user->user_email; 4024 } 4025 4026 if ( ! empty( $email ) ) { 4027 $email_hash = md5( strtolower( trim( $email ) ) ); 4028 } 4029 } 4030 4031 return array( 4032 'user_id' => ( $user instanceof WP_User ) ? $user->ID : 0, 4033 'email' => $email, 4034 'email_hash' => $email_hash, 4035 'context' => $context, 4036 ); 4037 } 4038 4039 4040 4041 /** 3987 4042 * Retrieves the URL of a file in the theme. 3988 4043 * 3989 4044 * Searches in the stylesheet directory before the template directory so themes -
tests/phpunit/tests/avatar.php
diff --git tests/phpunit/tests/avatar.php tests/phpunit/tests/avatar.php index e777f10..7fd469f 100644
class Tests_Avatar extends WP_UnitTestCase { 229 229 return $this->fakeURL; 230 230 } 231 231 232 /** 233 * @ticket 41544 234 * @dataProvider data_test_wp_parse_id_or_email 235 */ 236 public function test_wp_parse_id_or_email( $id_or_email, $expected ) { 237 $this->assertSame( $expected, wp_parse_id_or_email( $id_or_email ) ); 238 } 239 240 public function data_test_wp_parse_id_or_email() { 241 // Create a user 242 $user = self::factory()->user->create_and_get( array( 243 'user_email' => 'user_for_testing_wp_parse_id_or_email@example.org', 244 ) ); 245 $user_id = $user->ID; 246 $email = $user->user_email; 247 $email_hash = md5( strtolower( trim( $email ) ) ); 248 249 // Create a post 250 $post_by_user = self::factory()->post->create_and_get( array( 'post_author' => $user_id ) ); 251 $post_id = $post_by_user->ID; 252 253 // Create a comment with allowed type 254 $comment_id = self::factory()->comment->create( array( 255 'comment_post_ID' => $post_id, 256 'user_id' => $user_id 257 ) ); 258 $comment_with_allowed_type = get_comment( $comment_id ); 259 260 // Create a comment with disllowed type 261 $comment_id = self::factory()->comment->create( array( 262 'comment_post_ID' => $post_id, 263 'user_id' => $user_id, 264 'comment_type' => 'pingback', 265 ) ); 266 $comment_with_disallowed_type = get_comment( $comment_id ); 267 268 return array( 269 'Null' => array( 270 'id_or_email' => null, 271 'expected' => array( 272 'user_id' => 0, 273 'email' => '', 274 'email_hash' => '', 275 'context' => '', 276 ) 277 ), 278 'Empty email address' => array( 279 'id_or_email' => '', 280 'expected' => array( 281 'user_id' => 0, 282 'email' => '', 283 'email_hash' => '', 284 'context' => 'email', 285 ) 286 ), 287 'Email address' => array( 288 'id_or_email' => $email, 289 'expected' => array( 290 'user_id' => 0, 291 'email' => $email, 292 'email_hash' => $email_hash, 293 'context' => 'email', 294 ) 295 ), 296 'md5 gravatar email address' => array( 297 'id_or_email' => $email_hash . '@md5.gravatar.com', 298 'expected' => array( 299 'user_id' => 0, 300 'email' => '', 301 'email_hash' => $email_hash, 302 'context' => 'email-md5-gravatar', 303 ) 304 ), 305 'User ID' => array( 306 'id_or_email' => $user_id, 307 'expected' => array( 308 'user_id' => $user_id, 309 'email' => $email, 310 'email_hash' => $email_hash, 311 'context' => 'user_id', 312 ) 313 ), 314 'Non-existing user ID' => array( 315 'id_or_email' => 0, 316 'expected' => array( 317 'user_id' => 0, 318 'email' => '', 319 'email_hash' => '', 320 'context' => 'user_id', 321 ) 322 ), 323 'Post object' => array( 324 'id_or_email' => $post_by_user, 325 'expected' => array( 326 'user_id' => $user_id, 327 'email' => $email, 328 'email_hash' => $email_hash, 329 'context' => 'post', 330 ) 331 ), 332 'User object' => array( 333 'id_or_email' => $user, 334 'expected' => array( 335 'user_id' => $user_id, 336 'email' => $email, 337 'email_hash' => $email_hash, 338 'context' => 'user', 339 ) 340 ), 341 'Comment with allowed type' => array( 342 'id_or_email' => $comment_with_allowed_type, 343 'expected' => array( 344 'user_id' => $user_id, 345 'email' => $email, 346 'email_hash' => $email_hash, 347 'context' => 'comment-allowed-type', 348 ) 349 ), 350 'Comment with disallowed type' => array( 351 'id_or_email' => $comment_with_disallowed_type, 352 'expected' => array( 353 'user_id' => 0, 354 'email' => '', 355 'email_hash' => '', 356 'context' => 'comment-disallowed-type', 357 ) 358 ), 359 ); 360 } 232 361 }