-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
executable file
·261 lines (217 loc) · 6.54 KB
/
index.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
<?php
/*
* Plugin Name: WordPress Local Profile Picture
* Plugin URI: https://github.com/pontusab/WordPress-Avatar
* Description: Add local avatars to your profile from WordPress-admin.
* Version: 1.0
* Author: Pontus Abrahamsson
* Author URI: http://pontusab.se
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2013 Pontus Abrahamsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
class WP_avatar
{
/**
* Setup some vars in the object
* @since 1.0
*/
private
$upload_path,
$meta_key,
$avatar_url,
$mime_type,
$formats;
/**
* Name of the form
* @since 1.0
*/
private static $input_field = 'wp_avatar';
/**
* Run all the functions and filters on startup
* @since 1.0
*/
public function __construct()
{
// Plugin path
define( 'WP_AVATAR_URL', plugin_dir_url( __FILE__ ) );
// Set Object vars
$this->meta_key = 'avatar';
$this->formats = array( 'jpg', 'jpeg', 'png', 'gif' );
// Url and path to avatar dir
$this->paths();
// Activation hook
register_activation_hook( __FILE__, array( &$this, 'activation' ) );
// Get translations
load_plugin_textdomain( 'wpa', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
// Actions
add_action( 'admin_init', array( &$this, 'handle_upload' ) );
add_action( 'admin_menu', array( &$this, 'profile_menu' ) );
add_filter( 'get_avatar', array( &$this, 'get_avatar'), 10, 5 );
add_action( 'admin_init', array( &$this, 'scripts') );
}
/**
* Add path and url to avatar folder
* @since 1.0
*/
public function paths()
{
$upload = wp_upload_dir();
$this->avatar_url = $upload['baseurl'] . '/avatars/';
$this->upload_path = $upload['basedir'] . '/avatars/';
}
/**
* Add Scripts to Profile-page
* @since 1.0
*/
public function scripts()
{
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
// Only add on Upload Avatar Page
if( $page == 'upload-avatar' )
{
wp_register_style( 'style', WP_AVATAR_URL . 'assets/style.css' );
wp_register_script( 'script', WP_AVATAR_URL . 'assets/script.js' );
wp_enqueue_style( 'style' );
wp_enqueue_script( 'script' );
}
}
/**
* Add avatars folder on activation
* @since 1.0
*/
public function activation()
{
if( ! file_exists( $this->upload_path ) )
{
mkdir( $this->upload_path, 0777 ,true );
}
}
/**
* Add Pofile menu to Users
* @since 1.0
*/
public function profile_menu()
{
add_submenu_page(
'profile.php',
__('Profile picture', 'wpa'),
__('Profile picture', 'wpa'),
'read',
'upload-avatar',
array( &$this, 'avatar_page' )
);
}
/**
* Submitform for avatar uplaod
* @return Html and form
* @since 1.0
*/
public function avatar_page()
{
$user_id = get_current_user_id();
$output = '<div class="wrap">';
$output .= '<div id="icon-users" class="icon32"></div>';
$output .= '<h2>'. __('Change profile picture', 'wpa') .'</h2>';
$output .= '<div class="avatar-wrap">';
$output .= get_avatar( $user_id, 200 );
$output .= '<form method="post" enctype="multipart/form-data">';;
$output .= '<div class="file-upload button">';
$output .= '<label for="avatar-upload">'. __('Change Profile picture', 'wpa') .'</label>';
$output .= '<input id="avatar-upload" type="file" name="'. self::$input_field .'" />';
$output .= '</div>';
$output .= '<input type="submit" name="save_avatar" value="'. __('Change Profile picture', 'wpa') .'" class="button button-primary">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
echo $output;
}
/**
* Takes care of the upload
* @return mime_type and upload file
* @since 1.0
*/
public function handle_upload()
{
if ( count( $_FILES ) > 0 )
{
require_once( ABSPATH . '/wp-admin/includes/image.php' );
// Set the mime_type
$this->mime_type = $_FILES[self::$input_field]['name'];
// Save and run the magic on avatars
$this->save_avatar( $_FILES[self::$input_field]['tmp_name'], 200 );
}
}
/**
* Save the avatar to disk and update usermeta
* @since 1.0
*/
private function save_avatar( $sourcefile, $size )
{
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
$type = wp_check_filetype( $this->mime_type );
$image = wp_get_image_editor( $sourcefile );
$path_and_name = $this->upload_path . $user->user_login . '_' . $user->ID . '.';
// User have avatar but not the same format
foreach ( $this->formats as $format )
{
if( file_exists( $path_and_name . $format ) )
{
unlink( $path_and_name . $format );
}
}
if ( ! is_wp_error( $image ) )
{
$image->resize( $size, $size, true );
$image->save( $path_and_name . $type['ext'] );
// Save the name of the file to user_meta
update_user_meta( $user_id, 'avatar', $user->user_login . '_' . $user->ID . '.' . $type['ext'] );
}
}
/**
* Override get_avatar with uploaded avatar else default
* @return uploaded avatar else default
* @since 1.0
*/
public function get_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
if( $id_or_email )
{
$avatar = get_user_meta( $id_or_email, $this->meta_key, true );
if( ! empty( $avatar ) )
{
$avatar_path = $this->avatar_url . $avatar .'?s='. $size .'';
}
else
{
$avatar_path = $default;
}
$avatar = "<img alt='{$alt}' src='{$avatar_path}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
return $avatar;
}
return $avatar;
}
}
// Initialize the object
$wpa = new WP_avatar;