@@ -77,8 +77,8 @@ public static function update_comment( $activity ) {
7777 }
7878
7979 // 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 ' ] ) );
8282
8383 return self ::persist ( $ commentdata , self ::UPDATE );
8484 }
@@ -209,14 +209,22 @@ public static function allowed_comment_html( $allowed_tags, $context = '' ) {
209209 }
210210
211211 // Add `p` and `br` to the list of allowed tags.
212- if ( ! array_key_exists ( 'br ' , $ allowed_tags ) ) {
212+ if ( ! isset ( $ allowed_tags [ 'br ' ] ) ) {
213213 $ allowed_tags ['br ' ] = array ();
214214 }
215215
216- if ( ! array_key_exists ( 'p ' , $ allowed_tags ) ) {
216+ if ( ! isset ( $ allowed_tags [ 'p ' ] ) ) {
217217 $ allowed_tags ['p ' ] = array ();
218218 }
219219
220+ if ( ! isset ( $ allowed_tags ['img ' ] ) ) {
221+ $ allowed_tags ['img ' ] = array (
222+ 'src ' => array (),
223+ 'alt ' => array (),
224+ 'class ' => array (),
225+ );
226+ }
227+
220228 return $ allowed_tags ;
221229 }
222230
@@ -257,9 +265,9 @@ public static function activity_to_comment( $activity ) {
257265 }
258266
259267 $ commentdata = array (
260- 'comment_author ' => \esc_attr ( $ comment_author ),
268+ 'comment_author ' => self :: replace_custom_emoji ( $ comment_author, $ actor ),
261269 'comment_author_url ' => \esc_url_raw ( $ url ),
262- 'comment_content ' => $ comment_content ,
270+ 'comment_content ' => self :: replace_custom_emoji ( $ comment_content, $ activity [ ' object ' ] ) ,
263271 'comment_type ' => 'comment ' ,
264272 'comment_author_email ' => '' ,
265273 'comment_meta ' => array (
@@ -339,4 +347,34 @@ public static function count_by_type( $post_id, $type ) {
339347 )
340348 );
341349 }
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+ }
342380}
0 commit comments