PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.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 7.7.0, at includes/class-activitypub.php

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