Repository navigation
Expand file tree
/
Copy pathuninstall.php
More file actions
101 lines (90 loc) · 4.3 KB
/
Copy pathuninstall.php
File metadata and controls
101 lines (90 loc) · 4.3 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Socync Uninstall Script.
*
* Cleans up plugin data when deleted from WordPress admin. Persistent data is
* removed only when the "Delete data on uninstall" option is enabled on the
* Settings page; otherwise it is preserved so it can be restored on re-install.
*
* @package Socync
*/
// Prevent direct access or loading on non-deletion contexts.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || false === WP_UNINSTALL_PLUGIN ) {
exit;
}
/**
* Clean up plugin-specific data from WordPress database.
*
* This script is executed when the plugin is deleted via admin panel.
* Cron events are always cleared; options, post meta, and custom tables are
* removed only when the admin opted in on the Settings page.
*/
function socync_uninstall() {
global $wpdb;
/**
* Cron events are transient runtime state, not user data — always clear them.
*/
wp_clear_scheduled_hook( 'socync_run_delayed_posts' );
wp_clear_scheduled_hook( 'socync_purge_old_logs' );
wp_clear_scheduled_hook( 'socialsync_run_delayed_posts' );
wp_clear_scheduled_hook( 'socialsync_purge_old_logs' );
// Clear the cron lock so a re-install does not skip its first cron cycle
// if the preserved lock is still within the scheduler's 5-minute window.
delete_option( 'socync_cron_lock' );
/**
* Unless the admin opted in on the Settings page, keep all persistent data
* so it is restored if the plugin is re-installed.
*/
if ( ! get_option( 'socync_delete_data_on_uninstall', false ) ) {
// Cancel any rows still marked as scheduled so they are not published
// automatically if the plugin is re-installed.
foreach ( array(
$wpdb->prefix . 'socync_scheduled_posts',
$wpdb->prefix . 'socialsync_scheduled_posts',
) as $cancel_table ) {
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $cancel_table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $table_exists ) {
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"UPDATE {$cancel_table} SET status = 'cancelled' WHERE status = 'scheduled'"
);
}
}
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( 'Socync plugin uninstalled. Database data preserved; pending scheduled posts cancelled.' );
}
return;
}
/**
* Remove all Socync-specific option names from wp_options table.
*/
foreach ( array( 'socync_', 'socialsync_' ) as $prefix ) {
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
$wpdb->esc_like( $prefix ) . '%'
)
);
}
foreach ( array( '_socync_', '_socialsync_' ) as $prefix ) {
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
$wpdb->esc_like( $prefix ) . '%'
)
);
}
/**
* Drop custom scheduled posts table.
*/
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}socync_scheduled_posts" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}socialsync_scheduled_posts" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
/**
* Debug log uninstall completion (for support purposes only - not in production).
*/
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( 'Socync plugin uninstalled. Database cleanup complete.' );
}
}
socync_uninstall();