esc_like( 'foldivo_' ) . '%'; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT term_taxonomy_id, term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy LIKE %s", $like ) ); $tt_ids = array(); $term_ids = array(); foreach ( (array) $rows as $row ) { $tt_ids[] = (int) $row->term_taxonomy_id; $term_ids[] = (int) $row->term_id; } foreach ( array_chunk( $tt_ids, 500 ) as $chunk ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (" . implode( ',', array_fill( 0, count( $chunk ), '%d' ) ) . ')', $chunk ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id IN (" . implode( ',', array_fill( 0, count( $chunk ), '%d' ) ) . ')', $chunk ) ); } foreach ( array_chunk( array_values( array_unique( $term_ids ) ), 500 ) as $chunk ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->termmeta} WHERE term_id IN (" . implode( ',', array_fill( 0, count( $chunk ), '%d' ) ) . ')', $chunk ) ); // Only terms no other taxonomy uses (shared terms predate WP 4.4). $wpdb->query( $wpdb->prepare( "DELETE t FROM {$wpdb->terms} AS t LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_id = t.term_id WHERE t.term_id IN (" . implode( ',', array_fill( 0, count( $chunk ), '%d' ) ) . ') AND tt.term_id IS NULL', $chunk ) ); } // Options foldivo_* (settings, version, import maps, {taxonomy}_children caches), except the Pro add-on's. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s AND option_name NOT LIKE %s", $wpdb->esc_like( 'foldivo_' ) . '%', $wpdb->esc_like( 'foldivo_pro_' ) . '%' ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_foldivo_' ) . '%', $wpdb->esc_like( '_transient_timeout_foldivo_' ) . '%' ) ); // User meta: user preferences of this site. if ( is_multisite() ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = %s", 'foldivo_preferences_' . get_current_blog_id() ) ); } else { foldivo_uninstall_user_meta(); } // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Removes the temporary state of the current site, whatever the data choice: the cleanup event of the * batched JSON import and its stored jobs (`foldivo_json_job_*`, `foldivo_json_data_*`, `foldivo_json_lock_*`). * * These rows only hold half-finished imports, never the folder organization, so they are not kept. * * @return int Number of option rows deleted. */ function foldivo_uninstall_site_temporary() { global $wpdb; wp_clear_scheduled_hook( 'foldivo_json_import_cleanup' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-off purge on uninstall. return (int) $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( 'foldivo_json_job_' ) . '%', $wpdb->esc_like( 'foldivo_json_data_' ) . '%', $wpdb->esc_like( 'foldivo_json_lock_' ) . '%' ) ); } /** * Deletes the network-wide `foldivo_*` user meta (except the Pro add-on's and other sites' preferences). * * @return void */ function foldivo_uninstall_user_meta() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-off purge on uninstall. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s AND meta_key NOT LIKE %s AND meta_key NOT LIKE %s", $wpdb->esc_like( 'foldivo_' ) . '%', $wpdb->esc_like( 'foldivo_pro_' ) . '%', $wpdb->esc_like( 'foldivo_preferences_' ) . '%' ) ); } if ( is_multisite() ) { $foldivo_main_opted_in = false; $foldivo_offset = 0; do { $foldivo_site_ids = get_sites( array( 'fields' => 'ids', 'number' => 100, 'offset' => $foldivo_offset, ) ); foreach ( $foldivo_site_ids as $foldivo_site_id ) { switch_to_blog( (int) $foldivo_site_id ); foldivo_uninstall_site_temporary(); if ( foldivo_uninstall_site_opted_in() ) { if ( is_main_site() ) { $foldivo_main_opted_in = true; } foldivo_uninstall_site(); } restore_current_blog(); } $foldivo_offset += 100; $foldivo_batch_size = count( $foldivo_site_ids ); } while ( 100 === $foldivo_batch_size ); // Network-wide user meta (notice dismissals…) follows the main site's choice. if ( $foldivo_main_opted_in ) { foldivo_uninstall_user_meta(); } wp_cache_flush(); } else { $foldivo_flush = foldivo_uninstall_site_temporary() > 0; if ( foldivo_uninstall_site_opted_in() ) { foldivo_uninstall_site(); $foldivo_flush = true; } if ( $foldivo_flush ) { wp_cache_flush(); } }