Files

110 lines
3.7 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Core;
/**
* Removes what the plugin created. Dependency-free on purpose: uninstall.php loads this single file with
* require_once, without the plugin's autoloader or constants.
*
* Always removed on uninstall: the cron event and all transients (they are only caches/notices).
* Removed ONLY when the owner ticked "delete data on uninstall": every sg_ table and option.
*
* Keep the lists below in sync with Installer (tables) and every add/update_option call; UninstallerTest fails
* when something is missing.
*/
final class Uninstaller {
public const CRON_HOOK = 'sg_purge_abandoned';
/** @return string[] Table names without the WordPress prefix. */
public static function tables(): array {
return array(
'sg_goals',
'sg_donations',
'sg_rules',
'sg_rule_events',
'sg_state',
'sg_actions',
'sg_action_events',
'sg_alert_defs',
'sg_goal_tiers',
);
}
/** @return string[] Option names (per site). */
public static function options(): array {
return array(
'sg_settings',
'sg_db_version',
'sg_overlay_epoch',
'sg_overlay_rules_version',
'sg_currency',
'sg_paypal_verified',
'sg_donate_page_id',
'sg_wizard_goal_id',
);
}
/** @return string[] Fixed-name site transients. */
public static function site_transients(): array {
return array( 'sg_update_release' );
}
/** @return string[] Prefixes of transients whose names carry a suffix (rate limiter hashes, per-user notices, cached PayPal access token, webhook de-dupe markers). */
public static function transient_prefixes(): array {
return array( 'sg_rl_', 'sg_notice_', 'sg_pp_token_', 'sg_wh_' );
}
/** Whether this site's saved settings ask for all data to be deleted. */
public static function wants_data_deleted( mixed $settings ): bool {
return is_array( $settings ) && ! empty( $settings['delete_data_on_uninstall'] );
}
/** Drops every plugin table for the current site prefix. @param object $wpdb wpdb-like (prefix, query). */
public static function drop_tables( object $wpdb ): void {
foreach ( self::tables() as $t ) {
$wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->prefix . $t . '`' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
}
/** Deletes the dynamically named transients (and their timeouts) straight from the options table. @param object $wpdb wpdb-like (options, esc_like, prepare, query). */
public static function delete_dynamic_transients( object $wpdb ): void {
foreach ( self::transient_prefixes() as $prefix ) {
foreach ( array( '_transient_', '_transient_timeout_' ) as $kind ) {
$like = $wpdb->esc_like( $kind . $prefix ) . '%';
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $like ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
}
}
/** Uninstall for the current site (call inside switch_to_blog on multisite). */
public static function run_site( object $wpdb ): void {
$delete = self::wants_data_deleted( get_option( 'sg_settings', array() ) );
wp_clear_scheduled_hook( self::CRON_HOOK );
self::delete_dynamic_transients( $wpdb );
if ( $delete ) {
self::drop_tables( $wpdb );
foreach ( self::options() as $o ) {
delete_option( $o );
}
}
}
/** Entry point for uninstall.php. */
public static function run(): void {
global $wpdb;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $id ) {
switch_to_blog( (int) $id );
self::run_site( $wpdb );
restore_current_blog();
}
} else {
self::run_site( $wpdb );
}
foreach ( self::site_transients() as $t ) {
delete_site_transient( $t );
}
}
}