Ticket #41544: 41544.diff
File 41544.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..f0b1961 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_id = self::factory()->post->create( array( 'post_author' => 1 ) ); 251 252 // Create a comment with allowed type 253 $comment_id = self::factory()->comment->create( array( 254 'comment_post_ID' => $post_id, 255 'user_id' => $user_id 256 ) ); 257 $comment_with_allowed_type = get_comment( $comment_id ); 258 259 // Create a comment with disllowed type 260 $comment_id = self::factory()->comment->create( array( 261 'comment_post_ID' => $post_id, 262 'user_id' => $user_id, 263 'comment_type' => 'pingback', 264 ) ); 265 $comment_with_disallowed_type = get_comment( $comment_id ); 266 267 return array( 268 'Null' => array( 269 'id_or_email' => null, 270 'expected' => array( 271 'user_id' => 0, 272 'email' => '', 273 'email_hash' => '', 274 'context' => '', 275 ) 276 ), 277 'Empty email address' => array( 278 'id_or_email' => '', 279 'expected' => array( 280 'user_id' => 0, 281 'email' => '', 282 'email_hash' => '', 283 'context' => 'email', 284 ) 285 ), 286 'Email address' => array( 287 'id_or_email' => $email, 288 'expected' => array( 289 'user_id' => 0, 290 'email' => $email, 291 'email_hash' => $email_hash, 292 'context' => 'email', 293 ) 294 ), 295 'md5 gravatar email address' => array( 296 'id_or_email' => $email_hash . '@md5.gravatar.com', 297 'expected' => array( 298 'user_id' => 0, 299 'email' => '', 300 'email_hash' => $email_hash, 301 'context' => 'email-md5-gravatar', 302 ) 303 ), 304 'User ID' => array( 305 'id_or_email' => $user_id, 306 'expected' => array( 307 'user_id' => $user_id, 308 'email' => $email, 309 'email_hash' => $email_hash, 310 'context' => 'user_id', 311 ) 312 ), 313 'Non-existing user ID' => array( 314 'id_or_email' => 0, 315 'expected' => array( 316 'user_id' => 0, 317 'email' => '', 318 'email_hash' => '', 319 'context' => 'user_id', 320 ) 321 ), 322 'Post object' => array( 323 'id_or_email' => 0, 324 'expected' => array( 325 'user_id' => 0, 326 'email' => '', 327 'email_hash' => '', 328 'context' => 'user_id', 329 ) 330 ), 331 'User object' => array( 332 'id_or_email' => $user, 333 'expected' => array( 334 'user_id' => $user_id, 335 'email' => $email, 336 'email_hash' => $email_hash, 337 'context' => 'user', 338 ) 339 ), 340 'Comment with allowed type' => array( 341 'id_or_email' => $comment_with_allowed_type, 342 'expected' => array( 343 'user_id' => $user_id, 344 'email' => $email, 345 'email_hash' => $email_hash, 346 'context' => 'comment-allowed-type', 347 ) 348 ), 349 'Comment with disallowed type' => array( 350 'id_or_email' => $comment_with_disallowed_type, 351 'expected' => array( 352 'user_id' => 0, 353 'email' => '', 354 'email_hash' => '', 355 'context' => 'comment-disallowed-type', 356 ) 357 ), 358 ); 359 } 232 360 }