diff --git a/src/controllers/EntriesController.php b/src/controllers/EntriesController.php index 39204e11afb..626fdf4eee2 100644 --- a/src/controllers/EntriesController.php +++ b/src/controllers/EntriesController.php @@ -16,6 +16,7 @@ use craft\events\ElementEvent; use craft\helpers\DateTimeHelper; use craft\helpers\Json; +use craft\helpers\StringHelper; use craft\helpers\UrlHelper; use craft\models\EntryDraft; use craft\models\EntryVersion; @@ -531,6 +532,13 @@ public function actionSaveEntry() $revisionsService->saveVersion($currentEntry); } + // Validate that the title does not have an emoji + if (StringHelper::hasMb4($entry->title)) { + Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save entry with emoji in title.')); + return null; + } + + // Save the entry (finally!) if ($entry->enabled && $entry->enabledForSite) { $entry->setScenario(Element::SCENARIO_LIVE); diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php index 325f812ed0b..c53f64aeaf6 100644 --- a/src/helpers/StringHelper.php +++ b/src/helpers/StringHelper.php @@ -965,6 +965,17 @@ public static function encoding(string $string): string return static::toLowerCase(mb_detect_encoding($string, mb_detect_order(), true)); } + /** + * Detects whether the given string has any 4-byte UTF-8 characters. + * + * @param string $string + * @return bool + */ + public static function hasMb4(string $string): bool + { + return max(array_map('ord', str_split($string))) >= 240; + } + /** * HTML-encodes any 4-byte UTF-8 characters. *