-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.php
More file actions
82 lines (75 loc) · 2.9 KB
/
Copy pathuninstall.php
File metadata and controls
82 lines (75 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Uninstall routine — runs ONLY when the plugin is deleted from the admin
* ("Plugins → Löschen"), never on deactivation.
*
* Deactivation (see the main plugin file) intentionally keeps all data so the
* mirror survives a temporary disable. Deleting the plugin wipes everything the
* plugin itself created:
*
* REMOVED
* - the scheduled WP-Cron event
* - the plugin options (settings + last-run stats)
* - every mirrored post (identified by the source-id meta), including its
* sync meta and the featured image that was sideloaded for it
*
* INTENTIONALLY KEPT (may hold unrelated content the site still needs)
* - local categories/tags that the sync created
* - WP Media Folder terms ("wpmf-category")
* - the site's own, non-mirrored posts and media
*
* On multisite the full cleanup runs for every site in the network.
*
* @package netways-wp-postsync
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/*
* The plugin classes are NOT loaded during uninstall, so these identifiers are
* inlined. Keep them in sync with Netways_WP_PostSync_Settings and the main file.
*/
$netways_wp_postsync_option_keys = array( 'netways_wp_postsync_options', 'netways_wp_postsync_last_run' );
$netways_wp_postsync_cron_hook = 'netways_wp_postsync_sync_run';
$netways_wp_postsync_meta_id = '_netways_wp_postsync_source_id';
/**
* Remove everything the plugin created on the CURRENT site.
*
* @param string[] $option_keys Option names to delete.
* @param string $cron_hook Cron hook to clear.
* @param string $meta_id Meta key identifying mirrored posts.
*/
function netways_wp_postsync_uninstall_site( $option_keys, $cron_hook, $meta_id ) {
// 1. Clear the scheduled sync.
wp_clear_scheduled_hook( $cron_hook );
// 2. Delete mirrored posts + their sideloaded featured images.
$posts = get_posts( array(
'post_type' => 'post',
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_key' => $meta_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'no_found_rows' => true,
'suppress_filters' => true,
) );
foreach ( $posts as $post_id ) {
$thumb_id = get_post_thumbnail_id( $post_id );
if ( $thumb_id ) {
wp_delete_attachment( (int) $thumb_id, true );
}
wp_delete_post( (int) $post_id, true );
}
// 3. Drop plugin options.
foreach ( $option_keys as $option ) {
delete_option( $option );
}
}
if ( is_multisite() ) {
foreach ( get_sites( array( 'fields' => 'ids' ) ) as $netways_wp_postsync_site_id ) {
switch_to_blog( (int) $netways_wp_postsync_site_id );
netways_wp_postsync_uninstall_site( $netways_wp_postsync_option_keys, $netways_wp_postsync_cron_hook, $netways_wp_postsync_meta_id );
restore_current_blog();
}
} else {
netways_wp_postsync_uninstall_site( $netways_wp_postsync_option_keys, $netways_wp_postsync_cron_hook, $netways_wp_postsync_meta_id );
}