| @@ -1,399 +1,82 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * ActivityPub Class. | |
| 3 | + * ActivityPub Class | |
| 4 | 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 | 5 | * @author Matthias Pfefferle |
| 19 | 6 | */ |
| 20 | 7 | class Activitypub { |
| 21 | 8 | /** |
| 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. | |
| 9 | + * Return a AS2 JSON version of an author, post or page | |
| 39 | 10 | * |
| 40 | - * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site. | |
| 11 | + * @param string $template the path to the template object | |
| 12 | + * | |
| 13 | + * @return string the new path to the JSON template | |
| 41 | 14 | */ |
| 42 | - public static function activate( $network_wide ) { | |
| 43 | - self::flush_rewrite_rules(); | |
| 44 | - Scheduler::register_schedules(); | |
| 15 | + public static function render_json_template( $template ) { | |
| 16 | + if ( ! is_author() && ! is_singular() ) { | |
| 17 | + return $template; | |
| 18 | + } | |
| 45 | 19 | |
| 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 | - } | |
| 20 | + if ( is_author() ) { | |
| 21 | + $json_template = dirname( __FILE__ ) . '/../templates/json-author.php'; | |
| 22 | + } elseif ( is_singular() ) { | |
| 23 | + $json_template = dirname( __FILE__ ) . '/../templates/json-post.php'; | |
| 56 | 24 | } |
| 57 | - } | |
| 58 | 25 | |
| 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(); | |
| 26 | + global $wp_query; | |
| 67 | 27 | |
| 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 ); | |
| 28 | + if ( isset( $wp_query->query_vars['as2'] ) ) { | |
| 29 | + return $json_template; | |
| 30 | + } | |
| 70 | 31 | |
| 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 | - } | |
| 32 | + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) { | |
| 33 | + return $template; | |
| 78 | 34 | } |
| 79 | - } | |
| 80 | 35 | |
| 81 | - /** | |
| 82 | - * Uninstall Hook. | |
| 83 | - */ | |
| 84 | - public static function uninstall() { | |
| 85 | - Scheduler::deregister_schedules(); | |
| 36 | + // interpret accept header | |
| 37 | + $pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' ); | |
| 38 | + if ( $pos ) { | |
| 39 | + $accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos ); | |
| 40 | + } else { | |
| 41 | + $accept_header = $_SERVER['HTTP_ACCEPT']; | |
| 42 | + } | |
| 43 | + // accept header as an array | |
| 44 | + $accept = explode( ',', trim( $accept_header ) ); | |
| 86 | 45 | |
| 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 ); | |
| 46 | + if ( | |
| 47 | + ! in_array( 'application/activity+json', $accept, true ) && | |
| 48 | + ! in_array( 'application/ld+json', $accept, true ) && | |
| 49 | + ! in_array( 'application/json', $accept, true ) | |
| 50 | + ) { | |
| 51 | + return $template; | |
| 52 | + } | |
| 89 | 53 | |
| 90 | - Remote_Posts::delete_all(); | |
| 91 | - Client::delete_all(); | |
| 92 | - | |
| 93 | - Options::delete(); | |
| 54 | + return $json_template; | |
| 94 | 55 | } |
| 95 | 56 | |
| 96 | 57 | /** |
| 97 | - * Store permalink in meta, to send delete Activity. | |
| 98 | - * | |
| 99 | - * @param string $post_id The Post ID. | |
| 58 | + * Add the 'photos' query variable so WordPress | |
| 59 | + * won't mangle it. | |
| 100 | 60 | */ |
| 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 | - } | |
| 61 | + public static function add_query_vars( $vars ) { | |
| 62 | + $vars[] = 'as2'; | |
| 109 | 63 | |
| 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' ); | |
| 64 | + return $vars; | |
| 117 | 65 | } |
| 118 | 66 | |
| 119 | 67 | /** |
| 120 | - * Flush rewrite rules. | |
| 68 | + * Add our rewrite endpoint to permalinks and pages. | |
| 121 | 69 | */ |
| 122 | - public static function flush_rewrite_rules() { | |
| 123 | - Router::add_rewrite_rules(); | |
| 124 | - \flush_rewrite_rules(); | |
| 70 | + public static function add_rewrite_endpoint() { | |
| 71 | + add_rewrite_endpoint( 'as2', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); | |
| 125 | 72 | } |
| 126 | 73 | |
| 127 | 74 | /** |
| 128 | - * Add rewrite rules. | |
| 75 | + * Marks the post as "no webmentions sent yet" | |
| 129 | 76 | * |
| 130 | - * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}. | |
| 77 | + * @param int $post_id | |
| 131 | 78 | */ |
| 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; | |
| 79 | + public static function schedule_post_activity( $post_id ) { | |
| 80 | + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post_id ) ); | |
| 398 | 81 | } |
| 399 | 82 | } |