diff --git a/mailpoet/lib/Subscribers/SubscriberSaveController.php b/mailpoet/lib/Subscribers/SubscriberSaveController.php index a6d064c824..52bf2a554b 100644 --- a/mailpoet/lib/Subscribers/SubscriberSaveController.php +++ b/mailpoet/lib/Subscribers/SubscriberSaveController.php @@ -304,16 +304,32 @@ class SubscriberSaveController { private function updateTags(array $data, SubscriberEntity $subscriber): void { $removedTags = []; + + /** + * $data['tags'] is either an array of arrays containing name, id etc. of the tag or an array of strings - the names + * of the tag. + * + * Therefore we map it to be only an array of strings, containing the names of the tag. + */ + $tags = array_map( + function($tag): string { + if (is_array($tag)) { + return array_key_exists('name', $tag) ? (string)$tag['name'] : ''; + } + return (string)$tag; + }, + (array)$data['tags'] + ); foreach ($subscriber->getSubscriberTags() as $subscriberTag) { $tag = $subscriberTag->getTag(); - if (!$tag || !in_array($tag->getName(), $data['tags'], true)) { + if (!$tag || !in_array($tag->getName(), $tags, true)) { $subscriber->getSubscriberTags()->removeElement($subscriberTag); $removedTags[] = $subscriberTag; } } $newlyAddedTags = []; - foreach ($data['tags'] as $tagName) { + foreach ($tags as $tagName) { $tag = $this->tagRepository->createOrUpdate(['name' => $tagName]); $subscriberTag = $subscriber->getSubscriberTag($tag); if (!$subscriberTag) {