Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preliminary support for json metadata client discovery #268

Merged
merged 4 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 53 additions & 34 deletions includes/class-indieauth-client-discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class IndieAuth_Client_Discovery {
protected $manifest = array();
protected $html = array();
protected $mf2 = array();
protected $json = array();
dshanske marked this conversation as resolved.
Show resolved Hide resolved
public $client_id = '';
public $client_name = '';
public $client_icon = '';
Expand Down Expand Up @@ -74,45 +75,63 @@ private function parse( $url ) {
return $response;
}

$content = wp_remote_retrieve_body( $response );

if ( class_exists( 'Masterminds\\HTML5' ) ) {
$domdocument = new \Masterminds\HTML5( array( 'disable_html_ns' => true ) );
$domdocument = $domdocument->loadHTML( $content );
} else {
$domdocument = new DOMDocument();
libxml_use_internal_errors( true );
if ( function_exists( 'mb_convert_encoding' ) ) {
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', mb_detect_encoding( $content ) );
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse() seems like it should be designated as static since that's how it is used. Also it seems as though it could get to the bottom and not return so I think it needs a return at the bottom. I also suggest a doc block describe the params and what it does if there's an error.

if ( 'application/json' === $content_type ) {
$json = json_decode( wp_remote_retrieve_body( $response ), true );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$json = json_decode( wp_remote_retrieve_body( $response ), true );
/** @var $json array{
* client_id: string,
* client_name: string,
* logo_uri: string
* }
*/
$json = json_decode( wp_remote_retrieve_body( $response ), true );

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like using comments to describe what we expect to be in json like this since it makes identifying and using them slightly easier.

if ( ! is_array( $json ) && empty( $json ) ) {
dshanske marked this conversation as resolved.
Show resolved Hide resolved
return;
}
$domdocument->loadHTML( $content );
libxml_use_internal_errors( false );
}

$this->get_mf2( $domdocument, $url );
if ( ! empty( $this->mf2 ) ) {
if ( array_key_exists( 'name', $this->mf2 ) ) {
$this->client_name = $this->mf2['name'][0];
if ( ! array_key_exists( 'client_id', $json ) ) {
return;
}
$this->client_id = $json['client_id'];
if ( array_key_exists( 'client_name', $json ) ) {
$this->client_name = $json['client_name'];
}
if ( array_key_exists( 'logo_uri', $json ) ) {
$this->client_icon = $json['logo_uri'];
}
if ( array_key_exists( 'logo', $this->mf2 ) ) {
if ( is_string( $this->mf2['logo'][0] ) ) {
$this->client_icon = $this->mf2['logo'][0];
} else {
$this->client_icon = $this->mf2['logo'][0]['value'];
} elseif ( 'text/html' === $content_type ) {
$content = wp_remote_retrieve_body( $response );

if ( class_exists( 'Masterminds\\HTML5' ) ) {
$domdocument = new \Masterminds\HTML5( array( 'disable_html_ns' => true ) );
dshanske marked this conversation as resolved.
Show resolved Hide resolved
$domdocument = $domdocument->loadHTML( $content );
} else {
$domdocument = new DOMDocument();
libxml_use_internal_errors( true );
if ( function_exists( 'mb_convert_encoding' ) ) {
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', mb_detect_encoding( $content ) );
}
$domdocument->loadHTML( $content );
libxml_use_internal_errors( false );
}
} elseif ( isset( $this->rels['manifest'] ) ) {
self::get_manifest( $this->rels['manifest'] );
$this->client_icon = $this->determine_icon( $this->manifest );
$this->client_name = $this->manifest['name'];
} else {
$this->client_icon = $this->determine_icon( $this->rels );
$this->get_html( $domdocument );
$this->client_name = $this->html['title'];
}

if ( ! empty( $this->client_icon ) ) {
$this->client_icon = WP_Http::make_absolute_url( $this->client_icon, $url );
$this->get_mf2( $domdocument, $url );
if ( ! empty( $this->mf2 ) ) {
if ( array_key_exists( 'name', $this->mf2 ) ) {
$this->client_name = $this->mf2['name'][0];
}
if ( array_key_exists( 'logo', $this->mf2 ) ) {
if ( is_string( $this->mf2['logo'][0] ) ) {
$this->client_icon = $this->mf2['logo'][0];
} else {
$this->client_icon = $this->mf2['logo'][0]['value'];
}
}
} elseif ( isset( $this->rels['manifest'] ) ) {
self::get_manifest( $this->rels['manifest'] );
$this->client_icon = $this->determine_icon( $this->manifest );
$this->client_name = $this->manifest['name'];
} else {
$this->client_icon = $this->determine_icon( $this->rels );
$this->get_html( $domdocument );
$this->client_name = $this->html['title'];
}

if ( ! empty( $this->client_icon ) ) {
$this->client_icon = WP_Http::make_absolute_url( $this->client_icon, $url );
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion indieauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: IndieAuth
* Plugin URI: https://github.com/indieweb/wordpress-indieauth/
* Description: IndieAuth is a way to allow users to use their own domain to sign into other websites and services
* Version: 4.4.2
* Version: 4.4.3
* Author: IndieWeb WordPress Outreach Club
* Author URI: https://indieweb.org/WordPress_Outreach_Club
* License: MIT
Expand Down
15 changes: 7 additions & 8 deletions languages/indieauth.pot
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Copyright (C) 2024 IndieWebCamp WordPress Outreach Club
# Copyright (C) 2024 IndieWeb WordPress Outreach Club
# This file is distributed under the MIT.
msgid ""
msgstr ""
"Project-Id-Version: IndieAuth 4.4.2\n"
"Report-Msgid-Bugs-To: "
"https://wordpress.org/support/plugin/wordpress-indieauth\n"
"POT-Creation-Date: 2024-01-12 20:14:40+00:00\n"
"Project-Id-Version: IndieAuth 4.4.3\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/indieauth\n"
"POT-Creation-Date: 2024-06-17 21:55:46+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down Expand Up @@ -376,11 +375,11 @@ msgstr ""
msgid "Invalid access token"
msgstr ""

#: includes/class-indieauth-client-discovery.php:33
#: includes/class-indieauth-client-discovery.php:34
msgid "Failed to Retrieve IndieAuth Client Details "
msgstr ""

#: includes/class-indieauth-client-discovery.php:63
#: includes/class-indieauth-client-discovery.php:64
msgid "Failed to Retrieve Client Details"
msgstr ""

Expand Down Expand Up @@ -900,7 +899,7 @@ msgid ""
msgstr ""

#. Author of the plugin/theme
msgid "IndieWebCamp WordPress Outreach Club"
msgid "IndieWeb WordPress Outreach Club"
msgstr ""

#. Author URI of the plugin/theme
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
**Tags:** IndieAuth, IndieWeb, IndieWebCamp, login
**Requires at least:** 4.9.9
**Requires PHP:** 5.6
**Tested up to:** 6.4
**Stable tag:** 4.4.2
**Tested up to:** 6.5
**Stable tag:** 4.4.3
**License:** MIT
**License URI:** http://opensource.org/licenses/MIT
**Donate link:** https://opencollective.com/indieweb
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: indieweb, pfefferle, dshanske
Tags: IndieAuth, IndieWeb, IndieWebCamp, login
Requires at least: 4.9.9
Requires PHP: 5.6
Tested up to: 6.4
Stable tag: 4.4.2
Tested up to: 6.5
Stable tag: 4.4.3
License: MIT
License URI: http://opensource.org/licenses/MIT
Donate link: https://opencollective.com/indieweb
Expand Down
Loading