'ids', 'number' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); self::maybe_purge(); restore_current_blog(); } } /** * Purges the current site when the delete-data setting is enabled. * * @return void */ private static function maybe_purge(): void { $settings = get_option( 'hostkhata_settings', array() ); if ( ! is_array( $settings ) || empty( $settings['delete_data_on_uninstall'] ) ) { return; } self::drop_tables(); ( new Capabilities() )->uninstall(); foreach ( self::option_names() as $option ) { delete_option( $option ); } self::delete_transients(); // Saved customer-list views are stored per staff user. delete_metadata( 'user', 0, 'hostkhata_customer_filters', '', true ); /** * Fires when HostKhata data is purged on uninstall. * * Later phases hook in here to remove data they own, such as uploads. * * @since 0.1.0 */ do_action( 'hostkhata_uninstall_purge' ); } /** * Drops every HostKhata table of the current site. * * @return void */ private static function drop_tables(): void { global $wpdb; foreach ( Tables::all() as $table ) { $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table names come from the Schema whitelist. } } /** * Removes the short-lived values the plugin keeps (flash messages, rate * limits, order de-duplication keys). Skipped when an object cache holds * them, because they then expire on their own. * * @return void */ private static function delete_transients(): void { global $wpdb; foreach ( array( '_transient_hostkhata_', '_transient_timeout_hostkhata_' ) as $prefix ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( $prefix ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching } } /** * Options owned by the plugin. * * @return string[] */ private static function option_names(): array { return array( Installer::VERSION_OPTION, 'hostkhata_installed_at', Migrator::VERSION_OPTION, Migrator::ERROR_OPTION, Migrator::LOCK_OPTION, 'hostkhata_settings', 'hostkhata_email_templates', 'hostkhata_reminders_seeded', 'hostkhata_seq_order', 'hostkhata_seq_invoice', 'hostkhata_cron_status', 'hostkhata_auto_suspended', ); } }