-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlx-gravatar-block.php
227 lines (202 loc) · 5.43 KB
/
dlx-gravatar-block.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
<?php
/**
* Plugin Name: DLX Gravatar Block
* Plugin URI: https://dlxplugins.com/tutorials/
* Description: A sample Gravatar block for the block editor.
* Version: 1.0.0
* Requires at least: 6.3
* Requires PHP: 7.2
* Author: DLX Plugins
* Author URI: https://dlxplugins.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package DLXGravatarBlock
*/
namespace DLXPlugins\DLXGravatarBlock;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Register the block.
*/
function register_block() {
register_block_type(
plugin_dir_path( __FILE__ ) . 'build/blocks/gravatar-block/block.json',
array(
'render_callback' => __NAMESPACE__ . '\render_block',
)
);
}
/**
* Render the block.
*
* @param array $attributes The block attributes.
*
* @return string The block output.
*/
function render_block( $attributes ) {
$gravatar_hash = $attributes['gravatarHash'] ?? '';
$gravatar_size = $attributes['gravatarSize'] ?? 96;
$alignment = $attributes['align'] ?? 'none';
// Return if no hash is found.
if ( empty( $gravatar_hash ) ) {
return '';
}
// Get CSS classes.
$classes = array(
'wp-block-dlx-gravatar-block',
'align' . $alignment,
);
// Get the avatar based on hash.
$avatar = get_avatar(
$gravatar_hash,
$gravatar_size,
'',
'',
array( 'class' => implode( ' ', $classes ) )
);
return $avatar;
}
// Enqueue the block editor JavaScript.
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\register_block_editor_js' );
/**
* Enqueue the block editor JavaScript.
*/
function register_block_editor_js() {
$deps = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
wp_register_script(
'dlx-gravatar-block-editor',
plugins_url( 'build/index.js', __FILE__ ),
$deps['dependencies'],
$deps['version'],
true,
);
// Get avatar sizes.
$avatar_sizes = rest_get_avatar_sizes();
// Map to label/value pair.
$avatar_sizes = array_map(
function ( $size ) {
return array(
'label' => $size,
'value' => $size,
);
},
$avatar_sizes
);
// Add localized vars we'll need.
wp_localize_script(
'dlx-gravatar-block-editor',
'dlxGravatarBlock',
array(
'apiUrl' => rest_url( 'dlx-gravatar-block/v1/gravatar/' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'avatarSizes' => $avatar_sizes,
)
);
}
// Enqueue the block editor CSS.
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\register_block_css' );
/**
* Enqueue the block editor CSS.
*/
function register_block_css() {
$deps = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
wp_enqueue_style(
'dlx-gravatar-block-editor',
plugins_url( 'build/index.css', __FILE__ ),
array(),
$deps['version'],
'all'
);
}
/**
* Start REST.
*/
add_action( 'rest_api_init', __NAMESPACE__ . '\register_rest_fields' );
/**
* Register REST fields.
*/
function register_rest_fields() {
register_rest_route(
'dlx-gravatar-block/v1',
'/gravatar/(?P<id>.+)',
array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\rest_get_gravatar',
'sanitize_callback' => 'sanitize_text_field',
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
),
);
}
/**
* Get the Gravatar.
*
* @param \WP_REST_Request $request The REST request.
*
* @return \WP_REST_Response The REST response.
*/
function rest_get_gravatar( $request ) {
$maybe_id_or_email = sanitize_text_field( urldecode( $request->get_param( 'id' ) ) ); //Accepts a user ID, Gravatar MD5 hash, username, user email. Assumes urlencoded.
// Call local plugin function get_user to get a user object.
$maybe_user = get_user( $maybe_id_or_email );
// Get avatar URLs.
$avatar_urls = rest_get_avatar_urls( $maybe_id_or_email );
// Format email into hash.
$email_hash = false;
if ( $maybe_user ) {
$maybe_id_or_email = $maybe_user->user_email;
$email_hash = hash( 'sha256', strtolower( $maybe_id_or_email ) ) . '@md5.gravatar.com';
} elseif ( is_email( $maybe_id_or_email ) ) {
// Make sure email isn't a gravatar email.
if ( strpos( $maybe_id_or_email, '@md5.gravatar.com' ) === false ) {
$email_hash = hash( 'sha256', strtolower( $maybe_id_or_email ) ) . '@md5.gravatar.com';
}
}
if ( ! empty( $avatar_urls ) ) {
$return = array(
'avatarUrls' => $avatar_urls,
'emailHash' => $email_hash,
);
return rest_ensure_response( $return );
}
return rest_ensure_response(
new \WP_Error(
'no_avatar',
__( 'No avatar found.', 'dlx-gravatar-block' ),
)
);
}
/**
* Get the user.
*
* @param int|string|object $id_or_email The user ID, email address, or comment object.
*
* @return false|\WP_User The user object.
*/
function get_user( $id_or_email ) {
$id_or_email = sanitize_text_field( $id_or_email );
$user = false;
// Get user data.
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id', (int) $id_or_email );
} elseif ( is_object( $id_or_email ) ) {
$comment = $id_or_email;
if ( empty( $comment->user_id ) ) {
$user = get_user_by( 'id', $comment->user_id );
} else {
$user = get_user_by( 'email', $comment->comment_author_email );
}
} elseif ( is_string( $id_or_email ) ) {
if ( is_email( $id_or_email ) ) {
$user = get_user_by( 'email', $id_or_email );
} else {
$user = get_user_by( 'login', $id_or_email );
}
}
return $user;
}