Skip to content

Commit

Permalink
Fix Emoji support for notes in Craft 4.4+
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Apr 28, 2023
1 parent 064788b commit 0d5dade
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,39 @@ class StringHelper extends CraftStringHelper
// Static Methods
// =========================================================================

public static function sanitizeNotes($value): ?string
public static function sanitizeNotes(?string $value): ?string
{
// Support Emojis and sanitize HTML
$value = LitEmoji::unicodeToShortcode((string)$value);
$value = self::emojiToShortcodes((string)$value);
$value = StringHelper::htmlEncode((string)$value);

return $value;
}

public static function unSanitizeNotes($value): ?string
public static function unSanitizeNotes(?string $value): ?string
{
// Support Emojis and sanitize HTML
$value = LitEmoji::shortcodeToUnicode((string)$value);
$value = self::shortcodesToEmoji((string)$value);

return $value;
}

public static function emojiToShortcodes(string $str): string
{
// Add delimiters around all 4-byte chars
$dl = '__MB4_DL__';
$dr = '__MB4_DR__';
$str = self::replaceMb4($str, fn($char) => sprintf('%s%s%s', $dl, $char, $dr));

// Strip out consecutive delimiters
$str = str_replace(sprintf('%s%s', $dr, $dl), '', $str);

// Replace all 4-byte sequences individually
return preg_replace_callback("/$dl(.+?)$dr/", fn($m) => LitEmoji::unicodeToShortcode($m[1]), $str);
}

public static function shortcodesToEmoji(string $str): string
{
return LitEmoji::shortcodeToUnicode($str);
}
}

0 comments on commit 0d5dade

Please sign in to comment.