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

Remove discovery on each load if Client is Already Cached #276

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion includes/class-indieauth-authorization-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function authorize() {
$current_user = wp_get_current_user();
// phpcs:disable
$client_id = esc_url_raw( wp_unslash( $_GET['client_id'] ) ); // WPCS: CSRF OK
$client_term = IndieAuth_Client_Taxonomy::add_client_with_discovery( $client_id );
$client_term = IndieAuth_Client_Taxonomy::add_client( $client_id );
if ( ! is_wp_error( $client_term ) ) {
$client_name = $client_term['name'];
$client_icon = $client_term['icon'];
Expand Down
18 changes: 8 additions & 10 deletions includes/class-indieauth-client-discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public function __construct( $client_id ) {
if ( 'localhost' === wp_parse_url( $client_id, PHP_URL_HOST ) ) {
return;
}

error_log( 'Pre-Parse' );
$response = self::parse( $client_id );
error_log( 'Post-Parse' );
if ( is_wp_error( $response ) ) {
error_log( __( 'Failed to Retrieve IndieAuth Client Details ', 'indieauth' ) . wp_json_encode( $response ) ); // phpcs:ignore
return;
Expand Down Expand Up @@ -113,15 +114,8 @@ private function parse( $url ) {
$this->client_uri = $this->json['client_uri'];
}
} elseif ( 'text/html' === $content_type ) {
$content = wp_remote_retrieve_body( $response );
$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 );
$this->get_mf2( $domdocument, $url );
$content = wp_remote_retrieve_body( $response );
$this->get_mf2( $content, $url );
if ( ! empty( $this->mf2 ) ) {
if ( array_key_exists( 'name', $this->mf2 ) ) {
$this->client_name = $this->mf2['name'][0];
Expand Down Expand Up @@ -192,6 +186,10 @@ public function get_name() {
return $this->client_name;
}

public function get_uri() {
return $this->client_uri;
}

// Separate function for possible improved size picking later
private function determine_icon( $input ) {
if ( ! is_array( $input ) || empty( $input ) ) {
Expand Down
73 changes: 53 additions & 20 deletions includes/class-indieauth-client-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public static function register() {
'public' => true,
'publicly_queryable' => true,
'hierarchical' => false,
'show_ui' => false,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => true,
'show_in_nav_menus' => false,
'show_in_rest' => false,
'show_tagcloud' => false,
'show_in_quick_edit' => false,
Expand All @@ -79,14 +79,29 @@ public static function register() {
'show_in_rest' => true,
)
);
}

/**
* Add Client from Discovery
*/
public static function add_client_with_discovery( $url ) {
$client = new IndieAuth_Client_Discovery( $url );
return self::add_client( $url, $client->get_name(), $client->get_icon() );
register_meta(
'term',
'client_uri',
array(
'object_subtype' => 'indieauth_client',
'type' => 'string',
'description' => __( 'IndieAuth Client Application URI', 'indieauth' ),
'single' => true,
'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
)
);
register_meta(
'term',
'last_modified',
array(
'object_subtype' => 'indieauth_client',
'type' => 'integer',
'description' => __( 'Last Modified Client Timestamp', 'indieauth' ),
'single' => true,
'show_in_rest' => true,
)
);
}

/**
Expand Down Expand Up @@ -133,14 +148,21 @@ public static function add_client( $url, $name = null, $icon = null ) {
return $exists;
}

$client_uri = '';

if ( empty( $name ) ) {
$client = new IndieAuth_Client_Discovery( $url );
if ( defined( 'INDIEAUTH_UNIT_TESTS' ) ) {
return array(
'client_id' => $url,
);
}
return self::add_client( $url, $client->get_name(), $client->get_icon() );
$name = $client->get_name();
$icon = $client->get_icon();
$client_uri = $client->get_uri();
if ( empty( $name ) ) {
$name = self::generate_slug( $url );
}
}

$icon = self::sideload_icon( $icon, $url );
Expand All @@ -156,7 +178,14 @@ public static function add_client( $url, $name = null, $icon = null ) {
if ( is_wp_error( $term ) ) {
return $term;
}
add_term_meta( $term['term_id'], 'icon', $icon );
if ( ! empty( $icon ) ) {
add_term_meta( $term['term_id'], 'icon', $icon );
}
if ( ! empty( $client_uri ) ) {
add_term_meta( $term['term_id'], 'client_uri', $client_uri );
}

add_term_meta( $term['term_id'], 'last_modified', time() );
return array_filter(
array(
'url' => $url,
Expand All @@ -182,10 +211,12 @@ public static function get_client( $url = null ) {
$clients = array();
foreach ( $terms as $term ) {
$clients[] = array(
'url' => $term->description,
'name' => $term->name,
'id' => $term->term_id,
'icon' => get_term_meta( $term->term_id, 'icon', true ),
'url' => $term->description,
'name' => $term->name,
'id' => $term->term_id,
'icon' => get_term_meta( $term->term_id, 'icon', true ),
'uri' => get_term_meta( $term->term_id, 'client_uri', true ),
'last_modified' => get_term_meta( $term->term_id, 'last_modified', true ),
);
}
return $clients;
Expand Down Expand Up @@ -214,10 +245,12 @@ public static function get_client( $url = null ) {
$term = $terms[0];

return array(
'url' => $term->description,
'name' => $term->name,
'id' => $term->term_id,
'icon' => get_term_meta( $term->term_id, 'icon', true ),
'url' => $term->description,
'name' => $term->name,
'id' => $term->term_id,
'icon' => get_term_meta( $term->term_id, 'icon', true ),
'uri' => get_term_meta( $term->term_id, 'client_uri', true ),
'last_modified' => get_term_meta( $term->term_id, 'last_modified', true ),
);
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-indieauth-token-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function generate_token_response( $response ) {

// Issue a token
if ( ! empty( $scopes ) ) {
$client = IndieAuth_Client_Taxonomy::add_client_with_discovery( $response['client_id'] );
$client = IndieAuth_Client_Taxonomy::add_client( $response['client_id'] );
if ( is_wp_error( $client ) ) {
$client = array( 'id' => $client->get_error_message() );
}
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.5.1
* Version: 4.5.2
* Author: IndieWeb WordPress Outreach Club
* Author URI: https://indieweb.org/WordPress_Outreach_Club
* License: MIT
Expand Down
24 changes: 16 additions & 8 deletions languages/indieauth.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# This file is distributed under the MIT.
msgid ""
msgstr ""
"Project-Id-Version: IndieAuth 4.5.1\n"
"Project-Id-Version: IndieAuth 4.5.2\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/indieauth\n"
"POT-Creation-Date: 2024-08-14 00:32:52+00:00\n"
"POT-Creation-Date: 2024-08-26 02:02:31+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 @@ -375,19 +375,19 @@ msgstr ""
msgid "Invalid access token"
msgstr ""

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

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

#: includes/class-indieauth-client-discovery.php:100
#: includes/class-indieauth-client-discovery.php:101
msgid "Discovery Has Returned an Empty JSON Document"
msgstr ""

#: includes/class-indieauth-client-discovery.php:103
#: includes/class-indieauth-client-discovery.php:104
msgid "No Client ID Found in JSON Client Metadata"
msgstr ""

Expand All @@ -399,11 +399,19 @@ msgstr ""
msgid "IndieAuth Client Application Icon"
msgstr ""

#: includes/class-indieauth-client-taxonomy.php:207
#: includes/class-indieauth-client-taxonomy.php:88
msgid "IndieAuth Client Application URI"
msgstr ""

#: includes/class-indieauth-client-taxonomy.php:100
msgid "Last Modified Client Timestamp"
msgstr ""

#: includes/class-indieauth-client-taxonomy.php:238
msgid "No Term Found"
msgstr ""

#: includes/class-indieauth-client-taxonomy.php:211
#: includes/class-indieauth-client-taxonomy.php:242
msgid "Multiple Terms Found"
msgstr ""

Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
**Requires at least:** 4.9.9
**Requires PHP:** 7.2
**Tested up to:** 6.6
**Stable tag:** 4.5.1
**Stable tag:** 4.5.2
**License:** MIT
**License URI:** http://opensource.org/licenses/MIT
**Donate link:** https://opencollective.com/indieweb
Expand Down Expand Up @@ -189,6 +189,10 @@ In version 2.0, we added an IndieAuth endpoint to this plugin, which previously

Project and support maintained on github at [indieweb/wordpress-indieauth](https://github.com/indieweb/wordpress-indieauth).

### 4.5.2 ###
* Fix issue with loop on adding new clients
* Store client_uri and last modified date for new clients.

### 4.5.1 ###
* Fix issue with failure if logo_uri is not a URL
* Fix conflict with Jetpack plugin due not returning error property (props @janboddez)
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: IndieAuth, IndieWeb, IndieWebCamp, login
Requires at least: 4.9.9
Requires PHP: 7.2
Tested up to: 6.6
Stable tag: 4.5.1
Stable tag: 4.5.2
License: MIT
License URI: http://opensource.org/licenses/MIT
Donate link: https://opencollective.com/indieweb
Expand Down Expand Up @@ -189,6 +189,10 @@ In version 2.0, we added an IndieAuth endpoint to this plugin, which previously

Project and support maintained on github at [indieweb/wordpress-indieauth](https://github.com/indieweb/wordpress-indieauth).

= 4.5.2 =
* Fix issue with loop on adding new clients
* Store client_uri and last modified date for new clients.

= 4.5.1 =
* Fix issue with failure if logo_uri is not a URL
* Fix conflict with Jetpack plugin due not returning error property (props @janboddez)
Expand Down
Loading