Skip to content

Commit ae8b5e6

Browse files
committed
Emoji: First pass at support in Interactions
1 parent 0a761d4 commit ae8b5e6

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
* Undo API for Outbox items.
1616
* Metadata to New Follower E-Mail.
1717
* Allow Activities on URLs instead of requiring Activity-Objects. This is useful especially for sending Announces and Likes.
18+
* Support for custom emoji in interaction contents and actor names.
1819

1920
### Changed
2021

includes/collection/class-interactions.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ For reasons of data protection, it is not possible to see the followers of other
137137
* Added: Undo API for Outbox items.
138138
* Added: Setting to adjust the number of days Outbox items are kept before being purged.
139139
* Added: Show metadata in the New Follower E-Mail.
140+
* Added: Support for custom emoji in interaction contents and actor names.
140141
* Changed: Outbox now precesses the first batch of followers right away to avoid delays in processing new Activities.
141142
* Changed: Post bulk edits no longer create Outbox items, unless author or post status change.
142143
* Fixed: The Outbox purging routine no longer is limited to deleting 5 items at a time.

tests/includes/collection/class-test-interactions.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,4 +471,60 @@ function () {
471471

472472
remove_all_filters( 'pre_get_remote_metadata_by_actor' );
473473
}
474+
475+
/**
476+
* Test emoji replacement in activity_to_comment.
477+
*
478+
* @covers ::activity_to_comment
479+
* @covers ::replace_custom_emoji
480+
*/
481+
public function test_activity_to_comment_with_emoji() {
482+
$activity = array(
483+
'@context' => array(
484+
'https://www.w3.org/ns/activitystreams',
485+
array(
486+
'Emoji' => 'http://joinmastodon.org/ns#Emoji',
487+
),
488+
),
489+
'id' => 'https://example.com/activities/1',
490+
'type' => 'Note',
491+
'content' => 'Hello world :kappa: and :smile:',
492+
'actor' => $this->user_url,
493+
'object' => array(
494+
'id' => 'https://example.com/objects/1',
495+
'content' => 'Hello world :kappa: and :smile:',
496+
'tag' => array(
497+
array(
498+
'type' => 'Emoji',
499+
'name' => ':kappa:',
500+
'icon' => array(
501+
'type' => 'Image',
502+
'mediaType' => 'image/png',
503+
'url' => 'https://example.com/files/kappa.png',
504+
),
505+
),
506+
array(
507+
'type' => 'Emoji',
508+
'name' => ':smile:',
509+
'icon' => array(
510+
'type' => 'Image',
511+
'mediaType' => 'image/png',
512+
'url' => 'https://example.com/files/smile.png',
513+
),
514+
),
515+
),
516+
),
517+
);
518+
519+
$commentdata = Interactions::activity_to_comment( $activity );
520+
521+
$this->assertStringContainsString(
522+
'<img src="https://example.com/files/kappa.png" alt=":kappa:" class="emoji" />',
523+
$commentdata['comment_content']
524+
);
525+
$this->assertStringContainsString(
526+
'<img src="https://example.com/files/smile.png" alt=":smile:" class="emoji" />',
527+
$commentdata['comment_content']
528+
);
529+
}
474530
}

0 commit comments

Comments
 (0)