Skip to content

Commit 57299fc

Browse files
authored
Add "Stream" Plugin support (#863)
* Add "Stream" Plugin support This PR adds support for the Stream Plugin. It maps the Notifications to the Stream plugin, that might help us with debugging. * send type specific notification action ...to be able to subscribe to only a certain type of notifications.
1 parent 88675ea commit 57299fc

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

includes/class-notification.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function __construct( $type, $actor, $object, $target ) { // phpcs:ignore
5353
* Send the notification.
5454
*/
5555
public function send() {
56+
$type = \strtolower( $this->type );
57+
5658
do_action( 'activitypub_notification', $this );
59+
do_action( "activitypub_notification_{$type}", $this );
5760
}
5861
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace Activitypub\Integration;
3+
4+
/**
5+
* Stream Connector for ActivityPub
6+
*
7+
* This class is a Stream Connector for the Stream plugin.
8+
*
9+
* @see https://wordpress.org/plugins/stream/
10+
*/
11+
class Stream_Connector extends \WP_Stream\Connector {
12+
/**
13+
* Connector slug
14+
*
15+
* @var string
16+
*/
17+
public $name = 'activitypub';
18+
19+
/**
20+
* Actions registered for this connector
21+
*
22+
* @var array
23+
*/
24+
public $actions = array(
25+
'activitypub_notification_follow',
26+
);
27+
28+
/**
29+
* Return translated connector label
30+
*
31+
* @return string
32+
*/
33+
public function get_label() {
34+
return __( 'ActivityPub', 'activitypub' );
35+
}
36+
37+
/**
38+
* Return translated context labels
39+
*
40+
* @return array
41+
*/
42+
public function get_context_labels() {
43+
return array();
44+
}
45+
46+
/**
47+
* Return translated action labels
48+
*
49+
* @return array
50+
*/
51+
public function get_action_labels() {
52+
return array();
53+
}
54+
55+
/**
56+
* Callback for activitypub_notification_follow
57+
*
58+
* @param \Activitypub\Notification $notification The notification object
59+
*
60+
* @return void
61+
*/
62+
public function callback_activitypub_notification_follow( $notification ) {
63+
$this->log(
64+
sprintf(
65+
// translators: %s is a URL
66+
__( 'New Follower: %s', 'activitypub' ),
67+
$notification->actor
68+
),
69+
array(
70+
'notification' => \wp_json_encode( $notification, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
71+
),
72+
null,
73+
'notification',
74+
$notification->type,
75+
$notification->target
76+
);
77+
}
78+
}

integration/load.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,48 @@ function( $transformer, $object, $object_class ) {
9090
}
9191
\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' );
9292

93+
/**
94+
* Register the Stream Connector for ActivityPub.
95+
*
96+
* @param array $classes The Stream connectors.
97+
*
98+
* @return array The Stream connectors with the ActivityPub connector.
99+
*/
100+
function register_stream_connector( $classes ) {
101+
require plugin_dir_path( __FILE__ ) . '/class-stream-connector.php';
102+
103+
$class_name = '\Activitypub\Integration\Stream_Connector';
104+
105+
if ( ! class_exists( $class_name ) ) {
106+
return;
107+
}
108+
109+
wp_stream_get_instance();
110+
$class = new $class_name();
111+
112+
if ( ! method_exists( $class, 'is_dependency_satisfied' ) ) {
113+
return;
114+
}
115+
116+
if ( $class->is_dependency_satisfied() ) {
117+
$classes[] = $class;
118+
}
119+
120+
return $classes;
121+
}
122+
add_filter( 'wp_stream_connectors', __NAMESPACE__ . '\register_stream_connector' );
123+
124+
// Excluded ActivityPub post types from the Stream.
125+
add_filter(
126+
'wp_stream_posts_exclude_post_types',
127+
function( $post_types ) {
128+
$post_types[] = 'ap_follower';
129+
$post_types[] = 'ap_extrafield';
130+
$post_types[] = 'ap_extrafield_blog';
131+
return $post_types;
132+
}
133+
);
134+
93135
/**
94136
* Load the BuddyPress integration.
95137
*

0 commit comments

Comments
 (0)