forked from wp-plugins/custom-user-profile-photo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3five_cupp.php
executable file
·248 lines (206 loc) · 10.7 KB
/
3five_cupp.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
<?php
/*
Plugin Name: Custom User Profile Photo
Plugin URI: http://3five.com
Description: A simple and effective custom WordPress user profile photo plugin. This plugin leverages the WordPress Media Uploader functionality. To use this plugin go to the users tab and select a user. The new field can be found below the password fields for that user.
Author: 3five
Author URI: http://3five.com
Text Domain: custom-user-profile-photo
Domain Path: /languages/
Version: 0.4
*/
/**
* This program has been developed for use with the WordPress Software.
*
* It is distributed as free software with the intent that it will be
* usefull and does not ship with any WARRANTY.
*
* USAGE
* // Default:
* This will override the WordPress get_avatar hook
*
* // Custom placement:
* <?php $imgURL = get_cupp_meta( $user_id, $size ); ?>
* or
* <img src="<?php echo get_cupp_meta( $user_id, $size ); ?>">
*
* Beginner WordPress template editing skill required. Place the above tag in your template and provide the two parameters.
* @param $user_id Default: $post->post_author. Will accept any valid user ID passed into this parameter.
* @param $size Default: 'thumbnail'. Accepts all default WordPress sizes and any custom sizes made by the add_image_size() function.
* @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
*
* Inquiries, suggestions and feedback can be sent to [email protected]
*
* This is plugin is intented for Author, Editor and Admin role post/page authors. Thank you for downloading our plugin.
* We hope this WordPress plugin meets your needs.
*
* Happy coding!
* - 3five
*
* Resources:
* • Steven Slack - http://s2web.staging.wpengine.com/226/
* • Pippin Williamson - https://gist.github.com/pippinsplugins/29bebb740e09e395dc06
* • Mike Jolley - https://gist.github.com/mikejolley/3a3b366cb62661727263#file-gistfile1-php
*
*/
// Enqueue scripts and styles
add_action( 'admin_enqueue_scripts', 'cupp_enqueue_scripts_styles' );
function cupp_enqueue_scripts_styles() {
// Register
wp_register_style( 'cupp_admin_css', plugins_url( 'custom-user-profile-photo/css/styles.css' ), false, '1.0.0', 'all' );
wp_register_script( 'cupp_admin_js', plugins_url( 'custom-user-profile-photo/js/scripts.js' ), array('jquery'), '1.0.0', true );
// Enqueue
wp_enqueue_style( 'cupp_admin_css' );
wp_enqueue_script( 'cupp_admin_js' );
}
// Show the new image field in the user profile page.
add_action( 'show_user_profile', 'cupp_profile_img_fields' );
add_action( 'edit_user_profile', 'cupp_profile_img_fields' );
function cupp_profile_img_fields( $user ) {
if(!current_user_can('upload_files'))
return false;
// vars
$cupp_url = get_the_author_meta( 'cupp_meta', $user->ID );
$cupp_upload_url = get_the_author_meta( 'cupp_upload_meta', $user->ID );
$cupp_upload_edit_url = get_the_author_meta( 'cupp_upload_edit_meta', $user->ID );
if(!$cupp_upload_url){
$btn_text = 'Upload New Image';
} else {
$cupp_upload_edit_url = get_home_url().get_the_author_meta( 'cupp_upload_edit_meta', $user->ID );
$btn_text = 'Change Current Image';
}
?>
<div id="cupp_container">
<h3><?php _e( 'Custom User Profile Photo', 'custom-user-profile-photo' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="cupp_meta"><?php _e( 'Profile Photo', 'custom-user-profile-photo' ); ?></label></th>
<td>
<!-- Outputs the image after save -->
<div id="current_img">
<?php if($cupp_upload_url): ?>
<img src="<?php echo esc_url( $cupp_upload_url ); ?>" class="cupp-current-img">
<div class="edit_options uploaded">
<a class="remove_img"><span>Remove</span></a>
<a href="<?php echo $cupp_upload_edit_url; ?>" class="edit_img" target="_blank"><span>Edit</span></a>
</div>
<?php elseif($cupp_url) : ?>
<img src="<?php echo esc_url( $cupp_url ); ?>" class="cupp-current-img">
<div class="edit_options single">
<a class="remove_img"><span>Remove</span></a>
</div>
<?php else : ?>
<img src="<?php echo plugins_url( 'custom-user-profile-photo/img/placeholder.gif' ); ?>" class="cupp-current-img placeholder">
<?php endif; ?>
</div>
<!-- Select an option: Upload to WPMU or External URL -->
<div id="cupp_options">
<input type="radio" id="upload_option" name="img_option" value="upload" class="tog" checked>
<label for="upload_option">Upload New Image</label><br>
<input type="radio" id="external_option" name="img_option" value="external" class="tog">
<label for="external_option">Use External URL</label><br>
</div>
<!-- Hold the value here if this is a WPMU image -->
<div id="cupp_upload">
<input type="hidden" name="cupp_placeholder_meta" id="cupp_placeholder_meta" value="<?php echo plugins_url( 'custom-user-profile-photo/img/placeholder.gif' ); ?>" class="hidden" />
<input type="hidden" name="cupp_upload_meta" id="cupp_upload_meta" value="<?php echo esc_url_raw( $cupp_upload_url ); ?>" class="hidden" />
<input type="hidden" name="cupp_upload_edit_meta" id="cupp_upload_edit_meta" value="<?php echo esc_url_raw( $cupp_upload_edit_url ); ?>" class="hidden" />
<input type='button' class="cupp_wpmu_button button-primary" value="<?php _e( $btn_text, 'custom-user-profile-photo' ); ?>" id="uploadimage"/><br />
</div>
<!-- Outputs the text field and displays the URL of the image retrieved by the media uploader -->
<div id="cupp_external">
<input type="text" name="cupp_meta" id="cupp_meta" value="<?php echo esc_url_raw( $cupp_url ); ?>" class="regular-text" />
</div>
<!-- Outputs the save button -->
<span class="description"><?php _e( 'Upload a custom photo for your user profile or use a URL to a pre-existing photo.', 'custom-user-profile-photo' ); ?></span>
<p class="description"><?php _e('Update Profile to save your changes.', 'custom-user-profile-photo'); ?></p>
</td>
</tr>
</table><!-- end form-table -->
</div> <!-- end #cupp_container -->
<?php wp_enqueue_media(); // Enqueue the WordPress Media Uploader ?>
<?php }
// Save the new user CUPP url.
add_action( 'personal_options_update', 'cupp_save_img_meta' );
add_action( 'edit_user_profile_update', 'cupp_save_img_meta' );
function cupp_save_img_meta( $user_id ) {
if ( !current_user_can( 'upload_files', $user_id ) )
return false;
// If the current user can edit Users, allow this.
update_user_meta( $user_id, 'cupp_meta', $_POST['cupp_meta'] );
update_user_meta( $user_id, 'cupp_upload_meta', $_POST['cupp_upload_meta'] );
update_user_meta( $user_id, 'cupp_upload_edit_meta', $_POST['cupp_upload_edit_meta'] );
}
/**
* Retrieve the appropriate image size
*
* @param $user_id Default: $post->post_author. Will accept any valid user ID passed into this parameter.
* @param $size Default: 'thumbnail'. Accepts all default WordPress sizes and any custom sizes made by the add_image_size() function.
* @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
*/
function get_cupp_meta( $user_id, $size ) {
global $post;
//allow the user to specify the image size
if (!$size){
$size = 'thumbnail'; // Default image size if not specified.
}
if(!$user_id || !is_numeric( $user_id ) ){
// Here we're assuming that the avatar being called is the author of the post.
// The theory is that when a number is not supplied, this function is being used to
// get the avatar of a post author using get_avatar() and an email address is supplied
// for the $id_or_email parameter. We need an integer to get the custom image so we force that here.
// Also, many themes use get_avatar on the single post pages and pass it the author email address so this
// acts as a fall back.
$user_id = $post->post_author;
}
// get the custom uploaded image
$attachment_upload_url = esc_url( get_the_author_meta( 'cupp_upload_meta', $user_id ) );
// get the external image
$attachment_ext_url = esc_url( get_the_author_meta( 'cupp_meta', $user_id ) );
$attachment_url = '';
$image_url = '';
if($attachment_upload_url){
$attachment_url = $attachment_upload_url;
// grabs the id from the URL using the WordPress function attachment_url_to_postid @since 4.0.0
$attachment_id = attachment_url_to_postid( $attachment_url );
// retrieve the thumbnail size of our image
$image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
$image_url = $image_thumb[0];
} elseif($attachment_ext_url) {
$image_url = $attachment_ext_url;
}
if ( empty($image_url) )
return;
// return the image thumbnail
return $image_url;
}
/**
* WordPress Avatar Filter
*
* Replaces the WordPress avatar with your custom photo using the get_avatar hook.
*/
add_filter( 'get_avatar', 'cupp_avatar' , 1 , 5 );
function cupp_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = false;
$id = false;
if ( is_numeric( $id_or_email ) ) {
$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );
} elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
// $id = (int) $id_or_email;
$user = get_user_by( 'email', $id_or_email );
}
if ( $user && is_object( $user ) ) {
$custom_avatar = get_cupp_meta($id, 'thumbnail');
if (isset($custom_avatar) && !empty($custom_avatar)) {
$avatar = "<img alt='{$alt}' src='{$custom_avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
}
return $avatar;
}
?>