PluginProbe
ActivityPub / 8.2.1
ActivityPub v8.2.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 8.2.1, at includes/class-activitypub.php

400 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Class.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use Activitypub\Collection\Followers;
11 use Activitypub\Collection\Following;
12 use Activitypub\Collection\Remote_Posts;
13 use Activitypub\OAuth\Client;
14
15 /**
16 * ActivityPub Class.
17 *
18 * @author Matthias Pfefferle
19 */
20 class Activitypub {
21 /**
22 * Initialize the class, registering WordPress hooks.
23 */
24 public static function init() {
25 \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
26 \add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
27
28 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
29 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
30
31 \add_action( 'user_register', array( self::class, 'user_register' ) );
32
33 \add_action( 'activitypub_add_user_block', array( Followers::class, 'remove_blocked_actors' ), 10, 3 );
34 \add_action( 'activitypub_add_user_block', array( Following::class, 'remove_blocked_actors' ), 10, 3 );
35 }
36
37 /**
38 * Activation Hook.
39 *
40 * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
41 */
42 public static function activate( $network_wide ) {
43 self::flush_rewrite_rules();
44 Scheduler::register_schedules();
45
46 \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5, 3 );
47 Migration::update_comment_counts();
48
49 if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) {
50 $sites = \get_sites( array( 'fields' => 'ids' ) );
51 foreach ( $sites as $site ) {
52 \switch_to_blog( $site );
53 self::flush_rewrite_rules();
54 \restore_current_blog();
55 }
56 }
57 }
58
59 /**
60 * Deactivation Hook.
61 *
62 * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
63 */
64 public static function deactivate( $network_wide ) {
65 self::flush_rewrite_rules();
66 Scheduler::deregister_schedules();
67
68 \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 );
69 Migration::update_comment_counts( 2000 );
70
71 if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) {
72 $sites = \get_sites( array( 'fields' => 'ids' ) );
73 foreach ( $sites as $site ) {
74 \switch_to_blog( $site );
75 self::flush_rewrite_rules();
76 \restore_current_blog();
77 }
78 }
79 }
80
81 /**
82 * Uninstall Hook.
83 */
84 public static function uninstall() {
85 Scheduler::deregister_schedules();
86
87 \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 );
88 Migration::update_comment_counts( 2000 );
89
90 Remote_Posts::delete_all();
91 Client::delete_all();
92
93 Options::delete();
94 }
95
96 /**
97 * Store permalink in meta, to send delete Activity.
98 *
99 * @param string $post_id The Post ID.
100 */
101 public static function trash_post( $post_id ) {
102 \add_post_meta(
103 $post_id,
104 '_activitypub_canonical_url',
105 \get_permalink( $post_id ),
106 true
107 );
108 }
109
110 /**
111 * Delete permalink from meta.
112 *
113 * @param string $post_id The Post ID.
114 */
115 public static function untrash_post( $post_id ) {
116 \delete_post_meta( $post_id, '_activitypub_canonical_url' );
117 }
118
119 /**
120 * Flush rewrite rules.
121 */
122 public static function flush_rewrite_rules() {
123 Router::add_rewrite_rules();
124 \flush_rewrite_rules();
125 }
126
127 /**
128 * Add rewrite rules.
129 *
130 * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
131 */
132 public static function add_rewrite_rules() {
133 _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
134
135 Router::add_rewrite_rules();
136 }
137
138 /**
139 * Theme compatibility stuff.
140 */
141 public static function theme_compat() {
142 // We assume that you want to use Post-Formats when enabling the setting.
143 if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
144 if ( ! get_theme_support( 'post-formats' ) ) {
145 // Add support for the Aside, Gallery Post Formats...
146 add_theme_support(
147 'post-formats',
148 array(
149 'gallery',
150 'status',
151 'image',
152 'video',
153 'audio',
154 )
155 );
156 }
157 }
158 }
159
160 /**
161 * Add the 'activitypub' capability to users who can publish posts.
162 *
163 * @param int $user_id User ID.
164 */
165 public static function user_register( $user_id ) {
166 if ( \user_can( $user_id, 'publish_posts' ) ) {
167 $user = \get_user_by( 'id', $user_id );
168 $user->add_cap( 'activitypub' );
169 }
170 }
171
172 /**
173 * Register user meta.
174 */
175 public static function register_user_meta() {
176 $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
177
178 \register_meta(
179 'user',
180 $blog_prefix . 'activitypub_also_known_as',
181 array(
182 'type' => 'array',
183 'description' => 'An array of URLs that the user is known by.',
184 'single' => true,
185 'default' => array(),
186 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
187 )
188 );
189
190 \register_meta(
191 'user',
192 $blog_prefix . 'activitypub_hide_social_graph',
193 array(
194 'type' => 'integer',
195 'description' => 'Hide Followers and Followings on Profile.',
196 'single' => true,
197 'default' => 0,
198 'sanitize_callback' => 'absint',
199 'show_in_rest' => true,
200 )
201 );
202
203 \register_meta(
204 'user',
205 $blog_prefix . 'activitypub_old_host_data',
206 array(
207 'description' => 'Actor object for the user on the old host.',
208 'single' => true,
209 )
210 );
211
212 \register_meta(
213 'user',
214 $blog_prefix . 'activitypub_moved_to',
215 array(
216 'type' => 'string',
217 'description' => 'The new URL of the user.',
218 'single' => true,
219 'sanitize_callback' => 'sanitize_url',
220 )
221 );
222
223 \register_meta(
224 'user',
225 $blog_prefix . 'activitypub_description',
226 array(
227 'type' => 'string',
228 'description' => 'The user description.',
229 'single' => true,
230 'default' => '',
231 'sanitize_callback' => static function ( $value ) {
232 return wp_kses( $value, 'user_description' );
233 },
234 )
235 );
236
237 \register_meta(
238 'user',
239 $blog_prefix . 'activitypub_icon',
240 array(
241 'type' => 'integer',
242 'description' => 'The attachment ID for user profile image.',
243 'single' => true,
244 'default' => 0,
245 'sanitize_callback' => 'absint',
246 )
247 );
248
249 \register_meta(
250 'user',
251 $blog_prefix . 'activitypub_header_image',
252 array(
253 'type' => 'integer',
254 'description' => 'The attachment ID for the user header image.',
255 'single' => true,
256 'default' => 0,
257 'sanitize_callback' => 'absint',
258 )
259 );
260
261 \register_meta(
262 'user',
263 $blog_prefix . 'activitypub_mailer_new_dm',
264 array(
265 'type' => 'integer',
266 'description' => 'Send a notification when someone sends this user a direct message.',
267 'single' => true,
268 'sanitize_callback' => 'absint',
269 )
270 );
271 \add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
272
273 \register_meta(
274 'user',
275 $blog_prefix . 'activitypub_mailer_new_follower',
276 array(
277 'type' => 'integer',
278 'description' => 'Send a notification when someone starts to follow this user.',
279 'single' => true,
280 'sanitize_callback' => 'absint',
281 )
282 );
283 \add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
284
285 \register_meta(
286 'user',
287 $blog_prefix . 'activitypub_mailer_new_mention',
288 array(
289 'type' => 'integer',
290 'description' => 'Send a notification when someone mentions this user.',
291 'single' => true,
292 'sanitize_callback' => 'absint',
293 )
294 );
295 \add_filter( 'get_user_option_activitypub_mailer_new_mention', array( self::class, 'user_options_default' ) );
296
297 \register_meta(
298 'user',
299 $blog_prefix . 'activitypub_mailer_annual_report',
300 array(
301 'type' => 'integer',
302 'description' => 'Send the annual Fediverse Year in Review email.',
303 'single' => true,
304 'sanitize_callback' => 'absint',
305 )
306 );
307 \add_filter( 'get_user_option_activitypub_mailer_annual_report', array( self::class, 'user_options_default' ) );
308
309 \register_meta(
310 'user',
311 $blog_prefix . 'activitypub_mailer_monthly_report',
312 array(
313 'type' => 'integer',
314 'description' => 'Send a monthly Fediverse stats report email.',
315 'single' => true,
316 'sanitize_callback' => 'absint',
317 )
318 );
319
320 \register_meta(
321 'user',
322 'activitypub_show_welcome_tab',
323 array(
324 'type' => 'integer',
325 'description' => 'Whether to show the welcome tab.',
326 'single' => true,
327 'default' => 1,
328 'sanitize_callback' => 'absint',
329 )
330 );
331
332 \register_meta(
333 'user',
334 'activitypub_show_advanced_tab',
335 array(
336 'type' => 'integer',
337 'description' => 'Whether to show the advanced tab.',
338 'single' => true,
339 'default' => 0,
340 'sanitize_callback' => 'absint',
341 )
342 );
343
344 // Moderation user meta.
345 \register_meta(
346 'user',
347 'activitypub_blocked_actors',
348 array(
349 'type' => 'array',
350 'description' => 'User-specific blocked ActivityPub actors.',
351 'single' => true,
352 'default' => array(),
353 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
354 )
355 );
356
357 \register_meta(
358 'user',
359 'activitypub_blocked_domains',
360 array(
361 'type' => 'array',
362 'description' => 'User-specific blocked ActivityPub domains.',
363 'single' => true,
364 'default' => array(),
365 'sanitize_callback' => static function ( $value ) {
366 return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) );
367 },
368 )
369 );
370
371 \register_meta(
372 'user',
373 'activitypub_blocked_keywords',
374 array(
375 'type' => 'array',
376 'description' => 'User-specific blocked ActivityPub keywords.',
377 'single' => true,
378 'default' => array(),
379 'sanitize_callback' => static function ( $value ) {
380 return \array_map( 'sanitize_text_field', $value );
381 },
382 )
383 );
384 }
385
386 /**
387 * Set default values for user options.
388 *
389 * @param bool|string $value Option value.
390 * @return bool|string
391 */
392 public static function user_options_default( $value ) {
393 if ( false === $value ) {
394 return '1';
395 }
396
397 return $value;
398 }
399 }
400