PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.1
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Helpers / UtmHelper.php

UtmHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.1, at app/Helpers/UtmHelper.php

154 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Helpers;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\OrderOperation;
7 use FluentCart\Framework\Support\Arr;
8
9 class UtmHelper
10 {
11
12 public static function allowedUtmParameterKey(): array
13 {
14 $keys = [
15 'utm_campaign',
16 'utm_content',
17 'utm_term',
18 'utm_source',
19 'utm_medium',
20 'utm_id',
21 'refer_url',
22 'fbclid',
23 'gclid'
24 ];
25
26 return apply_filters('fluent_cart/utm/allowed_keys', $keys, []);
27 }
28
29 /**
30 * Hostnames that belong to the same store network (e.g. child/product sites
31 * that redirect visitors here for checkout). Referrers from these hosts are
32 * treated as internal navigation and never recorded as refer_url — the real
33 * source arrives as query params (refer_url, utm_*) appended by the child site.
34 */
35 public static function getInternalDomains(): array
36 {
37 $domains = apply_filters('fluent_cart/utm/internal_domains', []);
38
39 if (!is_array($domains)) {
40 return [];
41 }
42
43 $hosts = [];
44 foreach ($domains as $domain) {
45 if (!is_string($domain) || !$domain) {
46 continue;
47 }
48 $domain = strtolower(trim($domain));
49 if (strpos($domain, '//') !== false) {
50 $domain = (string)wp_parse_url($domain, PHP_URL_HOST);
51 }
52 $domain = trim($domain, '/');
53 if ($domain) {
54 $hosts[] = $domain;
55 }
56 }
57
58 return array_values(array_unique($hosts));
59 }
60
61 public static function addUtmToOrder($orderId, $data = [])
62 {
63 $directValueKeys = [
64 'utm_campaign',
65 'utm_content',
66 'utm_term',
67 'utm_source',
68 'utm_medium',
69 'utm_id',
70 'refer_url',
71 ];
72
73 $directValues = Arr::only($data, $directValueKeys);
74 $metaValues = Arr::except($data, $directValueKeys);
75
76 foreach ($directValues as $key => $value) {
77 if ($key == 'refer_url') {
78 $directValues[$key] = self::normalizeReferUrl($value);
79 } else {
80 $directValues[$key] = sanitize_text_field($value);
81 }
82 }
83
84 $allowedKeys = self::allowedUtmParameterKey();
85 $allowedMetaValues = [];
86 foreach ($metaValues as $key => $value) {
87 if (!in_array($key, $allowedKeys)) {
88 continue;
89 }
90 $allowedMetaValues[$key] = sanitize_text_field($value);
91 }
92
93 $directValues['meta'] = $allowedMetaValues;
94
95 $hasValues = array_filter($directValues) || array_filter($allowedMetaValues);
96
97
98 $oldOperation = OrderOperation::query()->where('order_id', $orderId)->first();
99
100 if (empty($oldOperation)) {
101 OrderOperation::query()->create(
102 array_merge($directValues, ['order_id' => $orderId])
103 );
104 } else {
105 $directValues['meta'] = Arr::mergeMissingValues($directValues['meta'], $oldOperation->meta);
106 Arr::mergeMissingValues($directValues, Arr::except($oldOperation->toArray(), 'meta'));
107 $directValues = array_merge($directValues);
108 $oldOperation->update($directValues);
109 }
110 }
111
112 /**
113 * Reduce a referrer (full URL or bare host) to its bare domain:
114 * no scheme, no www. prefix, no path — e.g. "google.com"
115 */
116 public static function normalizeReferUrl($value): string
117 {
118 $value = trim((string)$value);
119 if (!$value) {
120 return '';
121 }
122
123 if (strpos($value, '//') !== false) {
124 $host = wp_parse_url($value, PHP_URL_HOST);
125 if ($host) {
126 $value = $host;
127 }
128 } else {
129 $value = explode('/', $value)[0];
130 }
131
132 $value = strtolower($value);
133 if (strpos($value, 'www.') === 0) {
134 $value = substr($value, 4);
135 }
136
137 return sanitize_text_field($value);
138 }
139
140 public static function getUtmDataOfRequest(): array
141 {
142 $requestData = App::request()->all();
143 $requestUtmData = Arr::get($requestData, 'utm_data', []);
144 $sanitizedUtmData = [];
145 // Sanitize UTM data
146 foreach ($requestUtmData as $utmKey => $utmValue) {
147 $sanitizedKey = sanitize_text_field($utmKey);
148 $sanitizedUtmData[$sanitizedKey] = sanitize_text_field($utmValue);
149 }
150
151 return $sanitizedUtmData;
152 }
153
154 }