Skip to content

Commit 9cf521c

Browse files
authored
Prevent scheduler overload (#700)
1 parent 4d529e8 commit 9cf521c

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

includes/class-activity-dispatcher.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Activity_Dispatcher {
2727
* Initialize the class, registering WordPress hooks.
2828
*/
2929
public static function init() {
30+
\add_action( 'activitypub_send_post', array( self::class, 'send_post' ), 10, 2 );
31+
\add_action( 'activitypub_send_comment', array( self::class, 'send_comment' ), 10, 2 );
32+
3033
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 );
3134
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity_or_announce' ), 10, 2 );
3235
\add_action( 'activitypub_send_update_profile_activity', array( self::class, 'send_profile_update' ), 10, 1 );
@@ -172,4 +175,54 @@ private static function send_activity_to_followers( $activity, $user_id, $wp_obj
172175

173176
set_wp_object_state( $wp_object, 'federated' );
174177
}
178+
179+
/**
180+
* Send a "Create" or "Update" Activity for a WordPress Post.
181+
*
182+
* @param int $id The WordPress Post ID.
183+
* @param string $type The Activity-Type.
184+
*
185+
* @return void
186+
*/
187+
public static function send_post( $id, $type ) {
188+
$post = get_post( $id );
189+
190+
if ( ! $post ) {
191+
return;
192+
}
193+
194+
do_action( 'activitypub_send_activity', $post, $type );
195+
do_action(
196+
sprintf(
197+
'activitypub_send_%s_activity',
198+
\strtolower( $type )
199+
),
200+
$post
201+
);
202+
}
203+
204+
/**
205+
* Send a "Create" or "Update" Activity for a WordPress Comment.
206+
*
207+
* @param int $id The WordPress Comment ID.
208+
* @param string $type The Activity-Type.
209+
*
210+
* @return void
211+
*/
212+
public static function send_comment( $id, $type ) {
213+
$comment = get_comment( $id );
214+
215+
if ( ! $comment ) {
216+
return;
217+
}
218+
219+
do_action( 'activitypub_send_activity', $comment, $type );
220+
do_action(
221+
sprintf(
222+
'activitypub_send_%s_activity',
223+
\strtolower( $type )
224+
),
225+
$comment
226+
);
227+
}
175228
}

includes/class-admin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ function ( $allcaps, $caps, $arg ) {
341341
public static function comment_row_actions( $actions, $comment ) {
342342
if ( was_comment_received( $comment ) ) {
343343
unset( $actions['edit'] );
344-
unset( $actions['reply'] );
345344
unset( $actions['quickedit'] );
346345
}
347346

includes/class-scheduler.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,13 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
145145
return;
146146
}
147147

148-
\wp_schedule_single_event(
149-
\time(),
150-
'activitypub_send_activity',
151-
array( $post, $type )
152-
);
148+
$hook = 'activitypub_send_post';
149+
$args = array( $post->ID, $type );
153150

154-
\wp_schedule_single_event(
155-
\time(),
156-
sprintf(
157-
'activitypub_send_%s_activity',
158-
\strtolower( $type )
159-
),
160-
array( $post )
161-
);
151+
if ( false === wp_next_scheduled( $hook, $args ) ) {
152+
set_wp_object_state( $post, 'federate' );
153+
\wp_schedule_single_event( \time(), $hook, $args );
154+
}
162155
}
163156

164157
/**
@@ -204,22 +197,13 @@ public static function schedule_comment_activity( $new_status, $old_status, $com
204197
return;
205198
}
206199

207-
set_wp_object_state( $comment, 'federate' );
200+
$hook = 'activitypub_send_comment';
201+
$args = array( $comment->comment_ID, $type );
208202

209-
\wp_schedule_single_event(
210-
\time(),
211-
'activitypub_send_activity',
212-
array( $comment, $type )
213-
);
214-
215-
\wp_schedule_single_event(
216-
\time(),
217-
sprintf(
218-
'activitypub_send_%s_activity',
219-
\strtolower( $type )
220-
),
221-
array( $comment )
222-
);
203+
if ( false === wp_next_scheduled( $hook, $args ) ) {
204+
set_wp_object_state( $comment, 'federate' );
205+
\wp_schedule_single_event( \time(), $hook, $args );
206+
}
223207
}
224208

225209
/**

0 commit comments

Comments
 (0)