From 8e98fffab68d4138d11bc1d28407954d06e85920 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 15 Sep 2021 17:00:20 +0200 Subject: [PATCH] Add basic BuddyPress support fix #122 thanks and props @skysarwer --- activitypub.php | 9 +++++ integration/class-buddypress.php | 56 ++++++++++++++++++++++++++++++++ templates/author-json.php | 9 +++-- 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 integration/class-buddypress.php diff --git a/activitypub.php b/activitypub.php index 3ae1258..153efa7 100644 --- a/activitypub.php +++ b/activitypub.php @@ -107,3 +107,12 @@ function flush_rewrite_rules() { } \register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' ); \register_deactivation_hook( __FILE__, '\flush_rewrite_rules' ); + +/** + * Only load code that needs BuddyPress to run once BP is loaded and initialized. + */ +function enable_buddypress_features() { + require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php'; + \Activitypub\Integration\Buddypress::init(); +} +add_action( 'bp_include', '\Activitypub\enable_buddypress_features' ); diff --git a/integration/class-buddypress.php b/integration/class-buddypress.php new file mode 100644 index 0000000..1b8fd65 --- /dev/null +++ b/integration/class-buddypress.php @@ -0,0 +1,56 @@ + $author_id ) ); + + if ( $cover_image_url ) { + $object->image = array( + 'type' => 'Image', + 'url' => $cover_image_url, + ); + } + + // change profile URL to BuddyPress' profile URL + $object->attachment['profile_url'] = array( + 'type' => 'PropertyValue', + 'name' => \__( 'Profile', 'activitypub' ), + 'value' => \html_entity_decode( + '' . \wp_parse_url( \bp_core_get_user_domain( $author_id ), \PHP_URL_HOST ) . '', + \ENT_QUOTES, + 'UTF-8' + ), + ); + + // replace blog URL on multisite + if ( is_multisite() ) { + $user_blogs = get_blogs_of_user( $author_id ); //get sites of user to send as AP metadata + + if ( ! empty( $user_blogs ) ) { + unset( $object->attachment['blog_url'] ); + + foreach ( $user_blogs as $blog ) { + if ( 1 !== $blog->userblog_id ) { + $object->attachment[] = array( + 'type' => 'PropertyValue', + 'name' => $blog->blogname, + 'value' => \html_entity_decode( + '' . \wp_parse_url( $blog->siteurl, \PHP_URL_HOST ) . '', + \ENT_QUOTES, + 'UTF-8' + ), + ); + } + } + } + } + + return $object; + } +} diff --git a/templates/author-json.php b/templates/author-json.php index ccaa167..c5d39a5 100644 --- a/templates/author-json.php +++ b/templates/author-json.php @@ -43,7 +43,7 @@ $json->publicKey = array( $json->tag = array(); $json->attachment = array(); -$json->attachment[] = array( +$json->attachment['blog_url'] = array( 'type' => 'PropertyValue', 'name' => \__( 'Blog', 'activitypub' ), 'value' => \html_entity_decode( @@ -53,7 +53,7 @@ $json->attachment[] = array( ), ); -$json->attachment[] = array( +$json->attachment['profile_url'] = array( 'type' => 'PropertyValue', 'name' => \__( 'Profile', 'activitypub' ), 'value' => \html_entity_decode( @@ -64,7 +64,7 @@ $json->attachment[] = array( ); if ( \get_the_author_meta( 'user_url', $author_id ) ) { - $json->attachment[] = array( + $json->attachment['user_url'] = array( 'type' => 'PropertyValue', 'name' => \__( 'Website', 'activitypub' ), 'value' => \html_entity_decode( @@ -84,6 +84,9 @@ $json->endpoints = array( // filter output $json = \apply_filters( 'activitypub_json_author_array', $json, $author_id ); +// migrate to ActivityPub standard +$json->attachment = array_values( $json->attachment ); + /* * Action triggerd prior to the ActivityPub profile being created and sent to the client */