@@ -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 (
@@ -339,4 +347,34 @@ public static function count_by_type( $post_id, $type ) {
339
347
)
340
348
);
341
349
}
350
+
351
+ /**
352
+ * Replace custom emoji shortcodes with their corresponding emoji.
353
+ *
354
+ * @param string $text The text to process.
355
+ * @param array $activity The activity array containing emoji definitions.
356
+ *
357
+ * @return string The processed text with emoji replacements.
358
+ */
359
+ private static function replace_custom_emoji ( $ text , $ activity ) {
360
+ if ( empty ( $ activity ['tag ' ] ) || ! is_array ( $ activity ['tag ' ] ) ) {
361
+ return $ text ;
362
+ }
363
+
364
+ foreach ( $ activity ['tag ' ] as $ tag ) {
365
+ if ( isset ( $ tag ['type ' ] ) && 'Emoji ' === $ tag ['type ' ] && ! empty ( $ tag ['name ' ] ) && ! empty ( $ tag ['icon ' ]['url ' ] ) ) {
366
+ $ text = str_replace (
367
+ $ tag ['name ' ],
368
+ sprintf (
369
+ '<img src="%s" alt="%s" class="emoji" /> ' ,
370
+ \esc_url ( $ tag ['icon ' ]['url ' ] ),
371
+ \esc_attr ( $ tag ['name ' ] )
372
+ ),
373
+ $ text
374
+ );
375
+ }
376
+ }
377
+
378
+ return $ text ;
379
+ }
342
380
}
0 commit comments