-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp-local-avatars.php
170 lines (110 loc) · 4.42 KB
/
pp-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
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// avoid broken avatar icons if anon comments are allowed
function pp_pre_get_avatar( $avatar, $id_or_email, $args ) {
if ( isset( $id_or_email->user_id ) ) {
if ( $id_or_email->user_id === '0' ) {
$default = get_option('avatar_default');
if ( $default == 'identicon_local' ) {
$mystery_man = '<img src="' . site_url() . '/wp-content/plugins/buddypress/bp-core/images/Xmystery-man.jpg" class="avatar user-28-avatar avatar-74 photo" width="74" height="74">';
$avatar = apply_filters( 'pp_local_avatars_anon', $mystery_man );
}
}
}
return $avatar;
}
add_filter( 'pre_get_avatar', 'pp_pre_get_avatar', 15, 3 );
// settings in wp-admin
function pp_lc_add_settings() {
$show = get_option('show_avatars');
if ( $show ) {
add_filter( 'avatar_defaults', 'pp_lc_add_avatar_default_option', 11, 1 );
add_filter( 'default_avatar_select', 'pp_lc_add_avatar_default_option_img', 11, 1 );
$default_avatar = get_option('avatar_default');
if ( $default_avatar == 'identicon_local' )
pp_lc_add_settings_section();
}
}
add_action('admin_init', 'pp_lc_add_settings');
// add an option in Settings > Discussion > Avatars
function pp_lc_add_avatar_default_option( $avatar_defaults ) {
$avatar_defaults['identicon_local'] = __('BuddyPress Identicon (Generated and Stored Locally)');
return $avatar_defaults;
}
// add an icon to the option in Settings > Discussion > Avatars
function pp_lc_add_avatar_default_option_img( $avatar_list ) {
//var_dump( $avatar_list );
$str_array = array( //'http://0.gravatar.com/avatar/ffd294ab5833ba14aaf175f9acc71cc4?s=64&d=identicon_local&r=g&forcedefault=1 2x',
//'http://0.gravatar.com/avatar/ffd294ab5833ba14aaf175f9acc71cc4?s=32&d=identicon_local&r=g&forcedefault=1',
//'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=64&d=identicon_local&r=g&forcedefault=1 2x',
//'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=32&d=identicon_local&r=G&forcedefault=1',
//'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=32&d=identicon_local&f=y&r=g',
//'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=64&d=identicon_local&f=y&r=g 2x'
);
$icon = plugins_url( 'icon.png', __FILE__ );
$avatar_list = str_ireplace($str_array, $icon, $avatar_list);
return $avatar_list;
}
// Add Bulk Generation section to Settings > Discussion > Avatars
function pp_lc_add_settings_section() {
add_settings_section(
'generate_avatars',
'Bulk Generate',
'pp_lc_generate_avatars_callback',
'discussion'
);
}
function pp_lc_generate_avatars_callback( $arg ) {
?>
<table class="form-table">
<tr>
<th scope="row"><?php _e('BuddyPress Avatars'); ?></th>
<td><fieldset>
<label for="gen_avatars">
Generate Identicons and store them locally for all BP Members and Groups without an Avatar. <br/><br/>
If you have a large number of members without Avatars, <em>this may take too long</em>. <br/><br/>
<a href="<?php print wp_nonce_url(admin_url('options-discussion.php?task=bulk-generate'), 'bulk_gen', 'pp_nonce');?>">Generate Identicons</a>
</label>
</fieldset></td>
</tr>
</table>
<?php
}
/**
* create class instance
* maybe bulk generate avatars
* uses the bp_core_set_avatar_globals hook via bp_setup_globals
*/
function pp_lc_load_class() {
global $wpdb;
$default = get_option('avatar_default');
if ( $default == 'identicon_local' ) {
$instance = PP_Local_Avatars::get_instance();
}
if ( is_admin() ) {
if ( isset( $_GET['task'] ) && $_GET['task'] == 'bulk-generate' ) {
if ( ! wp_verify_nonce($_GET['pp_nonce'], 'bulk_gen') ) {
die( 'Local Avatars - Security Check Fail' );
} else {
$users = get_users( array( 'fields' => 'ID' ) );
foreach ( $users as $user ) {
$instance->create( $user );
}
$group_ids = $wpdb->get_col( "SELECT id FROM {$wpdb->prefix}bp_groups" );
foreach ( $group_ids as $group_id ) {
$instance->group_create( $group_id );
}
wp_redirect( admin_url( '/options-discussion.php?avs_gen=1' ) );
exit;
}
}
}
}
add_action( 'bp_core_set_avatar_globals', 'pp_lc_load_class', 100 );
function pp_lc_avatars_admin_notice() {
if ( ! empty( $_GET['avs_gen'] ) ) {
echo '<div class="updated"><p>Avatars have been generated.</p></div>';
}
}
add_action('admin_notices', 'pp_lc_avatars_admin_notice');