Skip to content

Commit 388ebdd

Browse files
committed
Support emojis in webhook/group names
1 parent e1e0161 commit 388ebdd

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
- Added support for webhooks that send GET requests.
7+
- Webhook names and group names can now contain emojis, even if using MySQL.
78

89
## 1.0.1 - 2018-12-13
910

src/WebhookManager.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Craft;
66
use craft\db\Query;
7+
use craft\helpers\StringHelper;
78
use yii\base\InvalidArgumentException;
89

910
/**
@@ -31,8 +32,13 @@ public function getAllGroups(): array
3132
->all();
3233

3334
$groups = [];
35+
$isMysql = Craft::$app->getDb()->getIsMysql();
3436

3537
foreach ($results as $result) {
38+
if ($isMysql) {
39+
$result['name'] = html_entity_decode($result['name'], ENT_QUOTES | ENT_HTML5);
40+
}
41+
3642
$groups[] = new Group($result);
3743
}
3844

@@ -55,18 +61,24 @@ public function saveGroup(Group $group, bool $runValidation = true): bool
5561

5662
$db = Craft::$app->getDb();
5763

64+
if ($db->getIsMysql()) {
65+
$name = StringHelper::encodeMb4($group->name);
66+
} else {
67+
$name = $group->name;
68+
}
69+
5870
if ($group->id) {
5971
$db->createCommand()
6072
->update('{{%webhookgroups}}', [
61-
'name' => $group->name,
73+
'name' => $name,
6274
], [
6375
'id' => $group->id,
6476
])
6577
->execute();
6678
} else {
6779
$db->createCommand()
6880
->insert('{{%webhookgroups}}', [
69-
'name' => $group->name,
81+
'name' => $name,
7082
])
7183
->execute();
7284

@@ -153,7 +165,7 @@ public function getWebhookById(int $id): Webhook
153165
throw new InvalidArgumentException('Invalid webhook ID: ' . $id);
154166
}
155167

156-
return new Webhook($result);
168+
return $this->_createWebhook($result);
157169
}
158170

159171
/**
@@ -170,10 +182,18 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
170182
return false;
171183
}
172184

185+
$db = Craft::$app->getDb();
186+
187+
if ($db->getIsMysql()) {
188+
$name = StringHelper::encodeMb4($webhook->name);
189+
} else {
190+
$name = $webhook->name;
191+
}
192+
173193
$data = [
174194
'groupId' => $webhook->groupId,
175195
'enabled' => (bool)$webhook->enabled,
176-
'name' => $webhook->name,
196+
'name' => $name,
177197
'class' => $webhook->class,
178198
'event' => $webhook->event,
179199
'type' => $webhook->type,
@@ -183,8 +203,6 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
183203
'eventAttributes' => $webhook->eventAttributes,
184204
];
185205

186-
$db = Craft::$app->getDb();
187-
188206
if ($webhook->id) {
189207
$db->createCommand()
190208
->update('{{%webhooks}}', $data, ['id' => $webhook->id])
@@ -221,16 +239,31 @@ private function _createWebhookQuery(): Query
221239
->from(['{{%webhooks}}']);
222240
}
223241

242+
/**
243+
* @param array $result
244+
* @param bool|null $isMysql
245+
* @return Webhook
246+
*/
247+
private function _createWebhook(array $result, bool $isMysql = null): Webhook
248+
{
249+
if ($isMysql ?? Craft::$app->getDb()->getIsMysql()) {
250+
$result['name'] = html_entity_decode($result['name'], ENT_QUOTES | ENT_HTML5);
251+
}
252+
253+
return new Webhook($result);
254+
}
255+
224256
/**
225257
* @param array
226258
* @return Webhook[]
227259
*/
228260
private function _createWebhooks(array $results): array
229261
{
230262
$webhooks = [];
263+
$isMysql = Craft::$app->getDb()->getIsMysql();
231264

232265
foreach ($results as $result) {
233-
$webhooks[] = new Webhook($result);
266+
$webhooks[] = $this->_createWebhook($result, $isMysql);
234267
}
235268

236269
return $webhooks;

0 commit comments

Comments
 (0)