Skip to content

Commit

Permalink
FIX: "Undefined index: HTTP_HOST" for no web server (#60)
Browse files Browse the repository at this point in the history
* Added a null guard for the dsq_filter_rest_url. Added functionality tests to ensure dsq_filter_rest_url does not error when _SERVER host is not set.
  • Loading branch information
gddh authored and dan-disqus committed Sep 5, 2018
1 parent 2df9eb9 commit 04e9f8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion disqus/admin/class-disqus-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function dsq_filter_rest_url( $rest_url ) {
$rest_host .= ':' . $rest_url_parts['port'];
}

$current_host = $_SERVER['HTTP_HOST'];
$current_host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : $rest_host;

if ( $rest_host !== $current_host ) {
$rest_url = preg_replace( '/' . $rest_host . '/', $current_host, $rest_url, 1 );
Expand Down
14 changes: 14 additions & 0 deletions tests/test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ function test_dsq_filter_rest_url_different_host() {
$_SERVER['HTTP_HOST'] = $previous_host;
}

/**
* Ensure that REST URL filter will not error when HTTP_HOST is undefined.
*/
function test_dsq_filter_rest_url_no_host() {
$admin = new Disqus_Admin( 'disqus', '0.0.0', 'foo' );
$previous_host = $_SERVER['HTTP_HOST'];
$_SERVER['HTTP_HOST'] = NULL;
$init_url = 'https://example.org/wp-json/disqus/v1';

$rest_url = $admin->dsq_filter_rest_url($init_url);

$this->assertEquals( $init_url, $rest_url );
}

}

0 comments on commit 04e9f8e

Please sign in to comment.