home = self::without_scheme( rtrim( $home_url, '/' ) ); $this->origin = rtrim( $origin_url ? $origin_url : $home_url, '/' ); } /** * @param string $url Absolute URL. * * @return string|null URL on the origin without fragment, or null when the URL is not on the site. */ public function rebase( $url ) { $url = (string) $url; $fragment = strpos( $url, '#' ); if ( false !== $fragment ) { $url = substr( $url, 0, $fragment ); } $bare = self::without_scheme( $url ); foreach ( [ self::without_scheme( $this->origin ), $this->home ] as $base ) { if ( '' === $base || 0 !== stripos( $bare, $base ) ) { continue; } $rest = (string) substr( $bare, strlen( $base ) ); if ( '' === $rest ) { return $this->origin . '/'; } if ( '/' === $rest[0] || '?' === $rest[0] ) { return $this->origin . $rest; } } return null; } /** * @param string $url URL. * * @return string */ private static function without_scheme( $url ) { return (string) preg_replace( '#^([a-z][a-z0-9+.-]*:)?//#i', '', $url ); } }