Changeset 4364
- Timestamp:
- 11/15/2016 11:51:56 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/includes/indesign-badges.php
r4363 r4364 73 73 } 74 74 75 $request_url = str_replace( '=blank', '=404', $attendee->avatar_url ); 76 $response = wp_remote_get( $request_url ); 77 $image = wp_remote_retrieve_body( $response ); 78 $status_code = wp_remote_retrieve_response_code( $response ); 79 80 if ( 404 == $status_code ) { 75 $request_url = str_replace( '=blank', '=404', $attendee->avatar_url ); 76 $gravatar_image = download_single_gravatar( $request_url ); 77 78 if ( ! $gravatar_image ) { 81 79 continue; 82 }83 84 if ( ! $image || 200 != $status_code ) {85 Logger\log( 'request_failed', compact( 'attendee', 'request_url', 'response' ) );86 throw new \Exception( __( "Couldn't download all Gravatars.", 'wordcamporg' ) );87 80 } 88 81 … … 90 83 $gravatar_file = fopen( $gravatar_folder . '/' . $filename, 'w' ); 91 84 92 fwrite( $gravatar_file, $image ); 85 if ( ! $gravatar_file ) { 86 Logger\log( 'gravatar_open_failed', compact( 'attendee', 'gravatar_folder', 'filename' ) ); 87 throw new \Exception( __( "Couldn't save all Gravatars.", 'wordcamporg' ) ); 88 } 89 90 fwrite( $gravatar_file, $gravatar_image ); 93 91 fclose( $gravatar_file ); 92 } 93 } 94 95 /** 96 * Download a Gravatar 97 * 98 * Sometimes the HTTP request times out, or Varnish returns a `503` error, but the batch will be ruined if even a 99 * single existing Gravatar cannot be downloaded successfully. In order to mitigate that, we retry the download 100 * multiple times. 101 * 102 * @param string $request_url 103 * 104 * @return bool|string `false` when the user does not have a Gravatar, `string` of image binary data when the 105 * image was successfully retrieved. 106 * 107 * @throws \Exception when the HTTP failed even after multiple attempts 108 */ 109 function download_single_gravatar( $request_url ) { 110 $attempt_count = 1; 111 112 while ( true ) { 113 $response = wp_remote_get( $request_url ); 114 $status_code = wp_remote_retrieve_response_code( $response ); 115 $image = wp_remote_retrieve_body( $response ); 116 $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ) ?: 5; 117 $retry_after = min( $retry_after * $attempt_count, 30 ); 118 119 // A 404 is expected when the attendee doesn't have a Gravatar setup, so don't retry them 120 if ( 404 == $status_code ) { 121 return false; 122 } 123 124 if ( ! is_wp_error( $response ) && $image ) { 125 return $image; 126 } 127 128 $response['body'] = '[redacted]'; // Avoid cluttering the logs with a ton of binary data 129 130 if ( $attempt_count < 3 ) { 131 Logger\log( 'request_failed_temporarily', compact( 'attendee', 'request_url', 'response', 'attempt_count', 'retry_after' ) ); 132 sleep( $retry_after ); 133 } else { 134 Logger\log( 'request_failed_permenantly', compact( 'attendee', 'request_url', 'response' ) ); 135 throw new \Exception( __( "Couldn't download all Gravatars.", 'wordcamporg' ) ); 136 } 137 138 $attempt_count++; 94 139 } 95 140 }
Note: See TracChangeset
for help on using the changeset viewer.