-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |