| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Configure or execute WDesignKit database cleanup. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/remove-database', [ |
| 13 |
'label' => __('WDesignKit Remove Database', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Manages the WDesignKit database-cleanup configuration and can execute an immediate cleanup. "get" reads the current config; "configure" saves which data should be removed on uninstall; "execute" runs the cleanup immediately. Destructive execute requires confirm: true; use dry_run: true to preview.', |
| 16 |
'wdesignkit', |
| 17 |
), |
| 18 |
'category' => 'wdesignkit', |
| 19 |
'input_schema' => [ |
| 20 |
'type' => 'object', |
| 21 |
'properties' => [ |
| 22 |
'action' => [ |
| 23 |
'type' => 'string', |
| 24 |
'description' => '"get" reads current config, "configure" saves cleanup settings, "execute" runs immediate cleanup.', |
| 25 |
'enum' => ['get', 'configure', 'execute'], |
| 26 |
], |
| 27 |
'remove_entries' => [ |
| 28 |
'type' => 'boolean', |
| 29 |
'description' => 'Master toggle: whether any cleanup runs on uninstall (or immediate execute).', |
| 30 |
], |
| 31 |
'promotion_data' => [ |
| 32 |
'type' => 'boolean', |
| 33 |
'description' => 'Delete user meta: wdkit_rating_banner_start_date for all users.', |
| 34 |
], |
| 35 |
'widget_builder_data' => [ |
| 36 |
'type' => 'boolean', |
| 37 |
'description' => 'Delete options: wkit_deactivate_widgets and wkit_builder.', |
| 38 |
], |
| 39 |
'all_data' => [ |
| 40 |
'type' => 'boolean', |
| 41 |
'description' => 'Delete all plugin data including settings, onboarding, and user meta. Superset of the other flags.', |
| 42 |
], |
| 43 |
'confirm' => [ |
| 44 |
'type' => 'boolean', |
| 45 |
'description' => 'Required when action is "execute". Must be true to run the cleanup immediately.', |
| 46 |
], |
| 47 |
'dry_run' => [ |
| 48 |
'type' => 'boolean', |
| 49 |
'description' => 'When true with action "execute", previews what would be deleted without making changes.', |
| 50 |
], |
| 51 |
], |
| 52 |
'additionalProperties' => false, |
| 53 |
], |
| 54 |
'output_schema' => [ |
| 55 |
'type' => 'object', |
| 56 |
'properties' => [ |
| 57 |
'success' => ['type' => 'boolean'], |
| 58 |
'message' => ['type' => 'string'], |
| 59 |
'config' => ['type' => 'object'], |
| 60 |
'deleted' => ['type' => 'array', 'items' => ['type' => 'string']], |
| 61 |
'dry_run' => ['type' => 'boolean'], |
| 62 |
], |
| 63 |
], |
| 64 |
'execute_callback' => 'wdesignkit_mcp_remove_database', |
| 65 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 66 |
'meta' => [ |
| 67 |
'show_in_rest' => true, |
| 68 |
'mcp' => ['public' => true], |
| 69 |
'annotations' => [ |
| 70 |
'instructions' => implode("\n", [ |
| 71 |
'Manages WDesignKit database cleanup.', |
| 72 |
'action "get": returns current remove_db config (default if action omitted).', |
| 73 |
'action "configure": saves settings that take effect on next plugin uninstall.', |
| 74 |
'action "execute": runs cleanup NOW — requires confirm: true. Use dry_run: true to preview first.', |
| 75 |
'WARNING: execute with all_data: true deletes ALL WDesignKit settings and cannot be undone.', |
| 76 |
'Deleted items by flag — widget_builder_data: wkit_deactivate_widgets, wkit_builder; all_data: additionally wkit_settings_panel, wkit_onbording_end, wdkit_dark_mode, wdkit_wintersale_notice_dismissed, user meta wdkit_rating_banner_start_date.', |
| 77 |
]), |
| 78 |
'readonly' => false, |
| 79 |
'destructive' => true, |
| 80 |
'idempotent' => false, |
| 81 |
], |
| 82 |
], |
| 83 |
]); |
| 84 |
|
| 85 |
function wdesignkit_mcp_remove_database(array $input): array { |
| 86 |
$action = sanitize_text_field((string) ($input['action'] ?? 'get')); |
| 87 |
|
| 88 |
$settings = get_option('wkit_settings_panel', []); |
| 89 |
if (!is_array($settings)) { |
| 90 |
$settings = []; |
| 91 |
} |
| 92 |
|
| 93 |
$stored = is_array($settings['remove_db'] ?? null) ? $settings['remove_db'] : []; |
| 94 |
$current_config = [ |
| 95 |
'remove_entries' => ($stored['remove_entries'] ?? '') === 'on', |
| 96 |
'promotion_data' => !empty($stored['promotion_data']), |
| 97 |
'widget_builder_data' => !empty($stored['widget_builder_data']), |
| 98 |
'all_data' => !empty($stored['all_data']), |
| 99 |
]; |
| 100 |
|
| 101 |
if ($action === 'get') { |
| 102 |
return [ |
| 103 |
'success' => true, |
| 104 |
'message' => 'Current remove_db configuration.', |
| 105 |
'config' => $current_config, |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
// Merge caller's overrides into current config |
| 110 |
$new_config = $current_config; |
| 111 |
foreach (['remove_entries', 'promotion_data', 'widget_builder_data', 'all_data'] as $key) { |
| 112 |
if (isset($input[$key])) { |
| 113 |
$new_config[$key] = (bool) $input[$key]; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ($action === 'configure') { |
| 118 |
$settings['remove_db'] = [ |
| 119 |
'remove_entries' => $new_config['remove_entries'] ? 'on' : 'off', |
| 120 |
'promotion_data' => $new_config['promotion_data'], |
| 121 |
'widget_builder_data' => $new_config['widget_builder_data'], |
| 122 |
'all_data' => $new_config['all_data'], |
| 123 |
]; |
| 124 |
update_option('wkit_settings_panel', $settings); |
| 125 |
|
| 126 |
return [ |
| 127 |
'success' => true, |
| 128 |
'message' => 'Database cleanup configuration saved. Settings take effect on next plugin uninstall.', |
| 129 |
'config' => $new_config, |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
// action === 'execute' |
| 134 |
// Use the stored configure() settings as the baseline so that a plain |
| 135 |
// dry_run: true call (no extra flags) previews exactly what configure() set up. |
| 136 |
// Per-call params override the stored values when explicitly provided. |
| 137 |
$new_config = [ |
| 138 |
'remove_entries' => isset($input['remove_entries']) ? (bool) $input['remove_entries'] : $current_config['remove_entries'], |
| 139 |
'promotion_data' => isset($input['promotion_data']) ? (bool) $input['promotion_data'] : $current_config['promotion_data'], |
| 140 |
'widget_builder_data' => isset($input['widget_builder_data']) ? (bool) $input['widget_builder_data'] : $current_config['widget_builder_data'], |
| 141 |
'all_data' => isset($input['all_data']) ? (bool) $input['all_data'] : $current_config['all_data'], |
| 142 |
]; |
| 143 |
|
| 144 |
$dry_run = (bool) ($input['dry_run'] ?? false); |
| 145 |
$confirm = (bool) ($input['confirm'] ?? false); |
| 146 |
|
| 147 |
if (!$dry_run && !$confirm) { |
| 148 |
return [ |
| 149 |
'success' => false, |
| 150 |
'message' => 'Immediate cleanup requires confirm: true (or dry_run: true to preview).', |
| 151 |
'config' => $new_config, |
| 152 |
'dry_run' => false, |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
// Resolve what would be deleted |
| 157 |
$to_delete_options = []; |
| 158 |
$to_delete_user_meta = []; |
| 159 |
|
| 160 |
if ($new_config['remove_entries']) { |
| 161 |
if ($new_config['promotion_data']) { |
| 162 |
$to_delete_user_meta[] = 'wdkit_rating_banner_start_date (all users)'; |
| 163 |
} |
| 164 |
if ($new_config['widget_builder_data']) { |
| 165 |
$to_delete_options[] = 'wkit_deactivate_widgets'; |
| 166 |
$to_delete_options[] = 'wkit_builder'; |
| 167 |
} |
| 168 |
if ($new_config['all_data']) { |
| 169 |
$to_delete_options = array_unique(array_merge($to_delete_options, [ |
| 170 |
'wkit_deactivate_widgets', |
| 171 |
'wkit_builder', |
| 172 |
'wkit_settings_panel', |
| 173 |
'wkit_onbording_end', |
| 174 |
'wdkit_dark_mode', |
| 175 |
'wdkit_wintersale_notice_dismissed', |
| 176 |
])); |
| 177 |
$to_delete_user_meta = ['wdkit_rating_banner_start_date (all users)']; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$preview = array_merge(array_values($to_delete_options), $to_delete_user_meta); |
| 182 |
|
| 183 |
if ($dry_run) { |
| 184 |
return [ |
| 185 |
'success' => true, |
| 186 |
'message' => empty($preview) ? 'Nothing would be deleted with these settings.' : 'Dry run: the following would be deleted.', |
| 187 |
'config' => $new_config, |
| 188 |
'deleted' => $preview, |
| 189 |
'dry_run' => true, |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
// Execute |
| 194 |
$actually_deleted = []; |
| 195 |
|
| 196 |
if ($new_config['remove_entries']) { |
| 197 |
if ($new_config['promotion_data']) { |
| 198 |
foreach (get_users() as $user) { |
| 199 |
delete_user_meta((int) $user->ID, 'wdkit_rating_banner_start_date'); |
| 200 |
} |
| 201 |
$actually_deleted[] = 'wdkit_rating_banner_start_date (all users)'; |
| 202 |
} |
| 203 |
|
| 204 |
if ($new_config['widget_builder_data']) { |
| 205 |
delete_option('wkit_deactivate_widgets'); |
| 206 |
delete_option('wkit_builder'); |
| 207 |
$actually_deleted[] = 'wkit_deactivate_widgets'; |
| 208 |
$actually_deleted[] = 'wkit_builder'; |
| 209 |
} |
| 210 |
|
| 211 |
if ($new_config['all_data']) { |
| 212 |
delete_option('wkit_deactivate_widgets'); |
| 213 |
delete_option('wkit_builder'); |
| 214 |
delete_option('wkit_settings_panel'); |
| 215 |
delete_option('wkit_onbording_end'); |
| 216 |
delete_option('wdkit_dark_mode'); |
| 217 |
delete_option('wdkit_wintersale_notice_dismissed'); |
| 218 |
foreach (get_users() as $user) { |
| 219 |
delete_user_meta((int) $user->ID, 'wdkit_rating_banner_start_date'); |
| 220 |
} |
| 221 |
$actually_deleted = [ |
| 222 |
'wkit_deactivate_widgets', |
| 223 |
'wkit_builder', |
| 224 |
'wkit_settings_panel', |
| 225 |
'wkit_onbording_end', |
| 226 |
'wdkit_dark_mode', |
| 227 |
'wdkit_wintersale_notice_dismissed', |
| 228 |
'wdkit_rating_banner_start_date (all users)', |
| 229 |
]; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return [ |
| 234 |
'success' => true, |
| 235 |
'message' => empty($actually_deleted) |
| 236 |
? 'Cleanup executed but nothing was deleted (remove_entries is off or no data flags selected).' |
| 237 |
: 'Database cleanup executed successfully.', |
| 238 |
'config' => $new_config, |
| 239 |
'deleted' => $actually_deleted, |
| 240 |
'dry_run' => false, |
| 241 |
]; |
| 242 |
} |
| 243 |
|