PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / integration / class-opengraph.php

class-opengraph.php in ActivityPub trunk, at integration/class-opengraph.php

114 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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