diff --git a/includes/desktop-files/cascade-cleanup.php b/includes/desktop-files/cascade-cleanup.php index 2ed8a8aae..dcec4c960 100644 --- a/includes/desktop-files/cascade-cleanup.php +++ b/includes/desktop-files/cascade-cleanup.php @@ -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 @@ -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 ); diff --git a/tests/phpunit/tests/desktopFilesCascadeCleanup.php b/tests/phpunit/tests/desktopFilesCascadeCleanup.php index 0c44b7626..daa69b5af 100644 --- a/tests/phpunit/tests/desktopFilesCascadeCleanup.php +++ b/tests/phpunit/tests/desktopFilesCascadeCleanup.php @@ -1,7 +1,7 @@ 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 */