Skip to content

Commit 1817ec5

Browse files
committed
Address PR review feedback for emoji support
- Validate emoji downloads are actual images using wp_get_image_mime() - Handle wp_mkdir_p() failure by cleaning up temp file - Use glob for timestamp comparison to handle webp-optimized files - Add unslash/re-slash in prepare_comment_data() to avoid escaping img attributes - Add test for download failure handling
1 parent 1e4f854 commit 1817ec5

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

includes/class-attachments.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ public static function import_emoji( $emoji_url, $updated = null ) {
194194
// Compare timestamps - re-download if remote is newer.
195195
$paths = self::get_emoji_storage_paths( $emoji_url );
196196
$url_path = \wp_parse_url( $emoji_url, PHP_URL_PATH );
197-
$file_name = \sanitize_file_name( \basename( $url_path ) );
198-
$file_path = $paths['basedir'] . '/' . $file_name;
199-
$local_time = \file_exists( $file_path ) ? \filemtime( $file_path ) : 0;
197+
$file_stem = \sanitize_file_name( \pathinfo( $url_path, PATHINFO_FILENAME ) );
198+
$matches = \glob( $paths['basedir'] . '/' . $file_stem . '.*' );
199+
$file_path = ( $matches && \is_file( $matches[0] ) ) ? $matches[0] : null;
200+
$local_time = $file_path ? \filemtime( $file_path ) : 0;
200201
$remote_time = \strtotime( $updated );
201202

202203
if ( $remote_time && $local_time >= $remote_time ) {
@@ -214,11 +215,19 @@ public static function import_emoji( $emoji_url, $updated = null ) {
214215
return false;
215216
}
216217

218+
if ( ! \wp_get_image_mime( $tmp_file ) ) {
219+
\wp_delete_file( $tmp_file );
220+
return false;
221+
}
222+
217223
// Get storage paths for this emoji.
218224
$paths = self::get_emoji_storage_paths( $emoji_url );
219225

220226
// Create directory if it doesn't exist.
221-
\wp_mkdir_p( $paths['basedir'] );
227+
if ( ! \wp_mkdir_p( $paths['basedir'] ) ) {
228+
\wp_delete_file( $tmp_file );
229+
return false;
230+
}
222231

223232
// Generate filename from URL path (consistent with get_emoji_url lookup).
224233
$url_path = \wp_parse_url( $emoji_url, PHP_URL_PATH );

includes/class-emoji.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class Emoji {
2626
public static function prepare_comment_data( $comment_data, $activity ) {
2727
// Replace emoji in content at insert-time.
2828
if ( ! empty( $comment_data['comment_content'] ) && ! empty( $activity['object'] ) ) {
29-
$comment_data['comment_content'] = self::replace_custom_emoji( $comment_data['comment_content'], $activity['object'] );
29+
// Unslash, replace emoji, then re-slash to avoid escaping img tag attributes.
30+
$content = \wp_unslash( $comment_data['comment_content'] );
31+
$content = self::replace_custom_emoji( $content, $activity['object'] );
32+
$comment_data['comment_content'] = \addslashes( $content );
3033
}
3134

3235
return $comment_data;

tests/phpunit/tests/includes/class-test-attachments.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,4 +1304,25 @@ public function test_import_emoji_returns_false_for_invalid_url() {
13041304
$this->assertFalse( Attachments::import_emoji( '' ) );
13051305
$this->assertFalse( Attachments::import_emoji( 'not-a-url' ) );
13061306
}
1307+
1308+
/**
1309+
* Test emoji import returns false when download fails.
1310+
*
1311+
* @covers ::import_emoji
1312+
*/
1313+
public function test_import_emoji_returns_false_on_download_failure() {
1314+
$emoji_url = 'https://example.com/emoji/download-fail.png';
1315+
1316+
// Mock a failed HTTP request.
1317+
$fail_download = function () {
1318+
return new \WP_Error( 'http_request_failed', 'Connection failed' );
1319+
};
1320+
\add_filter( 'pre_http_request', $fail_download );
1321+
1322+
$result = Attachments::import_emoji( $emoji_url );
1323+
1324+
\remove_filter( 'pre_http_request', $fail_download );
1325+
1326+
$this->assertFalse( $result );
1327+
}
13071328
}

0 commit comments

Comments
 (0)