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