From 034b38a58915b7a1870a8869cad1bb54063c7151 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:10:05 +0100 Subject: [PATCH 1/9] Ensure to use the URL --- integration/class-enable-mastodon-apps.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/integration/class-enable-mastodon-apps.php b/integration/class-enable-mastodon-apps.php index a412e4315..c874bcb9c 100644 --- a/integration/class-enable-mastodon-apps.php +++ b/integration/class-enable-mastodon-apps.php @@ -42,7 +42,7 @@ public static function init() { \add_filter( 'mastodon_api_search', array( self::class, 'api_search_by_url' ), 40, 2 ); \add_filter( 'mastodon_api_get_posts_query_args', array( self::class, 'api_get_posts_query_args' ) ); \add_filter( 'mastodon_api_statuses', array( self::class, 'api_statuses_external' ), 10, 2 ); - \add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 23 ); + \add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 3 ); \add_action( 'mastodon_api_update_credentials', array( self::class, 'api_update_credentials' ), 10, 2 ); } @@ -806,7 +806,14 @@ public static function api_get_replies( $context, $post_id, $url ) { return $context; } - foreach ( $replies['items'] as $url ) { + foreach ( $replies['items'] as $reply ) { + if ( is_string( $reply ) ){ + $url = $reply; + } elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) { + $url = $reply['url']; + } else { + continue; + } $response = Http::get( $url, true ); if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { continue; From 3988767e5322960bb22a408ee34427aebc5be1c4 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:16:32 +0100 Subject: [PATCH 2/9] Use provided status --- integration/class-enable-mastodon-apps.php | 30 ++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/integration/class-enable-mastodon-apps.php b/integration/class-enable-mastodon-apps.php index c874bcb9c..94f8bb98e 100644 --- a/integration/class-enable-mastodon-apps.php +++ b/integration/class-enable-mastodon-apps.php @@ -807,20 +807,24 @@ public static function api_get_replies( $context, $post_id, $url ) { } foreach ( $replies['items'] as $reply ) { - if ( is_string( $reply ) ){ - $url = $reply; - } elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) { - $url = $reply['url']; + if ( isset( $reply['id'] ) && is_string( $reply['id'] ) && isset( $reply['content'] ) && is_string( $reply['content'] ) ) { + $status = $reply; } else { - continue; - } - $response = Http::get( $url, true ); - if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { - continue; - } - $status = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! $status || is_wp_error( $status ) ) { - continue; + if ( is_string( $reply ) ) { + $url = $reply; + } elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) { + $url = $reply['url']; + } else { + continue; + } + $response = Http::get( $url, true ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { + continue; + } + $status = json_decode( wp_remote_retrieve_body( $response ), true ); + if ( ! $status || is_wp_error( $status ) ) { + continue; + } } $account = self::get_account_for_actor( $status['attributedTo'] ); From 2ef9e6e6f351c577c36d3775eb8593c446e195d6 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:18:54 +0100 Subject: [PATCH 3/9] Add changelog --- CHANGELOG.md | 6 ++++++ readme.txt | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a98f8116b..9fee30d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +### Fixed + +## [Unreleased] + +* Fetching replies for Enable Mastodon Apps + ## [4.6.0] - 2024-12-20 ### Added diff --git a/readme.txt b/readme.txt index 1d089a702..1c7da5ec3 100644 --- a/readme.txt +++ b/readme.txt @@ -132,6 +132,10 @@ For reasons of data protection, it is not possible to see the followers of other == Changelog == += Unreleased = + +* Fetching replies for Enable Mastodon Apps + = 4.6.0 = * Added: A filter to allow modifying the ActivityPub preview template From 1145e5c2e468f55fd57713bcd5389f82b3f556a0 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:22:06 +0100 Subject: [PATCH 4/9] typo --- integration/class-enable-mastodon-apps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/class-enable-mastodon-apps.php b/integration/class-enable-mastodon-apps.php index 94f8bb98e..3dbfdce24 100644 --- a/integration/class-enable-mastodon-apps.php +++ b/integration/class-enable-mastodon-apps.php @@ -812,7 +812,7 @@ public static function api_get_replies( $context, $post_id, $url ) { } else { if ( is_string( $reply ) ) { $url = $reply; - } elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) { + } elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) { $url = $reply['url']; } else { continue; From b6dddf0bd212b1c5e30da09c3c7f5c2600efea0d Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:32:25 +0100 Subject: [PATCH 5/9] Use replies that are already provided --- integration/class-enable-mastodon-apps.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/integration/class-enable-mastodon-apps.php b/integration/class-enable-mastodon-apps.php index 3dbfdce24..22c3b1f21 100644 --- a/integration/class-enable-mastodon-apps.php +++ b/integration/class-enable-mastodon-apps.php @@ -800,9 +800,15 @@ public static function api_get_replies( $context, $post_id, $url ) { return $context; } - $replies_url = $meta['replies']['first']['next']; - $replies = Http::get_remote_object( $replies_url, true ); - if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) { + if ( isset( $meta['replies']['first']['items'] ) ) { + $replies = $meta['replies']['first']; + } elseif ( isset( $meta['replies']['first']['next'] ) ) { + $replies_url = $meta['replies']['first']['next']; + $replies = Http::get_remote_object( $replies_url, true ); + if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) { + return $context; + } + } else { return $context; } From 441a21521f4657745308120e52bca0f3a94620af Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 31 Dec 2024 07:34:32 +0100 Subject: [PATCH 6/9] Do fetch replies if none are provided --- integration/class-enable-mastodon-apps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/class-enable-mastodon-apps.php b/integration/class-enable-mastodon-apps.php index 22c3b1f21..6bffa949e 100644 --- a/integration/class-enable-mastodon-apps.php +++ b/integration/class-enable-mastodon-apps.php @@ -800,7 +800,7 @@ public static function api_get_replies( $context, $post_id, $url ) { return $context; } - if ( isset( $meta['replies']['first']['items'] ) ) { + if ( ! empty( $meta['replies']['first']['items'] ) ) { $replies = $meta['replies']['first']; } elseif ( isset( $meta['replies']['first']['next'] ) ) { $replies_url = $meta['replies']['first']['next']; From 4634fe672e3c3a83427f28fffa658eac839df93f Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 6 Jan 2025 14:14:50 +0100 Subject: [PATCH 7/9] Fediverse Preview: Fix links (#1125) * Fix wp_kses for preview * We can use array_flip to keep the arrays lean * Changelog --- CHANGELOG.md | 1 + includes/constants.php | 8 ++++---- readme.txt | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fee30d55..495403587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] * Fetching replies for Enable Mastodon Apps +* Fediverse Preview: Fix links ## [4.6.0] - 2024-12-20 diff --git a/includes/constants.php b/includes/constants.php index f00582368..ba86ebae3 100644 --- a/includes/constants.php +++ b/includes/constants.php @@ -34,9 +34,9 @@ 'ACTIVITYPUB_MASTODON_HTML_SANITIZER', array( 'p' => array(), - 'span' => array( 'class' ), + 'span' => array_flip( array( 'class' ) ), 'br' => array(), - 'a' => array( 'href', 'rel', 'class' ), + 'a' => array_flip( array( 'href', 'rel', 'class' ) ), 'del' => array(), 'pre' => array(), 'code' => array(), @@ -46,8 +46,8 @@ 'i' => array(), 'u' => array(), 'ul' => array(), - 'ol' => array( 'start', 'reversed' ), - 'li' => array( 'value' ), + 'ol' => array_flip( array( 'start', 'reversed' ) ), + 'li' => array_flip( array( 'value' ) ), 'blockquote' => array(), 'h1' => array(), 'h2' => array(), diff --git a/readme.txt b/readme.txt index 1c7da5ec3..701f4dbe1 100644 --- a/readme.txt +++ b/readme.txt @@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other = Unreleased = * Fetching replies for Enable Mastodon Apps +* Fediverse Preview: Fix links = 4.6.0 = From 301c5eaa7efe1c8bd508e1c81f632f780806676e Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Wed, 8 Jan 2025 13:49:55 +0100 Subject: [PATCH 8/9] Remove array_flip --- includes/constants.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/constants.php b/includes/constants.php index ba86ebae3..f29aaf229 100644 --- a/includes/constants.php +++ b/includes/constants.php @@ -34,9 +34,9 @@ 'ACTIVITYPUB_MASTODON_HTML_SANITIZER', array( 'p' => array(), - 'span' => array_flip( array( 'class' ) ), + 'span' => array( 'class' => true ), 'br' => array(), - 'a' => array_flip( array( 'href', 'rel', 'class' ) ), + 'a' => array( 'href' => true, 'rel' => true, 'class' => true ), 'del' => array(), 'pre' => array(), 'code' => array(), @@ -46,8 +46,8 @@ 'i' => array(), 'u' => array(), 'ul' => array(), - 'ol' => array_flip( array( 'start', 'reversed' ) ), - 'li' => array_flip( array( 'value' ) ), + 'ol' => array( 'start' => true, 'reversed' => true ), + 'li' => array( 'value' => true ), 'blockquote' => array(), 'h1' => array(), 'h2' => array(), From 29536dad8de73d7c429238c1c473df501d41fdf6 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Wed, 8 Jan 2025 14:06:20 +0100 Subject: [PATCH 9/9] lint --- includes/constants.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/includes/constants.php b/includes/constants.php index f29aaf229..e12450205 100644 --- a/includes/constants.php +++ b/includes/constants.php @@ -36,7 +36,11 @@ 'p' => array(), 'span' => array( 'class' => true ), 'br' => array(), - 'a' => array( 'href' => true, 'rel' => true, 'class' => true ), + 'a' => array( + 'href' => true, + 'rel' => true, + 'class' => true, + ), 'del' => array(), 'pre' => array(), 'code' => array(), @@ -46,7 +50,10 @@ 'i' => array(), 'u' => array(), 'ul' => array(), - 'ol' => array( 'start' => true, 'reversed' => true ), + 'ol' => array( + 'start' => true, + 'reversed' => true, + ), 'li' => array( 'value' => true ), 'blockquote' => array(), 'h1' => array(),