Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions includes/desktop-files/cascade-cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* skips `wp_trash_post`.
* - `deleted_user` — user account removed (with or without
* content reassignment).
* - `trashed_comment` — comment moved to WP trash.
* - `deleted_comment` — comment hard-deleted.
*
* Implementation is a bulk UPDATE that bypasses the per-placement
* permission check used by user-initiated trash. The trashing
Expand Down Expand Up @@ -232,3 +234,24 @@ function openstation_files_cascade_on_user_delete( $user_id ) {
}

add_action( 'deleted_user', 'openstation_files_cascade_on_user_delete', 10, 1 );

/**
* Wrap a `(comment_id)` Core action so it cascades against
* `file_type='comment'`. Fires when a comment is trashed or
* permanently deleted so shortcuts pointing at it are soft-trashed.
*
* @param int $comment_id Comment id.
*/
function openstation_files_cascade_on_comment_trash( $comment_id ) {
$comment_id = (int) $comment_id;
if ( $comment_id <= 0 ) {
return;
}
openstation_files_cascade_trash_placements_for_entity(
'comment',
(string) $comment_id
);
}

add_action( 'trashed_comment', 'openstation_files_cascade_on_comment_trash', 10, 1 );
add_action( 'deleted_comment', 'openstation_files_cascade_on_comment_trash', 10, 1 );
34 changes: 33 additions & 1 deletion tests/phpunit/tests/desktopFilesCascadeCleanup.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Tests for the cascade cleanup of placements when a source
* entity (post, attachment, user) is trashed or deleted via any
* entity (post, attachment, user, comment) is trashed or deleted via any
* WordPress route.
*
* @package WordPress
Expand Down Expand Up @@ -190,6 +190,38 @@ public function test_deleted_user_triggers_cascade() {
$this->assertNotNull( $this->read_trash_columns( $p_id )['trashed_at_ms'] );
}

/**
* @covers ::openstation_files_cascade_on_comment_trash
*/
public function test_wp_trash_comment_triggers_cascade() {
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) );
$p_id = openstation_files_place( self::$admin_id, 0, 'comment', (string) $comment_id, array( 'x' => 0, 'y' => 0 ) );

wp_trash_comment( $comment_id );

$this->assertNotNull(
$this->read_trash_columns( $p_id )['trashed_at_ms'],
'wp_trash_comment() must cascade-soft-trash the matching placement.'
);
}

/**
* @covers ::openstation_files_cascade_on_comment_trash
*/
public function test_delete_comment_triggers_cascade() {
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) );
$p_id = openstation_files_place( self::$admin_id, 0, 'comment', (string) $comment_id, array( 'x' => 0, 'y' => 0 ) );

wp_delete_comment( $comment_id, true );

$this->assertNotNull(
$this->read_trash_columns( $p_id )['trashed_at_ms'],
'wp_delete_comment() must cascade-soft-trash the matching placement.'
);
}

/**
* @covers ::openstation_files_cascade_trash_placements_for_entity
*/
Expand Down
Loading