Skip to content

Commit a9aed5b

Browse files
authored
Merge pull request #132 from pfefferle/buddypress
Add basic BuddyPress support
2 parents 5032e5e + 180f11d commit a9aed5b

File tree

3 files changed

+73
-3
lines changed

3 files changed

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

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)