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