Ticket #41544: 41544.3.diff
File 41544.3.diff, 10.8 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..8afc202 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 $user = null; 3963 $email_hash = ''; 3964 $email = ''; 3965 $context = ''; 3966 3967 /* Process the user identifier.*/ 3968 if ( is_numeric( $id_or_email ) ) { 3969 $user = get_user_by( 'id', absint( $id_or_email ) ); 3970 $context = 'user_id'; 3971 } elseif ( is_string( $id_or_email ) ) { 3972 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 3973 /* md5 hash */ 3974 list( $email_hash ) = explode( '@', $id_or_email ); 3975 $context = 'email-md5-gravatar'; 3976 } else { 3977 /* Email address */ 3978 $email = $id_or_email; 3979 $context = 'email'; 3980 } 3981 } elseif ( $id_or_email instanceof WP_User ) { 3982 /* User Object */ 3983 $user = $id_or_email; 3984 $context = 'user'; 3985 } elseif ( $id_or_email instanceof WP_Post ) { 3986 /* Post Object */ 3987 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 3988 $context = 'post'; 3989 } elseif ( $id_or_email instanceof WP_Comment ) { 3990 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 3991 $id_or_email = get_comment( $id_or_email ); 3992 } 3993 3994 /** 3995 * Filters the list of allowed comment types for retrieving avatars. 3996 * 3997 * @since 3.0.0 3998 * 3999 * @param array $types An array of content types. Default only contains 'comment'. 4000 */ 4001 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 4002 4003 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) { 4004 return array( 4005 'user_id' => 0, 4006 'email' => '', 4007 'email_hash' => '', 4008 'context' => 'comment-disallowed-type', 4009 ); 4010 } 4011 4012 if ( ! empty( $id_or_email->user_id ) ) { 4013 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 4014 } 4015 4016 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 4017 $email = $id_or_email->comment_author_email; 4018 } 4019 4020 $context = 'comment-allowed-type'; 4021 } 4022 4023 if ( empty( $email_hash ) ) { 4024 if ( $user instanceof WP_User ) { 4025 $email = $user->user_email; 4026 } 4027 4028 if ( ! empty( $email ) ) { 4029 $email_hash = md5( strtolower( trim( $email ) ) ); 4030 } 4031 } 4032 4033 return array( 4034 'user_id' => ( $user instanceof WP_User ) ? $user->ID : 0, 4035 'email' => $email, 4036 'email_hash' => $email_hash, 4037 'context' => $context, 4038 ); 4039 } 4040 4041 4042 4043 /** 3987 4044 * Retrieves the URL of a file in the theme. 3988 4045 * 3989 4046 * 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..39eb253 100644
class Tests_Avatar extends WP_UnitTestCase { 229 229 return $this->fakeURL; 230 230 } 231 231 232 /** 233 * Testing the wp_parse_id_or_email() function 234 * 235 * @ticket 41544 236 * @dataProvider data_test_wp_parse_id_or_email 237 * 238 * @param mixed $id_or_email The input value we are testing against. 239 * @param array $expected Expected result. 240 */ 241 public function test_wp_parse_id_or_email( $id_or_email, $expected ) { 242 $this->assertSame( $expected, wp_parse_id_or_email( $id_or_email ) ); 243 } 244 245 /** 246 * Data Provider for test_wp_parse_id_or_email() 247 * 248 * @return array { 249 * @type array { 250 * @mixed mixed $id_or_email Generic input value 251 * @array array $expected The expected result. 252 * } 253 * } 254 */ 255 public function data_test_wp_parse_id_or_email() { 256 /* Create a user */ 257 $user = self::factory()->user->create_and_get( array( 258 'user_email' => 'user_for_testing_wp_parse_id_or_email@example.org', 259 ) ); 260 $user_id = $user->ID; 261 $user_email = $user->user_email; 262 $user_email_hash = md5( strtolower( trim( $user_email ) ) ); 263 264 /* Create a post */ 265 $post_by_user = self::factory()->post->create_and_get( array( 266 'post_author' => $user_id, 267 ) ); 268 $post_id = $post_by_user->ID; 269 270 /* Create a comment with allowed type */ 271 $comment_with_allowed_type = self::factory()->comment->create_and_get( array( 272 'comment_post_ID' => $post_id, 273 'user_id' => $user_id, 274 ) ); 275 276 /* Create a comment with disllowed type */ 277 $comment_with_disallowed_type = self::factory()->comment->create_and_get( array( 278 'comment_post_ID' => $post_id, 279 'user_id' => $user_id, 280 'comment_type' => 'pingback', 281 ) ); 282 283 return array( 284 'Null' => array( 285 'id_or_email' => null, 286 'expected' => array( 287 'user_id' => 0, 288 'email' => '', 289 'email_hash' => '', 290 'context' => '', 291 ), 292 ), 293 'Empty email address' => array( 294 'id_or_email' => '', 295 'expected' => array( 296 'user_id' => 0, 297 'email' => '', 298 'email_hash' => '', 299 'context' => 'email', 300 ), 301 ), 302 'Email address' => array( 303 'id_or_email' => $user_email, 304 'expected' => array( 305 'user_id' => 0, 306 'email' => $user_email, 307 'email_hash' => $user_email_hash, 308 'context' => 'email', 309 ), 310 ), 311 'md5 gravatar email address' => array( 312 'id_or_email' => $user_email_hash . '@md5.gravatar.com', 313 'expected' => array( 314 'user_id' => 0, 315 'email' => '', 316 'email_hash' => $user_email_hash, 317 'context' => 'email-md5-gravatar', 318 ), 319 ), 320 'User ID' => array( 321 'id_or_email' => $user_id, 322 'expected' => array( 323 'user_id' => $user_id, 324 'email' => $user_email, 325 'email_hash' => $user_email_hash, 326 'context' => 'user_id', 327 ), 328 ), 329 'Non-existing user ID' => array( 330 'id_or_email' => 0, 331 'expected' => array( 332 'user_id' => 0, 333 'email' => '', 334 'email_hash' => '', 335 'context' => 'user_id', 336 ), 337 ), 338 'Post object' => array( 339 'id_or_email' => $post_by_user, 340 'expected' => array( 341 'user_id' => $user_id, 342 'email' => $user_email, 343 'email_hash' => $user_email_hash, 344 'context' => 'post', 345 ), 346 ), 347 'User object' => array( 348 'id_or_email' => $user, 349 'expected' => array( 350 'user_id' => $user_id, 351 'email' => $user_email, 352 'email_hash' => $user_email_hash, 353 'context' => 'user', 354 ), 355 ), 356 'Comment with allowed type' => array( 357 'id_or_email' => $comment_with_allowed_type, 358 'expected' => array( 359 'user_id' => $user_id, 360 'email' => $user_email, 361 'email_hash' => $user_email_hash, 362 'context' => 'comment-allowed-type', 363 ), 364 ), 365 'Comment with disallowed type' => array( 366 'id_or_email' => $comment_with_disallowed_type, 367 'expected' => array( 368 'user_id' => 0, 369 'email' => '', 370 'email_hash' => '', 371 'context' => 'comment-disallowed-type', 372 ), 373 ), 374 ); 375 } 232 376 }