Merge the Smartlinks plugin (SmartlinksBundle 0.6.0) into the bundle as the Smart Links domain (0.18.0)
Two domains, one MT3S bundle: Marketing Programs (unchanged) and the
imported Smart Links (smart links + UTM taxonomies), each keeping its
own menu, tables, routes and translations.
- Code moved from MauticPlugin\SmartlinksBundle\ to
MauticPlugin\MT3SMarketingProgramBundle\; template namespace
@Smartlinks/ -> @MT3SMarketingProgram/; smartlinks.js ->
Assets/js/mt3s-mp-smartlinks.js; icon -> Assets/img/smartlinks-icon.svg.
- Incoming migrations renamed Version_0_X_0_SmartLinks.php to avoid
the class-name collision; schema untouched, data intact.
- Permissions folded into MT3SMarketingProgramPermissions:
smart_links + taxonomies under the mt3smarketingprogram: base
(roles that had the old smartlinks: grants must be re-granted).
- config.php: Smart Links routes + menu group, public /l/{slug},
SmartlinksIntegration services, smartlinks_* parameters (legacy
utm_manager_* keys still readable), version 0.18.0. The 0.17.0
placeholder /s/marketing-program-utm-taxonomies is replaced by the
real /s/utm-taxonomies.
- Translations merged (en_US) and added (fr_FR); Tests/ + phpunit moved
in; readme documents both domains and the 0.18.0 merge history.
Verified live: menus, both config tabs, both lists + forms, program
pages, public redirect with taxonomy UTM values, host + unknown-slug
404s, 90/90 unit tests, full php -l, all ini catalogs parse.
This commit is contained in:
1 parent
debce4ba4a
commit
dce849610c
69 files changed
+8225
-43
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
|
||||
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Form\Type\SmartLinksConfigType;
|
||||
|
||||
/**
|
||||
* Builds the public form of a Smart Link: https://{short_link_domain}/l/{slug}.
|
||||
* The domain is set in Settings > Configuration > Smart Links Settings; the form shows it
|
||||
* as a reminder (getShortLinkDomain(), without the scheme).
|
||||
*/
|
||||
final class SmartLinkShortUrlBuilder
|
||||
{
|
||||
public const PATH_PREFIX = '/l/';
|
||||
|
||||
public function __construct(
|
||||
private readonly CoreParametersHelper $coreParametersHelper,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The configured short link domain, without scheme or trailing slash, for example lnk.example.com.
|
||||
* Empty when nothing is configured yet.
|
||||
*/
|
||||
public function getShortLinkDomain(): string
|
||||
{
|
||||
$stored = $this->coreParametersHelper->get(SmartLinksConfigType::CONFIG_PARAMETER, []);
|
||||
|
||||
// Upgrades from < 0.5.0 keep the domain in local.php under the legacy key; serve it until
|
||||
// the Configuration page is saved again (which writes the new key).
|
||||
if (!is_array($stored) || '' === trim((string) ($stored['short_link_domain'] ?? ''))) {
|
||||
$legacy = $this->coreParametersHelper->get(SmartLinksConfigType::LEGACY_CONFIG_PARAMETER, []);
|
||||
if (is_array($legacy) && '' !== trim((string) ($legacy['short_link_domain'] ?? ''))) {
|
||||
$stored = $legacy;
|
||||
}
|
||||
}
|
||||
|
||||
$domain = trim(is_array($stored) ? (string) ($stored['short_link_domain'] ?? '') : '');
|
||||
|
||||
return '' === $domain ? '' : rtrim($domain, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* The short link of a Smart Link, for example https://lnk.example.com/l/ab3x.
|
||||
* Empty when no domain is configured, or when the link has no slug yet.
|
||||
*/
|
||||
public function buildShortUrl(SmartLink $smartLink): string
|
||||
{
|
||||
return $this->buildShortUrlFromSlug((string) ($smartLink->getSlug() ?? ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as buildShortUrl(), for a slug that was just typed in the form (live preview).
|
||||
*/
|
||||
public function buildShortUrlFromSlug(string $slug): string
|
||||
{
|
||||
$slug = trim($slug);
|
||||
$domain = $this->getShortLinkDomain();
|
||||
|
||||
if ('' === $domain || '' === $slug) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return 'https://'.$domain.self::PATH_PREFIX.$slug;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
|
||||
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterCatalog;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterDefinition;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
|
||||
|
||||
/**
|
||||
* Turns the stored `smartlinks_utm_config` array into a canonical, typed structure.
|
||||
*
|
||||
* - Every catalog parameter is present (catalog defaults fill the gaps), followed by valid custom parameters.
|
||||
* - Mautic writes nested config scalars as strings ('1' / ''), so booleans are cast back.
|
||||
* - Only `dropdown` parameters carry `values` and `default`; `free` and `disabled` parameters keep
|
||||
* `enabled`, `label` and `mode` only.
|
||||
*
|
||||
* @phpstan-type UtmValue array{value: string, label: string}
|
||||
* @phpstan-type UtmParameter array{enabled: bool, label: string, mode: string, values?: list<UtmValue>, default?: ?string}
|
||||
* @phpstan-type UtmConfig array{enforce_lowercase: bool, separator_replacement: string, parameters: array<string, UtmParameter>}
|
||||
*/
|
||||
final class UtmConfigurationNormalizer
|
||||
{
|
||||
public const SEPARATORS = ['-', '_', '.'];
|
||||
public const DEFAULT_SEPARATOR = '-';
|
||||
|
||||
public function __construct(
|
||||
private readonly UtmParameterCatalog $catalog,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UtmConfig
|
||||
*/
|
||||
public function normalize(mixed $raw): array
|
||||
{
|
||||
$raw = is_array($raw) ? $raw : [];
|
||||
$separator = $raw['separator_replacement'] ?? self::DEFAULT_SEPARATOR;
|
||||
$stored = is_array($raw['parameters'] ?? null) ? $raw['parameters'] : [];
|
||||
|
||||
$parameters = [];
|
||||
foreach ($this->catalog->all() as $key => $definition) {
|
||||
$parameters[$key] = $this->normalizeParameter($stored[$key] ?? null, $definition);
|
||||
}
|
||||
|
||||
foreach ($stored as $key => $parameter) {
|
||||
$key = (string) $key;
|
||||
if (isset($parameters[$key]) || !$this->catalog->isValidCustomKey($key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parameters[$key] = $this->normalizeParameter($parameter, null);
|
||||
}
|
||||
|
||||
return [
|
||||
'enforce_lowercase' => array_key_exists('enforce_lowercase', $raw) ? self::toBool($raw['enforce_lowercase']) : true,
|
||||
'separator_replacement' => in_array($separator, self::SEPARATORS, true) ? $separator : self::DEFAULT_SEPARATOR,
|
||||
'parameters' => $parameters,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UtmParameter
|
||||
*/
|
||||
private function normalizeParameter(mixed $stored, ?UtmParameterDefinition $definition): array
|
||||
{
|
||||
$defaultMode = $definition?->defaultMode ?? UtmParameterMode::DISABLED;
|
||||
$allowedModes = $definition?->getAllowedModes() ?? UtmParameterMode::ALL;
|
||||
|
||||
if (!is_array($stored)) {
|
||||
// Free parameters start enabled. Dropdown parameters start switched off: an enabled dropdown
|
||||
// needs values, and an invalid default would block saving the whole Configuration page.
|
||||
$stored = ['enabled' => UtmParameterMode::FREE === $defaultMode, 'mode' => $defaultMode];
|
||||
}
|
||||
|
||||
$mode = (string) ($stored['mode'] ?? $defaultMode);
|
||||
if (!in_array($mode, $allowedModes, true)) {
|
||||
$mode = $defaultMode;
|
||||
}
|
||||
|
||||
$parameter = [
|
||||
'enabled' => self::toBool($stored['enabled'] ?? false),
|
||||
'label' => trim((string) ($stored['label'] ?? '')),
|
||||
'mode' => $mode,
|
||||
];
|
||||
|
||||
if (UtmParameterMode::DROPDOWN !== $mode) {
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
$values = $this->normalizeValues($stored['values'] ?? []);
|
||||
$default = (string) ($stored['default'] ?? '');
|
||||
|
||||
$parameter['values'] = $values;
|
||||
$parameter['default'] = in_array($default, array_column($values, 'value'), true) ? $default : null;
|
||||
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{value: string, label: string}>
|
||||
*/
|
||||
private function normalizeValues(mixed $values): array
|
||||
{
|
||||
if (!is_array($values)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$normalized = [];
|
||||
foreach ($values as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = trim((string) ($row['value'] ?? ''));
|
||||
if ('' === $value || isset($normalized[$value])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$label = trim((string) ($row['label'] ?? ''));
|
||||
$normalized[$value] = ['value' => $value, 'label' => '' !== $label ? $label : $value];
|
||||
}
|
||||
|
||||
return array_values($normalized);
|
||||
}
|
||||
|
||||
public static function toBool(mixed $value): bool
|
||||
{
|
||||
if (is_string($value)) {
|
||||
return in_array(strtolower(trim($value)), ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
return (bool) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
|
||||
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterCatalog;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Read-only access to the UTM Manager configuration. Other services (e.g. the link builder)
|
||||
* must use this class instead of reading `smartlinks_utm_config` from local.php.
|
||||
*
|
||||
* Every returned parameter has this shape:
|
||||
* key, label, mode, enabled, group (standard|platform|custom), custom, auto_generated
|
||||
* + values (list of {value, label}) and default (?string) for `dropdown` parameters only.
|
||||
*/
|
||||
final class UtmConfigurationProvider
|
||||
{
|
||||
public const CONFIG_PARAMETER = 'smartlinks_utm_config';
|
||||
|
||||
/** Key of the UtmManagerBundle era (< 0.5.0); still honored until the settings are saved again. */
|
||||
public const LEGACY_CONFIG_PARAMETER = 'utm_manager_config';
|
||||
|
||||
public function __construct(
|
||||
private readonly CoreParametersHelper $coreParametersHelper,
|
||||
private readonly UtmConfigurationNormalizer $normalizer,
|
||||
private readonly UtmParameterCatalog $catalog,
|
||||
private readonly TranslatorInterface $translator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters that are enabled and not in `disabled` mode, in display order, keyed by parameter key.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
public function getEnabledParameters(): array
|
||||
{
|
||||
$enabled = [];
|
||||
foreach ($this->getConfig()['parameters'] as $key => $parameter) {
|
||||
if ($parameter['enabled'] && UtmParameterMode::DISABLED !== $parameter['mode']) {
|
||||
$enabled[$key] = $this->decorate($key, $parameter);
|
||||
}
|
||||
}
|
||||
|
||||
return $enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Any catalog or custom parameter, whatever its state. Null when the key is unknown.
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function getParameter(string $key): ?array
|
||||
{
|
||||
$parameter = $this->getConfig()['parameters'][$key] ?? null;
|
||||
|
||||
return null === $parameter ? null : $this->decorate($key, $parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allowed values of a `dropdown` parameter. Always empty for `free` and `disabled` parameters.
|
||||
*
|
||||
* @return list<array{value: string, label: string}>
|
||||
*/
|
||||
public function getDropdownValues(string $key): array
|
||||
{
|
||||
$parameter = $this->getConfig()['parameters'][$key] ?? null;
|
||||
|
||||
if (null === $parameter || UtmParameterMode::DROPDOWN !== $parameter['mode']) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $parameter['values'] ?? [];
|
||||
}
|
||||
|
||||
public function isLowercaseEnforced(): bool
|
||||
{
|
||||
return $this->getConfig()['enforce_lowercase'];
|
||||
}
|
||||
|
||||
public function getSeparatorReplacement(): string
|
||||
{
|
||||
return $this->getConfig()['separator_replacement'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{enforce_lowercase: bool, separator_replacement: string, parameters: array<string, array<string, mixed>>}
|
||||
*/
|
||||
private function getConfig(): array
|
||||
{
|
||||
$raw = $this->coreParametersHelper->get(self::CONFIG_PARAMETER, []);
|
||||
|
||||
// Upgrades from < 0.5.0 keep their settings in local.php under the legacy key; serve them
|
||||
// until the Configuration page is saved again (which writes the new key).
|
||||
if (null === $raw || [] === $raw) {
|
||||
$legacy = $this->coreParametersHelper->get(self::LEGACY_CONFIG_PARAMETER, []);
|
||||
if (null !== $legacy && [] !== $legacy) {
|
||||
$raw = $legacy;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->normalizer->normalize($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $parameter
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function decorate(string $key, array $parameter): array
|
||||
{
|
||||
$definition = $this->catalog->get($key);
|
||||
|
||||
if ('' === $parameter['label']) {
|
||||
$parameter['label'] = null !== $definition ? $this->translator->trans($definition->getLabelKey()) : $key;
|
||||
}
|
||||
|
||||
return ['key' => $key] + $parameter + [
|
||||
'group' => $definition?->group ?? UtmParameterCatalog::GROUP_CUSTOM,
|
||||
'custom' => null === $definition,
|
||||
'auto_generated' => $definition?->autoGenerated ?? false,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user