From ab2e7088968a3c62468daf56b4a3e7f82055e1d5 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 19 Dec 2024 14:16:13 -0600 Subject: [PATCH] Use get_remote_object() to, well, get the remote objects Props @pfefferle. --- includes/functions.php | 185 ++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 105 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index e56059b85..0b4e1d9a3 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1611,125 +1611,100 @@ function embed_get( $url ) { return $embed; } - // Create unique transient keys for both HTTP calls. - $main_transient_key = '_activitypub_embed_' . md5( $url ); - $embed = \get_transient( $main_transient_key ); - - if ( false === $embed ) { - $response = Http::get( $url ); - if ( \wp_remote_retrieve_response_code( $response ) === 200 ) { - $embed = \json_decode( \wp_remote_retrieve_body( $response ), true ); - \set_transient( $main_transient_key, $embed, DAY_IN_SECONDS ); - } else { - return \wp_oembed_get( $url ); - } + $embed = Http::get_remote_object( $url ); + if ( is_wp_error( $embed ) ) { + return false; } - if ( $embed ) { - $author_name = isset( $embed['attributedTo'] ) ? $embed['attributedTo'] : ''; - $author_url = $author_name; - $avatar_url = isset( $embed['icon']['url'] ) ? $embed['icon']['url'] : ''; - - // If we don't have an avatar URL but we have an author URL, try to fetch it. - if ( ! $avatar_url && $author_url ) { - $author_transient_key = '_activitypub_author_' . md5( $author_url ); - $author = \get_transient( $author_transient_key ); - - if ( false === $author ) { - $author_response = Http::get( $author_url ); - if ( \wp_remote_retrieve_response_code( $author_response ) === 200 ) { - $author = \json_decode( \wp_remote_retrieve_body( $author_response ), true ); - \set_transient( $author_transient_key, $author, DAY_IN_SECONDS ); - } - } + $author_name = isset( $embed['attributedTo'] ) ? $embed['attributedTo'] : ''; + $author_url = $author_name; + $avatar_url = isset( $embed['icon']['url'] ) ? $embed['icon']['url'] : ''; - if ( $author ) { - if ( isset( $author['icon']['url'] ) ) { - $avatar_url = $author['icon']['url']; - } - if ( isset( $author['name'] ) ) { - $author_name = $author['name']; - } - } - } + // If we don't have an avatar URL but we have an author URL, try to fetch it. + if ( ! $avatar_url && $author_url ) { + $author = Http::get_remote_object( $author_url ); - $published = isset( $embed['published'] ) ? \gmdate( get_option( 'date_format' ) . ', ' . get_option( 'time_format' ), \strtotime( $embed['published'] ) ) : ''; - $title = isset( $embed['name'] ) ? $embed['name'] : ''; - $content = isset( $embed['content'] ) ? $embed['content'] : ''; - $boosts = isset( $embed['shares']['totalItems'] ) ? (int) $embed['shares']['totalItems'] : 0; - $favorites = isset( $embed['likes']['totalItems'] ) ? (int) $embed['likes']['totalItems'] : 0; - - $image = ''; - if ( isset( $embed['image']['url'] ) ) { - $image = $embed['image']['url']; - } elseif ( isset( $embed['attachment'] ) ) { - foreach ( $embed['attachment'] as $attachment ) { - if ( isset( $attachment['type'] ) && 'Document' === $attachment['type'] ) { - $image = $attachment['url']; - break; - } + if ( ! is_wp_error( $author ) ) { + if ( isset( $author['icon']['url'] ) ) { + $avatar_url = $author['icon']['url']; + } + if ( isset( $author['name'] ) ) { + $author_name = $author['name']; } } + } - ob_start(); - ?> -
-
- - - -
-

- - - -
-
+ $published = isset( $embed['published'] ) ? \gmdate( get_option( 'date_format' ) . ', ' . get_option( 'time_format' ), \strtotime( $embed['published'] ) ) : ''; + $title = isset( $embed['name'] ) ? $embed['name'] : ''; + $content = isset( $embed['content'] ) ? $embed['content'] : ''; + $boosts = isset( $embed['shares']['totalItems'] ) ? (int) $embed['shares']['totalItems'] : 0; + $favorites = isset( $embed['likes']['totalItems'] ) ? (int) $embed['likes']['totalItems'] : 0; -
- -

- + $image = ''; + if ( isset( $embed['image']['url'] ) ) { + $image = $embed['image']['url']; + } elseif ( isset( $embed['attachment'] ) ) { + foreach ( $embed['attachment'] as $attachment ) { + if ( isset( $attachment['type'] ) && 'Document' === $attachment['type'] ) { + $image = $attachment['url']; + break; + } + } + } - -
- + \wp_enqueue_style( 'activitypub-embed', ACTIVITYPUB_PLUGIN_URL . 'assets/css/activitypub-embed.css', array(), ACTIVITYPUB_PLUGIN_VERSION ); - -
- -
+ ob_start(); + ?> +
+
+ + + +
+

+ +
+
-
- - - +
+ +

+ - - ' . \esc_html( $boosts ) . '' ); - ?> - - - - ' . \esc_html( $favorites ) . '' ); - ?> - -
-
- +
+ - if ( $output ) { - \wp_enqueue_style( 'activitypub-embed', ACTIVITYPUB_PLUGIN_URL . 'assets/css/activitypub-embed.css', array(), ACTIVITYPUB_PLUGIN_VERSION ); - return $output; - } - } + +
+ +
+ +
- return false; +
+ + + + + + ' . \esc_html( $boosts ) . '' ); + ?> + + + + ' . \esc_html( $favorites ) . '' ); + ?> + +
+
+