Skip to content

Commit 6ac1425

Browse files
authored
Merge pull request #2315 from jefcollier/patch-1
Update to requested_url function.
2 parents 2f0efaf + b3ec7f3 commit 6ac1425

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

libs/Utilities/Utilities.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -651,21 +651,36 @@ public function valid_hostname_regex($domains=null){
651651
return implode("|", $all_domains); //Return a valid hostname regex string
652652
}
653653

654-
//Get the full URL. Not intended for secure use ($_SERVER var can be manipulated by client/server).
655-
public function requested_url($host="HTTP_HOST"){ //Can use "SERVER_NAME" as an alternative to "HTTP_HOST".
656-
$override = apply_filters('pre_nebula_requested_url', null, $host);
657-
if ( isset($override) ){return $override;}
658-
659-
if ( $this->is_cli() ){
660-
return null;
661-
}
662-
663-
$protocol = ( is_ssl() )? 'https' : 'http';
664-
$full_url = $protocol . '://' . $this->super->server["$host"] . $this->super->server["REQUEST_URI"];
665-
666-
return esc_url($full_url);
654+
// Get the full URL. Not intended for secure use ($_SERVER var can be manipulated by client/server).
655+
public function requested_url($host = "HTTP_HOST") { // Can use "SERVER_NAME" as an alternative to "HTTP_HOST".
656+
$override = apply_filters('pre_nebula_requested_url', null, $host);
657+
if (isset($override)) {
658+
return $override;
659+
}
660+
661+
// Bail early if CLI or no REQUEST_URI
662+
if ($this->is_cli() || empty($this->super->server['REQUEST_URI'])) {
663+
return null;
664+
}
665+
666+
// Check if host key exists
667+
$server = $this->super->server;
668+
if (!isset($server[$host]) || empty($server[$host])) {
669+
// Fall back to SERVER_NAME or return null
670+
if (!empty($server['SERVER_NAME'])) {
671+
$server[$host] = $server['SERVER_NAME'];
672+
} else {
673+
return null;
674+
}
675+
}
676+
677+
$protocol = is_ssl() ? 'https' : 'http';
678+
$full_url = $protocol . '://' . $server[$host] . $server['REQUEST_URI'];
679+
680+
return esc_url($full_url);
667681
}
668682

683+
669684
//Separate a URL into it's components.
670685
public function url_components($segment="all", $url=null){
671686
$override = apply_filters('pre_nebula_url_components', null, $segment, $url);
@@ -2225,4 +2240,4 @@ public function hubspot_curl($url, $content=null){
22252240
return null;
22262241
}
22272242
}
2228-
}
2243+
}

0 commit comments

Comments
 (0)