| @@ -1,13 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Activitypub; |
| 4 | 4 | |
| 5 | +use Activitypub\Transformer\Post; | |
| 5 | 6 | use Activitypub\Collection\Users; |
| 6 | 7 | use Activitypub\Collection\Followers; |
| 7 | -use Activitypub\Transformer\Post; | |
| 8 | 8 | |
| 9 | +use function Activitypub\was_comment_sent; | |
| 9 | 10 | use function Activitypub\is_user_type_disabled; |
| 11 | +use function Activitypub\should_comment_be_federated; | |
| 12 | +use function Activitypub\get_remote_metadata_by_actor; | |
| 10 | 13 | |
| 11 | 14 | /** |
| 12 | 15 | * ActivityPub Scheduler Class |
| 13 | 16 | * |
| @@ -39,30 +42,29 @@ | ||
| 39 | 42 | self::schedule_post_activity( 'trash', '', $post_id ); |
| 40 | 43 | } |
| 41 | 44 | ); |
| 42 | 45 | |
| 43 | - // Comment transitions | |
| 44 | - \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 ); | |
| 45 | - \add_action( | |
| 46 | - 'edit_comment', | |
| 47 | - function ( $comment_id ) { | |
| 48 | - self::schedule_comment_activity( 'approved', 'approved', $comment_id ); | |
| 49 | - } | |
| 50 | - ); | |
| 51 | - \add_action( | |
| 52 | - 'wp_insert_comment', | |
| 53 | - function ( $comment_id ) { | |
| 54 | - self::schedule_comment_activity( 'approved', '', $comment_id ); | |
| 55 | - } | |
| 56 | - ); | |
| 46 | + if ( ! ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS ) { | |
| 47 | + // Comment transitions | |
| 48 | + \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 ); | |
| 49 | + \add_action( | |
| 50 | + 'edit_comment', | |
| 51 | + function ( $comment_id ) { | |
| 52 | + self::schedule_comment_activity( 'approved', 'approved', $comment_id ); | |
| 53 | + } | |
| 54 | + ); | |
| 55 | + \add_action( | |
| 56 | + 'wp_insert_comment', | |
| 57 | + function ( $comment_id ) { | |
| 58 | + self::schedule_comment_activity( 'approved', '', $comment_id ); | |
| 59 | + } | |
| 60 | + ); | |
| 61 | + } | |
| 57 | 62 | |
| 58 | 63 | // Follower Cleanups |
| 59 | 64 | \add_action( 'activitypub_update_followers', array( self::class, 'update_followers' ) ); |
| 60 | 65 | \add_action( 'activitypub_cleanup_followers', array( self::class, 'cleanup_followers' ) ); |
| 61 | 66 | |
| 62 | - // Migration | |
| 63 | - \add_action( 'admin_init', array( self::class, 'schedule_migration' ) ); | |
| 64 | - | |
| 65 | 67 | // profile updates for blog options |
| 66 | 68 | if ( ! is_user_type_disabled( 'blog' ) ) { |
| 67 | 69 | \add_action( 'update_option_site_icon', array( self::class, 'blog_user_update' ) ); |
| 68 | 70 | \add_action( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) ); |
| @@ -114,8 +116,22 @@ | ||
| 114 | 116 | */ |
| 115 | 117 | public static function schedule_post_activity( $new_status, $old_status, $post ) { |
| 116 | 118 | $post = get_post( $post ); |
| 117 | 119 | |
| 120 | + if ( ! $post ) { | |
| 121 | + return; | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( 'ap_extrafield' === $post->post_type ) { | |
| 125 | + self::schedule_profile_update( $post->post_author ); | |
| 126 | + return; | |
| 127 | + } | |
| 128 | + | |
| 129 | + if ( 'ap_extrafield_blog' === $post->post_type ) { | |
| 130 | + self::schedule_profile_update( 0 ); | |
| 131 | + return; | |
| 132 | + } | |
| 133 | + | |
| 118 | 134 | // Do not send activities if post is password protected. |
| 119 | 135 | if ( \post_password_required( $post ) ) { |
| 120 | 136 | return; |
| 121 | 137 | } |
| @@ -127,34 +143,35 @@ | ||
| 127 | 143 | } |
| 128 | 144 | |
| 129 | 145 | $type = false; |
| 130 | 146 | |
| 131 | - if ( 'publish' === $new_status && 'publish' !== $old_status ) { | |
| 147 | + if ( | |
| 148 | + 'publish' === $new_status && | |
| 149 | + 'publish' !== $old_status | |
| 150 | + ) { | |
| 132 | 151 | $type = 'Create'; |
| 133 | - } elseif ( 'publish' === $new_status ) { | |
| 152 | + } elseif ( | |
| 153 | + 'publish' === $new_status || | |
| 154 | + // We want to send updates for posts that are published and then moved to draft. | |
| 155 | + ( 'draft' === $new_status && | |
| 156 | + 'publish' === $old_status ) | |
| 157 | + ) { | |
| 134 | 158 | $type = 'Update'; |
| 135 | 159 | } elseif ( 'trash' === $new_status ) { |
| 136 | 160 | $type = 'Delete'; |
| 137 | 161 | } |
| 138 | 162 | |
| 139 | - if ( ! $type ) { | |
| 163 | + if ( empty( $type ) ) { | |
| 140 | 164 | return; |
| 141 | 165 | } |
| 142 | 166 | |
| 143 | - \wp_schedule_single_event( | |
| 144 | - \time(), | |
| 145 | - 'activitypub_send_activity', | |
| 146 | - array( $post, $type ) | |
| 147 | - ); | |
| 167 | + $hook = 'activitypub_send_post'; | |
| 168 | + $args = array( $post->ID, $type ); | |
| 148 | 169 | |
| 149 | - \wp_schedule_single_event( | |
| 150 | - \time(), | |
| 151 | - sprintf( | |
| 152 | - 'activitypub_send_%s_activity', | |
| 153 | - \strtolower( $type ) | |
| 154 | - ), | |
| 155 | - array( $post ) | |
| 156 | - ); | |
| 170 | + if ( false === wp_next_scheduled( $hook, $args ) ) { | |
| 171 | + set_wp_object_state( $post, 'federate' ); | |
| 172 | + \wp_schedule_single_event( \time(), $hook, $args ); | |
| 173 | + } | |
| 157 | 174 | } |
| 158 | 175 | |
| 159 | 176 | /** |
| 160 | 177 | * Schedule Comment Activities |
| @@ -167,13 +184,15 @@ | ||
| 167 | 184 | */ |
| 168 | 185 | public static function schedule_comment_activity( $new_status, $old_status, $comment ) { |
| 169 | 186 | $comment = get_comment( $comment ); |
| 170 | 187 | |
| 171 | - // Federate only approved comments. | |
| 172 | - if ( ! $comment->user_id ) { | |
| 188 | + // federate only comments that are written by a registered user. | |
| 189 | + if ( ! $comment || ! $comment->user_id ) { | |
| 173 | 190 | return; |
| 174 | 191 | } |
| 175 | 192 | |
| 193 | + $type = false; | |
| 194 | + | |
| 176 | 195 | if ( |
| 177 | 196 | 'approved' === $new_status && |
| 178 | 197 | 'approved' !== $old_status |
| 179 | 198 | ) { |
| @@ -187,26 +206,24 @@ | ||
| 187 | 206 | ) { |
| 188 | 207 | $type = 'Delete'; |
| 189 | 208 | } |
| 190 | 209 | |
| 191 | - if ( ! $type ) { | |
| 210 | + if ( empty( $type ) ) { | |
| 192 | 211 | return; |
| 193 | 212 | } |
| 194 | 213 | |
| 195 | - \wp_schedule_single_event( | |
| 196 | - \time(), | |
| 197 | - 'activitypub_send_activity', | |
| 198 | - array( $comment, $type ) | |
| 199 | - ); | |
| 214 | + // check if comment should be federated or not | |
| 215 | + if ( ! should_comment_be_federated( $comment ) ) { | |
| 216 | + return; | |
| 217 | + } | |
| 200 | 218 | |
| 201 | - \wp_schedule_single_event( | |
| 202 | - \time(), | |
| 203 | - sprintf( | |
| 204 | - 'activitypub_send_%s_activity', | |
| 205 | - \strtolower( $type ) | |
| 206 | - ), | |
| 207 | - array( $comment ) | |
| 208 | - ); | |
| 219 | + $hook = 'activitypub_send_comment'; | |
| 220 | + $args = array( $comment->comment_ID, $type ); | |
| 221 | + | |
| 222 | + if ( false === wp_next_scheduled( $hook, $args ) ) { | |
| 223 | + set_wp_object_state( $comment, 'federate' ); | |
| 224 | + \wp_schedule_single_event( \time(), $hook, $args ); | |
| 225 | + } | |
| 209 | 226 | } |
| 210 | 227 | |
| 211 | 228 | /** |
| 212 | 229 | * Update followers |
| @@ -219,8 +236,9 @@ | ||
| 219 | 236 | if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { |
| 220 | 237 | $number = 50; |
| 221 | 238 | } |
| 222 | 239 | |
| 240 | + $number = apply_filters( 'activitypub_update_followers_number', $number ); | |
| 223 | 241 | $followers = Followers::get_outdated_followers( $number ); |
| 224 | 242 | |
| 225 | 243 | foreach ( $followers as $follower ) { |
| 226 | 244 | $meta = get_remote_metadata_by_actor( $follower->get_id(), false ); |
| @@ -245,8 +263,9 @@ | ||
| 245 | 263 | if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { |
| 246 | 264 | $number = 50; |
| 247 | 265 | } |
| 248 | 266 | |
| 267 | + $number = apply_filters( 'activitypub_update_followers_number', $number ); | |
| 249 | 268 | $followers = Followers::get_faulty_followers( $number ); |
| 250 | 269 | |
| 251 | 270 | foreach ( $followers as $follower ) { |
| 252 | 271 | $meta = get_remote_metadata_by_actor( $follower->get_url(), false ); |
| @@ -255,8 +274,13 @@ | ||
| 255 | 274 | $follower->delete(); |
| 256 | 275 | } elseif ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) { |
| 257 | 276 | if ( $follower->count_errors() >= 5 ) { |
| 258 | 277 | $follower->delete(); |
| 278 | + \wp_schedule_single_event( | |
| 279 | + \time(), | |
| 280 | + 'activitypub_delete_actor_interactions', | |
| 281 | + array( $follower->get_id() ) | |
| 282 | + ); | |
| 259 | 283 | } else { |
| 260 | 284 | Followers::add_error( $follower->get__id(), $meta ); |
| 261 | 285 | } |
| 262 | 286 | } else { |
| @@ -265,19 +289,8 @@ | ||
| 265 | 289 | } |
| 266 | 290 | } |
| 267 | 291 | |
| 268 | 292 | /** |
| 269 | - * Schedule migration if DB-Version is not up to date. | |
| 270 | - * | |
| 271 | - * @return void | |
| 272 | - */ | |
| 273 | - public static function schedule_migration() { | |
| 274 | - if ( ! \wp_next_scheduled( 'activitypub_schedule_migration' ) && ! Migration::is_latest_version() ) { | |
| 275 | - \wp_schedule_single_event( \time(), 'activitypub_schedule_migration' ); | |
| 276 | - } | |
| 277 | - } | |
| 278 | - | |
| 279 | - /** | |
| 280 | 293 | * Send a profile update when relevant user meta is updated. |
| 281 | 294 | * |
| 282 | 295 | * @param int $meta_id Meta ID being updated. |
| 283 | 296 | * @param int $user_id User ID being updated. |
| @@ -286,14 +299,15 @@ | ||
| 286 | 299 | * @return void |
| 287 | 300 | */ |
| 288 | 301 | public static function user_meta_update( $meta_id, $user_id, $meta_key ) { |
| 289 | 302 | // don't bother if the user can't publish |
| 290 | - if ( ! \user_can( $user_id, 'publish_posts' ) ) { | |
| 303 | + if ( ! \user_can( $user_id, 'activitypub' ) ) { | |
| 291 | 304 | return; |
| 292 | 305 | } |
| 293 | 306 | // the user meta fields that affect a profile. |
| 294 | 307 | $fields = array( |
| 295 | - 'activitypub_user_description', | |
| 308 | + 'activitypub_description', | |
| 309 | + 'activitypub_header_image', | |
| 296 | 310 | 'description', |
| 297 | 311 | 'user_url', |
| 298 | 312 | 'display_name', |
| 299 | 313 | ); |
| @@ -310,9 +324,9 @@ | ||
| 310 | 324 | * @return void |
| 311 | 325 | */ |
| 312 | 326 | public static function user_update( $user_id ) { |
| 313 | 327 | // don't bother if the user can't publish |
| 314 | - if ( ! \user_can( $user_id, 'publish_posts' ) ) { | |
| 328 | + if ( ! \user_can( $user_id, 'activitypub' ) ) { | |
| 315 | 329 | return; |
| 316 | 330 | } |
| 317 | 331 | |
| 318 | 332 | self::schedule_profile_update( $user_id ); |
| @@ -319,9 +333,11 @@ | ||
| 319 | 333 | } |
| 320 | 334 | |
| 321 | 335 | /** |
| 322 | 336 | * Theme mods only have a dynamic filter so we fudge it like this. |
| 323 | - * @param mixed $value | |
| 337 | + * | |
| 338 | + * @param mixed $value | |
| 339 | + * | |
| 324 | 340 | * @return mixed |
| 325 | 341 | */ |
| 326 | 342 | public static function blog_user_update( $value = null ) { |
| 327 | 343 | self::schedule_profile_update( 0 ); |
| @@ -329,8 +345,9 @@ | ||
| 329 | 345 | } |
| 330 | 346 | |
| 331 | 347 | /** |
| 332 | 348 | * Send a profile update to all followers. Gets hooked into all relevant options/meta etc. |
| 349 | + * | |
| 333 | 350 | * @param int $user_id The user ID to update (Could be 0 for Blog-User). |
| 334 | 351 | */ |
| 335 | 352 | public static function schedule_profile_update( $user_id ) { |
| 336 | 353 | \wp_schedule_single_event( |