-
Notifications
You must be signed in to change notification settings - Fork 12
/
avatar-manager.php
1512 lines (1277 loc) · 52.6 KB
/
avatar-manager.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Avatar_Manager
*/
/*
Plugin Name: Avatar Manager
Plugin URI: https://wordpress.org/plugins/avatar-manager/
Description: Avatar Manager for WordPress is a sweet and simple plugin for storing avatars locally and more. Easily.
Version: 1.6.1
Author: Cătălin Dogaru
Author URI: https://profiles.wordpress.org/cdog/
License: GPLv2 or later
Text Domain: avatar-manager
Domain Path: /languages
*/
/*
Copyright © 2013-2016 Cătălin Dogaru
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
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
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
define( 'AVATAR_MANAGER_VERSION', '1.6.1' );
define( 'AVATAR_MANAGER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'AVATAR_MANAGER_AVATAR_UPLOADS', 0 );
define( 'AVATAR_MANAGER_DEFAULT_SIZE', 96 );
/**
* Sets up plugin defaults and makes Avatar Manager available for translation.
*
* @uses load_theme_textdomain() For translation/localization support.
* @uses plugin_basename() For retrieving the basename of the plugin.
*
* @since Avatar Manager 1.0.0
*/
function avatar_manager_init() {
// Makes Avatar Manager available for translation.
load_plugin_textdomain( 'avatar-manager', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', 'avatar_manager_init' );
/**
* Registers sanitization callback and plugin setting fields.
*
* @uses register_setting() For registering a setting and its sanitization
* callback.
* @uses add_settings_field() For registering a settings field to a settings
* page and section.
* @uses __() For retrieving the translated string from the translate().
*
* @since Avatar Manager 1.0.0
*/
function avatar_manager_admin_init() {
// Registers plugin setting and its sanitization callback.
register_setting( 'discussion', 'avatar_manager', 'avatar_manager_sanitize_options' );
// Registers Avatar Uploads settings field under the Settings Discussion
// Screen.
add_settings_field( 'avatar-manager-avatar_uploads', __( 'Avatar Uploads', 'avatar-manager' ), 'avatar_manager_avatar_uploads_settings_field', 'discussion', 'avatars' );
// Registers Default Size settings field under the Settings Discussion
// Screen.
add_settings_field( 'avatar-manager-default-size', __( 'Default Size', 'avatar-manager' ), 'avatar_manager_default_size_settings_field', 'discussion', 'avatars' );
}
add_action( 'admin_init', 'avatar_manager_admin_init' );
/**
* Returns plugin default options.
*
* @since Avatar Manager 1.0.0
*
* @return array Plugin default options.
*/
function avatar_manager_get_default_options() {
$options = array(
'avatar_uploads' => AVATAR_MANAGER_AVATAR_UPLOADS,
'default_size' => AVATAR_MANAGER_DEFAULT_SIZE
);
return $options;
}
/**
* Returns plugin options.
*
* @uses get_option() For getting values for a named option.
* @uses avatar_manager_get_default_options() For retrieving plugin default
* options.
*
* @since Avatar Manager 1.0.0
*
* @return array Plugin options.
*/
function avatar_manager_get_options() {
return get_option( 'avatar_manager', avatar_manager_get_default_options() );
}
/**
* Sanitizes and validates plugin options.
*
* @uses avatar_manager_get_default_options() For retrieving plugin default
* options.
* @uses absint() For converting a value to a non-negative integer.
*
* @since Avatar Manager 1.0.0
*
* @param array $input An associative array with user input.
* @return array Sanitized plugin options.
*/
function avatar_manager_sanitize_options( $input ) {
$options = avatar_manager_get_default_options();
if ( isset( $input['avatar_uploads'] ) && trim( $input['avatar_uploads'] ) )
$options['avatar_uploads'] = trim( $input['avatar_uploads'] ) ? 1 : 0;
if ( isset( $input['default_size'] ) && is_numeric( trim( $input['default_size'] ) ) ) {
$options['default_size'] = absint( trim( $input['default_size'] ) );
if ( $options['default_size'] < 1 )
$options['default_size'] = 1;
elseif ( $options['default_size'] > 512 )
$options['default_size'] = 512;
}
return $options;
}
/**
* Prints Avatar Uploads settings field.
*
* @uses avatar_manager_get_options() For retrieving plugin options.
* @uses _e() For displaying the translated string from the translate().
* @uses checked() For comparing two given values.
*
* @since Avatar Manager 1.0.0
*/
function avatar_manager_avatar_uploads_settings_field() {
// Retrieves plugin options.
$options = avatar_manager_get_options();
?>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php _e( 'Avatar Uploads', 'avatar-manager' ); ?>
</span>
</legend><!-- .screen-reader-text -->
<label>
<input <?php checked( $options['avatar_uploads'], 1, true ); ?> name="avatar_manager[avatar_uploads]" type="checkbox" value="1">
<?php _e( 'Anyone can upload', 'avatar-manager' ); ?>
</label>
</fieldset>
<?php
}
/**
* Prints Default Size settings field.
*
* @uses avatar_manager_get_options() For retrieving plugin options.
* @uses _e() For displaying the translated string from the translate().
*
* @since Avatar Manager 1.0.0
*/
function avatar_manager_default_size_settings_field() {
// Retrieves plugin options.
$options = avatar_manager_get_options();
?>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php _e( 'Default Size', 'avatar-manager' ); ?>
</span>
</legend><!-- .screen-reader-text -->
<label>
<?php _e( 'Default size of the avatar image', 'avatar-manager' ); ?>
<input class="small-text" min="1" name="avatar_manager[default_size]" step="1" type="number" value="<?php echo $options['default_size']; ?>">
</label>
</fieldset>
<?php
}
/**
* Prints Avatar section.
*
* @uses avatar_manager_get_options() For retrieving plugin options.
* @uses is_multisite() For determining whether Multisite support is enabled.
* @uses switch_to_blog() For switching the current blog to a different blog.
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses restore_current_blog() For restoring the current blog.
* @uses remove_filter() For removing a function attached to a specified action
* hook.
* @uses _e() For displaying the translated string from the translate().
* @uses checked() For comparing two given values.
* @uses get_avatar() For retrieving the avatar for a user.
* @uses avatar_manager_get_custom_avatar() For retrieving user custom avatar
* based on user ID.
* @uses current_user_can() For checking whether the current user has a certain
* capability.
* @uses add_query_arg() For retrieving a modified URL (with) query string.
* @uses self_admin_url() For retrieving an admin url link with optional path
* appended.
* @uses wp_nonce_url() For retrieving URL with nonce added to URL query.
* @uses esc_attr_e() For displaying translated text that has been escaped for
* safe use in an attribute.
* @uses did_action() For retrieving the number of times an action is fired.
* @uses __() For retrieving the translated string from the translate().
* @uses esc_attr() For escaping HTML attributes.
*
* @since Avatar Manager 1.0.0
*
* @param array $profileuser User to edit.
*/
function avatar_manager_edit_user_profile( $profileuser ) {
// Retrieves plugin options.
$options = avatar_manager_get_options();
$avatar_type = isset( $profileuser->avatar_manager_avatar_type ) ? $profileuser->avatar_manager_avatar_type : 'gravatar';
if ( isset( $profileuser->avatar_manager_custom_avatar ) ) {
// Determines whether Multisite support is enabled.
if ( is_multisite() ) {
// Switches the current blog to a different blog.
switch_to_blog( $profileuser->avatar_manager_blog_id );
}
// Retrieves attachment meta fields based on attachment ID.
$custom_avatar_rating = get_post_meta( $profileuser->avatar_manager_custom_avatar, '_avatar_manager_custom_avatar_rating', true );
$user_has_custom_avatar = get_post_meta( $profileuser->avatar_manager_custom_avatar, '_avatar_manager_is_custom_avatar', true );
// Determines whether Multisite support is enabled.
if ( is_multisite() ) {
// Restores the current blog.
restore_current_blog();
}
}
if ( ! isset( $custom_avatar_rating ) || empty( $custom_avatar_rating ) )
$custom_avatar_rating = 'G';
if ( ! isset( $user_has_custom_avatar ) || empty( $user_has_custom_avatar ) )
$user_has_custom_avatar = false;
if ( $user_has_custom_avatar ) {
// Removes the function attached to the specified action hook.
remove_filter( 'get_avatar', 'avatar_manager_get_avatar' );
}
?>
<h3>
<?php _e( 'Avatar', 'avatar-manager' ); ?>
</h3>
<table class="form-table" id="avatar-manager">
<tr>
<th>
<?php _e( 'Display this avatar', 'avatar-manager' ); ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php _e( 'Display this avatar', 'avatar-manager' ); ?>
</span><!-- .screen-reader-text -->
</legend>
<label>
<input <?php checked( $avatar_type, 'gravatar', true ); ?> name="avatar_manager_avatar_type" type="radio" value="gravatar">
<?php echo get_avatar( $profileuser->ID, 32, '', false ); ?>
<?php _e( 'Gravatar', 'avatar-manager' ); ?>
</label>
<?php _e( '<a href="http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress" target="_blank">More information</a>', 'avatar-manager' ); ?>
<?php if ( $user_has_custom_avatar ) : ?>
<br>
<label>
<input <?php checked( $avatar_type, 'custom', true ); ?> name="avatar_manager_avatar_type" type="radio" value="custom">
<?php echo avatar_manager_get_custom_avatar( $profileuser->ID, 32, '', false ); ?>
<?php _e( 'Custom', 'avatar-manager' ); ?>
</label>
<?php
if ( current_user_can( 'upload_files' ) || $options['avatar_uploads'] ) {
$href = add_query_arg( array(
'action' => 'update',
'avatar_manager_action' => 'remove-avatar',
'user_id' => $profileuser->ID
),
self_admin_url( IS_PROFILE_PAGE ? 'profile.php' : 'user-edit.php' ) );
?>
<a class="delete" href="<?php echo wp_nonce_url( $href, 'update-user_' . $profileuser->ID ); ?>" onclick="return showNotice.warn();">
<?php _e( 'Delete', 'avatar-manager' ); ?>
</a><!-- .delete -->
<?php
}
?>
<?php endif; ?>
</fieldset>
</td><!-- .avatar-manager -->
</tr>
<?php if ( current_user_can( 'upload_files' ) || $options['avatar_uploads'] ) : ?>
<tr>
<th>
<?php _e( 'Select Image', 'avatar-manager' ); ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php _e( 'Select Image', 'avatar-manager' ); ?>
</span>
</legend><!-- .screen-reader-text -->
<p>
<label class="description" for="avatar-manager-upload">
<?php _e( 'Choose an image from your computer:', 'avatar-manager' ); ?>
</label><!-- .description -->
<br>
<input id="avatar-manager-upload" name="avatar_manager_import" type="file">
<input class="button" name="avatar_manager_submit" type="submit" value="<?php esc_attr_e( 'Upload', 'avatar-manager' ); ?>">
</p>
<?php if ( current_user_can( 'upload_files' ) && did_action( 'wp_enqueue_media' ) ) : ?>
<p>
<label class="description" for="avatar-manager-choose-from-library-link">
<?php _e( 'Or choose an image from your media library:', 'avatar-manager' ); ?>
</label><!-- .description -->
<br>
<?php
$modal_update_href = add_query_arg( array(
'action' => 'update',
'avatar_manager_action' => 'set-avatar',
'user_id' => $profileuser->ID
),
self_admin_url( IS_PROFILE_PAGE ? 'profile.php' : 'user-edit.php' ) );
?>
<a class="button" data-choose="<?php esc_attr_e( 'Choose a Custom Avatar', 'avatar-manager' ); ?>" data-update="<?php esc_attr_e( 'Set as avatar', 'avatar-manager' ); ?>" data-update-link="<?php echo wp_nonce_url( $modal_update_href, 'update-user_' . $profileuser->ID ); ?>" id="avatar-manager-choose-from-library-link">
<?php _e( 'Choose Image', 'avatar-manager' ); ?>
</a><!-- #avatar-manager-choose-from-library-link -->
</p>
<?php endif; ?>
</fieldset>
</td>
</tr>
<?php endif; ?>
<?php if ( $user_has_custom_avatar ) : ?>
<tr>
<th>
<?php _e( 'Avatar Rating', 'avatar-manager' ); ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php _e( 'Avatar Rating', 'avatar-manager' ); ?>
</span>
</legend><!-- .screen-reader-text -->
<?php
$ratings = array(
// Translators: Content suitability rating:
// http://bit.ly/89QxZA
'G' => __( 'G — Suitable for all audiences', 'avatar-manager' ),
// Translators: Content suitability rating:
// http://bit.ly/89QxZA
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'avatar-manager' ),
// Translators: Content suitability rating:
// http://bit.ly/89QxZA
'R' => __( 'R — Intended for adult audiences above 17', 'avatar-manager' ),
// Translators: Content suitability rating:
// http://bit.ly/89QxZA
'X' => __( 'X — Even more mature than above', 'avatar-manager' )
);
foreach ( $ratings as $key => $rating ) {
?>
<label>
<input <?php checked( $custom_avatar_rating, $key, true ); ?> name="avatar_manager_custom_avatar_rating" type="radio" value="<?php echo esc_attr( $key ); ?>">
<?php echo $rating; ?>
</label>
<br>
<?php
}
?>
<span class="description">
<?php _e( 'Choose a rating for your custom avatar.', 'avatar-manager' ); ?>
</span><!-- .description -->
</fieldset>
</td>
</tr>
<?php endif; ?>
</table><!-- .form-table #avatar-manager -->
<?php
}
add_action( 'edit_user_profile', 'avatar_manager_edit_user_profile' );
add_action( 'show_user_profile', 'avatar_manager_edit_user_profile' );
/**
* Enqueues plugin scripts and styles for Users Your Profile Screen.
*
* @uses is_admin() For checking if the Dashboard or the administration panel is
* attempting to be displayed.
* @uses current_user_can() For checking whether the current user has a certain
* capability.
* @uses wp_enqueue_media() For enqueuing all scripts, styles, settings, and
* templates necessary to use all media JavaScript APIs.
* @uses wp_register_style() For registering a CSS style file.
* @uses wp_enqueue_style() For enqueuing a CSS style file.
* @uses wp_register_script() For registering a JS script file.
* @uses wp_enqueue_script() For enqueuing a JS script file.
*
* @since Avatar Manager 1.0.0
*/
function avatar_manager_admin_enqueue_scripts() {
if ( ! defined( 'IS_PROFILE_PAGE' ) )
return;
if ( current_user_can( 'upload_files' ) ) {
// Enqueues all scripts, styles, settings, and templates necessary to
// use all media JavaScript APIs.
wp_enqueue_media();
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
// Registers plugin CSS style file.
wp_register_style( 'avatar-manager', AVATAR_MANAGER_PLUGIN_URL . 'assets/css/avatar-manager' . $suffix . '.css', array(), '1.2.1' );
// Enqueues plugin CSS style file.
wp_enqueue_style( 'avatar-manager' );
// Registers plugin JS script file.
wp_register_script( 'avatar-manager', AVATAR_MANAGER_PLUGIN_URL . 'assets/js/avatar-manager' . $suffix . '.js', array( 'jquery' ), '1.2.1' );
// Enqueues plugin JS script file.
wp_enqueue_script( 'avatar-manager' );
}
add_action( 'admin_enqueue_scripts', 'avatar_manager_admin_enqueue_scripts' );
add_action( 'wp_enqueue_scripts', 'avatar_manager_admin_enqueue_scripts' );
/**
* Generates a file path of an avatar image based on attachment ID and size.
*
* @uses get_attached_file() For retrieving attached file path based on
* attachment ID.
* @uses wp_basename() For i18n friendly version of basename().
*
* @since Avatar Manager 1.5.0
*
* @param int $attachment_id ID of the attachment.
* @param int $size Size of the avatar image.
* @return string The file path to the avatar image.
*/
function avatar_manager_generate_avatar_path( $attachment_id, $size ) {
// Retrieves attached file path based on attachment ID.
$filename = get_attached_file( $attachment_id );
$pathinfo = pathinfo( $filename );
$dirname = $pathinfo['dirname'];
$extension = $pathinfo['extension'];
// i18n friendly version of basename().
$basename = wp_basename( $filename, '.' . $extension );
$suffix = $size . 'x' . $size;
$dest_path = $dirname . '/' . $basename . '-' . $suffix . '.' . $extension;
return $dest_path;
}
/**
* Generates a full URI of an avatar image based on attachment ID and size.
*
* @uses wp_upload_dir() For retrieving path information on the currently
* configured uploads directory.
* @uses avatar_manager_generate_avatar_path() For generating a file path of an
* avatar image based on attachment ID and size.
*
* @since Avatar Manager 1.5.0
*
* @param int $attachment_id ID of the attachment.
* @param int $size Size of the avatar image.
* @return string The full URI to the avatar image.
*/
function avatar_manager_generate_avatar_url( $attachment_id, $size ) {
// Retrieves path information on the currently configured uploads directory.
$upload_dir = wp_upload_dir();
// Generates a file path of an avatar image based on attachment ID and size.
$path = avatar_manager_generate_avatar_path( $attachment_id, $size );
return str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $path );
}
/**
* Generates a resized copy of the specified avatar image.
*
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses avatar_manager_generate_avatar_path() For generating a file path of an
* avatar image based on attachment ID and size.
* @uses get_attached_file() For retrieving attached file path based on
* attachment ID.
* @uses wp_get_image_editor() For retrieving a WP_Image_Editor instance and
* loading a file into it.
* @uses is_wp_error() For checking whether the passed variable is a WordPress
* Error.
* @uses do_action() For calling the functions added to an action hook.
*
* @since Avatar Manager 1.0.0
*
* @param string $attachment_id ID of the avatar image to resize.
* @param int $size Size of the new avatar image.
* @return bool True if a new file is created; false if the file already exists.
*/
function avatar_manager_resize_avatar( $attachment_id, $size ) {
// Retrieves attachment meta field based on attachment ID.
$custom_avatar = get_post_meta( $attachment_id, '_avatar_manager_custom_avatar', true );
if ( isset( $custom_avatar[ $size ] ) )
return $custom_avatar[ $size ];
// Generates a file path of an avatar image based on attachment ID and size.
$dest_path = avatar_manager_generate_avatar_path( $attachment_id, $size );
if ( file_exists( $dest_path ) ) {
$skip = true;
} else {
// Retrieves attached file path based on attachment ID.
$path = get_attached_file( $attachment_id );
// Retrieves a WP_Image_Editor instance and loads a file into it.
$image = wp_get_image_editor( $path );
if ( ! is_wp_error( $image ) ) {
// Resizes current image.
$image->resize( $size, $size, true );
// Saves current image to file.
$image->save( $dest_path );
$skip = false;
}
}
// Calls the functions added to avatar_manager_resize_avatar action hook.
do_action( 'avatar_manager_resize_avatar', $attachment_id, $size );
return $skip;
}
/**
* Sets user's avatar.
*
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses avatar_manager_get_options() For retrieving plugin options.
* @uses avatar_manager_resize_avatar() For generating a resized copy of the
* specified avatar image.
* @uses update_post_meta() For updating attachment meta fields.
* @uses get_user_meta() For retrieving user meta fields.
* @uses avatar_manager_delete_avatar() For deleting an avatar image based on
* user ID.
* @uses update_user_meta() For updating user meta fields.
* @uses is_multisite() For determining whether Multisite support is enabled.
* @uses get_current_blog_id() For retrieving the current blog id.
*
* @since Avatar Manager 1.6.0
*
* @param int $user_id ID of the user.
* @param int $attachment_id ID of the attachment.
*/
function avatar_manager_set_avatar( $user_id, $attachment_id ) {
// Retrieves attachment meta field based on attachment ID.
$meta_avatar = get_post_meta( $attachment_id, '_avatar_manager_is_custom_avatar', true );
if ( empty( $meta_avatar ) ) {
// Retrieves plugin options.
$options = avatar_manager_get_options();
// Generates a resized copy of the avatar image.
$custom_avatar[ $options['default_size'] ] = avatar_manager_resize_avatar( $attachment_id, $options['default_size'] );
// Updates attachment meta fields based on attachment ID.
update_post_meta( $attachment_id, '_avatar_manager_custom_avatar', $custom_avatar );
update_post_meta( $attachment_id, '_avatar_manager_custom_avatar_rating', 'G' );
update_post_meta( $attachment_id, '_avatar_manager_is_custom_avatar', true );
}
// Retrieves user meta field based on user ID.
$custom_avatar = get_user_meta( $user_id, 'avatar_manager_custom_avatar', true );
if ( ! empty( $custom_avatar ) && $custom_avatar != $attachment_id ) {
// Deletes user's old avatar image.
avatar_manager_delete_avatar( $user_id );
}
// Updates user meta fields based on user ID.
update_user_meta( $user_id, 'avatar_manager_avatar_type', 'custom' );
update_user_meta( $user_id, 'avatar_manager_custom_avatar', $attachment_id );
// Determines whether Multisite support is enabled.
if ( is_multisite() ) {
// Retrieves the current blog id.
update_user_meta( $user_id, 'avatar_manager_blog_id', get_current_blog_id() );
}
}
/**
* Deletes an avatar image based on user ID.
*
* @uses get_user_meta() For retrieving user meta fields.
* @uses is_multisite() For determining whether Multisite support is enabled.
* @uses switch_to_blog() For switching the current blog to a different blog.
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses avatar_manager_generate_avatar_path() For generating a file path of an
* avatar image based on attachment ID and size.
* @uses delete_post_meta() For deleting attachment meta fields.
* @uses restore_current_blog() For restoring the current blog.
* @uses delete_user_meta() For deleting user meta fields.
* @uses do_action() For calling the functions added to an action hook.
*
* @since Avatar Manager 1.0.0
*
* @param int $user_id ID of the user.
* @return bool Operation status.
*/
function avatar_manager_delete_avatar( $user_id ) {
// Retrieves user meta field based on user ID.
$attachment_id = get_user_meta( $user_id, 'avatar_manager_custom_avatar', true );
if ( empty( $attachment_id ) )
return false;
// Determines whether Multisite support is enabled.
if ( is_multisite() ) {
// Switches the current blog to a different blog.
switch_to_blog( get_user_meta( $user_id, 'avatar_manager_blog_id', true ) );
}
// Retrieves attachment meta field based on attachment ID.
$custom_avatar = get_post_meta( $attachment_id, '_avatar_manager_custom_avatar', true );
if ( is_array( $custom_avatar ) ) {
foreach ( $custom_avatar as $size => $skip ) {
if ( ! $skip ) {
// Generates a file path of an avatar image based on attachment
// ID and size.
$file = avatar_manager_generate_avatar_path( $attachment_id, $size );
@unlink( $file );
}
}
}
// Deletes attachment meta fields based on attachment ID.
delete_post_meta( $attachment_id, '_avatar_manager_custom_avatar' );
delete_post_meta( $attachment_id, '_avatar_manager_custom_avatar_rating' );
delete_post_meta( $attachment_id, '_avatar_manager_is_custom_avatar' );
// Determines whether Multisite support is enabled.
if ( is_multisite() ) {
// Restores the current blog.
restore_current_blog();
}
// Deletes user meta fields based on user ID.
delete_user_meta( $user_id, 'avatar_manager_avatar_type' );
delete_user_meta( $user_id, 'avatar_manager_custom_avatar' );
// Determines whether Multisite support is enabled.
if ( is_multisite() )
delete_user_meta( $user_id, 'avatar_manager_blog_id' );
// Calls the functions added to avatar_manager_delete_avatar action hook.
do_action( 'avatar_manager_delete_avatar', $user_id );
return true;
}
add_action( 'delete_user', 'avatar_manager_delete_avatar' );
add_action( 'wpmu_delete_user', 'avatar_manager_delete_avatar' );
/**
* Deletes an avatar image based on attachment ID.
*
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses get_users() For retrieving an array of users matching the criteria
* given in $args.
* @uses avatar_manager_delete_avatar() For deleting an avatar image based on
* user ID.
*
* @since Avatar Manager 1.5.0
*
* @param int $attachment_id ID of the attachment.
*/
function avatar_manager_delete_attachment( $attachment_id ) {
// Retrieves attachment meta field based on attachment ID.
$meta_avatar = get_post_meta( $attachment_id, '_avatar_manager_is_custom_avatar', true );
if ( empty( $meta_avatar ) )
return;
// An associative array with criteria to match.
$args = array(
'meta_key' => 'avatar_manager_custom_avatar',
'meta_value' => $attachment_id
);
// Retrieves an array of users matching the criteria given in $args.
$users = get_users( $args );
foreach ( $users as $user ) {
// Deletes an avatar image based on user ID.
avatar_manager_delete_avatar( $user->ID );
}
}
add_action( 'delete_attachment', 'avatar_manager_delete_attachment' );
/**
* Updates user profile based on user ID.
*
* @uses sanitize_text_field() For sanitizing a string from user input or from
* the database.
* @uses update_user_meta() For updating user meta fields.
* @uses get_user_meta() For retrieving user meta fields.
* @uses update_post_meta() For updating attachment meta fields.
* @uses wp_handle_upload() For handling PHP uploads in WordPress.
* @uses wp_die() For killing WordPress execution and displaying HTML error
* message.
* @uses __() For retrieving the translated string from the translate().
* @uses wp_insert_attachment() For inserting an attachment into the media
* library.
* @uses wp_generate_attachment_metadata() For generating metadata for an
* attachment.
* @uses wp_update_attachment_metadata() For updating metadata for an
* attachment.
* @uses avatar_manager_set_avatar() For setting user's avatar.
* @uses avatar_manager_delete_avatar() For deleting an avatar image based on
* user ID.
* @uses get_edit_user_link() For getting the link to the user's edit profile
* page in the WordPress admin.
* @uses add_query_arg() For retrieving a modified URL (with) query string.
* @uses wp_redirect() For redirecting the user to a specified absolute URI.
*
* @since Avatar Manager 1.0.0
*
* @param int $user_id ID of the user to update.
*/
function avatar_manager_edit_user_profile_update( $user_id ) {
// Sanitizes the string from user input.
$avatar_type = isset( $_POST['avatar_manager_avatar_type'] ) ? sanitize_text_field( $_POST['avatar_manager_avatar_type'] ) : 'gravatar';
// Updates user meta field based on user ID.
update_user_meta( $user_id, 'avatar_manager_avatar_type', $avatar_type );
// Retrieves user meta field based on user ID.
$attachment_id = get_user_meta( $user_id, 'avatar_manager_custom_avatar', true );
if ( ! empty( $attachment_id ) ) {
// Sanitizes the string from user input.
$custom_avatar_rating = isset( $_POST['avatar_manager_custom_avatar_rating'] ) ? sanitize_text_field( $_POST['avatar_manager_custom_avatar_rating'] ) : 'G';
// Updates attachment meta field based on attachment ID.
update_post_meta( $attachment_id, '_avatar_manager_custom_avatar_rating', $custom_avatar_rating );
}
if ( isset( $_POST['avatar_manager_submit'] ) && $_POST['avatar_manager_submit'] ) {
if ( ! function_exists( 'wp_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/file.php' );
if ( ! function_exists( 'wp_generate_attachment_metadata' ) )
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// An associative array with allowed MIME types.
$mimes = array(
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff'
);
// An associative array to override default variables.
$overrides = array(
'mimes' => $mimes,
'test_form' => false
);
// Handles PHP uploads in WordPress.
$file_attr = wp_handle_upload( $_FILES['avatar_manager_import'], $overrides );
if ( isset( $file_attr['error'] ) ) {
// Kills WordPress execution and displays HTML error message.
wp_die( $file_attr['error'], __( 'Image Upload Error', 'avatar-manager' ) );
}
// An associative array about the attachment.
$attachment = array(
'guid' => $file_attr['url'],
'post_content' => $file_attr['url'],
'post_mime_type' => $file_attr['type'],
'post_title' => basename( $file_attr['file'] )
);
// Inserts the attachment into the media library.
$attachment_id = wp_insert_attachment( $attachment, $file_attr['file'] );
// Generates metadata for the attachment.
$attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $file_attr['file'] );
// Updates metadata for the attachment.
wp_update_attachment_metadata( $attachment_id, $attachment_metadata );
// Sets user's avatar.
avatar_manager_set_avatar( $user_id, $attachment_id );
}
if ( isset( $_GET['avatar_manager_action'] ) && $_GET['avatar_manager_action'] ) {
global $wp_http_referer;
$action = $_GET['avatar_manager_action'];
switch ( $action ) {
case 'set-avatar':
if ( isset( $_GET['avatar_manager_attachment_id'] ) ) {
// Sets user's avatar.
avatar_manager_set_avatar( $user_id, absint( $_GET['avatar_manager_attachment_id'] ) );
}
break;
case 'remove-avatar':
// Deletes an avatar image based on user ID.
avatar_manager_delete_avatar( $user_id );
break;
}
// Gets the link to the user's edit profile page in the WordPress admin.
$edit_user_link = get_edit_user_link( $user_id );
// Retrieves a modified URL (with) query string.
$redirect = add_query_arg( 'updated', true, $edit_user_link );
if ( $wp_http_referer ) {
// Retrieves a modified URL (with) query string.
$redirect = add_query_arg( 'wp_http_referer', urlencode( $wp_http_referer ), $redirect );
}
// Redirects the user to a specified absolute URI.
wp_redirect( $redirect );
exit;
}
}
add_action( 'edit_user_profile_update', 'avatar_manager_edit_user_profile_update' );
add_action( 'personal_options_update', 'avatar_manager_edit_user_profile_update' );
/**
* Returns user custom avatar based on user ID.
*
* @uses get_option() For getting values for a named option.
* @uses avatar_manager_get_options() For retrieving plugin options.
* @uses get_userdata() For retrieving user data by user ID.
* @uses is_ssl() For checking if SSL is being used.
* @uses add_query_arg() For retrieving a modified URL (with) query string.
* @uses esc_attr() For escaping HTML attributes.
* @uses get_user_meta() For retrieving user meta fields.
* @uses is_multisite() For determining whether Multisite support is enabled.
* @uses switch_to_blog() For switching the current blog to a different blog.
* @uses get_post_meta() For retrieving attachment meta fields.
* @uses avatar_manager_resize_avatar() For generating a resized copy of the
* specified avatar image.
* @uses update_post_meta() For updating attachment meta fields.
* @uses avatar_manager_generate_avatar_url() For generating a full URI of an
* avatar image based on attachment ID and size.
* @uses restore_current_blog() For restoring the current blog.
* @uses apply_filters() For calling the functions added to a filter hook.
*
* @since Avatar Manager 1.0.0
*
* @param int $user_id ID of the user.
* @param int $size Size of the avatar image.
* @param string $default URL to a default image to use if no avatar is
* available.
* @param string $alt Alternative text to use in image tag. Defaults to blank.
* @return string <img> tag for the user's avatar.
*/
function avatar_manager_get_custom_avatar( $user_id, $size = '', $default = '', $alt = false ) {
// Returns if showing avatars is not enabled.
if ( ! get_option( 'show_avatars' ) )
return false;
// Retrieves plugin options.
$options = avatar_manager_get_options();
if ( empty( $size ) || ! is_numeric( $size ) ) {
$size = $options['default_size'];
} else {
$size = absint( $size );
if ( $size < 1 )
$size = 1;
elseif ( $size > 512 )
$size = 512;
}
// Retrieves user data by user ID.
$user = get_userdata( $user_id );
// Returns if no user data was retrieved.
if ( empty( $user ) )
return false;
$email = $user->user_email;
if ( empty( $default ) ) {
// Retrieves values for the named option.
$avatar_default = get_option( 'avatar_default' );
if ( empty( $avatar_default ) )
$default = 'mystery';
else
$default = $avatar_default;
}
$email_hash = md5( strtolower( trim( $email ) ) );
if ( is_ssl() )
$host = 'https://secure.gravatar.com';
else
$host = sprintf( 'http://%d.gravatar.com', ( hexdec( $email_hash[0] ) % 2 ) );
if ( $default == 'mystery' ) {
$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
} elseif ( $default == 'gravatar_default' ) {
$default = '';
} elseif ( strpos( $default, 'http://' ) === 0 ) {
// Retrieves a modified URL (with) query string.
$default = add_query_arg( 's', $size, $default );
}
if ( $alt === false ) {
$alt = '';
} else {
// Escapes HTML attributes.
$alt = esc_attr( $alt );
}
// Retrieves values for the named option.
$avatar_rating = get_option( 'avatar_rating' );
// Retrieves user meta field based on user ID.
$attachment_id = get_user_meta( $user_id, 'avatar_manager_custom_avatar', true );
// Returns if no attachment ID was retrieved.
if ( empty( $attachment_id ) )
return false;
// Determine whether Multisite support is enabled.
if ( is_multisite() ) {
// Switches the current blog to a different blog.
switch_to_blog( get_user_meta( $user_id, 'avatar_manager_blog_id', true ) );
}
// Retrieves attachment meta field based on attachment ID.
$custom_avatar_rating = get_post_meta( $attachment_id, '_avatar_manager_custom_avatar_rating', true );
$ratings['G'] = 1;
$ratings['PG'] = 2;
$ratings['R'] = 3;
$ratings['X'] = 4;
if ( $ratings[ $custom_avatar_rating ] <= $ratings[ $avatar_rating ] ) {
// Retrieves attachment meta field based on attachment ID.
$custom_avatar = get_post_meta( $attachment_id, '_avatar_manager_custom_avatar', true );
if ( ! isset( $custom_avatar[ $size ] ) ) {
// Generates a resized copy of the avatar image.
$custom_avatar[ $size ] = avatar_manager_resize_avatar( $attachment_id, $size );
// Updates attachment meta field based on attachment ID.
update_post_meta( $attachment_id, '_avatar_manager_custom_avatar', $custom_avatar );
}
// Generates a full URI of an avatar image based on attachment ID and
// size.
$src = avatar_manager_generate_avatar_url( $attachment_id, $size );
$custom_avatar = '<img alt="' . $alt . '" class="avatar avatar-' . $size . ' photo avatar-default" height="' . $size . '" src="' . $src . '" width="' . $size . '">';
} else {