* hook that runs this (Freemius' _uninstall_plugin_hook() reads it the * same way). EUCHECK_FILE names the first copy loaded in the request: * when the free plugin and EU Check Pro are deleted together, the * second uninstall still runs on the first copy's code. * * @return string Relative to the plugins folder. */ public static function deleted_file() { $hooks = isset( $GLOBALS['wp_current_filter'] ) ? array_reverse( (array) $GLOBALS['wp_current_filter'] ) : array(); foreach ( $hooks as $hook ) { if ( is_string( $hook ) && 0 === strpos( $hook, 'uninstall_' ) && strlen( $hook ) > strlen( 'uninstall_' ) ) { return substr( $hook, strlen( 'uninstall_' ) ); } } return plugin_basename( EUCHECK_FILE ); } /** * Cleans every site of the network, or the one site. * * @param string $file The deleted copy's main file, relative to the plugins folder. */ public static function cleanup( $file ) { if ( self::other_copy_installed( $file ) ) { // The other copy still runs on the log, the settings and Pro's data. return; } if ( is_multisite() ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0, ) ) as $site_id ) { switch_to_blog( $site_id ); self::site(); restore_current_blog(); } return; } self::site(); } /** * Whether another copy of EU Check stays installed. The free plugin and * EU Check Pro sit in two folders (eu-check and eu-check-premium) and * share the price log and the settings. A copy counts only while its * main file is there: get_plugins() keeps listing a copy deleted earlier * in the same request (delete_plugins() deleting both). * * @param string $file The deleted copy's main file. * @return bool */ public static function other_copy_installed( $file ) { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } foreach ( get_plugins() as $other => $data ) { if ( $file !== $other && isset( $data['TextDomain'] ) && 'eu-check' === $data['TextDomain'] && file_exists( WP_PLUGIN_DIR . '/' . $other ) ) { return true; } } return false; } /** * Cleans one site. */ private static function site() { global $wpdb; // A cache, not data: it always goes. delete_transient( 'eucheck_site_scan' ); // Read before any option goes: Pro's own setting, kept in its option // because Pro's code may be gone (an expired licence, the free zip). $withdrawal = get_option( 'eucheck_pro_withdrawal', array() ); if ( is_array( $withdrawal ) && ! empty( $withdrawal['remove_data'] ) ) { self::withdrawals(); } $settings = get_option( 'eucheck_settings', array() ); if ( empty( $settings['remove_data'] ) ) { return; } // The price log, and EU Check Pro's tables when it ran on this site. // Never the withdrawal statements or anything else of the withdrawal // function: they have their own setting. foreach ( array( 'eucheck_price_log', 'eucheck_evidence', 'eucheck_audit', 'eucheck_rates', 'eucheck_currency_log' ) as $table ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Removing the plugin's own tables on uninstall. $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . $table ) ); } foreach ( array_merge( array( 'eucheck_settings', 'eucheck_db_version', 'eucheck_log_started', 'eucheck_snapshot_pending' ), self::PRO_OPTIONS ) as $option ) { delete_option( $option ); } // Pro's rate limits and short locks (transients, not data). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Pro's rate limits, removed once on uninstall. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_eucheck_pro_' ) . '%', $wpdb->esc_like( '_transient_timeout_eucheck_pro_' ) . '%' ) ); // EU Check Pro's product safety details on products and brands. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Removing the plugin's own meta on uninstall. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", $wpdb->esc_like( '_eucheck_gpsr_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Removing the plugin's own meta on uninstall. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE %s", $wpdb->esc_like( '_eucheck_gpsr_' ) . '%' ) ); if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( '', array(), 'eu-check-pro' ); } wp_cache_flush_group( 'post_meta' ); wp_cache_flush_group( 'term_meta' ); wp_cache_flush_group( 'eucheck' ); wp_cache_flush_group( 'eucheck_pro' ); } /** * Removes EU Check Pro's withdrawal statements, the delivery days on * orders, the withdrawal exceptions on products, the withdrawal settings * and the two withdrawal emails' settings, when the shop asked for it on * Pro's withdrawal page. */ private static function withdrawals() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Removing the plugin's own table on uninstall. $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'eucheck_withdrawals' ) ); delete_metadata( 'post', 0, '_eucheck_delivered_on', '', true ); delete_metadata( 'post', 0, '_eucheck_withdrawal_exclusion', '', true ); $orders_meta = $wpdb->prefix . 'wc_orders_meta'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Checking for WooCommerce's order table once on uninstall. if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $orders_meta ) ) ) === $orders_meta ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Pro's order field, removed once on uninstall. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key = %s', $orders_meta, '_eucheck_delivered_on' ) ); } foreach ( array( 'eucheck_pro_withdrawal', 'woocommerce_eucheck_withdrawal_received_settings', 'woocommerce_eucheck_withdrawal_new_settings' ) as $option ) { delete_option( $option ); } } }