- Updates site URL detection logic
- Adds unit test for Cron Helper class
This commit is contained in:
@ -60,6 +60,7 @@ class CronHelper {
|
||||
QueueEndpoint::ACTION_RUN,
|
||||
$data
|
||||
);
|
||||
$url = str_replace(home_url(), self::getSiteUrl(), $url);
|
||||
$args = array(
|
||||
'blocking' => false,
|
||||
'sslverify' => false,
|
||||
@ -70,20 +71,21 @@ class CronHelper {
|
||||
return wp_remote_retrieve_body($result);
|
||||
}
|
||||
|
||||
private static function getSiteUrl() {
|
||||
// additional check for some sites running on a virtual machine or behind
|
||||
static function getSiteUrl($site_url = false) {
|
||||
// additional check for some sites running inside a virtual machine or behind
|
||||
// proxy where there could be different ports (e.g., host:8080 => guest:80)
|
||||
|
||||
// if the site URL does not contain a port, return the URL
|
||||
if(!preg_match('!^https?://.*?:\d+!', site_url())) return site_url();
|
||||
preg_match('!://(?P<host>.*?):(?P<port>\d+)!', site_url(), $server);
|
||||
// connect to the URL with port
|
||||
$fp = @fsockopen($server['host'], $server['port'], $errno, $errstr, 1);
|
||||
if($fp) return site_url();
|
||||
// connect to the URL without port
|
||||
$fp = @fsockopen($server['host'], $server['port'], $errno, $errstr, 1);
|
||||
if($fp) return preg_replace('!(?=:\d+):\d+!', '$1', site_url());
|
||||
// throw an error if all connection attempts failed
|
||||
$site_url = ($site_url) ? $site_url : home_url();
|
||||
$parsed_url = parse_url($site_url);
|
||||
// 1. if the site URL does not contain a port, return the URL
|
||||
if(empty($parsed_url['port'])) return $site_url;
|
||||
// 2. connect to the URL with specified port
|
||||
$fp = @fsockopen($parsed_url['host'], $parsed_url['port'], $errno, $errstr, 1);
|
||||
if($fp) return $site_url;
|
||||
$port = (strtolower($parsed_url['scheme']) === 'http') ? 80 : 443;
|
||||
// 3. connect to the URL with default port
|
||||
$fp = @fsockopen($parsed_url['host'], $port, $errno, $errstr, 1);
|
||||
if($fp) return sprintf('%s://%s', $parsed_url['scheme'], $parsed_url['host']);
|
||||
// 4. throw an error if all connection attempts failed
|
||||
throw new \Exception(__('Site URL is unreachable.'));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user