PluginProbe
ActivityPub / 0.7.3
ActivityPub v0.7.3
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
← All changes | includes/class-activitypub.php +83 -244 1.2.00.7.3 View file →
@@ -1,10 +1,7 @@
1 1 <?php
2 2 namespace Activitypub;
3 3
4 -use Activitypub\Signature;
5 -use Activitypub\Collection\Users;
6 -
7 4 /**
8 5 * ActivityPub Class
9 6 *
10 7 * @author Matthias Pfefferle
@@ -10,101 +7,66 @@
10 7 * @author Matthias Pfefferle
11 8 */
12 9 class Activitypub {
13 10 /**
14 - * Initialize the class, registering WordPress hooks.
11 + * Initialize the class, registering WordPress hooks
15 12 */
16 13 public static function init() {
17 - \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
18 - \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
19 - \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
20 - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
14 + add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 );
15 + add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) );
16 + add_action( 'init', array( '\Activitypub\Activitypub', 'add_rewrite_endpoint' ) );
17 + add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 );
21 18
22 - // Add support for ActivityPub to custom post types
23 - $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
19 + add_post_type_support( 'post', 'activitypub' );
20 + add_post_type_support( 'page', 'activitypub' );
24 21
25 - foreach ( $post_types as $post_type ) {
26 - \add_post_type_support( $post_type, 'activitypub' );
27 - }
28 -
29 - \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
30 - \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
31 -
32 - \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
33 -
34 - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 );
35 -
36 - \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
22 + add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
37 23 }
38 24
39 25 /**
40 - * Activation Hook
26 + * Return a AS2 JSON version of an author, post or page
41 27 *
42 - * @return void
43 - */
44 - public static function activate() {
45 - self::flush_rewrite_rules();
46 -
47 - Scheduler::register_schedules();
48 - }
49 -
50 - /**
51 - * Deactivation Hook
28 + * @param string $template the path to the template object
52 29 *
53 - * @return void
30 + * @return string the new path to the JSON template
54 31 */
55 - public static function deactivate() {
56 - self::flush_rewrite_rules();
57 -
58 - Scheduler::deregister_schedules();
59 - }
60 -
61 - /**
62 - * Uninstall Hook
63 - *
64 - * @return void
65 - */
66 - public static function uninstall() {
67 - Scheduler::deregister_schedules();
68 - }
69 -
70 - /**
71 - * Return a AS2 JSON version of an author, post or page.
72 - *
73 - * @param string $template The path to the template object.
74 - *
75 - * @return string The new path to the JSON template.
76 - */
77 32 public static function render_json_template( $template ) {
78 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
33 + if ( ! is_author() && ! is_singular() ) {
79 34 return $template;
80 35 }
81 36
82 - if ( ! is_activitypub_request() ) {
83 - return $template;
37 + if ( is_author() ) {
38 + $json_template = dirname( __FILE__ ) . '/../templates/json-author.php';
39 + } elseif ( is_singular() ) {
40 + $json_template = dirname( __FILE__ ) . '/../templates/json-post.php';
84 41 }
85 42
86 - $json_template = false;
43 + global $wp_query;
87 44
88 - // check if user can publish posts
89 - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) {
45 + if ( isset( $wp_query->query_vars['activitypub'] ) ) {
46 + return $json_template;
47 + }
48 +
49 + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
90 50 return $template;
91 51 }
92 52
93 - if ( \is_author() ) {
94 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
95 - } elseif ( \is_singular() ) {
96 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
97 - } elseif ( \is_home() ) {
98 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
53 + // interpret accept header
54 + $pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' );
55 + if ( $pos ) {
56 + $accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos );
57 + } else {
58 + $accept_header = $_SERVER['HTTP_ACCEPT'];
99 59 }
60 + // accept header as an array
61 + $accept = explode( ',', trim( $accept_header ) );
100 62
101 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
102 - $verification = Signature::verify_http_signature( $_SERVER );
103 - if ( \is_wp_error( $verification ) ) {
104 - // fallback as template_loader can't return http headers
105 - return $template;
106 - }
63 + if (
64 + ! in_array( 'application/activity+json', $accept, true ) &&
65 + ! in_array( 'application/ld+json', $accept, true ) &&
66 + ! in_array( 'application/json', $accept, true )
67 + ) {
68 + return $template;
107 69 }
108 70
109 71 return $json_template;
110 72 }
@@ -109,9 +71,10 @@
109 71 return $json_template;
110 72 }
111 73
112 74 /**
113 - * Add the 'activitypub' query variable so WordPress won't mangle it.
75 + * Add the 'photos' query variable so WordPress
76 + * won't mangle it.
114 77 */
115 78 public static function add_query_vars( $vars ) {
116 79 $vars[] = 'activitypub';
117 80
@@ -118,13 +81,44 @@
118 81 return $vars;
119 82 }
120 83
121 84 /**
122 - * Replaces the default avatar.
85 + * Add our rewrite endpoint to permalinks and pages.
86 + */
87 + public static function add_rewrite_endpoint() {
88 + add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
89 + }
90 +
91 + /**
92 + * Marks the post as "no webmentions sent yet"
123 93 *
124 - * @param array $args Arguments passed to get_avatar_data(), after processing.
125 - * @param int|string|object $id_or_email A user ID, email address, or comment object.
94 + * @param int $post_id
95 + */
96 + public static function schedule_post_activity( $new_status, $old_status, $post ) {
97 + // do not send activities if post is password protected
98 + if ( post_password_required( $post ) ) {
99 + return;
100 + }
101 +
102 + // check if post-type supports ActivityPub
103 + $post_types = get_post_types_by_support( 'activitypub' );
104 + if ( ! in_array( $post->post_type, $post_types, true ) ) {
105 + return;
106 + }
107 +
108 + if ( 'publish' === $new_status && 'publish' !== $old_status ) {
109 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
110 + } elseif ( 'publish' === $new_status ) {
111 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
112 + }
113 + }
114 +
115 + /**
116 + * Replaces the default avatar
126 117 *
118 + * @param array $args Arguments passed to get_avatar_data(), after processing.
119 + * @param int|string|object $id_or_email A user ID, email address, or comment object
120 + *
127 121 * @return array $args
128 122 */
129 123 public static function pre_get_avatar_data( $args, $id_or_email ) {
130 124 if (
@@ -134,31 +128,24 @@
134 128 ) {
135 129 return $args;
136 130 }
137 131
138 - $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
139 - if (
140 - ! empty( $id_or_email->comment_type ) &&
141 - ! \in_array(
142 - $id_or_email->comment_type,
143 - (array) $allowed_comment_types,
144 - true
145 - )
146 - ) {
132 + $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
133 + if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
147 134 $args['url'] = false;
148 135 /** This filter is documented in wp-includes/link-template.php */
149 - return \apply_filters( 'get_avatar_data', $args, $id_or_email );
136 + return apply_filters( 'get_avatar_data', $args, $id_or_email );
150 137 }
151 138
152 - // Check if comment has an avatar.
139 + // check if comment has an avatar
153 140 $avatar = self::get_avatar_url( $id_or_email->comment_ID );
154 141
155 142 if ( $avatar ) {
156 - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
143 + if ( ! isset( $args['class'] ) || ! is_array( $args['class'] ) ) {
157 144 $args['class'] = array( 'u-photo' );
158 145 } else {
159 146 $args['class'][] = 'u-photo';
160 - $args['class'] = \array_unique( $args['class'] );
147 + $args['class'] = array_unique( $args['class'] );
161 148 }
162 149 $args['url'] = $avatar;
163 150 $args['class'][] = 'avatar-activitypub';
164 151 }
@@ -166,166 +153,18 @@
166 153 return $args;
167 154 }
168 155
169 156 /**
170 - * Function to retrieve Avatar URL if stored in meta.
157 + * Function to retrieve Avatar URL if stored in meta
171 158 *
159 + *
172 160 * @param int|WP_Comment $comment
173 161 *
174 162 * @return string $url
175 163 */
176 164 public static function get_avatar_url( $comment ) {
177 - if ( \is_numeric( $comment ) ) {
178 - $comment = \get_comment( $comment );
165 + if ( is_numeric( $comment ) ) {
166 + $comment = get_comment( $comment );
179 167 }
180 - return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
181 - }
182 -
183 - /**
184 - * Link remote comments to source url.
185 - *
186 - * @param string $comment_link
187 - * @param object|WP_Comment $comment
188 - *
189 - * @return string $url
190 - */
191 - public static function remote_comment_link( $comment_link, $comment ) {
192 - $remote_comment_link = get_comment_meta( $comment->comment_ID, 'source_url', true );
193 - if ( $remote_comment_link ) {
194 - $comment_link = esc_url( $remote_comment_link );
195 - }
196 - return $comment_link;
197 - }
198 -
199 - /**
200 - * Store permalink in meta, to send delete Activity.
201 - *
202 - * @param string $post_id The Post ID.
203 - *
204 - * @return void
205 - */
206 - public static function trash_post( $post_id ) {
207 - \add_post_meta(
208 - $post_id,
209 - 'activitypub_canonical_url',
210 - \get_permalink( $post_id ),
211 - true
212 - );
213 - }
214 -
215 - /**
216 - * Delete permalink from meta
217 - *
218 - * @param string $post_id The Post ID
219 - *
220 - * @return void
221 - */
222 - public static function untrash_post( $post_id ) {
223 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
224 - }
225 -
226 - /**
227 - * Add rewrite rules
228 - */
229 - public static function add_rewrite_rules() {
230 - // If another system needs to take precedence over the ActivityPub rewrite rules,
231 - // they can define their own and will manually call the appropriate functions as required.
232 - if ( ACTIVITYPUB_DISABLE_REWRITES ) {
233 - return;
234 - }
235 -
236 - if ( ! \class_exists( 'Webfinger' ) ) {
237 - \add_rewrite_rule(
238 - '^.well-known/webfinger',
239 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger',
240 - 'top'
241 - );
242 - }
243 -
244 - if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
245 - \add_rewrite_rule(
246 - '^.well-known/nodeinfo',
247 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
248 - 'top'
249 - );
250 - \add_rewrite_rule(
251 - '^.well-known/x-nodeinfo2',
252 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
253 - 'top'
254 - );
255 - }
256 -
257 - \add_rewrite_rule(
258 - '^@([\w\-\.]+)',
259 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
260 - 'top'
261 - );
262 -
263 - \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
264 - }
265 -
266 - /**
267 - * Flush rewrite rules;
268 - */
269 - public static function flush_rewrite_rules() {
270 - self::add_rewrite_rules();
271 - \flush_rewrite_rules();
272 - }
273 -
274 - /**
275 - * Theme compatibility stuff
276 - *
277 - * @return void
278 - */
279 - public static function theme_compat() {
280 - $site_icon = get_theme_support( 'custom-logo' );
281 -
282 - if ( ! $site_icon ) {
283 - // custom logo support
284 - add_theme_support(
285 - 'custom-logo',
286 - array(
287 - 'height' => 80,
288 - 'width' => 80,
289 - )
290 - );
291 - }
292 -
293 - $custom_header = get_theme_support( 'custom-header' );
294 -
295 - if ( ! $custom_header ) {
296 - // This theme supports a custom header
297 - $custom_header_args = array(
298 - 'width' => 1250,
299 - 'height' => 600,
300 - 'header-text' => true,
301 - );
302 - add_theme_support( 'custom-header', $custom_header_args );
303 - }
304 - }
305 -
306 - /**
307 - * Display plugin upgrade notice to users
308 - *
309 - * @param array $data The plugin data
310 - *
311 - * @return void
312 - */
313 - public static function plugin_update_message( $data ) {
314 - if ( ! isset( $data['upgrade_notice'] ) ) {
315 - return;
316 - }
317 -
318 - printf(
319 - '<div class="update-message">%s</div>',
320 - wp_kses(
321 - wpautop( $data['upgrade_notice '] ),
322 - array(
323 - 'p' => array(),
324 - 'a' => array( 'href', 'title' ),
325 - 'strong' => array(),
326 - 'em' => array(),
327 - )
328 - )
329 - );
168 + return get_comment_meta( $comment->comment_ID, 'avatar_url', true );
330 169 }
331 170 }