prefix . self::OLD_PREFIX . '_codes'; $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $old_table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $found || false !== get_option( self::OLD_PREFIX . '_db_version', false ); } /** * Run once, when old data exists and the old plugin is no longer active. */ public static function maybe_run() { if ( get_option( self::DONE_OPTION ) || ! self::has_old_data() || self::old_plugin_active() ) { return; } self::run(); } public static function run() { global $wpdb; $old = self::OLD_PREFIX; // 1) Tables: copy rows from the old tables into the new ones (old tables are left as a backup). HyperDrop_DB::create_tables(); foreach ( array( 'codes', 'redemptions' ) as $t ) { $old_table = $wpdb->prefix . $old . '_' . $t; $new_table = $wpdb->prefix . 'hyperdrop_' . $t; $has_old = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $old_table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( ! $has_old ) { continue; } if ( (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $new_table ) ) > 0 ) { continue; // New table already in use — never overwrite. } $cols = array_values( array_intersect( $wpdb->get_col( $wpdb->prepare( 'DESCRIBE %i', $old_table ), 0 ), $wpdb->get_col( $wpdb->prepare( 'DESCRIBE %i', $new_table ), 0 ) ) ); if ( $cols ) { // Copy row by row with $wpdb->insert(); only columns both tables have. $keep = array_flip( $cols ); foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i', $old_table ), ARRAY_A ) as $row ) { $wpdb->insert( $new_table, array_intersect_key( $row, $keep ) ); } } } // 2) Options: old_* → hyperdrop_* (except the db version, which install() sets). $names = $wpdb->get_col( $wpdb->prepare( 'SELECT option_name FROM %i WHERE option_name LIKE %s', $wpdb->options, $wpdb->esc_like( $old . '_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $names as $name ) { $suffix = substr( $name, strlen( $old ) + 1 ); if ( 'db_version' !== $suffix ) { update_option( 'hyperdrop_' . $suffix, get_option( $name ), false ); } delete_option( $name ); } // 3) Meta keys: _old_* → _hyperdrop_* on products, categories, users, order items and orders. $like = '\\_' . $old . '\\_%'; $cut = strlen( $old ) + 3; // Position after "_old_" (1-based for SUBSTRING). $tables = array( $wpdb->postmeta, $wpdb->termmeta, $wpdb->usermeta ); foreach ( array( 'woocommerce_order_itemmeta', 'wc_orders_meta' ) as $wc_table ) { $full = $wpdb->prefix . $wc_table; if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $full ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery $tables[] = $full; } } foreach ( $tables as $table ) { $wpdb->query( $wpdb->prepare( "UPDATE %i SET meta_key = CONCAT('_hyperdrop_', SUBSTRING(meta_key, %d)) WHERE meta_key LIKE %s", $table, $cut, $like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } // 4) Scheduled "It's live" emails: move to the new hook. $crons = _get_cron_array(); if ( is_array( $crons ) ) { foreach ( $crons as $events ) { if ( empty( $events[ $old . '_drop_started' ] ) ) { continue; } foreach ( $events[ $old . '_drop_started' ] as $event ) { if ( ! empty( $event['args'][0] ) ) { wp_clear_scheduled_hook( $old . '_drop_started', $event['args'] ); HyperDrop_Schedule::reschedule( $event['args'][0] ); } } } } wp_cache_flush(); update_option( self::DONE_OPTION, time(), false ); do_action( 'hyperdrop_migrated' ); } /** * Tell the admin to switch off the old plugin (both running at once would double-lock the store). */ public static function admin_notice() { if ( ! current_user_can( 'activate_plugins' ) || ! self::old_plugin_active() ) { return; } echo '
' . esc_html__( 'HyperDrop:', 'hyperdrop' ) . ' ' . esc_html__( 'Please deactivate and delete the old "Zyrix Exclusive Drops" plugin. HyperDrop is its new version — your codes, settings and order history move over automatically as soon as the old one is deactivated.', 'hyperdrop' ) . '