1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
<?php /** * Class Gravatar is responsible for handling Gravatar Cache. * * @since 1.6.0 */
namespace Hummingbird\Core\Modules;
use Hummingbird\Core\Filesystem; use Hummingbird\Core\Module; use Hummingbird\Core\Settings; use WP_Comment; use WP_Error; use WP_Post; use WP_User;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Gravatar * * @package Hummingbird\Core\Modules */ class Gravatar extends Module {
/** * Last error. * * @since 1.6.0 * @var WP_Error $error */ public $error = false;
/** * Initialize module. * * @since 1.6.0 */ public function init() { add_filter( 'wp_hummingbird_is_active_module_gravatar', array( $this, 'module_status' ) ); }
/** * Execute module actions * * @since 1.6.0 */ public function run() { global $wphb_fs;
// Init filesystem. if ( ! $wphb_fs ) { $wphb_fs = Filesystem::instance(); }
// If error - save error status and exit. if ( is_wp_error( $wphb_fs->status ) ) { $this->error = $wphb_fs->status; return; }
// Everything else is only for frontend. if ( is_admin() && !( wp_doing_ajax() && isset( $_REQUEST['action'] ) && 'get_comments_template' === $_REQUEST['action'] ) ) { return; }
add_filter( 'get_avatar_data', array( $this, 'get_avatar_data' ), 10, 2 ); }
/** * Implement abstract parent method for clearing cache. * * Delete cached files. * * @return bool * @since 1.6.0 * @since 1.7.1 name changed from delete_files to clear_cache */ public function clear_cache() { /* @var Filesystem $wphb_fs */ global $wphb_fs;
return $wphb_fs->purge( 'gravatar' ); }
/** * Activate module. * * @since 1.9.0 */ public function enable() { Settings::update_setting( 'enabled', true, $this->get_slug() ); }
/** * Deactivate module. * * @since 1.9.0 */ public function disable() { Settings::update_setting( 'enabled', false, $this->get_slug() ); }
/** * Fetch remote avatar and save to local cache * * @access private * @see Requests_Utility_CaseInsensitiveDictionary for $remote_avatar['headers']->getAll(). * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param int $size Size of avatar. * @since 1.6.0 * @return bool|WP_Error Returns true if file write is ok, WP_Error on error. */ private function get_remote_avatar( $id_or_email, $size ) { /* @var Filesystem $wphb_fs */ global $wphb_fs;
$gravatar = get_avatar_data( $id_or_email, array( 'size' => $size, ) );
if ( true === $gravatar['found_avatar'] ) { $remote_avatar = wp_remote_get( $gravatar['url'] ); /** * TODO: if png is used here, then we need to use png in get_cached_avatar(). $header = $remote_avatar['headers']->getAll(); switch ( $header['content-type'] ) { case 'image/jpeg': default: $extension = '.jpg'; break; case 'image/png': $extension = '.png'; break; } */
/** * Filename. * Format: [md5_hash]x[size].[extension] * Example: 0973085bb3339de14706edda7bc62581x100.jpg */ $email_hash = $this->get_email_hash( $id_or_email ); $file = $email_hash . 'x' . $size . '.jpg'; return $wphb_fs->write( $file, $remote_avatar['body'], true ); } else { return new WP_Error( 'gravatar-not-found', __( 'Error fetching Gravatar. Gravatar not found.', 'wphb' ) ); } }
/** * Calculate email hash * * @access private * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @return string Email hash. * @since 1.6.0 */ private function get_email_hash( $id_or_email ) { $email_hash = ''; $user = $email = false;
// Process the user identifier. if ( is_numeric( $id_or_email ) ) { $user = get_user_by( 'id', absint( $id_or_email ) ); } elseif ( is_string( $id_or_email ) ) { if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { // MD5 hash. list( $email_hash ) = explode( '@', $id_or_email ); } else { // Email address. $email = $id_or_email; } } elseif ( $id_or_email instanceof WP_User ) { // User Object. $user = $id_or_email; } elseif ( $id_or_email instanceof WP_Post ) { // Post Object. $user = get_user_by( 'id', (int) $id_or_email->post_author ); } elseif ( $id_or_email instanceof WP_Comment ) { /** * Filters the list of allowed comment types for retrieving avatars. * * @since 3.0.0 * * @param array $types An array of content types. Default only contains 'comment'. */ $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment', 'pingback' ) ); if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) { $args['url'] = false; /** This filter is documented in wp-includes/link-template.php */ return apply_filters( 'get_email_hash', false, $id_or_email, $args ); }
if ( ! empty( $id_or_email->user_id ) ) { $user = get_user_by( 'id', (int) $id_or_email->user_id ); } if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { $email = $id_or_email->comment_author_email; } }
if ( ! $email_hash ) { if ( $user ) { $email = $user->user_email; }
if ( $email ) { $email_hash = md5( strtolower( trim( $email ) ) ); } }
return $email_hash; }
/** * Get cached avatar. * * @param string $image Image source. * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param int $size Image size. * @param bool $default Not used. URL for an image, defaults to the "Mystery Person". * @param string $alt Alternate text to use in the avatar image tag. * @param array $args Arguments passed to get_avatar_url(), after processing. * @return string $image Image source. * @since 1.6.0 * @deprecated 1.6.1 */ public function get_cached_avatar( $image, $id_or_email, $size, $default, $alt, $args ) { /* @var Filesystem $wphb_fs */ global $wphb_fs;
$email_hash = $this->get_email_hash( $id_or_email );
// Avatar file names for normal and retina. $images = array( 'normal' => array( 'file' => $email_hash . 'x' . $size . '.jpg', 'size' => $size, ), 'retina' => array( 'file' => $email_hash . 'x' . ( $size * 2 ) . '.jpg', 'size' => $size * 2, ), );
foreach ( $images as $img ) { // Try to save the avatar. if ( $wphb_fs->find( $img['file'], true ) ) { break; }
$file_write = $this->get_remote_avatar( $id_or_email, $img['size'] ); // If error creating file - log and return original image. if ( is_wp_error( $file_write ) ) { $this->log( $file_write->get_error_message() ); $this->error = $file_write; return $image; } }
$gravatar_dir = trailingslashit( substr( $images['normal']['file'], 0, 3 ) );
$src = $wphb_fs->baseurl . $gravatar_dir . $images['normal']['file']; $srcset = $wphb_fs->baseurl . $gravatar_dir . $images['retina']['file'];
$class = array( 'avatar', 'avatar-' . (int) $size, 'photo' );
if ( $args['class'] ) { if ( is_array( $args['class'] ) ) { $class = array_merge( $class, $args['class'] ); } else { $class[] = $args['class']; } }
$avatar = sprintf( "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d'/>", esc_attr( $alt ), esc_url( $src ), esc_attr( "$srcset 2x" ), esc_attr( join( ' ', $class ) ), (int) $size, (int) $size );
return $avatar; }
/** * Get avatar url. * * @since 1.6.1 * @param array $args Arguments passed to get_avatar_data(), after processing. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @return mixed */ public function get_avatar_data( $args, $id_or_email ) { /* @var Filesystem $wphb_fs */ global $wphb_fs;
$email_hash = $this->get_email_hash( $id_or_email );
if ( ! $args['found_avatar'] ) { return $args; }
// Try to save the avatar. $file = $email_hash . 'x' . $args['size'] . '.jpg';
if ( ! $wphb_fs->find( $file, true ) && isset( $args['url'] ) ) { $remote_avatar = wp_remote_get( $args['url'] ); // If error fetching avatar. if ( is_wp_error( $remote_avatar ) ) { $this->error = $remote_avatar; return $args; } // Write index.html file to directory. $wphb_fs->write( 'index.html', '', true ); // Write gravatar file. $file_write = $wphb_fs->write( $file, $remote_avatar['body'], true );
// If error creating file - log and return original image. if ( is_wp_error( $file_write ) ) { $this->log( $file_write->get_error_message() ); $this->error = $file_write; return $args; } }
$gravatar_dir = trailingslashit( substr( $file, 0, 3 ) ); $args['url'] = $wphb_fs->baseurl . $gravatar_dir . $file;
return $args; }
/** * Get module status. * * @param bool $current Current status. * * @return bool */ public function module_status( $current ) { $options = $this->get_options(); if ( ! $options['enabled'] ) { return false; }
return $current; }
}
|