Skip to content

Commit a74e8fc

Browse files
committed
Emoji: First pass at support in Interactions
1 parent 3dac6c1 commit a74e8fc

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* Support for custom emoji in interaction contents and actor names
13+
1014
### Fixed
1115

1216
* Undefined array key warnings in various places

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(
@@ -318,4 +326,34 @@ function () {
318326
return $state; // Either WP_Comment, false, a WP_Error, 0, or 1!
319327
}
320328
}
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+
}
321359
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ For reasons of data protection, it is not possible to see the followers of other
134134

135135
= Unreleased =
136136

137+
* Added: Support for custom emoji in interaction contents and actor names
137138
* Fixed: Undefined array key warnings in various places
138139

139140
= 4.6.0 =

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function create_test_rich_object( $id = 'https://example.com/123' ) {
118118
'id' => $id,
119119
'url' => 'https://example.com/example',
120120
'inReplyTo' => $this->post_permalink,
121-
'content' => 'Hello<br />example<p>example</p><img src="https://example.com/image.jpg" />',
121+
'content' => 'Hello<br />example<p>example</p><video src="https://example.com/image.jpg"></video>',
122122
),
123123
);
124124
}
@@ -263,4 +263,60 @@ public function test_get_interaction_by_id() {
263263
$this->assertIsArray( $interactions );
264264
$this->assertEquals( $comment->comment_ID, $interactions[0]->comment_ID );
265265
}
266+
267+
/**
268+
* Test emoji replacement in activity_to_comment.
269+
*
270+
* @covers ::activity_to_comment
271+
* @covers ::replace_custom_emoji
272+
*/
273+
public function test_activity_to_comment_with_emoji() {
274+
$activity = array(
275+
'@context' => array(
276+
'https://www.w3.org/ns/activitystreams',
277+
array(
278+
'Emoji' => 'http://joinmastodon.org/ns#Emoji',
279+
),
280+
),
281+
'id' => 'https://example.com/activities/1',
282+
'type' => 'Note',
283+
'content' => 'Hello world :kappa: and :smile:',
284+
'actor' => $this->user_url,
285+
'object' => array(
286+
'id' => 'https://example.com/objects/1',
287+
'content' => 'Hello world :kappa: and :smile:',
288+
'tag' => array(
289+
array(
290+
'type' => 'Emoji',
291+
'name' => ':kappa:',
292+
'icon' => array(
293+
'type' => 'Image',
294+
'mediaType' => 'image/png',
295+
'url' => 'https://example.com/files/kappa.png',
296+
),
297+
),
298+
array(
299+
'type' => 'Emoji',
300+
'name' => ':smile:',
301+
'icon' => array(
302+
'type' => 'Image',
303+
'mediaType' => 'image/png',
304+
'url' => 'https://example.com/files/smile.png',
305+
),
306+
),
307+
),
308+
),
309+
);
310+
311+
$commentdata = Interactions::activity_to_comment( $activity );
312+
313+
$this->assertStringContainsString(
314+
'<img src="https://example.com/files/kappa.png" alt=":kappa:" class="emoji" />',
315+
$commentdata['comment_content']
316+
);
317+
$this->assertStringContainsString(
318+
'<img src="https://example.com/files/smile.png" alt=":smile:" class="emoji" />',
319+
$commentdata['comment_content']
320+
);
321+
}
266322
}

0 commit comments

Comments
 (0)