Skip to content

Commit c14ae81

Browse files
authored
Delete remote posts on plugin uninstall (#2632)
1 parent 0290dc3 commit c14ae81

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Delete remote posts on plugin uninstall.

includes/class-activitypub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Activitypub\Collection\Followers;
1111
use Activitypub\Collection\Following;
12+
use Activitypub\Collection\Posts;
1213

1314
/**
1415
* ActivityPub Class.
@@ -85,6 +86,8 @@ public static function uninstall() {
8586
\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
8687
Migration::update_comment_counts( 2000 );
8788

89+
Posts::delete_all();
90+
8891
Options::delete();
8992
}
9093

includes/class-cli.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Activitypub\Activity\Activity;
1111
use Activitypub\Collection\Actors;
1212
use Activitypub\Collection\Outbox;
13+
use Activitypub\Collection\Posts;
1314
use Activitypub\Scheduler\Actor;
1415

1516
/**
@@ -107,6 +108,13 @@ private function execute_self_destruct( $assoc_args ) {
107108
}
108109

109110
$processed = $this->process_user_deletions( $user_ids );
111+
112+
// Delete all remote posts.
113+
$deleted_posts = Posts::delete_all();
114+
if ( $deleted_posts > 0 ) {
115+
\WP_CLI::line( \WP_CLI::colorize( "%G✓%n Deleted {$deleted_posts} remote post(s)." ) );
116+
}
117+
110118
$this->display_completion_message( $processed );
111119
}
112120

@@ -123,6 +131,7 @@ private function display_self_destruct_warning() {
123131
\WP_CLI::line( \WP_CLI::colorize( "%y{$question}%n" ) );
124132
\WP_CLI::line( \WP_CLI::colorize( '%y• Send Delete activities to all followers%n' ) );
125133
\WP_CLI::line( \WP_CLI::colorize( '%y• Remove your blog from ActivityPub networks%n' ) );
134+
\WP_CLI::line( \WP_CLI::colorize( '%y• Delete all cached remote posts%n' ) );
126135
\WP_CLI::line( '' );
127136
}
128137

includes/collection/class-posts.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,30 @@ public static function remove_recipient( $post_id, $user_id ) {
453453
return \delete_post_meta( $post_id, '_activitypub_user_id', $user_id );
454454
}
455455

456+
/**
457+
* Delete all posts.
458+
*
459+
* Used during plugin uninstall to clean up all remote posts.
460+
*
461+
* @return int The number of posts deleted.
462+
*/
463+
public static function delete_all() {
464+
$post_ids = \get_posts(
465+
array(
466+
'post_type' => self::POST_TYPE,
467+
'post_status' => array( 'any', 'trash', 'auto-draft' ),
468+
'fields' => 'ids',
469+
'numberposts' => -1,
470+
)
471+
);
472+
473+
foreach ( $post_ids as $post_id ) {
474+
\wp_delete_post( $post_id, true );
475+
}
476+
477+
return count( $post_ids );
478+
}
479+
456480
/**
457481
* Purge old remote posts.
458482
*

tests/phpunit/tests/includes/collection/class-test-posts.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,85 @@ public function test_remove_hashtags_preserves_structure() {
13431343
$this->assertStringNotContainsString( '#php', $result );
13441344
}
13451345

1346+
/**
1347+
* Test delete_all method deletes all posts.
1348+
*
1349+
* @covers ::delete_all
1350+
*/
1351+
public function test_delete_all() {
1352+
// Create some posts.
1353+
self::factory()->post->create_many(
1354+
5,
1355+
array(
1356+
'post_type' => Posts::POST_TYPE,
1357+
'post_status' => 'publish',
1358+
)
1359+
);
1360+
1361+
// Verify posts were created.
1362+
$count_before = \wp_count_posts( Posts::POST_TYPE )->publish;
1363+
$this->assertEquals( 5, $count_before );
1364+
1365+
// Delete all posts.
1366+
$deleted = Posts::delete_all();
1367+
1368+
// Clear cache to get accurate count.
1369+
\wp_cache_delete( \_count_posts_cache_key( Posts::POST_TYPE ), 'counts' );
1370+
1371+
// Verify all posts were deleted.
1372+
$count_after = \wp_count_posts( Posts::POST_TYPE )->publish;
1373+
$this->assertEquals( 0, $count_after );
1374+
1375+
// Verify return value.
1376+
$this->assertEquals( 5, $deleted );
1377+
}
1378+
1379+
/**
1380+
* Test delete_all method with mixed post statuses.
1381+
*
1382+
* @covers ::delete_all
1383+
*/
1384+
public function test_delete_all_mixed_statuses() {
1385+
// Create posts with different statuses.
1386+
self::factory()->post->create_many(
1387+
3,
1388+
array(
1389+
'post_type' => Posts::POST_TYPE,
1390+
'post_status' => 'publish',
1391+
)
1392+
);
1393+
self::factory()->post->create_many(
1394+
2,
1395+
array(
1396+
'post_type' => Posts::POST_TYPE,
1397+
'post_status' => 'draft',
1398+
)
1399+
);
1400+
self::factory()->post->create(
1401+
array(
1402+
'post_type' => Posts::POST_TYPE,
1403+
'post_status' => 'trash',
1404+
)
1405+
);
1406+
1407+
// Delete all posts.
1408+
$deleted = Posts::delete_all();
1409+
1410+
// Verify all posts were deleted regardless of status.
1411+
$remaining = \get_posts(
1412+
array(
1413+
'post_type' => Posts::POST_TYPE,
1414+
'post_status' => 'any',
1415+
'numberposts' => -1,
1416+
'fields' => 'ids',
1417+
)
1418+
);
1419+
$this->assertEmpty( $remaining );
1420+
1421+
// Verify return value includes all posts.
1422+
$this->assertEquals( 6, $deleted );
1423+
}
1424+
13461425
/**
13471426
* Test purge method with more than 200 posts.
13481427
*

0 commit comments

Comments
 (0)