Skip to content

Commit 9e7e1a9

Browse files
normalize request urls for matching
1 parent 56ea147 commit 9e7e1a9

3 files changed

Lines changed: 67 additions & 18 deletions

File tree

src/admin/class-admin.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -550,23 +550,7 @@ public function sanitize_settings($opts)
550550
*/
551551
public function sanitize_url($url_string)
552552
{
553-
554-
// if empty, just return a slash
555-
if (empty($url_string)) {
556-
return '/';
557-
}
558-
559-
// if string looks like an absolute URL, extract just the path
560-
if (preg_match('/^((https|http)?\:\/\/)?(\w+\.)?\w+\.\w+\.*/i', $url_string)) {
561-
// make sure URL has scheme prepended, to make parse_url() understand..
562-
$url_string = 'https://' . str_replace([ 'http://', 'https://' ], '', $url_string);
563-
564-
// get just the path
565-
$url_string = parse_url($url_string, PHP_URL_PATH);
566-
}
567-
568-
// leading slash it
569-
return $url_string;
553+
return \boxzilla_normalize_relative_url($url_string);
570554
}
571555

572556
/**

src/class-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function match_patterns($string, array $patterns, $contains = false)
108108
*/
109109
protected function get_request_url()
110110
{
111-
return rtrim($_SERVER['REQUEST_URI'], '/');
111+
return \boxzilla_normalize_relative_url(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
112112
}
113113

114114
/**

src/functions.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,68 @@ function boxzilla()
1515

1616
return $instance;
1717
}
18+
19+
/**
20+
* Normalize a relative URL for Boxzilla rule storage and matching.
21+
*
22+
* @param string $url_string
23+
*
24+
* @return string
25+
*/
26+
function boxzilla_normalize_relative_url($url_string)
27+
{
28+
$url_string = trim((string) $url_string);
29+
if ($url_string === '') {
30+
return '/';
31+
}
32+
33+
if (strpos($url_string, '//') === 0) {
34+
$url_string = 'https:' . $url_string;
35+
} elseif (preg_match('/^[\w.-]+\.[a-z]{2,}(?:[\/?#]|$)/i', $url_string)) {
36+
$url_string = 'https://' . $url_string;
37+
}
38+
39+
$parts = wp_parse_url($url_string);
40+
if (! is_array($parts)) {
41+
return '/';
42+
}
43+
44+
$path = isset($parts['path']) ? $parts['path'] : '/';
45+
$path = '/' . ltrim((string) $path, '/');
46+
$path = untrailingslashit($path);
47+
if ($path === '') {
48+
$path = '/';
49+
}
50+
51+
if (empty($parts['query'])) {
52+
return $path;
53+
}
54+
55+
parse_str($parts['query'], $query_args);
56+
if (empty($query_args)) {
57+
return $path;
58+
}
59+
60+
$tracking_keys = [
61+
'_ga',
62+
'_gl',
63+
'dclid',
64+
'fbclid',
65+
'gclid',
66+
'mc_cid',
67+
'mc_eid',
68+
'msclkid',
69+
];
70+
71+
foreach (array_keys($query_args) as $key) {
72+
if (strpos($key, 'utm_') === 0 || in_array($key, $tracking_keys, true)) {
73+
unset($query_args[ $key ]);
74+
}
75+
}
76+
77+
if (empty($query_args)) {
78+
return $path;
79+
}
80+
81+
return $path . '?' . build_query($query_args);
82+
}

0 commit comments

Comments
 (0)