Skip to content

Commit 8983629

Browse files
authored
Add the Fediverse creator of a post to opengraph (#786)
* add the fediverse creator of a post to opengraph see: * https://wordpress.org/plugins/opengraph/ * https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md * mastodon/mastodon#30398 * remove prefix until it is clearified * Do not add the metadata if it already exists
1 parent 0bb4489 commit 8983629

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

activitypub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ function plugin_init() {
101101
require_once __DIR__ . '/integration/class-enable-mastodon-apps.php';
102102
Integration\Enable_Mastodon_Apps::init();
103103

104+
require_once __DIR__ . '/integration/class-opengraph.php';
105+
Integration\Opengraph::init();
106+
104107
if ( \defined( 'JETPACK__VERSION' ) && ! \defined( 'IS_WPCOM' ) ) {
105108
require_once __DIR__ . '/integration/class-jetpack.php';
106109
Integration\Jetpack::init();

integration/class-opengraph.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace Activitypub\Integration;
3+
4+
use Activitypub\Collection\Users;
5+
6+
/**
7+
* Compatibility with the OpenGraph plugin
8+
*
9+
* @see https://wordpress.org/plugins/opengraph/
10+
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md
11+
* @see https://github.com/mastodon/mastodon/pull/30398
12+
*/
13+
class Opengraph {
14+
/**
15+
* Initialize the class, registering WordPress hooks
16+
*/
17+
public static function init() {
18+
// \add_filter( 'opengraph_prefixes', array( self::class, 'add_opengraph_prefixes' ) );
19+
\add_filter( 'opengraph_metadata', array( self::class, 'add_opengraph_metadata' ) );
20+
}
21+
22+
/**
23+
* Add the ActivityPub prefix to the OpenGraph prefixes.
24+
*
25+
* @param array $prefixes the current prefixes.
26+
*
27+
* @return array the updated prefixes.
28+
*/
29+
public static function add_opengraph_prefixes( $prefixes ) {
30+
// @todo discuss namespace
31+
$prefixes['fediverse'] = 'https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md';
32+
33+
return $prefixes;
34+
}
35+
36+
/**
37+
* Add the ActivityPub metadata to the OpenGraph metadata.
38+
*
39+
* @param array $metadata the current metadata.
40+
*
41+
* @return array the updated metadata.
42+
*/
43+
public static function add_opengraph_metadata( $metadata ) {
44+
// Do not add the metadata if it already exists
45+
if ( array_key_exists( 'fediverse:creator', $metadata ) ) {
46+
return $metadata;
47+
}
48+
49+
if ( \is_author() ) {
50+
$user_id = \get_queried_object_id();
51+
} elseif (
52+
\is_singular() &&
53+
\post_type_supports( get_post_type(), 'activitypub' )
54+
) {
55+
$user_id = \get_the_author_meta( 'ID' );
56+
} else {
57+
return $metadata;
58+
}
59+
60+
$user = Users::get_by_id( $user_id );
61+
62+
if ( ! $user || \is_wp_error( $user ) ) {
63+
return $metadata;
64+
}
65+
66+
// add WebFinger resource
67+
$metadata['fediverse:creator'] = $user->get_webfinger();
68+
69+
return $metadata;
70+
}
71+
}

0 commit comments

Comments
 (0)