-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp-custom-gravatar.php
133 lines (113 loc) · 4.15 KB
/
sp-custom-gravatar.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
<?php
/**
* Plugin Name: Custom Gravatar
* Description: Add custom gravatar field to user profile.
* Version: 1.0.0
* Plugin URI: https://github.com/sunnypixels-io/wp-custom-gravatar
* Author: Alex Zappa at SunnyPixels
* Author URI: https://alex.zappa.dev/
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: sp-custom-gravatar
*/
if (!defined('ABSPATH'))
exit;
if (class_exists('ACF')) {
add_action('acf/include_fields', function () {
if (!function_exists('acf_add_local_field_group')) {
return;
}
acf_add_local_field_group(array(
'key' => 'user_extra_options',
'title' => __('User Extra Options', 'sp-custom-gravatar'),
'fields' => array(
array(
'key' => 'user_extra_options__custom_gravatar',
'label' => __('Custom Gravatar', 'sp-custom-gravatar'),
'name' => 'custom_gravatar',
'aria-label' => '',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'id',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'preview_size' => 'thumbnail',
),
),
'location' => array(
array(
array(
'param' => 'user_form',
'operator' => '==',
'value' => 'all',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
});
function sunnypixels_custom_avatar($avatar, $id_or_email, $size, $default, $alt, $args)
{
$id = sunnypixels_get_user_id($id_or_email);
if (!$id)
return $avatar;
$custom_avatar = get_user_meta($id, 'custom_gravatar', true);
if ($custom_avatar) {
$custom_avatar = wp_get_attachment_image_src($custom_avatar)[0];
$avatar = "<img alt='{$alt}' src='{$custom_avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' {$args['extra_attr']} />";
}
return $avatar;
}
function sunnypixels_custom_avatar_url($url, $id_or_email, $args)
{
$id = sunnypixels_get_user_id($id_or_email);
if (!$id)
return $url;
$custom_avatar = get_user_meta($id, 'custom_gravatar', true);
if ($custom_avatar) {
$custom_avatar = wp_get_attachment_image_src($custom_avatar)[0];
$url = $custom_avatar;
}
return $url;
}
function sunnypixels_get_user_id($id_or_email)
{
if (is_numeric($id_or_email))
return $id_or_email;
if (is_object($id_or_email))
return $id_or_email->user_id;
if (is_string($id_or_email))
return get_user_by('email', $id_or_email) ? get_user_by('email', $id_or_email)->ID : 0;
return 0;
}
add_filter('get_avatar', 'sunnypixels_custom_avatar', 100500, 6);
add_filter('get_avatar_url', 'sunnypixels_custom_avatar_url', 100500, 3);
} else {
add_action('admin_notices', function () {
printf(
'<div class="notice notice-warning is-dismissible"><p>%s.</p></div>',
__('Advanced Custom Fields plugin is required for Custom Gravatar functionality.', 'sp-custom-gravatar')
);
});
}