-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-custompostavatar.php
343 lines (318 loc) · 11.4 KB
/
class-custompostavatar.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
<?php
/**
* @package custom-post-avatar
*/
class CustomPostAvatar {
protected $custom_post_avatar_user_id = false;
protected $user_path = false;
protected $user_path_url = false;
protected $screen = false;
public function __construct() {
$this->hooks();
}
private function hooks() {
add_action( 'admin_head', array( $this, 'admin_init_later' ) );
add_action( 'wp_ajax_custom-post-avatar-upload-action', array( $this, 'upload_ajax' ) );
add_action( 'show_user_profile', array( $this, 'profile_fields' ), 10, 1 );
add_action( 'edit_user_profile', array( $this, 'profile_fields' ), 10, 1 );
add_action( 'personal_options_update', array( $this, 'save_custom_post_avatar_profile_fields' ), 10, 1 );
add_action( 'edit_user_profile_update', array( $this, 'save_custom_post_avatar_profile_fields' ), 10, 1 );
/* --- */
add_filter( 'posts_where', array( $this, 'exclude_from_media_library' ) );
}
public function admin_init_later() {
$screen = get_current_screen();
$this->screen = $screen->id;
if ( 'user-edit' === $this->screen ) {
if ( isset( $_GET['user_id'] ) ) {
$this->custom_post_avatar_user_id = (int) $_GET['user_id'];
}
} else {
$this->custom_post_avatar_user_id = get_current_user_id();
}
$this->user_path = $this->get_user_path( $this->custom_post_avatar_user_id );
$this->user_path_url = $this->get_user_path_url( $this->custom_post_avatar_user_id );
if ( 'profile' === $this->screen ) {
$this->admin_head();
}
}
public function get_user_dir( $id ) {
if ( ! $id || ! get_userdata( $id ) ) {
return false;
}
$plugin_path = '/avatars';
$user_hash = md5( get_userdata( $id )->user_email );
$user_dir = $plugin_path . '/' . $user_hash;
return $user_dir;
}
private function get_user_path( $id ) {
$user_dir = $this->get_user_dir( $id );
if ( ! $user_dir ) {
return false;
}
$user_path = wp_get_upload_dir()['basedir'] . $user_dir;
if ( ! file_exists( $user_path ) ) {
wp_mkdir_p( $user_path );
}
return $user_path;
}
private function get_user_path_url( $id ) {
$user_path = $this->get_user_path( $id );
if ( ! $user_path ) {
return false;
}
$user_path_url = str_replace( wp_get_upload_dir()['basedir'], wp_get_upload_dir()['baseurl'], $user_path );
return $user_path_url;
}
public function upload_dir_filter( $upload_dir = array() ) {
$curr_user_id = get_current_user_id();
$user_dir = $this->get_user_dir( $curr_user_id );
if ( ! $user_dir ) {
return $upload_dir;
}
return apply_filters(
'custom_post_avatar_upload_dir',
array(
'path' => $upload_dir['basedir'] . $user_dir,
'url' => $upload_dir['baseurl'] . $user_dir,
'subdir' => $user_dir,
'basedir' => $upload_dir['basedir'] . $user_dir,
'baseurl' => $upload_dir['baseurl'] . $user_dir,
'error' => false,
),
$upload_dir
);
}
public function upload_filename_filter( $filename, $filename_raw ) {
$info = pathinfo( $filename );
$extension = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$new_filename = time() . $extension;
if ( $new_filename !== $filename_raw ) {
$new_filename = sanitize_file_name( $new_filename );
}
return $new_filename;
}
public function admin_head() {
$uploader_options = array(
'runtimes' => 'html5,silverlight,flash,html4',
'browse_button' => 'custom-post-avatar-uploader-button',
'container' => 'custom-post-avatar-uploader',
'drop_element' => 'custom-post-avatar-uploader-inner',
'file_data_name' => 'async-upload',
'multiple_queues' => true,
'max_file_size' => wp_max_upload_size() . 'b',
'url' => admin_url( 'admin-ajax.php' ),
'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
'filters' => array(
array(
'title' => __( 'Allowed Files', 'custom-post-avatar' ),
'extensions' => '*',
),
),
'multipart' => true,
'urlstream_upload' => true,
'multi_selection' => true,
'multipart_params' => array(
'_ajax_nonce' => wp_create_nonce( __FILE__ ),
'action' => 'custom-post-avatar-upload-action',
),
);
?>
<script type="text/javascript">
window.wpcpa_uploader_options = <?php echo wp_json_encode( $uploader_options ); ?>;
window.wpcpa_user_path_url = '<?php echo esc_url_raw( $this->user_path_url ); ?>/';
</script>
<?php
}
public function upload_ajax() {
check_ajax_referer( __FILE__ );
if ( current_user_can( 'upload_files' ) ) {
$upload_dir_filter = array( $this, 'upload_dir_filter' );
add_filter( 'upload_dir', $upload_dir_filter, 10 );
$upload_filename_filter = array( $this, 'upload_filename_filter' );
add_filter( 'sanitize_file_name', $upload_filename_filter, 10, 2 );
$response = array();
$id = media_handle_upload(
'async-upload',
0,
array(
'test_form' => true,
'action' => 'custom-post-avatar-upload-action',
)
);
if ( is_wp_error( $id ) ) {
$response['status'] = 'error';
$response['error'] = $id->get_error_messages();
} else {
$src = esc_url( get_post( $id )->guid );
$response['status'] = 'success';
$response['attachment'] = array();
$response['attachment']['id'] = $id;
$response['attachment']['src'] = $src;
}
remove_filter( 'upload_dir', $upload_dir_filter, 10 );
remove_filter( 'sanitize_file_name', $upload_filename_filter, 10, 2 );
}
echo wp_json_encode( $response );
exit;
}
public function profile_fields( $user ) {
wp_nonce_field( basename( __FILE__ ), '_custom_post_avatar_nonce' );
?>
<table class="form-table">
<tr>
<th>
<label for="user_custom_post_avatars"><?php esc_html_e( 'Custom Post Avatar', 'custom-post-avatar' ); ?></label>
</th>
<td>
<div id="custom-post-avatar-uploader" class="custom-post-avatar-uploader multiple">
<div id="custom-post-avatar-uploader-inner" class="custom-post-avatar-uploader-inner">
<div id="custom-post-avatar-list" class="custom-post-avatar-list">
<?php
echo wp_kses(
$this->get_all_avatars_radio(),
array(
'input' => array(
'type' => array(),
'name' => array(),
'value' => array(),
'class' => array(),
'style' => array(),
'checked' => array(),
),
'label' => array(),
'img' => array(
'src' => array(),
'class' => array(),
'alt' => array(),
),
)
);
?>
</div>
<?php if ( 'profile' === get_current_screen()->id ) : ?>
<input id="custom-post-avatar-uploader-button" type="button" value="<?php esc_attr_e( 'Select Files', 'custom-post-avatar' ); ?>" class="custom-post-avatar-uploader-button button browser button-hero">
<span class="ajaxnonce" id="<?php echo esc_attr( wp_create_nonce( __FILE__ ) ); ?>"></span>
<?php endif; ?>
</div>
</div>
<br><span class="description"><?php esc_html_e( 'Upload new avatars and select your default (blue border).', 'custom-post-avatar' ); ?></span>
</td>
</tr>
</table>
<?php
}
public function get_all_avatars() {
if ( function_exists( 'list_files' ) ) {
$files = list_files( $this->user_path );
} else {
$files = list_files_copy( $this->user_path );
}
$count_files = count( $files );
for ( $i = 0; $i < $count_files; ++$i ) {
$files[ $i ] = str_replace( wp_get_upload_dir()['basedir'], wp_get_upload_dir()['baseurl'], $files[ $i ] );
}
return $files;
}
public function get_all_avatars_images() {
$files = $this->get_all_avatars();
$default = $this->get_user_default_avatar_filename( $this->custom_post_avatar_user_id );
$return = '';
$count_files = count( $files );
for ( $i = 0; $i < $count_files; ++$i ) {
$class = '';
$guid = basename( $files[ $i ] );
if ( $default === $guid ) {
$class = ' default';
}
$return .= '<img src="' . $files[ $i ] . '" title="' . $guid . '" class="' . $class . '">';
}
return $return;
}
public function get_all_avatars_radio( $name = 'custom_post_avatar_default', $guid = 'filename', $default = true ) {
$files = $this->get_all_avatars();
$return = '';
$value = '';
$count_files = count( $files );
if ( true === $default ) {
$default = $this->get_user_default_avatar_filename( $this->custom_post_avatar_user_id );
}
for ( $i = 0; $i < $count_files; ++$i ) {
$class = '';
$checked = '';
if ( 'filename' === $guid ) {
$value = basename( $files[ $i ] );
}
if ( 'url' === $guid ) {
$value = $files[ $i ];
}
if ( $default === $value ) {
$class = ' default';
$checked = 'checked="checked"';
}
//$return .= '<img src="' . $files[ $i ] . '" title="' . $guid . '" class="' . $class . '">';
$return .= '<label>';
$return .= '<input type="radio" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" ' . $checked . ' class="' . esc_attr( $class ) . '">';
$return .= '<img src="' . esc_attr( $files[ $i ] ) . '">';
$return .= '</label>';
}
return $return;
}
public function get_default_avatar_url() {
$default = plugin_dir_url( __FILE__ ) . 'default.png';
return $default;
}
public function get_user_default_avatar_url( $id ) {
$user_path = $this->get_user_path( $id );
$user_path_url = $this->get_user_path_url( $id );
$default_by_meta = get_user_meta( $id, 'custom_post_avatar_default', true );
if ( isset( $default_by_meta ) && '' !== $default_by_meta ) {
$default_by_meta = $user_path . '/' . $default_by_meta;
if ( file_exists( $default_by_meta ) ) {
$default_by_meta = str_replace( $user_path, $user_path_url, $default_by_meta );
return $default_by_meta;
}
}
if ( function_exists( 'list_files' ) ) {
$list = list_files( $user_path );
} else {
$list = list_files_copy( $user_path );
}
if ( isset( $list ) && 1 <= count( $list ) ) {
$default_by_list = $list[0];
if ( file_exists( $default_by_list ) ) {
$default_by_list = str_replace( $user_path, $user_path_url, $default_by_list );
return $default_by_list;
}
}
return $this->get_default_avatar_url();
}
public function get_user_default_avatar_filename( $id ) {
$src = $this->get_user_default_avatar_url( $id );
return basename( $src );
}
public function get_user_default_avatar( $id ) {
$src = $this->get_user_default_avatar_url( $id );
return '<img src="' . $src . '">';
}
public function save_custom_post_avatar_profile_fields( $user_id ) {
if ( ! isset( $_POST['_custom_post_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_custom_post_avatar_nonce'], basename( __FILE__ ) ) ) {
return $user_id;
}
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if ( isset( $_POST['custom_post_avatar_default'] ) ) {
update_user_meta( $user_id, 'custom_post_avatar_default', sanitize_text_field( wp_unslash( $_POST['custom_post_avatar_default'] ) ) );
} else {
delete_user_meta( $user_id, 'custom_post_avatar_default' );
}
}
public function exclude_from_media_library( $where ) {
if ( isset( $_POST['action'] ) && ( 'query-attachments' === $_POST['action'] ) ) {
$where .= ' AND guid NOT LIKE "%wp-content/uploads/avatars%"';
}
return $where;
}
}