Skip to content

Commit

Permalink
Encode emoji in job queue title
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 26, 2023
1 parent 321478b commit 5ff8194
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 7 additions & 12 deletions src/elements/SendoutElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
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;
use putyourlightson\campaign\elements\actions\EditSendout;
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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 46 additions & 5 deletions src/helpers/SendoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace putyourlightson\campaign\helpers;

use Craft;
use LitEmoji\LitEmoji;

/**
* @since 1.8.2
*/
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/services/SendoutsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5ff8194

Please sign in to comment.