@@ -77,8 +77,8 @@ public static function update_comment( $activity ) {
77
77
}
78
78
79
79
// Found a local comment id.
80
- $ commentdata ['comment_author ' ] = \esc_attr ( $ meta ['name ' ] ? $ meta ['name ' ] : $ meta ['preferredUsername ' ] );
81
- $ commentdata ['comment_content ' ] = \addslashes ( $ activity ['object ' ]['content ' ] );
80
+ $ commentdata ['comment_author ' ] = self :: replace_custom_emoji ( $ meta ['name ' ] ? $ meta ['name ' ] : $ meta ['preferredUsername ' ], $ meta );
81
+ $ commentdata ['comment_content ' ] = \addslashes ( self :: replace_custom_emoji ( $ activity ['object ' ]['content ' ], $ activity [ ' object ' ] ) );
82
82
83
83
return self ::persist ( $ commentdata , self ::UPDATE );
84
84
}
@@ -209,14 +209,22 @@ public static function allowed_comment_html( $allowed_tags, $context = '' ) {
209
209
}
210
210
211
211
// Add `p` and `br` to the list of allowed tags.
212
- if ( ! array_key_exists ( 'br ' , $ allowed_tags ) ) {
212
+ if ( ! isset ( $ allowed_tags [ 'br ' ] ) ) {
213
213
$ allowed_tags ['br ' ] = array ();
214
214
}
215
215
216
- if ( ! array_key_exists ( 'p ' , $ allowed_tags ) ) {
216
+ if ( ! isset ( $ allowed_tags [ 'p ' ] ) ) {
217
217
$ allowed_tags ['p ' ] = array ();
218
218
}
219
219
220
+ if ( ! isset ( $ allowed_tags ['img ' ] ) ) {
221
+ $ allowed_tags ['img ' ] = array (
222
+ 'src ' => array (),
223
+ 'alt ' => array (),
224
+ 'class ' => array (),
225
+ );
226
+ }
227
+
220
228
return $ allowed_tags ;
221
229
}
222
230
@@ -257,9 +265,9 @@ public static function activity_to_comment( $activity ) {
257
265
}
258
266
259
267
$ commentdata = array (
260
- 'comment_author ' => \esc_attr ( $ comment_author ),
268
+ 'comment_author ' => self :: replace_custom_emoji ( $ comment_author, $ actor ),
261
269
'comment_author_url ' => \esc_url_raw ( $ url ),
262
- 'comment_content ' => $ comment_content ,
270
+ 'comment_content ' => self :: replace_custom_emoji ( $ comment_content, $ activity [ ' object ' ] ) ,
263
271
'comment_type ' => 'comment ' ,
264
272
'comment_author_email ' => '' ,
265
273
'comment_meta ' => array (
@@ -318,4 +326,34 @@ function () {
318
326
return $ state ; // Either WP_Comment, false, a WP_Error, 0, or 1!
319
327
}
320
328
}
329
+
330
+ /**
331
+ * Replace custom emoji shortcodes with their corresponding emoji.
332
+ *
333
+ * @param string $text The text to process.
334
+ * @param array $activity The activity array containing emoji definitions.
335
+ *
336
+ * @return string The processed text with emoji replacements.
337
+ */
338
+ private static function replace_custom_emoji ( $ text , $ activity ) {
339
+ if ( empty ( $ activity ['tag ' ] ) || ! is_array ( $ activity ['tag ' ] ) ) {
340
+ return $ text ;
341
+ }
342
+
343
+ foreach ( $ activity ['tag ' ] as $ tag ) {
344
+ if ( isset ( $ tag ['type ' ] ) && 'Emoji ' === $ tag ['type ' ] && ! empty ( $ tag ['name ' ] ) && ! empty ( $ tag ['icon ' ]['url ' ] ) ) {
345
+ $ text = str_replace (
346
+ $ tag ['name ' ],
347
+ sprintf (
348
+ '<img src="%s" alt="%s" class="emoji" /> ' ,
349
+ \esc_url ( $ tag ['icon ' ]['url ' ] ),
350
+ \esc_attr ( $ tag ['name ' ] )
351
+ ),
352
+ $ text
353
+ );
354
+ }
355
+ }
356
+
357
+ return $ text ;
358
+ }
321
359
}
0 commit comments