EligibilityService.php
10 months ago
StockManagementHelper.php
10 months ago
UtmHelper.php
1 month ago
UtmHelper.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Utilities; |
| 6 | |
| 7 | /** |
| 8 | * Helper for appending UTM campaign parameters to outgoing stock-notification email links. |
| 9 | * |
| 10 | * Centralizes the `utm_source` / `utm_medium` values so order attribution stays consistent |
| 11 | * across all three email types (verify, verified, back-in-stock). |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | class UtmHelper { |
| 16 | |
| 17 | /** |
| 18 | * UTM source value used for all stock notification emails. |
| 19 | */ |
| 20 | public const UTM_SOURCE = 'back-in-stock-notifications'; |
| 21 | |
| 22 | /** |
| 23 | * Default UTM medium for stock notification emails. |
| 24 | */ |
| 25 | public const UTM_MEDIUM_EMAIL = 'email'; |
| 26 | |
| 27 | /** |
| 28 | * Append the standard email UTM parameters to a URL. |
| 29 | * |
| 30 | * @param string $url The URL to annotate. |
| 31 | * @param string $medium The UTM medium (defaults to `email`). |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function add_email_utm_params( string $url, string $medium = self::UTM_MEDIUM_EMAIL ): string { |
| 35 | if ( empty( $url ) ) { |
| 36 | return $url; |
| 37 | } |
| 38 | |
| 39 | // Defensive: lock down the medium to a safe URL-friendly slug, falling back to the default |
| 40 | // if sanitization strips everything. Prevents any future caller from piping user-controlled |
| 41 | // input into the outbound tracking URL. |
| 42 | $sanitized_medium = sanitize_key( $medium ); |
| 43 | if ( '' === $sanitized_medium ) { |
| 44 | $sanitized_medium = self::UTM_MEDIUM_EMAIL; |
| 45 | } |
| 46 | |
| 47 | return add_query_arg( |
| 48 | array( |
| 49 | 'utm_source' => self::UTM_SOURCE, |
| 50 | 'utm_medium' => $sanitized_medium, |
| 51 | ), |
| 52 | $url |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 |