-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bootstrap.php
executable file
·183 lines (156 loc) · 4.72 KB
/
Bootstrap.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
<?php
/**
* Plugin Name: (WCM) Avatar
* Plugin URI: https://github.com/wecodemore/wcm-avatar
* Description: Allows uploading an attachment in a users profile to be used instead of an avatar
* Author: Franz Josef Kaiser <[email protected]>
* Author URl: http://unserkaiser.com
* Text Domain: wcmavatar
* Domain Path: /lang
*/
/**
* This file is part of the WCM Avatar package.
*
* © Franz Josef Kaiser / wecodemore
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if ( file_exists( __DIR__.'/vendor/autoload.php' ) )
require __DIR__.'/vendor/autoload.php';
use WCM\User\Avatar\Controllers;
use WCM\User\Avatar\Services;
use WCM\User\Avatar\Views;
use WCM\User\Avatar\Models;
use WCM\User\Avatar\Templates;
add_action( 'plugins_loaded', function()
{
// Handles the drag & drop uploader logic for the user avatar
// Use this filter to adjust the meta key with which a theme author
// can fetch the attachment ID to load the avatar
$key = apply_filters( 'wcm.avatar.meta_key', 'user_avatar' );
// When switching the plugin, this can be reused to allow
// access to the data in other plugins
register_meta( 'user', $key, null, null );
// Register the 'user_id' and 'screen_id'
// as additional data for attachment uploading
add_filter( 'upload_post_params', [
new Services\AvatarRegisterMetaService,
'setup'
] );
// Connect user and attachment via their IDs as meta data
add_filter( 'wp_generate_attachment_metadata', [
new Services\AvatarAddMetaService( $key ),
'setup'
], 10, 2 );
// Remove the user meta data when the attachment gets deleted
add_filter( 'delete_attachment', [
new Services\AvatarDeleteMetaService( $key ),
'setup'
] );
// Remove the attachment post when the user gets deleted
add_filter( 'delete_user', [
new Services\AvatarDeleteService( $key ),
'setup'
], 10, 2 );
// Save files when using the "Browser Uploader"
foreach ( [ 'load-profile.php', 'load-user-edit.php', ] as $filter )
{
add_filter( $filter, [
new Services\AvatarBrowserUploaderSaveService( $key ),
'setup'
] );
}
// Allow registering Underscore Templates using the WP Dependency API
add_filter( 'script_loader_tag', [
new Services\UnderscoreTemplateScripts,
'setup'
], 20, 3 );
// Ajax Model
$ajax = new Models\AjaxAware(
$key,
"{$key}_nonce"
);
// Register the AJAX handling scripts, localize the needed data
add_filter( 'admin_enqueue_scripts', [
new Services\AvatarScriptsService(
$ajax,
$key,
plugin_dir_url( __FILE__ ),
plugin_dir_path( __FILE__ )
),
'setup'
] );
// Register the AJAX handler/callback to fetch/delete Attachments
// using the "Multi-File Uploader"
add_filter( "wp_ajax_{$ajax->getAction()}", [
new Services\AvatarFetchDeleteAjaxService( $ajax ),
'setup'
] );
// Adds either the drag & drop uploader for the user avatar or the avatar itself
add_filter( 'all_admin_notices', [
new Views\UploadView(
new Templates\AvatarUploadTemplate( $key ),
new Templates\AvatarDisplayTemplate,
$key
),
'setup'
] );
// Target for the Avatar Backbone filled template
add_action( 'all_admin_notices', function() use ( $key )
{
if ( in_array(
get_current_screen()->base,
[ 'profile', 'user-edit', ]
) )
echo '<div id="tmpl-main--container"></div>';
}, 20 );
// Limit upload size of image to 256 * 512 = 128 kB
add_filter( 'upload_size_limit', [
new Services\AvatarUploadSizeLimit(
'manage_options',
256 * 512,
[ 'profile', 'user-edit', 'media', 'upload', ]
),
'setup'
], 10, 3 );
// Limit allowed MIME types for image uploads
add_filter( 'upload_mimes', [
new Services\AvatarMIMELimitService(
'manage_options',
[ 'jpg|jpeg|jpe', 'gif', 'png', 'webp', ],
[ 'profile', 'user-edit', 'media', 'upload', ]
),
'setup'
], 10, 2 );
# Sizes
# 32 Admin User List Table
// Limit width/height of uploaded images
add_filter( 'wp_handle_upload_prefilter', [
new Services\AvatarDimensionLimitService(),
'setup'
] );
// Add Image to User Columns
// Use the filter to enable this feature
if ( apply_filters( 'wcm.avatar.enable_custom_column', NULL ) )
{
add_filter( 'manage_users_columns', [
new Services\AvatarUserColumnService( $key ),
'setup'
] );
}
// Replace "Gravatar"-Avatar with custom attachment in User Column
// Use the filter to disable this feature
if ( apply_filters( 'wcm.avatar.enable_custom_avatar', TRUE ) )
{
add_filter( 'pre_get_avatar', [
new Services\AvatarReplacementService( $key ),
'setup'
], 20, 3 );
}
// Register stylesheet for Icons and other UI integration
add_filter( 'admin_enqueue_scripts', [
new Services\AdminStylesService( __FILE__ ),
'setup'
] );
} );