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
activitypub / includes / class-activitypub.php

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

171 lines 4.8 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_post_type_support( 'post', 'activitypub' );
20 add_post_type_support( 'page', 'activitypub' );
21
22 add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
23 }
24
25 /**
26 * Return a AS2 JSON version of an author, post or page
27 *
28 * @param string $template the path to the template object
29 *
30 * @return string the new path to the JSON template
31 */
32 public static function render_json_template( $template ) {
33 if ( ! is_author() && ! is_singular() ) {
34 return $template;
35 }
36
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';
41 }
42
43 global $wp_query;
44
45 if ( isset( $wp_query->query_vars['activitypub'] ) ) {
46 return $json_template;
47 }
48
49 if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
50 return $template;
51 }
52
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'];
59 }
60 // accept header as an array
61 $accept = explode( ',', trim( $accept_header ) );
62
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;
69 }
70
71 return $json_template;
72 }
73
74 /**
75 * Add the 'photos' query variable so WordPress
76 * won't mangle it.
77 */
78 public static function add_query_vars( $vars ) {
79 $vars[] = 'activitypub';
80
81 return $vars;
82 }
83
84 /**
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"
93 *
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
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 *
121 * @return array $args
122 */
123 public static function pre_get_avatar_data( $args, $id_or_email ) {
124 if (
125 ! $id_or_email instanceof \WP_Comment ||
126 ! isset( $id_or_email->comment_type ) ||
127 $id_or_email->user_id
128 ) {
129 return $args;
130 }
131
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 ) ) {
134 $args['url'] = false;
135 /** This filter is documented in wp-includes/link-template.php */
136 return apply_filters( 'get_avatar_data', $args, $id_or_email );
137 }
138
139 // check if comment has an avatar
140 $avatar = self::get_avatar_url( $id_or_email->comment_ID );
141
142 if ( $avatar ) {
143 if ( ! isset( $args['class'] ) || ! is_array( $args['class'] ) ) {
144 $args['class'] = array( 'u-photo' );
145 } else {
146 $args['class'][] = 'u-photo';
147 $args['class'] = array_unique( $args['class'] );
148 }
149 $args['url'] = $avatar;
150 $args['class'][] = 'avatar-activitypub';
151 }
152
153 return $args;
154 }
155
156 /**
157 * Function to retrieve Avatar URL if stored in meta
158 *
159 *
160 * @param int|WP_Comment $comment
161 *
162 * @return string $url
163 */
164 public static function get_avatar_url( $comment ) {
165 if ( is_numeric( $comment ) ) {
166 $comment = get_comment( $comment );
167 }
168 return get_comment_meta( $comment->comment_ID, 'avatar_url', true );
169 }
170 }
171