robotstxt-documentation-mar.../uninstall.php
2026-01-30 18:51:40 +00:00

113 lines
2.7 KiB
PHP

<?php
/**
* Uninstall Handler
*
* Fired when the plugin is uninstalled. Removes plugin data based on settings.
*
* @package RobotsTxt\DocumentationMarkdown
* @author ROBOTSTXT
* @license GPL-3.0-or-later
* @link https://github.com/robotstxt/documentation-markdown
* @since 1.0.0
*/
// Exit if accessed directly or not called from WordPress.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Uninstall the plugin
*
* Removes plugin data if user has enabled the cleanup option.
* IMPORTANT: Does NOT delete synced content (posts/pages), only mapping
* configurations and plugin settings.
*
* @since 1.0.0
*
* @return void
*/
function robotstxt_docmd_uninstall(): void {
// Get plugin settings.
$settings = get_option( 'robotstxt_docmd_settings', array() );
// Check if user wants to delete data on uninstall.
if ( empty( $settings['delete_on_uninstall'] ) ) {
// User chose to keep data, do nothing.
return;
}
global $wpdb;
// Delete all mapping posts (CPT).
$mapping_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_type = %s",
'robotstxt_map'
)
);
foreach ( $mapping_ids as $mapping_id ) {
// Force delete (bypass trash).
wp_delete_post( $mapping_id, true );
}
// Delete all plugin-specific post meta.
$wpdb->query(
"DELETE FROM {$wpdb->postmeta}
WHERE meta_key LIKE '_robotstxt_docmd_%'"
);
// Delete plugin options.
delete_option( 'robotstxt_docmd_settings' );
// Delete transients (cached data).
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( '_transient_robotstxt_docmd_' ) . '%'
)
);
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( '_transient_timeout_robotstxt_docmd_' ) . '%'
)
);
// Clear all scheduled cron events for each mapping.
foreach ( $mapping_ids as $mapping_id ) {
$hook = 'robotstxt_docmd_sync_event';
$args = array( $mapping_id );
$timestamp = wp_next_scheduled( $hook, $args );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, $hook, $args );
}
}
// Clear notification transients.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( '_transient_robotstxt_docmd_notification_' ) . '%'
)
);
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( '_transient_timeout_robotstxt_docmd_notification_' ) . '%'
)
);
// Note: We deliberately DO NOT delete the synced posts/pages themselves.
// Users may want to keep the documentation even after uninstalling the plugin.
}
// Run uninstall.
robotstxt_docmd_uninstall();