Skip to content

Commit e535b9c

Browse files
committed
Add basic BuddyPress support
fix #122 thanks and props @skysarwer
1 parent 5032e5e commit e535b9c

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

activitypub.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ function flush_rewrite_rules() {
106106
}
107107
\register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' );
108108
\register_deactivation_hook( __FILE__, '\flush_rewrite_rules' );
109+
110+
/**
111+
* Only load code that needs BuddyPress to run once BP is loaded and initialized.
112+
*/
113+
function enable_buddypress_features() {
114+
require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php';
115+
\Activitypub\Integration\Buddypress::init();
116+
}
117+
add_action( 'bp_include', '\Activitypub\enable_buddypress_features' );

integration/class-buddypress.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace Activitypub\Integration;
3+
4+
class Buddypress {
5+
public static function init() {
6+
\add_filter( 'activitypub_json_author_array', array( 'Activitypub\Integration\Buddypress', 'add_user_metadata' ), 11, 2 );
7+
}
8+
9+
public static function add_user_metadata( $object, $author_id ) {
10+
// add BuddyPress' cover_image instead of WordPress' header_image
11+
$cover_image_url = bp_attachments_get_attachment( 'url', array( 'item_id' => $author_id ) );
12+
13+
if ( $cover_image_url ) {
14+
$object->image = array(
15+
'type' => 'Image',
16+
'url' => $cover_image_url,
17+
);
18+
}
19+
20+
// change profile URL to BuddyPress' profile URL
21+
$object->attachment['profile_url'] = array(
22+
'type' => 'PropertyValue',
23+
'name' => \__( 'Profile', 'activitypub' ),
24+
'value' => \html_entity_decode(
25+
'<a rel="me" title="' . \esc_attr( bp_core_get_user_domain( $author_id ) ) . '" target="_blank" href="' . \bp_core_get_user_domain( $author_id ) . '">' . \wp_parse_url( \bp_core_get_user_domain( $author_id ), \PHP_URL_HOST ) . '</a>',
26+
\ENT_QUOTES,
27+
'UTF-8'
28+
),
29+
);
30+
31+
// replace blog URL on multisite
32+
if ( is_multisite() ) {
33+
$user_blogs = get_blogs_of_user( $author_id ); //get sites of user to send as AP metadata
34+
35+
if ( ! empty( $user_blogs ) ) {
36+
unset( $object->attachment['blog_url'] );
37+
38+
foreach ( $user_blogs as $blog ) {
39+
if ( 1 !== $blog->userblog_id ) {
40+
$object->attachment[] = array(
41+
'type' => 'PropertyValue',
42+
'name' => $blog->blogname,
43+
'value' => \html_entity_decode(
44+
'<a rel="me" title="' . \esc_attr( $blog->siteurl ) . '" target="_blank" href="' . $blog->siteurl . '">' . \wp_parse_url( $blog->siteurl, \PHP_URL_HOST ) . '</a>',
45+
\ENT_QUOTES,
46+
'UTF-8'
47+
),
48+
);
49+
}
50+
}
51+
}
52+
}
53+
54+
return $object;
55+
}
56+
}

templates/author-json.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
$json->tag = array();
4444
$json->attachment = array();
4545

46-
$json->attachment[] = array(
46+
$json->attachment['blog_url'] = array(
4747
'type' => 'PropertyValue',
4848
'name' => \__( 'Blog', 'activitypub' ),
4949
'value' => \html_entity_decode(
@@ -53,7 +53,7 @@
5353
),
5454
);
5555

56-
$json->attachment[] = array(
56+
$json->attachment['profile_url'] = array(
5757
'type' => 'PropertyValue',
5858
'name' => \__( 'Profile', 'activitypub' ),
5959
'value' => \html_entity_decode(
@@ -64,7 +64,7 @@
6464
);
6565

6666
if ( \get_the_author_meta( 'user_url', $author_id ) ) {
67-
$json->attachment[] = array(
67+
$json->attachment['user_url'] = array(
6868
'type' => 'PropertyValue',
6969
'name' => \__( 'Website', 'activitypub' ),
7070
'value' => \html_entity_decode(
@@ -84,6 +84,9 @@
8484
// filter output
8585
$json = \apply_filters( 'activitypub_json_author_array', $json, $author_id );
8686

87+
// migrate to ActivityPub standard
88+
$json->attachment = array_values( $json->attachment );
89+
8790
/*
8891
* Action triggerd prior to the ActivityPub profile being created and sent to the client
8992
*/

0 commit comments

Comments
 (0)