From 9208eb2f45b43ff8730b0130e470f9c536990701 Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Wed, 26 Oct 2016 10:38:40 +1100 Subject: [PATCH 1/6] Method to get home URL --- lib/Site.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Site.php b/lib/Site.php index 386b4f6..f86ba91 100644 --- a/lib/Site.php +++ b/lib/Site.php @@ -58,6 +58,15 @@ public function getURL() { return $this->data->url; } + /** + * Get the home (URL) for a site. + * + * @return string + */ + public function getHome() { + return $this->data->home; + } + /** * Get the index URL for the API. * From b3b89bab0e3095c5c708cb8f31dce1bbb22c434c Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Sat, 12 Nov 2016 08:43:20 +1100 Subject: [PATCH 2/6] Handle different URL rel for API discovery --- lib/namespace.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/namespace.php b/lib/namespace.php index f5da48d..cadbde4 100644 --- a/lib/namespace.php +++ b/lib/namespace.php @@ -47,6 +47,7 @@ function discover_api_root( $uri, $legacy = false ) { } switch ( $attrs['rel'] ) { case 'https://api.w.org/': + case 'shortlink': break; case 'https://github.com/WP-API/WP-API': From 41764cf438f23072efb80169ec414175f1d52b5c Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Mon, 27 Feb 2017 21:35:16 +1100 Subject: [PATCH 3/6] Handle home not being set --- lib/Site.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Site.php b/lib/Site.php index f86ba91..1919e41 100644 --- a/lib/Site.php +++ b/lib/Site.php @@ -60,11 +60,12 @@ public function getURL() { /** * Get the home (URL) for a site. + * If none, return false. * - * @return string + * @return string|bool */ public function getHome() { - return $this->data->home; + return isset($this->data->home) ? $this->data->home : false; } /** From 71768f2444d60527b4689cadc9a22e57f2b8d20e Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Thu, 20 Apr 2017 18:43:07 +1000 Subject: [PATCH 4/6] Check for white space in body and remove --- lib/namespace.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/namespace.php b/lib/namespace.php index cadbde4..c8271f4 100644 --- a/lib/namespace.php +++ b/lib/namespace.php @@ -102,11 +102,16 @@ function parse_link_header( $link ) { function get_index_information( $url ) { $response = Requests::get( $url ); $response->throw_for_status(); + $body = $response->body; - $index = json_decode( $response->body ); + if (0 === strpos(bin2hex($body), 'efbbbf')) { + $body = substr($body, 3); + } + + $index = json_decode( $body ); if ( empty( $index ) && json_last_error() !== JSON_ERROR_NONE ) { throw new Exception( json_last_error_msg(), json_last_error() ); } return new Site( $index, $url ); -} +} \ No newline at end of file From 2724841059192fac2e52ec1dd5808a2c94ee5377 Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Tue, 2 May 2017 09:52:37 +0900 Subject: [PATCH 5/6] Check for discovering root failures --- lib/namespace.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/namespace.php b/lib/namespace.php index c8271f4..e4d9f18 100644 --- a/lib/namespace.php +++ b/lib/namespace.php @@ -18,6 +18,14 @@ function discover( $uri, $legacy = false ) { if ( empty( $root ) ) { return null; } + + // Step 1.5: If root matches URI, return default with wp-json. + $root = rtrim($root,"/"); + $uri = rtrim($uri,"/"); + + if ($root == $uri) { + $root .= '/wp-json'; + } // Step 2: Ask the API for information. return get_index_information( $root ); From 6b49f357686d350bff84a60b7d11ee0ce04489e6 Mon Sep 17 00:00:00 2001 From: Bryce Adams Date: Tue, 13 Jun 2017 20:27:14 +1000 Subject: [PATCH 6/6] Fix for multiple values on "Link" header See https://github.com/WP-API/discovery-php/pull/6 --- lib/namespace.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/namespace.php b/lib/namespace.php index e4d9f18..ae9429b 100644 --- a/lib/namespace.php +++ b/lib/namespace.php @@ -44,7 +44,8 @@ function discover_api_root( $uri, $legacy = false ) { $response = Requests::head( $uri ); $response->throw_for_status(); - $links = $response->headers->getValues( 'Link' ); + $header_value = $response->headers->getValues( 'Link' ); + $links = explode( ',', $header_value[0] ); // Find the correct link by relation foreach ( $links as $link ) {