Skip to content

Commit

Permalink
Add the Fediverse creator of a post to opengraph (#786)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pfefferle committed Jul 1, 2024
1 parent 0bb4489 commit 8983629
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function plugin_init() {
require_once __DIR__ . '/integration/class-enable-mastodon-apps.php';
Integration\Enable_Mastodon_Apps::init();

require_once __DIR__ . '/integration/class-opengraph.php';
Integration\Opengraph::init();

if ( \defined( 'JETPACK__VERSION' ) && ! \defined( 'IS_WPCOM' ) ) {
require_once __DIR__ . '/integration/class-jetpack.php';
Integration\Jetpack::init();
Expand Down
71 changes: 71 additions & 0 deletions integration/class-opengraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Activitypub\Integration;

use Activitypub\Collection\Users;

/**
* Compatibility with the OpenGraph plugin
*
* @see https://wordpress.org/plugins/opengraph/
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md
* @see https://github.com/mastodon/mastodon/pull/30398
*/
class Opengraph {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
// \add_filter( 'opengraph_prefixes', array( self::class, 'add_opengraph_prefixes' ) );
\add_filter( 'opengraph_metadata', array( self::class, 'add_opengraph_metadata' ) );
}

/**
* Add the ActivityPub prefix to the OpenGraph prefixes.
*
* @param array $prefixes the current prefixes.
*
* @return array the updated prefixes.
*/
public static function add_opengraph_prefixes( $prefixes ) {
// @todo discuss namespace
$prefixes['fediverse'] = 'https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md';

return $prefixes;
}

/**
* Add the ActivityPub metadata to the OpenGraph metadata.
*
* @param array $metadata the current metadata.
*
* @return array the updated metadata.
*/
public static function add_opengraph_metadata( $metadata ) {
// Do not add the metadata if it already exists
if ( array_key_exists( 'fediverse:creator', $metadata ) ) {
return $metadata;
}

if ( \is_author() ) {
$user_id = \get_queried_object_id();
} elseif (
\is_singular() &&
\post_type_supports( get_post_type(), 'activitypub' )
) {
$user_id = \get_the_author_meta( 'ID' );
} else {
return $metadata;
}

$user = Users::get_by_id( $user_id );

if ( ! $user || \is_wp_error( $user ) ) {
return $metadata;
}

// add WebFinger resource
$metadata['fediverse:creator'] = $user->get_webfinger();

return $metadata;
}
}

0 comments on commit 8983629

Please sign in to comment.