PluginProbe
ActivityPub / 0.14.1
ActivityPub v0.14.1
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.14.1, at includes/class-activitypub.php

184 lines 5.5 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
29 /**
30 * Return a AS2 JSON version of an author, post or page.
31 *
32 * @param string $template The path to the template object.
33 *
34 * @return string The new path to the JSON template.
35 */
36 public static function render_json_template( $template ) {
37 if ( ! \is_author() && ! \is_singular() && ! \is_home() ) {
38 return $template;
39 }
40
41 if ( \is_author() ) {
42 $json_template = \dirname( __FILE__ ) . '/../templates/author-json.php';
43 } elseif ( \is_singular() ) {
44 $json_template = \dirname( __FILE__ ) . '/../templates/post-json.php';
45 } elseif ( \is_home() ) {
46 $json_template = \dirname( __FILE__ ) . '/../templates/blog-json.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 'activitypub' query variable so WordPress won't mangle it.
85 */
86 public static function add_query_vars( $vars ) {
87 $vars[] = 'activitypub';
88
89 return $vars;
90 }
91
92 /**
93 * Add our rewrite endpoint to permalinks and pages.
94 */
95 public static function add_rewrite_endpoint() {
96 \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
97 }
98
99 /**
100 * Schedule Activities.
101 *
102 * @param string $new_status New post status.
103 * @param string $old_status Old post status.
104 * @param WP_Post $post Post object.
105 */
106 public static function schedule_post_activity( $new_status, $old_status, $post ) {
107 // Do not send activities if post is password protected.
108 if ( \post_password_required( $post ) ) {
109 return;
110 }
111
112 // Check if post-type supports ActivityPub.
113 $post_types = \get_post_types_by_support( 'activitypub' );
114 if ( ! \in_array( $post->post_type, $post_types, true ) ) {
115 return;
116 }
117
118 $activitypub_post = new \Activitypub\Model\Post( $post );
119
120 if ( 'publish' === $new_status && 'publish' !== $old_status ) {
121 \wp_schedule_single_event( \time(), 'activitypub_send_post_activity', array( $activitypub_post ) );
122 } elseif ( 'publish' === $new_status ) {
123 \wp_schedule_single_event( \time(), 'activitypub_send_update_activity', array( $activitypub_post ) );
124 } elseif ( 'trash' === $new_status ) {
125 \wp_schedule_single_event( \time(), 'activitypub_send_delete_activity', array( $activitypub_post ) );
126 }
127 }
128
129 /**
130 * Replaces the default avatar.
131 *
132 * @param array $args Arguments passed to get_avatar_data(), after processing.
133 * @param int|string|object $id_or_email A user ID, email address, or comment object.
134 *
135 * @return array $args
136 */
137 public static function pre_get_avatar_data( $args, $id_or_email ) {
138 if (
139 ! $id_or_email instanceof \WP_Comment ||
140 ! isset( $id_or_email->comment_type ) ||
141 $id_or_email->user_id
142 ) {
143 return $args;
144 }
145
146 $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
147 if ( ! empty( $id_or_email->comment_type ) && ! \in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
148 $args['url'] = false;
149 /** This filter is documented in wp-includes/link-template.php */
150 return \apply_filters( 'get_avatar_data', $args, $id_or_email );
151 }
152
153 // Check if comment has an avatar.
154 $avatar = self::get_avatar_url( $id_or_email->comment_ID );
155
156 if ( $avatar ) {
157 if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
158 $args['class'] = array( 'u-photo' );
159 } else {
160 $args['class'][] = 'u-photo';
161 $args['class'] = \array_unique( $args['class'] );
162 }
163 $args['url'] = $avatar;
164 $args['class'][] = 'avatar-activitypub';
165 }
166
167 return $args;
168 }
169
170 /**
171 * Function to retrieve Avatar URL if stored in meta.
172 *
173 * @param int|WP_Comment $comment
174 *
175 * @return string $url
176 */
177 public static function get_avatar_url( $comment ) {
178 if ( \is_numeric( $comment ) ) {
179 $comment = \get_comment( $comment );
180 }
181 return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
182 }
183 }
184