query( 'DROP TABLE IF EXISTS ' . Kothok_Queue::table() ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- table name is $wpdb->prefix plus a constant. // Idempotency locks held while a chat order is created: "kothok_order_lock_". // The orders and their _kothok_* meta stay: they are the merchant's orders. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( Kothok_Selling::LOCK_PREFIX ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery // Status-refresh claims: "kothok_claim_". $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( Kothok_Orders::CLAIM_PREFIX ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery // Per-user connect state: "_transient_kothok_connect_". $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_kothok\\_%' OR option_name LIKE '\\_transient\\_timeout\\_kothok\\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( '', array(), Kothok_Jobs::GROUP ); } kothok_uninstall_action_history(); kothok_uninstall_action_scheduler(); } /** * Versions before 1.2.2 ran their jobs on Action Scheduler. It keeps finished * and canceled actions, with their logs, for a month. Nothing of the plugin * may stay, so the history of the * "kothok" group goes too. Action Scheduler has no API to delete by group, * so the code uses SQL on the Action Scheduler tables, only where they * exist. A site that used the WP-Post store, or never ran a job, has none. */ function kothok_uninstall_action_history() { global $wpdb; $actions = $wpdb->prefix . 'actionscheduler_actions'; $logs = $wpdb->prefix . 'actionscheduler_logs'; $groups = $wpdb->prefix . 'actionscheduler_groups'; foreach ( array( $actions, $groups ) as $kothok_table ) { if ( 0 !== strcasecmp( $kothok_table, (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $kothok_table ) ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery return; } } $group_id = $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$groups} WHERE slug = %s", Kothok_Jobs::GROUP ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- table names are $wpdb->prefix plus a constant. if ( null === $group_id ) { return; } if ( 0 === strcasecmp( $logs, (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $logs ) ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->query( $wpdb->prepare( "DELETE l FROM {$logs} l INNER JOIN {$actions} a ON a.action_id = l.action_id WHERE a.group_id = %d", $group_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter } $wpdb->query( $wpdb->prepare( "DELETE FROM {$actions} WHERE group_id = %d", $group_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->query( $wpdb->prepare( "DELETE FROM {$groups} WHERE group_id = %d", $group_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Versions before 1.2.2 included a copy of Action Scheduler. On a site * without WooCommerce, that copy made its own tables, options and WP-Cron * event. Remove them only when no Action Scheduler is loaded now and the * tables hold no action of another plugin: only the actions that Action * Scheduler adds for itself ("action_scheduler/..."). An Action Scheduler * that loads later makes its tables again. */ function kothok_uninstall_action_scheduler() { global $wpdb; if ( class_exists( 'ActionScheduler' ) || function_exists( 'as_get_scheduled_actions' ) ) { return; } wp_unschedule_hook( 'action_scheduler_run_queue' ); $actions = $wpdb->prefix . 'actionscheduler_actions'; if ( 0 !== strcasecmp( $actions, (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $actions ) ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery return; } $others = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$actions} WHERE hook NOT LIKE %s", $wpdb->esc_like( 'action_scheduler/' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- table name is $wpdb->prefix plus a constant. if ( '0' !== (string) $others ) { return; } foreach ( array( 'actionscheduler_logs', 'actionscheduler_claims', 'actionscheduler_groups', 'actionscheduler_actions' ) as $kothok_table ) { $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . $kothok_table ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- table name is $wpdb->prefix plus a constant. } foreach ( array( 'schema-ActionScheduler_StoreSchema', 'schema-ActionScheduler_LoggerSchema', 'action_scheduler_hybrid_store_demarkation', 'action_scheduler_migration_status' ) as $kothok_option ) { delete_option( $kothok_option ); } $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( 'action_scheduler_lock_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** * Multisite: drop the queue tables that the loop above did not reach. A site * that activated the plugin only for itself leaves its table when the * network admin deletes the site. * * A deleted site usually has an id below the highest site id. The table of * a deleted site with the highest id stays. A table with a higher number can * belong to another install in the same database, so it stays too. If the * query for the highest id fails, only the table of site 1 is dropped. The * loop above already dropped the tables of the other sites. */ function kothok_uninstall_network_tables() { global $wpdb; $kothok_tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->base_prefix ) . '%' . $wpdb->esc_like( Kothok_Queue::TABLE ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $kothok_max = $wpdb->get_var( "SELECT MAX(blog_id) FROM {$wpdb->blogs}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( null === $kothok_max || '' !== $wpdb->last_error ) { $kothok_max = 0; } foreach ( Kothok_Queue::network_tables( (array) $kothok_tables, $wpdb->base_prefix, (int) $kothok_max ) as $kothok_table ) { $wpdb->query( 'DROP TABLE IF EXISTS `' . str_replace( '`', '', $kothok_table ) . '`' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- a table name from SHOW TABLES that matches the queue table pattern. } } if ( is_multisite() ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $kothok_site_id ) { switch_to_blog( $kothok_site_id ); kothok_uninstall_site(); restore_current_blog(); } kothok_uninstall_network_tables(); } else { kothok_uninstall_site(); }