Skip to content

Commit

Permalink
Outbox: Use callbacks that can be unhooked.
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Jan 20, 2025
1 parent 8a7b5cc commit 089052d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
25 changes: 13 additions & 12 deletions includes/scheduler/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ public static function init() {

// Comment transitions.
\add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 );
\add_action(
'edit_comment',
function ( $comment_id ) {
self::schedule_comment_activity( 'approved', 'approved', $comment_id );
}
);
\add_action(
'wp_insert_comment',
function ( $comment_id ) {
self::schedule_comment_activity( 'approved', '', $comment_id );
}
);
\add_action( 'wp_insert_comment', array( self::class, 'schedule_comment_activity_on_insert' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -84,4 +73,16 @@ public static function schedule_comment_activity( $new_status, $old_status, $com

add_to_outbox( $comment, $type, $comment->user_id );
}

/**
* Schedule Comment Activities on insert.
*
* @param int $comment_id Comment ID.
* @param \WP_Comment $comment Comment object.
*/
public static function schedule_comment_activity_on_insert( $comment_id, $comment ) {
if ( 1 === (int) $comment->comment_approved ) {
self::schedule_comment_activity( 'approved', '', $comment );
}
}
}
44 changes: 25 additions & 19 deletions includes/scheduler/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,38 @@ class Post {
public static function init() {
// Post transitions.
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
\add_action(
'edit_attachment',
function ( $post_id ) {
self::schedule_post_activity( 'publish', 'publish', $post_id );
}
);
\add_action(
'add_attachment',
function ( $post_id ) {

// Attachment transitions.
\add_action( 'add_attachment', array( self::class, 'transition_attachment_status' ) );
\add_action( 'edit_attachment', array( self::class, 'transition_attachment_status' ) );
\add_action( 'delete_attachment', array( self::class, 'transition_attachment_status' ) );
}

/**
* Schedules Activities for attachment transitions.
*
* @param int $post_id Attachment ID.
*/
public static function transition_attachment_status( $post_id ) {
switch ( current_action() ) {
case 'add_attachment':
self::schedule_post_activity( 'publish', '', $post_id );
}
);
\add_action(
'delete_attachment',
function ( $post_id ) {
break;
case 'edit_attachment':
self::schedule_post_activity( 'publish', 'publish', $post_id );
break;
case 'delete_attachment':
self::schedule_post_activity( 'trash', '', $post_id );
}
);
break;
}
}

/**
* Schedule Activities.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param \WP_Post $post Post object.
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param int|\WP_Post $post Post ID or post object.
*/
public static function schedule_post_activity( $new_status, $old_status, $post ) {
$post = get_post( $post );
Expand Down

0 comments on commit 089052d

Please sign in to comment.