robotstxt-og/uninstall.php
2026-08-24 12:20:42 +00:00

138 lines
4.3 KiB
PHP

<?php
/**
* Uninstall handler for OpenGraph (by ROBOTSTXT)
*
* Fired when the plugin is uninstalled.
*
* @package ROBOTSTXT_OG
* @version 1.2.2
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit; // Exit if accessed directly.
}
/**
* Delete plugin data if user opted in to data deletion.
*/
function robotstxt_og_uninstall_cleanup(): void {
// Check if user opted in to delete data on uninstall.
$delete_data = get_option( 'robotstxt_og_delete_data_on_uninstall', false );
if ( ! $delete_data ) {
// Preserve data by default.
return;
}
// All plugin options (general, social, platform integration, mobile/web-app).
$options = array(
'robotstxt_og_fallback_image',
'robotstxt_og_homepage_image',
'robotstxt_og_enable_facebook',
'robotstxt_og_enable_twitter',
'robotstxt_og_twitter_card_type',
'robotstxt_og_twitter_site',
'robotstxt_og_fb_app_id',
'robotstxt_og_fb_admins',
'robotstxt_og_fb_pages',
'robotstxt_og_pinterest_verify',
'robotstxt_og_pinterest_nopin',
'robotstxt_og_telegram_channel',
'robotstxt_og_slack_app_id',
'robotstxt_og_theme_color',
'robotstxt_og_app_name',
'robotstxt_og_web_app_capable',
'robotstxt_og_app_status_bar_style',
'robotstxt_og_format_detection',
'robotstxt_og_delete_data_on_uninstall',
);
foreach ( $options as $option_name ) {
delete_option( $option_name );
}
// Delete all cached postmeta (fallback URLs and per-post OG overrides).
global $wpdb;
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Bulk delete during uninstall — no WP API exists to delete all postmeta rows by key at once.
foreach ( array( '_og_image_fallback_url', '_og_title', '_og_description', '_og_image_alt', '_og_type', '_twitter_creator' ) as $post_meta_key ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
$post_meta_key
)
);
}
// Delete all term-level OG meta (per-term overrides + cached taxonomy fallback).
foreach ( array( '_og_title', '_og_image', '_og_description', '_og_image_alt', '_og_image_fallback_url' ) as $term_meta_key ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->termmeta} WHERE meta_key = %s",
$term_meta_key
)
);
}
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Clear related transients.
delete_transient( 'robotstxt_og_diagnostics_cache' );
// Clear the legacy self-updater cache (updater removed in 1.2.1). Site
// transients are network-wide on Multisite, so one call covers all sites.
delete_site_transient( 'robotstxt_updater_' . md5( 'robotstxt-og/robotstxt-og.php' ) );
// Delete the per-user Manager notice dismissal flag. User meta is
// network-global on Multisite, so one call covers all users and sites.
delete_metadata( 'user', 0, '_robotstxt_og_manager_notice_dismissed', '', true );
// On Multisite, clean up site-by-site.
if ( is_multisite() ) {
$sites = get_sites(
array(
'number' => 0,
)
);
foreach ( $sites as $site ) {
switch_to_blog( (int) $site->blog_id );
// Delete site-specific options.
foreach ( $options as $option_name ) {
delete_option( $option_name );
}
// Delete site-specific postmeta.
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Bulk delete during uninstall — no WP API exists to delete all postmeta rows by key at once.
foreach ( array( '_og_image_fallback_url', '_og_title', '_og_description', '_og_image_alt', '_og_type', '_twitter_creator' ) as $post_meta_key ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
$post_meta_key
)
);
}
// Delete all term-level OG meta (per-term overrides + cached taxonomy fallback).
foreach ( array( '_og_title', '_og_image', '_og_description', '_og_image_alt', '_og_image_fallback_url' ) as $term_meta_key ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->termmeta} WHERE meta_key = %s",
$term_meta_key
)
);
}
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Clear site-specific transients.
delete_transient( 'robotstxt_og_diagnostics_cache' );
restore_current_blog();
}
}
}
// Execute cleanup.
robotstxt_og_uninstall_cleanup();