From 5ff819491a3ab5c95de09c355dace997f6326313 Mon Sep 17 00:00:00 2001 From: bencroker Date: Thu, 26 Oct 2023 15:19:09 +0200 Subject: [PATCH] Encode emoji in job queue title --- CHANGELOG.md | 1 + src/elements/SendoutElement.php | 19 +++++------- src/helpers/SendoutHelper.php | 51 ++++++++++++++++++++++++++++---- src/services/SendoutsService.php | 3 +- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e81469d6..ec536aca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Fixed a bug in which selecting a user group to import was throwing an exception ([#425](https://github.com/putyourlightson/craft-campaign/issues/425)). +- Fixed a bug in which saving a sendout with emojis in the subject could throw an error when adding the sendout to the queue ([#426](https://github.com/putyourlightson/craft-campaign/issues/426)). ## 2.9.1 - 2023-10-17 diff --git a/src/elements/SendoutElement.php b/src/elements/SendoutElement.php index d48f2def..d3a69788 100755 --- a/src/elements/SendoutElement.php +++ b/src/elements/SendoutElement.php @@ -18,7 +18,6 @@ use craft\validators\DateTimeValidator; use craft\web\CpScreenResponseBehavior; use DateTime; -use LitEmoji\LitEmoji; use putyourlightson\campaign\base\ScheduleModel; use putyourlightson\campaign\Campaign; use putyourlightson\campaign\elements\actions\CancelSendouts; @@ -26,6 +25,7 @@ use putyourlightson\campaign\elements\actions\PauseSendouts; use putyourlightson\campaign\elements\db\SendoutElementQuery; use putyourlightson\campaign\fieldlayoutelements\sendouts\SendoutFieldLayoutTab; +use putyourlightson\campaign\helpers\SendoutHelper; use putyourlightson\campaign\helpers\StringHelper; use putyourlightson\campaign\models\AutomatedScheduleModel; use putyourlightson\campaign\models\RecurringScheduleModel; @@ -543,11 +543,8 @@ public function init(): void { parent::init(); - if (Craft::$app->getDb()->getIsMysql()) { - // Decode emojis - $this->title = $this->title ? LitEmoji::shortcodeToUnicode($this->title) : ''; - $this->subject = $this->subject ? LitEmoji::shortcodeToUnicode($this->subject) : ''; - } + $this->title = SendoutHelper::decodeEmojis($this->title); + $this->subject = SendoutHelper::decodeEmojis($this->subject); } /** @@ -1089,8 +1086,8 @@ public function getPlaintextBody(): ?string */ public function getSearchKeywords(string $attribute): string { - if ($attribute == 'subject') { - return LitEmoji::unicodeToShortcode($this->{$attribute} ?? ''); + if ($attribute == 'title' || $attribute == 'subject') { + return SendoutHelper::encodeEmojis($this->{$attribute}); } return parent::getSearchKeywords($attribute); @@ -1265,10 +1262,8 @@ public function beforeSave(bool $isNew): bool $this->lastSent = null; } - if (Craft::$app->getDb()->getIsMysql()) { - // Encode subject for emojis - $this->subject = LitEmoji::unicodeToShortcode($this->subject ?? ''); - } + $this->title = SendoutHelper::encodeEmojis($this->title); + $this->subject = SendoutHelper::encodeEmojis($this->subject); if (Campaign::$plugin->settings->showSendoutTitleField === false) { $this->title = $this->subject; diff --git a/src/helpers/SendoutHelper.php b/src/helpers/SendoutHelper.php index 51c07a13..bbc4a6a8 100644 --- a/src/helpers/SendoutHelper.php +++ b/src/helpers/SendoutHelper.php @@ -5,6 +5,9 @@ namespace putyourlightson\campaign\helpers; +use Craft; +use LitEmoji\LitEmoji; + /** * @since 1.8.2 */ @@ -18,10 +21,48 @@ public static function memoryInBytes(string $value): int $unit = strtolower(substr($value, -1, 1)); return (int)$value * match ($unit) { - 'g' => pow(1024, 3), - 'm' => pow(1024, 2), - 'k' => 1024, - default => 1, - }; + 'g' => pow(1024, 3), + 'm' => pow(1024, 2), + 'k' => 1024, + default => 1, + }; + } + + /** + * Encodes emojis if necessary. + * TODO: remove in Campaign 3 when Craft 5 supports utf8mb4 encoding. + * + * @since 2.9.2 + */ + public static function encodeEmojis(string $value): string + { + if (!Craft::$app->getDb()->getIsMysql()) { + return $value; + } + + if (empty($value)) { + return $value; + } + + return LitEmoji::unicodeToShortcode($value); + } + + /** + * Decodes emojis if necessary. + * TODO: remove in Campaign 3 when Craft 5 supports utf8mb4 encoding. + * + * @since 2.9.2 + */ + public static function decodeEmojis(string $value): string + { + if (!Craft::$app->getDb()->getIsMysql()) { + return $value; + } + + if (empty($value)) { + return $value; + } + + return LitEmoji::shortcodeToUnicode($value); } } diff --git a/src/services/SendoutsService.php b/src/services/SendoutsService.php index c9d34b16..c543fd6c 100755 --- a/src/services/SendoutsService.php +++ b/src/services/SendoutsService.php @@ -20,6 +20,7 @@ use putyourlightson\campaign\elements\MailingListElement; use putyourlightson\campaign\elements\SendoutElement; use putyourlightson\campaign\events\SendoutEmailEvent; +use putyourlightson\campaign\helpers\SendoutHelper; use putyourlightson\campaign\jobs\SendoutJob; use putyourlightson\campaign\models\AutomatedScheduleModel; use putyourlightson\campaign\records\ContactCampaignRecord; @@ -156,7 +157,7 @@ public function queuePendingSendouts(): int // Add sendout job to queue $job = new SendoutJob([ 'sendoutId' => $sendout->id, - 'title' => $sendout->title, + 'title' => SendoutHelper::encodeEmojis($sendout->title), ]); Queue::push($job, Campaign::$plugin->settings->sendoutJobPriority);