From adc154dc58db1c86900ff8f735df7c154055249f Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 4 Jul 2024 15:06:17 +0200 Subject: [PATCH] Publicize: add Fediverse meta tag on posts when available See https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/ If we have an existing Mastodon connection on the site, let's add it as meta tag on each post. --- .../changelog/add-fediverse-tag-social | 4 ++ projects/packages/publicize/package.json | 2 +- .../publicize/src/class-publicize-base.php | 48 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 projects/packages/publicize/changelog/add-fediverse-tag-social diff --git a/projects/packages/publicize/changelog/add-fediverse-tag-social b/projects/packages/publicize/changelog/add-fediverse-tag-social new file mode 100644 index 0000000000000..d7e2865964e4e --- /dev/null +++ b/projects/packages/publicize/changelog/add-fediverse-tag-social @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Mastodon: display a Fediverse Creator tag when the post author has connected their account to a Mastodon account. diff --git a/projects/packages/publicize/package.json b/projects/packages/publicize/package.json index b47c13691b3b5..f938bb9acf31c 100644 --- a/projects/packages/publicize/package.json +++ b/projects/packages/publicize/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@automattic/jetpack-publicize", - "version": "0.47.1", + "version": "0.47.2-alpha", "description": "Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.", "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/publicize/#readme", "bugs": { diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index ca788a9ac7f3f..ca39206d5a747 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -254,6 +254,10 @@ public function __construct() { // The custom priority for this action ensures that any existing code that // removes post-thumbnails support during 'init' continues to work. add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 ); + + // Add a Fediverse Open Graph Tag when an author has connected their Mastodon account. + add_filter( 'jetpack_open_graph_tags', array( $this, 'add_fediverse_creator_open_graph_tag' ), 10, 1 ); + add_filter( 'jetpack_open_graph_output', array( $this, 'filter_fediverse_cards_output' ), 10, 1 ); } /** @@ -2128,6 +2132,50 @@ public function get_dismissed_notices() { public static function can_manage_connection( $connection_data ) { return current_user_can( 'edit_others_posts' ) || get_current_user_id() === (int) $connection_data['user_id']; } + + /** + * Display a Fediverse actor Open Graph tag when the post author has a Mastodon connection. + * + * @see https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/ + * + * @param array $tags Current tags. + * + * @return array + */ + public function add_fediverse_creator_open_graph_tag( $tags ) { + if ( ! is_singular() ) { + return $tags; + } + + // Loop through active connections. + foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { + if ( 'mastodon' !== $service_name ) { + continue; + } + + // services have multiple connections. + foreach ( $connections as $connection ) { + if ( ! empty( $connection['external_display'] ) ) { + $tags['fediverse:creator'] = esc_attr( $connection['external_display'] ); + } + break; + } + } + + return $tags; + } + + /** + * Update the markup for the Open Graph tag to match the expected output for Mastodon + * (name instead of property). + * + * @param string $og_tag A single OG tag. + * + * @return string Result of the OG tag. + */ + public static function filter_fediverse_cards_output( $og_tag ) { + return ( str_contains( $og_tag, 'fediverse:' ) ) ? preg_replace( '/property="([^"]+)"/', 'name="\1"', $og_tag ) : $og_tag; + } } // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.