| 1 |
<?php |
| 2 |
namespace Activitypub\Integration; |
| 3 |
|
| 4 |
use Activitypub\Model\Blog; |
| 5 |
use Activitypub\Collection\Users; |
| 6 |
|
| 7 |
use function Activitypub\is_single_user; |
| 8 |
use function Activitypub\is_user_type_disabled; |
| 9 |
|
| 10 |
/** |
| 11 |
* Compatibility with the OpenGraph plugin |
| 12 |
* |
| 13 |
* @see https://wordpress.org/plugins/opengraph/ |
| 14 |
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md |
| 15 |
* @see https://github.com/mastodon/mastodon/pull/30398 |
| 16 |
*/ |
| 17 |
class Opengraph { |
| 18 |
/** |
| 19 |
* Initialize the class, registering WordPress hooks |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
if ( ! function_exists( 'opengraph_metadata' ) ) { |
| 23 |
\add_action( 'wp_head', array( self::class, 'add_meta_tags' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
\add_filter( 'opengraph_metadata', array( self::class, 'add_metadata' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add the ActivityPub prefix to the OpenGraph prefixes. |
| 31 |
* |
| 32 |
* @param array $prefixes the current prefixes. |
| 33 |
* |
| 34 |
* @return array the updated prefixes. |
| 35 |
*/ |
| 36 |
public static function add_prefixes( $prefixes ) { |
| 37 |
// @todo discuss namespace |
| 38 |
$prefixes['fediverse'] = 'https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md'; |
| 39 |
|
| 40 |
return $prefixes; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add the ActivityPub metadata to the OpenGraph metadata. |
| 45 |
* |
| 46 |
* @param array $metadata the current metadata. |
| 47 |
* |
| 48 |
* @return array the updated metadata. |
| 49 |
*/ |
| 50 |
public static function add_metadata( $metadata ) { |
| 51 |
// Always show Blog-User if the Blog is in single user mode |
| 52 |
if ( is_single_user() ) { |
| 53 |
$user = new Blog(); |
| 54 |
|
| 55 |
// add WebFinger resource |
| 56 |
$metadata['fediverse:creator'] = $user->get_webfinger(); |
| 57 |
|
| 58 |
return $metadata; |
| 59 |
} |
| 60 |
|
| 61 |
if ( \is_author() ) { |
| 62 |
// Use the Author of the Archive-Page |
| 63 |
$user_id = \get_queried_object_id(); |
| 64 |
} elseif ( \is_singular() ) { |
| 65 |
// Use the Author of the Post |
| 66 |
$user_id = \get_post_field( 'post_author', \get_queried_object_id() ); |
| 67 |
} elseif ( ! is_user_type_disabled( 'blog' ) ) { |
| 68 |
// Use the Blog-User for any other page, if the Blog-User is not disabled |
| 69 |
$user_id = Users::BLOG_USER_ID; |
| 70 |
} else { |
| 71 |
// Do not add any metadata otherwise |
| 72 |
return $metadata; |
| 73 |
} |
| 74 |
|
| 75 |
$user = Users::get_by_id( $user_id ); |
| 76 |
|
| 77 |
if ( ! $user || \is_wp_error( $user ) ) { |
| 78 |
return $metadata; |
| 79 |
} |
| 80 |
|
| 81 |
// add WebFinger resource |
| 82 |
$metadata['fediverse:creator'] = $user->get_webfinger(); |
| 83 |
|
| 84 |
return $metadata; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Output Open Graph <meta> tags in the page header. |
| 89 |
*/ |
| 90 |
public static function add_meta_tags() { |
| 91 |
$metadata = apply_filters( 'opengraph_metadata', array() ); |
| 92 |
foreach ( $metadata as $key => $value ) { |
| 93 |
if ( empty( $key ) || empty( $value ) ) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
$value = (array) $value; |
| 97 |
|
| 98 |
foreach ( $value as $v ) { |
| 99 |
printf( |
| 100 |
'<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL, |
| 101 |
esc_attr( $key ), |
| 102 |
esc_attr( $v ) |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|