esc_like( '_misticgate_ticket_' ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall must remove every non-autoloaded ticket option created by this plugin. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $like ) ); } /** * Deletes the settings and tickets of a network. * * @param int $network_id Network ID. * * @return void */ function misticgate_uninstall_network_data( $network_id ) { if ( ! is_multisite() ) { return; } $network_id = (int) $network_id; delete_network_option( $network_id, 'misticgate_network_settings' ); global $wpdb; $like = $wpdb->esc_like( '_misticgate_network_ticket_' ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall must remove every ticket in the selected network created by this plugin. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d AND meta_key LIKE %s", $network_id, $like ) ); } /** * Deletes MisticGate data from every site and network. * * @return void */ function misticgate_uninstall_all_data() { global $wpdb; if ( ! is_multisite() ) { misticgate_uninstall_site_data(); return; } $network_offset = 0; do { $network_ids = get_networks( array( 'fields' => 'ids', 'number' => 100, 'offset' => $network_offset, ) ); foreach ( $network_ids as $network_id ) { misticgate_uninstall_network_data( $network_id ); } $network_count = count( $network_ids ); $network_offset += $network_count; } while ( 100 === $network_count ); $offset = 0; do { $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 100, 'offset' => $offset, ) ); foreach ( $site_ids as $site_id ) { $table = $wpdb->get_blog_prefix( (int) $site_id ) . 'options'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Skip absent options tables without assuming that the site's posts table still exists during partial deletion or initialization. $existing_table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); if ( $table !== $existing_table ) { continue; } switch_to_blog( (int) $site_id ); try { misticgate_uninstall_site_data(); } finally { restore_current_blog(); } } $site_count = count( $site_ids ); $offset += $site_count; } while ( 100 === $site_count ); } misticgate_uninstall_all_data();