forked from leewillis77/wapuuvatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwapuuvatar.php
264 lines (247 loc) · 7.09 KB
/
wapuuvatar.php
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
<?php
/*
Plugin Name: Wapuuvatar
Description: Use Wapuus for your user avatars.
Plugin URI: http://www.leewillis.co.uk
Author: Lee Willis
Author URI: http://www.leewillis.co.uk
Version: 2.7
License: GPL2
Text Domain: wapuuvatar
*/
/*
Copyright (C) 2015-2021 Lee Willis wordpress@leewillis.co.uk
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Init. Setup translation for the plugin.
*/
function wapuuvatar_init() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'wapuuvatar' );
load_textdomain( 'wapuuvatar', WP_LANG_DIR . '/wapuuvatar/wapuuvatar-' . $locale . '.mo' );
load_plugin_textdomain( 'wapuuvatar', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', 'wapuuvatar_init' );
/**
* Register our avatar type so it can be chosen on the admin screens.
*
* @param array $avatar_defaults Array of avatar types.
*
* @return array Modified array of avatar types.
*/
function wapuuvatar_avatar_defaults( $avatar_defaults ) {
$avatar_defaults['dwapuuvatar'] = __( 'Random wapuus', 'wapuuvatar' );
$avatar_defaults['wapuuvatar'] = __( 'Random wapuus everywhere (No gravatars at all)', 'wapuuvatar' );
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'wapuuvatar_avatar_defaults' );
/**
* Implements get_avatar().
*
* Generate a Wapuuvatar if requested.
*/
function wapuuvatar_get_avatar( $avatar, $id_or_email, $size, $default, $alt, $args ) {
if ( is_admin() ) {
$screen = get_current_screen();
if ( is_object($screen) && in_array( $screen->id, array( 'dashboard', 'edit-comments' ) ) && $default == 'mm') {
$default = get_option( 'avatar_default', 'mystery' );
}
}
if ( $default != 'wapuuvatar' && $default != 'dwapuuvatar' ) {
return $avatar;
}
list ( $url, $url2x ) = wapuuvatar_generate_avatar_url( $id_or_email, $size );
$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
if ( $default == 'wapuuvatar' ) {
return sprintf(
"<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
esc_attr( $args['alt'] ),
esc_url( $url ),
esc_attr( "$url2x 2x" ),
esc_attr( join( ' ', $class ) ),
(int) $args['height'],
(int) $args['width'],
$args['extra_attr']
);
}
if ( 'dwapuuvatar' == $default ) {
return str_replace( 'dwapuuvatar', urlencode( esc_url( $url ) ), $avatar );
}
return $avatar;
}
add_filter( 'get_avatar', 'wapuuvatar_get_avatar', 10, 6 );
/**
* Generate the Wapuuvatar URL for a specific ID or email.
*
* @param mixed $id_or_email The ID / email / hash of the requested avatar.
* @param int $size The requested size.
* @return array Array of standard and 2x URLs.
*/
function wapuuvatar_generate_avatar_url( $id_or_email, $requested_size ) {
// Select a size.
$sizes = array( 128, 64, 32 );
$selected_size = max($sizes);
foreach( $sizes as $choice ) {
if ( $choice >= $requested_size ) {
$selected_size = $choice;
}
}
// Pick a wapuu.
$hash = wapuuvatar_id_or_email_to_hash( $id_or_email );
$wapuus = wapuuvatar_get_wapuus();
$wapuu = hexdec( substr( $hash, 0, 4) ) % count( $wapuus );
$wapuu_base = apply_filters( 'wapuuvatar_chosen_wapuu', $wapuus[ $wapuu ], $id_or_email, $hash );
$wapuu_img = $wapuu_base . '-' . $selected_size . '.png';
$wapuu_img2x = $wapuu_base . '-' . ( $selected_size * 2 ) . '.png';
// Common base URL.
$base_url = plugins_url() . '/wapuuvatar/dist/';
return array(
$base_url . $wapuu_img,
$base_url . $wapuu_img2x,
);
}
/**
* Deal with mapping an id_or_email to a hash.
*
* Borrows from get_avatar_data() in link-template.php.
*
* @param mixed $id_or_email ID / email / hash of the requested avatar.
*
* @return string Hash to use to map the wapuu.
*/
function wapuuvatar_id_or_email_to_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 ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
// Comment Object
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;
}
function wapuuvatar_get_wapuus() {
return array(
'WapuuPepa',
'WapuuPepe',
'basile-wapuu',
'canvas-wapuu',
'cheesesteak-wapuu',
'dokuganryu-wapuu',
'edinburgh-wapuu',
'fes-wapuu',
'kani-wapuu',
'krimpet-wapuu',
'maikochan-and-wapuu',
'manchester-wapuu',
'masuzushi-wapuu',
'matsuri-wapuu',
'mineiro-wapuu',
'onsen-wapuu',
'original-wapuu',
'pretzel-wapuu',
'rocky-wapuu',
'scott-wapuu',
'shachihoko-wapuu',
'shikasenbei-wapuu',
'sydney-wapuu',
'takoyaki-wapuu',
'tampa-gasparilla-wapuu',
'tonkotsu-wapuu',
'wapuda-shingen',
'wapuu-brainhurts',
'wapuu-cheesehead',
'wapuu-cosplay',
'wapuu-der-ber',
'wapuu-dev',
'wapuu-france-hd',
'wapuu-france',
'wapuu-guitar',
'wapuu-hampton-roads',
'wapuu-heropress',
'wapuu-hipster',
'wapuu-magic',
'wapuu-minion',
'wapuu-moto',
'wapuu-nyc',
'wapuu-orange',
'wapuu-pixar',
'wapuu-pizza',
'wapuu-poststatus',
'wapuu-sleepy-wordcamp',
'wapuu-snitch',
'wapuu-spy',
'wapuu-struggle',
'wapuu-torque',
'wapuu-travel',
'wapuu-tron',
'wapuu-unipiper',
'wapuu-wptavern',
'wapuujlo',
'wapuunder',
'wapuushkin-wapuu',
'wapuutah-wapuu',
'wck-wapuu',
'wct2012',
'wct2013',
'wctokyo_wapuu',
'wapevil-wapuu',
'sweden-wapuu',
'eduwapuu',
'wapmas-wapuu',
'swiss-wapuu',
'bapuu-wapuu',
'benpuu',
'heian-wapuu',
'london2016-wapuu',
'mercenary-wapuu',
'okita-wapuu',
'r2-wapuu',
'r2wapuu',
'shikari-wapuu',
'sunshinecoast-wapuu',
'taekwon-blue-wapuu',
'taekwon-red-wapuu',
'wapumura-kenshin',
'wapuu-alaaf',
'wapuu-ji-chaudhary',
'wapuu-tiger',
);
}