-
Notifications
You must be signed in to change notification settings - Fork 43
/
class-simple-local-avatars.php
1726 lines (1493 loc) · 55.9 KB
/
class-simple-local-avatars.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
/**
* Main Class file: Simple_Local_Avatars
* Adds an avatar upload field to user profiles.
*
* @package SimpleLocalAvatars
*/
/**
* Main SLA Class.
*/
class Simple_Local_Avatars {
/**
* The user ID.
*
* @var int.
*/
private $user_id_being_edited;
/**
* The upload error comment.
*
* @var string.
*/
private $avatar_upload_error;
/**
* The nonce token.
*
* @var string
*/
private $remove_nonce;
/**
* The ratings.
*
* @var array
*/
private $avatar_ratings;
/**
* The meta key a user.
*
* @var string
*/
private $user_key;
/**
* The meta key a user.
*
* @var string he meta key for ratings.
*/
private $rating_key;
/**
* Configured setting values.
*
* @var array
*/
public $options;
/**
* Set up the hooks and default values
*/
public function __construct() {
$this->add_hooks();
$this->options = (array) get_option( 'simple_local_avatars' );
$this->user_key = 'simple_local_avatar';
$this->rating_key = 'simple_local_avatar_rating';
if (
! $this->is_avatar_shared() // Are we sharing avatars?
&& (
( // And either an ajax request not in the network admin.
defined( 'DOING_AJAX' ) && DOING_AJAX
&& isset( $_SERVER['HTTP_REFERER'] ) && ! preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
)
||
( // Or normal request not in the network admin.
( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )
&& ! is_network_admin()
)
)
&& is_multisite()
) {
$this->user_key = sprintf( $this->user_key . '_%d', get_current_blog_id() );
$this->rating_key = sprintf( $this->rating_key . '_%d', get_current_blog_id() );
}
}
/**
* Register actions and filters.
*/
public function add_hooks() {
global $pagenow;
global $wp_version;
add_filter( 'plugin_action_links_' . SLA_PLUGIN_BASENAME, array( $this, 'plugin_filter_action_links' ) );
add_filter( 'pre_get_avatar_data', array( $this, 'get_avatar_data' ), 10, 2 );
add_filter( 'pre_option_simple_local_avatars', array( $this, 'pre_option_simple_local_avatars' ), 10, 1 );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'init', array( $this, 'define_avatar_ratings' ) );
// Load the JS on BE & FE both, in order to support third party plugins like bbPress.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
add_action( 'admin_action_remove-simple-local-avatar', array( $this, 'action_remove_simple_local_avatar' ) );
add_action( 'wp_ajax_assign_simple_local_avatar_media', array( $this, 'ajax_assign_simple_local_avatar_media' ) );
add_action( 'wp_ajax_remove_simple_local_avatar', array( $this, 'action_remove_simple_local_avatar' ) );
add_action( 'user_edit_form_tag', array( $this, 'user_edit_form_tag' ) );
add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) );
add_action( 'wp_ajax_migrate_from_wp_user_avatar', array( $this, 'ajax_migrate_from_wp_user_avatar' ) );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'wp_cli_migrate_from_wp_user_avatar' ) );
}
add_action( 'wp_ajax_sla_clear_user_cache', array( $this, 'sla_clear_user_cache' ) );
add_filter( 'avatar_defaults', array( $this, 'add_avatar_default_field' ) );
if ( version_compare( $wp_version, '5.1', '<' ) ) {
add_action( 'wpmu_new_blog', array( $this, 'set_defaults' ) );
} else {
add_action( 'wp_initialize_site', array( $this, 'set_defaults' ) );
}
if ( 'profile.php' === $pagenow ) {
add_filter( 'media_view_strings', function ( $strings ) {
$strings['skipCropping'] = esc_html__( 'Default Crop', 'simple-local-avatars' );
return $strings;
}, 10, 1 );
}
// Fix: An error occurred cropping the image (https://github.com/10up/simple-local-avatars/issues/141).
if ( isset( $_POST['action'] ) && 'crop-image' === $_POST['action'] && is_admin() && wp_doing_ajax() ) {
add_action( 'plugins_loaded', function () {
remove_all_actions( 'setup_theme' );
} );
}
}
/**
* Determine if plugin is network activated.
*
* @param string $plugin The plugin slug to check.
*
* @return boolean
*/
public static function is_network( $plugin ) {
$plugins = get_site_option( 'active_sitewide_plugins', array() );
if ( is_multisite() && isset( $plugins[ $plugin ] ) ) {
return true;
}
return false;
}
/**
* Get current plugin network mode
*/
public function get_network_mode() {
if ( SLA_IS_NETWORK ) {
return get_site_option( 'simple_local_avatars_mode', 'default' );
}
return 'default';
}
/**
* Determines if settings handling is enforced on a network level
*
* Important: this is only meant for admin UI purposes.
*
* @return boolean
*/
public function is_enforced() {
if (
( ! is_network_admin() && ( SLA_IS_NETWORK && 'enforce' === $this->get_network_mode() ) )
) {
return true;
}
return false;
}
/**
* Determine if avatars should be shared
*
* @return boolean
*/
public function is_avatar_shared() {
if ( ! is_multisite() ) {
return false;
}
if (
! isset( $this->options['shared'] ) // Our shared option doesn't exist.
|| 1 === $this->options['shared'] // Or our shared option is set.
) {
return true;
}
return false;
}
/**
* Add the settings action link to the plugin page.
*
* @param array $links The Action links for the plugin.
*
* @return array
*/
public function plugin_filter_action_links( $links ) {
if ( ! is_array( $links ) ) {
return $links;
}
$links['settings'] = sprintf(
'<a href="%s"> %s </a>',
esc_url( admin_url( 'options-discussion.php' ) ),
__( 'Settings', 'simple-local-avatars' )
);
return $links;
}
/**
* Retrieve the local avatar for a user who provided a user ID, email address or post/comment object.
*
* @param string $avatar Avatar return by original function
* @param int|string|object $id_or_email A user ID, email address, or post/comment object
* @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
* @param array $args Optional. Extra arguments to retrieve the avatar.
*
* @return string <img> tag for the user's avatar
*/
public function get_avatar( $avatar = '', $id_or_email = '', $size = 96, $default = '', $alt = '', $args = array() ) {
return apply_filters( 'simple_local_avatar', get_avatar( $id_or_email, $size, $default, $alt, $args ) );
}
/**
* Filter avatar data early to add avatar url if needed. This filter hooks
* before Gravatar setup to prevent wasted requests.
*
* @since 2.2.0
*
* @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.
*/
public function get_avatar_data( $args, $id_or_email ) {
if ( ! empty( $args['force_default'] ) ) {
return $args;
}
$simple_local_avatar_url = $this->get_simple_local_avatar_url( $id_or_email, $args['size'] );
if ( $simple_local_avatar_url ) {
$args['url'] = $simple_local_avatar_url;
}
// Local only mode
if ( ! $simple_local_avatar_url ) {
$default_url = $this->get_default_avatar_url( $args['size'] );
$avatar_default = get_option( 'avatar_default' );
if ( ! empty( $this->options['only'] ) ) {
$args['url'] = $default_url;
} elseif ( 'simple_local_avatar' === $avatar_default ) {
$args['default'] = $default_url;
}
}
if ( ! empty( $args['url'] ) ) {
$args['found_avatar'] = true;
// If custom alt text isn't passed, pull alt text from the local image.
if ( empty( $args['alt'] ) ) {
$args['alt'] = $this->get_simple_local_avatar_alt( $id_or_email );
}
}
return $args;
}
/**
* Get a user ID from certain possible values.
*
* @since 2.5.0
*
* @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 int|false
*/
public function get_user_id( $id_or_email ) {
$user_id = false;
if ( is_numeric( $id_or_email ) ) {
$user_id = (int) $id_or_email;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
$user_id = (int) $id_or_email->user_id;
} elseif ( $id_or_email instanceof WP_User ) {
$user_id = $id_or_email->ID;
} elseif ( $id_or_email instanceof WP_Post && ! empty( $id_or_email->post_author ) ) {
$user_id = (int) $id_or_email->post_author;
} elseif ( is_string( $id_or_email ) ) {
$user = get_user_by( 'email', $id_or_email );
$user_id = $user ? $user->ID : '';
}
return $user_id;
}
/**
* Get the local avatar user meta.
*
* @param int $user_id User ID.
* @return array Array with avatar data.
*/
public function get_user_local_avatar( $user_id ) {
$local_avatars = get_user_meta( $user_id, $this->user_key, true );
if ( ! is_array( $local_avatars ) || empty( $local_avatars ) ) {
return [];
}
return $local_avatars;
}
/**
* Get local avatar url.
*
* @since 2.2.0
*
* @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.
* @param int $size Requested avatar size.
*/
public function get_simple_local_avatar_url( $id_or_email, $size ) {
$user_id = $this->get_user_id( $id_or_email );
if ( empty( $user_id ) ) {
return '';
}
// Fetch local avatar from meta and make sure it's properly set.
$local_avatars = $this->get_user_local_avatar( $user_id );
if ( ! isset( $local_avatars['full'] ) || empty( $local_avatars['full'] ) ) {
return '';
}
// check rating
$avatar_rating = get_user_meta( $user_id, $this->rating_key, true );
$site_rating = get_option( 'avatar_rating' );
$all_avatar_ratings = ! empty( $this->avatar_ratings ) && is_array( $this->avatar_ratings )
? $this->avatar_ratings
: array();
if ( ! empty( $avatar_rating ) && 'G' !== $avatar_rating && $site_rating ) {
$ratings = array_keys( $all_avatar_ratings );
$site_rating_weight = array_search( $site_rating, $ratings, true );
$avatar_rating_weight = array_search( $avatar_rating, $ratings, true );
if ( false !== $avatar_rating_weight && $avatar_rating_weight > $site_rating_weight ) {
return '';
}
}
/**
* Filter the URL of an avatar for the given user and size.
*
* This filters is applied before Simple Local Avatars validates the value of the
* `$local_avatars` array which comes from the user meta field. This allows the URL
* to be short-circuited, for example by a dynamic image resizing service.
*
* @param string|null $url The URL of the avatar. If null, the URL will be
* generated by Simple Local Avatars.
* @param int $user_id The user ID.
* @param int $size Requested avatar size.
* @param array $local_avatars The local avatars for the user.
* @return string|null The URL of the avatar, or null to allow Simple Local Avatars to
* generate the URL.
*/
$url = apply_filters( 'pre_simple_local_avatar_url', null, $user_id, $size, $local_avatars );
if ( is_string( $url ) ) {
return esc_url( $url );
}
// handle "real" media
if ( ! empty( $local_avatars['media_id'] ) ) {
// If using shared avatars, make sure we validate the URL on the main site.
if ( $this->is_avatar_shared() ) {
$origin_blog_id = isset( $local_avatars['blog_id'] ) && ! empty( $local_avatars['blog_id'] ) ? $local_avatars['blog_id'] : get_main_site_id();
switch_to_blog( $origin_blog_id );
}
$avatar_full_path = get_attached_file( $local_avatars['media_id'] );
if ( $this->is_avatar_shared() ) {
restore_current_blog();
}
// has the media been deleted?
if ( ! $avatar_full_path ) {
return '';
}
}
$size = (int) $size;
// Generate a new size.
if ( ! array_key_exists( $size, $local_avatars ) ) {
$local_avatars[ $size ] = $local_avatars['full']; // just in case of failure elsewhere
// allow automatic rescaling to be turned off
if ( apply_filters( 'simple_local_avatars_dynamic_resize', true ) ) :
$upload_path = wp_upload_dir();
// get path for image by converting URL, unless its already been set, thanks to using media library approach
if ( ! isset( $avatar_full_path ) ) {
$avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
}
// generate the new size
$editor = wp_get_image_editor( $avatar_full_path );
if ( ! is_wp_error( $editor ) ) {
$resized = $editor->resize( $size, $size, true );
if ( ! is_wp_error( $resized ) ) {
$dest_file = $editor->generate_filename();
$saved = $editor->save( $dest_file );
if ( ! is_wp_error( $saved ) ) {
// Transform the destination file path into URL.
$dest_file_url = '';
if ( false !== strpos( $dest_file, $upload_path['basedir'] ) ) {
$dest_file_url = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
} elseif ( is_multisite() && false !== strpos( $dest_file, ABSPATH . 'wp-content/uploads' ) ) {
$dest_file_url = str_replace( ABSPATH . 'wp-content/uploads', network_site_url( '/wp-content/uploads' ), $dest_file );
}
$local_avatars[ $size ] = $dest_file_url;
}
}
}
// save updated avatar sizes
update_user_meta( $user_id, $this->user_key, $local_avatars );
endif;
}
if ( 'http' !== substr( $local_avatars[ $size ], 0, 4 ) ) {
$local_avatars[ $size ] = home_url( $local_avatars[ $size ] );
}
return esc_url( $local_avatars[ $size ] );
}
/**
* Get local avatar alt text.
*
* @since 2.5.0
*
* @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 string
*/
public function get_simple_local_avatar_alt( $id_or_email ) {
$user_id = $this->get_user_id( $id_or_email );
/**
* Filter the default avatar alt text.
*
* @param string $alt Default alt text.
* @return string
*/
$default_alt = apply_filters( 'simple_local_avatars_default_alt', __( 'Avatar photo', 'simple-local-avatars' ) );
if ( empty( $user_id ) ) {
return $default_alt;
}
// Fetch local avatar from meta and make sure we have a media ID.
$local_avatars = $this->get_user_local_avatar( $user_id );
if ( ! isset( $local_avatars['media_id'] ) || empty( $local_avatars['media_id'] ) ) {
$alt = '';
// If no avatar is set, check if we are using a default avatar with alt text.
if ( 'simple_local_avatar' === get_option( 'avatar_default' ) ) {
$default_avatar_id = get_option( 'simple_local_avatar_default', '' );
if ( ! empty( $default_avatar_id ) ) {
$alt = get_post_meta( $default_avatar_id, '_wp_attachment_image_alt', true );
}
}
return $alt ? $alt : $default_alt;
}
$alt = get_post_meta( $local_avatars['media_id'], '_wp_attachment_image_alt', true );
return $alt ? $alt : $default_alt;
}
/**
* Get default avatar url
*
* @since 2.2.0
*
* @param int $size Requested avatar size.
*/
public function get_default_avatar_url( $size ) {
if ( empty( $default ) ) {
$avatar_default = get_option( 'avatar_default' );
if ( empty( $avatar_default ) ) {
$default = 'mystery';
} else {
$default = $avatar_default;
}
}
$host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com';
if ( 'mystery' === $default ) {
$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]')
} elseif ( 'blank' === $default ) {
$default = includes_url( 'images/blank.gif' );
} elseif ( 'gravatar_default' === $default ) {
$default = "$host/avatar/?s={$size}";
} elseif ( 'simple_local_avatar' === $default ) {
$default = "$host/avatar/?d=$default&s={$size}";
$default_avatar_id = get_option( 'simple_local_avatar_default', '' );
if ( ! empty( $default_avatar_id ) ) {
$default = wp_get_attachment_image_url( $default_avatar_id );
}
} else {
$default = "$host/avatar/?d=$default&s={$size}";
}
return $default;
}
/**
* Define the ratings avatar ratings.
*
* The ratings need to be defined after the languages have been loaded so
* they can be translated. This method exists to define the ratings
* after that has been done.
*
* @since 2.7.3
*/
public function define_avatar_ratings() {
/*
* Avatar ratings.
*
* The key should not be translated as it's used by WP Core in it's
* english form (G, PG, etc).
*
* The values should be translated, these include the initial rating
* name and the description for display to users.
*/
$this->avatar_ratings = array(
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
'G' => __( 'G — Suitable for all audiences', 'simple-local-avatars' ),
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'simple-local-avatars' ),
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
'R' => __( 'R — Intended for adult audiences above 17', 'simple-local-avatars' ),
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
'X' => __( 'X — Even more mature than above', 'simple-local-avatars' ),
);
}
/**
* Register admin settings.
*/
public function admin_init() {
$this->define_avatar_ratings();
// upgrade pre 2.0 option
$old_ops = get_option( 'simple_local_avatars_caps' );
if ( $old_ops ) {
if ( ! empty( $old_ops['simple_local_avatars_caps'] ) ) {
update_option( 'simple_local_avatars', array( 'caps' => 1 ) );
}
delete_option( 'simple_local_avatar_caps' );
}
register_setting( 'discussion', 'simple_local_avatars', array( $this, 'sanitize_options' ) );
add_settings_field(
'simple-local-avatars-only',
__( 'Local Avatars Only', 'simple-local-avatars' ),
array( $this, 'avatar_settings_field' ),
'discussion',
'avatars',
array(
'class' => 'simple-local-avatars',
'key' => 'only',
'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars)', 'simple-local-avatars' ),
)
);
add_settings_field(
'simple-local-avatars-caps',
__( 'Local Upload Permissions', 'simple-local-avatars' ),
array( $this, 'avatar_settings_field' ),
'discussion',
'avatars',
array(
'class' => 'simple-local-avatars',
'key' => 'caps',
'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars' ),
)
);
if ( is_multisite() ) {
add_settings_field(
'simple-local-avatars-shared',
__( 'Shared network avatars', 'simple-local-avatars' ),
array( $this, 'avatar_settings_field' ),
'discussion',
'avatars',
array(
'class' => 'simple-local-avatars',
'key' => 'shared',
'desc' => __( 'Uploaded avatars will be shared across the entire network, instead of being unique per site', 'simple-local-avatars' ),
'default' => 1,
)
);
}
add_action( 'load-options-discussion.php', array( $this, 'load_discussion_page' ) );
// This is for network site settings.
if ( SLA_IS_NETWORK && is_network_admin() ) {
add_action( 'load-settings.php', array( $this, 'load_network_settings' ) );
}
add_settings_field(
'simple-local-avatars-migration',
__( 'Migrate Other Local Avatars', 'simple-local-avatars' ),
array( $this, 'migrate_from_wp_user_avatar_settings_field' ),
'discussion',
'avatars'
);
add_settings_field(
'simple-local-avatars-clear',
esc_html__( 'Clear local avatar cache', 'simple-local-avatars' ),
array( $this, 'avatar_settings_field' ),
'discussion',
'avatars',
array(
'key' => 'clear_cache',
'desc' => esc_html__( 'Clear cache of stored avatars', 'simple-local-avatars' ),
)
);
// Save default avatar file.
$this->save_default_avatar_file_id();
}
/**
* Fire code on the Discussion page
*/
public function load_discussion_page() {
add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) );
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
}
/**
* Load needed hooks to handle network settings
*/
public function load_network_settings() {
$this->options = (array) get_site_option( 'simple_local_avatars', array() );
add_action( 'wpmu_options', array( $this, 'show_network_settings' ) );
add_action( 'update_wpmu_options', array( $this, 'save_network_settings' ) );
}
/**
* Show the network settings
*/
public function show_network_settings() {
$mode = $this->get_network_mode();
?>
<h2><?php esc_html_e( 'Simple Local Avatars Settings', 'simple-local-avatars' ); ?></h2>
<table id="simple-local-avatars" class="form-table">
<tr>
<th scope="row">
<?php esc_html_e( 'Mode', 'simple-local-avatars' ); ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text"><?php esc_html_e( 'Mode', 'simple-local-avatars' ); ?></legend>
<label><input name="simple_local_avatars[mode]" type="radio" id="sla-mode-default" value="default"<?php checked( $mode, 'default' ); ?> /> <?php esc_html_e( 'Default to the settings below when creating a new site', 'simple-local-avatars' ); ?></label><br/>
<label><input name="simple_local_avatars[mode]" type="radio" id="sla-mode-enforce" value="enforce"<?php checked( $mode, 'enforce' ); ?> /> <?php esc_html_e( 'Enforce the settings below across all sites', 'simple-local-avatars' ); ?></label><br/>
</fieldset>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Local avatars only', 'simple-local-avatars' ); ?>
</th>
<td>
<?php
$this->avatar_settings_field(
array(
'key' => 'only',
'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars)', 'simple-local-avatars' ),
)
);
?>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Local upload permissions', 'simple-local-avatars' ); ?>
</th>
<td>
<?php
$this->avatar_settings_field(
array(
'key' => 'caps',
'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars' ),
)
);
?>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Shared network avatars', 'simple-local-avatars' ); ?>
</th>
<td>
<?php
$this->avatar_settings_field(
array(
'key' => 'shared',
'desc' => __( 'Uploaded avatars will be shared across the entire network, instead of being unique per site', 'simple-local-avatars' ),
'default' => 1,
)
);
?>
</td>
</tr>
</table>
<?php
}
/**
* Handle saving the network settings
*/
public static function save_network_settings() {
$options = array(
'caps',
'mode',
'only',
'shared',
);
$sanitized = array();
foreach ( $options as $option_name ) {
if ( ! isset( $_POST['simple_local_avatars'][ $option_name ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
continue;
}
switch ( $option_name ) {
case 'mode':
update_site_option( 'simple_local_avatars_mode', sanitize_text_field( $_POST['simple_local_avatars'][ $option_name ] ) );
break;
default:
$sanitized[ $option_name ] = empty( $_POST['simple_local_avatars'][ $option_name ] ) ? 0 : 1;
}
}
update_site_option( 'simple_local_avatars', $sanitized );
}
/**
* Add scripts to the profile editing page
*
* @param string $hook_suffix Page hook
*/
public function enqueue_scripts( $hook_suffix ) {
/**
* Filter the admin screens where we enqueue our scripts.
*
* @param array $screens Array of admin screens.
* @param string $hook_suffix Page hook.
* @return array
*/
$screens = apply_filters( 'simple_local_avatars_admin_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix );
// Allow SLA actions on a bbPress profile edit page at FE.
if ( function_exists( 'bbp_is_user_home_edit' ) && bbp_is_user_home_edit() ) {
$hook_suffix = 'profile.php';
}
if ( ! in_array( $hook_suffix, $screens, true ) ) {
return;
}
if ( current_user_can( 'upload_files' ) ) {
wp_enqueue_media();
}
$user_id = filter_input( INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT );
$user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $user_id;
$this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' );
wp_enqueue_script( 'simple-local-avatars', plugins_url( '', dirname( __FILE__ ) ) . '/dist/simple-local-avatars.js', array( 'jquery' ), SLA_VERSION, true );
wp_localize_script(
'simple-local-avatars',
'i10n_SimpleLocalAvatars',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'user_id' => $user_id,
'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ),
'selectCrop' => __( 'Select avatar and Crop', 'simple-local-avatars' ),
'deleteNonce' => $this->remove_nonce,
'cacheNonce' => wp_create_nonce( 'sla_clear_cache_nonce' ),
'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ),
'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ),
'clearCacheError' => esc_html__( 'Something went wrong while clearing cache, please try again.', 'simple-local-avatars' ),
'insertMediaTitle' => esc_html__( 'Choose default avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ),
'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ),
)
);
}
/**
* Sanitize new settings field before saving
*
* @param array|string $input Passed input values to sanitize
* @return array|string Sanitized input fields
*/
public function sanitize_options( $input ) {
$new_input['caps'] = empty( $input['caps'] ) ? 0 : 1;
$new_input['only'] = empty( $input['only'] ) ? 0 : 1;
if ( is_multisite() ) {
$new_input['shared'] = empty( $input['shared'] ) ? 0 : 1;
}
return $new_input;
}
/**
* Settings field for avatar upload capabilities
*
* @param array $args Field arguments
*/
public function avatar_settings_field( $args ) {
$args = wp_parse_args(
$args,
array(
'key' => '',
'desc' => '',
'default' => 0,
)
);
if ( ! isset( $this->options[ $args['key'] ] ) ) {
$this->options[ $args['key'] ] = $args['default'];
}
if ( 'clear_cache' !== $args['key'] ) {
echo '
<label for="simple-local-avatars-' . esc_attr( $args['key'] ) . '">
<input type="checkbox" name="simple_local_avatars[' . esc_attr( $args['key'] ) . ']" id="simple-local-avatars-' . esc_attr( $args['key'] ) . '" value="1" ' . checked( $this->options[ $args['key'] ], 1, false ) . ' />
' . esc_html( $args['desc'] ) . '
</label>
';
} else {
echo '<button id="clear_cache_btn" class="button delete" name="clear_cache_btn" >' . esc_html__( 'Clear cache', 'simple-local-avatars' ) . '</button><br/>';
echo '<span id="clear_cache_message" style="font-style:italic;font-size:14px;line-height:2;"></span>';
}
// Output warning if needed.
if (
SLA_IS_NETWORK // If network activated.
&& $this->is_enforced() // And in enforce mode.
&& 'shared' === $args['key'] // And we are displaying the last setting.
) {
echo '
<div class="notice notice-warning inline">
<p><strong>' . esc_html__( 'Simple Local Avatar settings are currently enforced across all sites on the network.', 'simple-local-avatars' ) . '</strong></p>
</div>
';
}
}
/**
* Settings field for migrating avatars away from WP User Avatar
*/
public function migrate_from_wp_user_avatar_settings_field() {
printf(
'<p><button type="button" name="%1$s" id="%1$s" class="button button-secondary">%2$s</button></p><p class="%1$s-progress"></p>',
esc_attr( 'simple-local-avatars-migrate-from-wp-user-avatar' ),
esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' )
);
}
/**
* Output new Avatar fields to user editing / profile screen
*
* @param object $profileuser User object
*/
public function edit_user_profile( $profileuser ) {
?>
<div id="simple-local-avatar-section">
<h3><?php esc_html_e( 'Avatar', 'simple-local-avatars' ); ?></h3>
<table class="form-table">
<tr class="upload-avatar-row">
<th scope="row"><label for="simple-local-avatar"><?php esc_html_e( 'Upload Avatar', 'simple-local-avatars' ); ?></label></th>
<td colspan="2">
<div class="right-wrapper" style="display: flex; align-items: center;">
<div id="simple-local-avatar-photo" class="image-container" style="width: 100px; height: 100px; display: flex; align-items: center; justify-content: center; flex-direction: column;">
<?php
add_filter( 'pre_option_avatar_rating', '__return_empty_string' ); // ignore ratings here
echo get_simple_local_avatar( $profileuser->ID );
remove_filter( 'pre_option_avatar_rating', '__return_empty_string' );
?>
<span class="spinner" id="simple-local-avatar-spinner"></span>
</div>
<div class="button-containet" style="padding-left: 20px;">
<?php
$upload_rights = current_user_can( 'upload_files' );
if ( ! $upload_rights ) {
$upload_rights = empty( $this->options['caps'] );
}
if ( $upload_rights ) {
do_action( 'simple_local_avatar_notices' );
wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
$remove_url = add_query_arg(
array(
'action' => 'remove-simple-local-avatar',
'user_id' => $profileuser->ID,
'_wpnonce' => $this->remove_nonce,
)
);
?>
<?php
// if user is author and above hide the choose file option
// force them to use the WP Media Selector
// At FE, show the file input field regardless of the caps.
if ( ! is_admin() || ! current_user_can( 'upload_files' ) ) {
?>
<p style="display: inline-block; width: 26em;">
<span class="description"><?php esc_html_e( 'Choose an image from your computer:', 'simple-local-avatars' ); ?></span><br />
<input type="file" name="simple-local-avatar" id="simple-local-avatar" class="standard-text" />
</p>
<?php } ?>
<p style="width: 28em">
<?php if ( current_user_can( 'upload_files' ) && did_action( 'wp_enqueue_media' ) ) : ?>
<a href="#" class="button hide-if-no-js" id="simple-local-avatar-media"><?php esc_html_e( 'Choose from Media Library', 'simple-local-avatars' ); ?></a>
<?php endif; ?>
<a href="<?php echo esc_url( $remove_url ); ?>" class="button item-delete submitdelete deletion" id="simple-local-avatar-remove" <?php echo empty( $profileuser->simple_local_avatar ) ? ' style="display:none;"' : ''; ?>>
<?php esc_html_e( 'Remove local avatar', 'simple-local-avatars' ); ?>
</a>
</p>
<?php
} else {
if ( empty( $profileuser->simple_local_avatar ) ) {
echo '<span class="description">' . esc_html__( 'No local avatar is set. Set up your avatar at Gravatar.com.', 'simple-local-avatars' ) . '</span>';
} else {
echo '<span class="description">' . esc_html__( 'You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'simple-local-avatars' ) . '</span>';
}
}
?>
</div>
</div>
</td>
</tr>
<tr class="ratings-row">
<th scope="row"><?php esc_html_e( 'Rating', 'simple-local-avatars' ); ?></th>
<td colspan="2">
<fieldset id="simple-local-avatar-ratings" <?php disabled( empty( $profileuser->simple_local_avatar ) ); ?>>
<legend class="screen-reader-text"><span><?php esc_html_e( 'Rating', 'simple-local-avatars' ); ?></span></legend>
<?php
if ( empty( $profileuser->simple_local_avatar_rating ) || ! array_key_exists( $profileuser->simple_local_avatar_rating, $this->avatar_ratings ) ) {
$profileuser->simple_local_avatar_rating = 'G';
}