PluginProbe
ActivityPub / 0.9.0
ActivityPub v0.9.0
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
activitypub / includes / class-activitypub.php

class-activitypub.php in ActivityPub 0.9.0, at includes/class-activitypub.php

182 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 /**
5 * ActivityPub Class
6 *
7 * @author Matthias Pfefferle
8 */
9 class Activitypub {
10 /**
11 * Initialize the class, registering WordPress hooks
12 */
13 public static function init() {
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 );
18
19 // Add support for ActivityPub to custom post types
20 $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
21
22 foreach ( $post_types as $post_type ) {
23 \add_post_type_support( $post_type, 'activitypub' );
24 }
25
26 \add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
27
28 \add_action( 'trash_post', array( '\Activitypub\Activitypub', 'schedule_delete_activity' ), 10 );
29 }
30
31 /**
32 * Return a AS2 JSON version of an author, post or page
33 *
34 * @param string $template the path to the template object
35 *
36 * @return string the new path to the JSON template
37 */
38 public static function render_json_template( $template ) {
39 if ( ! \is_author() && ! \is_singular() ) {
40 return $template;
41 }
42
43 if ( \is_author() ) {
44 $json_template = \dirname( __FILE__ ) . '/../templates/json-author.php';
45 } elseif ( \is_singular() ) {
46 $json_template = \dirname( __FILE__ ) . '/../templates/json-post.php';
47 }
48
49 global $wp_query;
50
51 if ( isset( $wp_query->query_vars['activitypub'] ) ) {
52 return $json_template;
53 }
54
55 if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
56 return $template;
57 }
58
59 $accept_header = $_SERVER['HTTP_ACCEPT'];
60
61 if (
62 stristr( $accept_header, 'application/activity+json' ) ||
63 stristr( $accept_header, 'application/ld+json' )
64 ) {
65 return $json_template;
66 }
67
68 // accept header as an array
69 $accept = \explode( ',', trim( $accept_header ) );
70
71 if (
72 \in_array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', $accept, true ) ||
73 \in_array( 'application/activity+json', $accept, true ) ||
74 \in_array( 'application/ld+json', $accept, true ) ||
75 \in_array( 'application/json', $accept, true )
76 ) {
77 return $json_template;
78 }
79
80 return $template;
81 }
82
83 /**
84 * Add the 'photos' query variable so WordPress
85 * won't mangle it.
86 */
87 public static function add_query_vars( $vars ) {
88 $vars[] = 'activitypub';
89
90 return $vars;
91 }
92
93 /**
94 * Add our rewrite endpoint to permalinks and pages.
95 */
96 public static function add_rewrite_endpoint() {
97 \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
98 }
99
100 /**
101 * Schedule Activities
102 *
103 * @param int $post_id
104 */
105 public static function schedule_post_activity( $new_status, $old_status, $post ) {
106 // do not send activities if post is password protected
107 if ( \post_password_required( $post ) ) {
108 return;
109 }
110
111 // check if post-type supports ActivityPub
112 $post_types = \get_post_types_by_support( 'activitypub' );
113 if ( ! \in_array( $post->post_type, $post_types, true ) ) {
114 return;
115 }
116
117 if ( 'publish' === $new_status && 'publish' !== $old_status ) {
118 \wp_schedule_single_event( time(), 'activitypub_send_post_activity', array( $post->ID ) );
119 } elseif ( 'publish' === $new_status ) {
120 \wp_schedule_single_event( time(), 'activitypub_send_update_activity', array( $post->ID ) );
121 } elseif ( 'trash' === $new_status ) {
122 \wp_schedule_single_event( time(), 'activitypub_send_delete_activity', array( get_permalink( $post ) ) );
123 }
124 }
125
126 /**
127 * Replaces the default avatar
128 *
129 * @param array $args Arguments passed to get_avatar_data(), after processing.
130 * @param int|string|object $id_or_email A user ID, email address, or comment object
131 *
132 * @return array $args
133 */
134 public static function pre_get_avatar_data( $args, $id_or_email ) {
135 if (
136 ! $id_or_email instanceof \WP_Comment ||
137 ! isset( $id_or_email->comment_type ) ||
138 $id_or_email->user_id
139 ) {
140 return $args;
141 }
142
143 $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
144 if ( ! empty( $id_or_email->comment_type ) && ! \in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
145 $args['url'] = false;
146 /** This filter is documented in wp-includes/link-template.php */
147 return \apply_filters( 'get_avatar_data', $args, $id_or_email );
148 }
149
150 // check if comment has an avatar
151 $avatar = self::get_avatar_url( $id_or_email->comment_ID );
152
153 if ( $avatar ) {
154 if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
155 $args['class'] = array( 'u-photo' );
156 } else {
157 $args['class'][] = 'u-photo';
158 $args['class'] = \array_unique( $args['class'] );
159 }
160 $args['url'] = $avatar;
161 $args['class'][] = 'avatar-activitypub';
162 }
163
164 return $args;
165 }
166
167 /**
168 * Function to retrieve Avatar URL if stored in meta
169 *
170 *
171 * @param int|WP_Comment $comment
172 *
173 * @return string $url
174 */
175 public static function get_avatar_url( $comment ) {
176 if ( \is_numeric( $comment ) ) {
177 $comment = \get_comment( $comment );
178 }
179 return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
180 }
181 }
182