Support Blog-User only mode! (#795)

* fix #794

* do not require the OpenGraph plugin
This commit is contained in:
Matthias Pfefferle 2024-07-02 11:28:57 +02:00 committed by GitHub
parent 5f595c6354
commit 94b2f01df4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,8 +1,12 @@
<?php
namespace Activitypub\Integration;
use Activitypub\Model\Blog;
use Activitypub\Collection\Users;
use function Activitypub\is_single_user;
use function Activitypub\is_user_type_disabled;
/**
* Compatibility with the OpenGraph plugin
*
@ -15,8 +19,11 @@ 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' ) );
if ( ! function_exists( 'opengraph_metadata' ) ) {
\add_action( 'wp_head', array( self::class, 'add_meta_tags' ) );
}
\add_filter( 'opengraph_metadata', array( self::class, 'add_metadata' ) );
}
/**
@ -26,7 +33,7 @@ class Opengraph {
*
* @return array the updated prefixes.
*/
public static function add_opengraph_prefixes( $prefixes ) {
public static function add_prefixes( $prefixes ) {
// @todo discuss namespace
$prefixes['fediverse'] = 'https://codeberg.org/fediverse/fep/src/branch/main/fep/XXXX/fep-XXXX.md';
@ -40,20 +47,36 @@ class Opengraph {
*
* @return array the updated metadata.
*/
public static function add_opengraph_metadata( $metadata ) {
public static function add_metadata( $metadata ) {
// Do not add the metadata if it already exists
if ( array_key_exists( 'fediverse:creator', $metadata ) ) {
return $metadata;
}
// Always show Blog-User if the Blog is in single user mode
if ( is_single_user() ) {
$user = new Blog();
// add WebFinger resource
$metadata['fediverse:creator'] = $user->get_webfinger();
return $metadata;
}
if ( \is_author() ) {
// Use the Author of the Archive-Page
$user_id = \get_queried_object_id();
} elseif (
\is_singular() &&
\post_type_supports( get_post_type(), 'activitypub' )
) {
$user_id = \get_the_author_meta( 'ID' );
// Use the Author of the Post
$user_id = \get_post_field( 'post_author', \get_queried_object_id() );
} elseif ( ! is_user_type_disabled( 'blog' ) ) {
// Use the Blog-User for any other page, if the Blog-User is not disabled
$user_id = Users::BLOG_USER_ID;
} else {
// Do not add any metadata otherwise
return $metadata;
}
@ -68,4 +91,25 @@ class Opengraph {
return $metadata;
}
/**
* Output Open Graph <meta> tags in the page header.
*/
public static function add_meta_tags() {
$metadata = apply_filters( 'opengraph_metadata', array() );
foreach ( $metadata as $key => $value ) {
if ( empty( $key ) || empty( $value ) ) {
continue;
}
$value = (array) $value;
foreach ( $value as $v ) {
printf(
'<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
esc_attr( $key ),
esc_attr( $v )
);
}
}
}
}