PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
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
← All changes | includes/class-scheduler.php +83 -65 2.0.0 → 3.2.5 View file →
@@ -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 ) {
@@ -179,8 +198,9 @@
179 198 ) {
180 199 $type = 'Create';
181 200 } elseif ( 'approved' === $new_status ) {
182 201 $type = 'Update';
202 + \update_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', time(), true );
183 203 } elseif (
184 204 'trash' === $new_status ||
185 205 'spam' === $new_status
186 206 ) {
@@ -186,26 +206,24 @@
186 206 ) {
187 207 $type = 'Delete';
188 208 }
189 209
190 - if ( ! $type ) {
210 + if ( empty( $type ) ) {
191 211 return;
192 212 }
193 213
194 - \wp_schedule_single_event(
195 - \time(),
196 - 'activitypub_send_activity',
197 - array( $comment, $type )
198 - );
214 + // check if comment should be federated or not
215 + if ( ! should_comment_be_federated( $comment ) ) {
216 + return;
217 + }
199 218
200 - \wp_schedule_single_event(
201 - \time(),
202 - sprintf(
203 - 'activitypub_send_%s_activity',
204 - \strtolower( $type )
205 - ),
206 - array( $comment )
207 - );
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 + }
208 226 }
209 227
210 228 /**
211 229 * Update followers
@@ -218,8 +236,9 @@
218 236 if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
219 237 $number = 50;
220 238 }
221 239
240 + $number = apply_filters( 'activitypub_update_followers_number', $number );
222 241 $followers = Followers::get_outdated_followers( $number );
223 242
224 243 foreach ( $followers as $follower ) {
225 244 $meta = get_remote_metadata_by_actor( $follower->get_id(), false );
@@ -244,8 +263,9 @@
244 263 if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
245 264 $number = 50;
246 265 }
247 266
267 + $number = apply_filters( 'activitypub_update_followers_number', $number );
248 268 $followers = Followers::get_faulty_followers( $number );
249 269
250 270 foreach ( $followers as $follower ) {
251 271 $meta = get_remote_metadata_by_actor( $follower->get_url(), false );
@@ -254,8 +274,13 @@
254 274 $follower->delete();
255 275 } elseif ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
256 276 if ( $follower->count_errors() >= 5 ) {
257 277 $follower->delete();
278 + \wp_schedule_single_event(
279 + \time(),
280 + 'activitypub_delete_actor_interactions',
281 + array( $follower->get_id() )
282 + );
258 283 } else {
259 284 Followers::add_error( $follower->get__id(), $meta );
260 285 }
261 286 } else {
@@ -264,19 +289,8 @@
264 289 }
265 290 }
266 291
267 292 /**
268 - * Schedule migration if DB-Version is not up to date.
269 - *
270 - * @return void
271 - */
272 - public static function schedule_migration() {
273 - if ( ! \wp_next_scheduled( 'activitypub_schedule_migration' ) && ! Migration::is_latest_version() ) {
274 - \wp_schedule_single_event( \time(), 'activitypub_schedule_migration' );
275 - }
276 - }
277 -
278 - /**
279 293 * Send a profile update when relevant user meta is updated.
280 294 *
281 295 * @param int $meta_id Meta ID being updated.
282 296 * @param int $user_id User ID being updated.
@@ -285,14 +299,15 @@
285 299 * @return void
286 300 */
287 301 public static function user_meta_update( $meta_id, $user_id, $meta_key ) {
288 302 // don't bother if the user can't publish
289 - if ( ! \user_can( $user_id, 'publish_posts' ) ) {
303 + if ( ! \user_can( $user_id, 'activitypub' ) ) {
290 304 return;
291 305 }
292 306 // the user meta fields that affect a profile.
293 307 $fields = array(
294 - 'activitypub_user_description',
308 + 'activitypub_description',
309 + 'activitypub_header_image',
295 310 'description',
296 311 'user_url',
297 312 'display_name',
298 313 );
@@ -309,9 +324,9 @@
309 324 * @return void
310 325 */
311 326 public static function user_update( $user_id ) {
312 327 // don't bother if the user can't publish
313 - if ( ! \user_can( $user_id, 'publish_posts' ) ) {
328 + if ( ! \user_can( $user_id, 'activitypub' ) ) {
314 329 return;
315 330 }
316 331
317 332 self::schedule_profile_update( $user_id );
@@ -318,9 +333,11 @@
318 333 }
319 334
320 335 /**
321 336 * Theme mods only have a dynamic filter so we fudge it like this.
322 - * @param mixed $value
337 + *
338 + * @param mixed $value
339 + *
323 340 * @return mixed
324 341 */
325 342 public static function blog_user_update( $value = null ) {
326 343 self::schedule_profile_update( 0 );
@@ -328,8 +345,9 @@
328 345 }
329 346
330 347 /**
331 348 * Send a profile update to all followers. Gets hooked into all relevant options/meta etc.
349 + *
332 350 * @param int $user_id The user ID to update (Could be 0 for Blog-User).
333 351 */
334 352 public static function schedule_profile_update( $user_id ) {
335 353 \wp_schedule_single_event(