*/ public const OPTIONS = [ 'wpi_db_version' => 'crocoai_dbridge_db_version', 'wpi_settings' => 'crocoai_dbridge_settings', 'wpi_api_key' => 'crocoai_dbridge_api_key', 'wpi_legacy_sources_retired' => 'crocoai_dbridge_legacy_sources_retired', 'wpi_remove_data_on_uninstall' => 'crocoai_dbridge_remove_data_on_uninstall', 'wpi_worker_lock' => 'crocoai_dbridge_worker_lock', 'wpi_attribute_lookup_rebuild' => 'crocoai_dbridge_attribute_lookup_rebuild', ]; /** @var list */ public const TABLES = [ 'media_assets', 'attribute_ops', 'remote_releases', 'media_queue', 'jobs', 'aliases', 'changes', 'links', 'records', 'runs', 'profiles', 'sources', ]; /** @var array */ public const POST_META = [ '_wpi_translation_identity' => '_crocoai_dbridge_translation_identity', '_wpi_translation_family' => '_crocoai_dbridge_translation_family', '_wpi_translation_language' => '_crocoai_dbridge_translation_language', '_wpi_attribute_labels' => '_crocoai_dbridge_attribute_labels', '_wpi_asset_hash' => '_crocoai_dbridge_asset_hash', '_wpi_image_profile_hash' => '_crocoai_dbridge_image_profile_hash', '_wpi_source_url' => '_crocoai_dbridge_source_url', ]; public const CAPABILITY_LEGACY = 'manage_product_imports'; public const CAPABILITY = 'crocoai_dbridge_manage'; public static function migrate(): void { if (get_option(self::FLAG) === '1') { return; } self::copyOptions(); delete_option('wpi_migration_lock'); self::renameTables(); self::renamePostMeta(); self::migrateCapability(); update_option(self::FLAG, '1', false); } private static function copyOptions(): void { global $wpdb; foreach (self::OPTIONS as $legacy => $current) { $legacyRow = $wpdb->get_row($wpdb->prepare( 'SELECT option_value, autoload FROM %i WHERE option_name = %s LIMIT 1', $wpdb->options, $legacy )); if (!$legacyRow) { continue; } $currentExists = $wpdb->get_var($wpdb->prepare( 'SELECT option_id FROM %i WHERE option_name = %s LIMIT 1', $wpdb->options, $current )); if (!$currentExists) { add_option($current, maybe_unserialize($legacyRow->option_value), '', $legacyRow->autoload); } delete_option($legacy); } } private static function renameTables(): void { global $wpdb; foreach (self::TABLES as $name) { $legacy = $wpdb->prefix . 'wpi_' . $name; $current = $wpdb->prefix . 'crocoai_dbridge_' . $name; $legacyExists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $legacy)) === $legacy; $currentExists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $current)) === $current; if ($legacyExists && !$currentExists) { $wpdb->query($wpdb->prepare('RENAME TABLE %i TO %i', $legacy, $current)); } } } private static function renamePostMeta(): void { global $wpdb; foreach (self::POST_META as $legacy => $current) { $wpdb->query($wpdb->prepare( 'UPDATE %i SET meta_key = %s WHERE meta_key = %s', $wpdb->postmeta, $current, $legacy )); } } private static function migrateCapability(): void { $roles = wp_roles(); foreach (array_keys($roles->roles) as $roleName) { $role = get_role((string) $roleName); if ($role && $role->has_cap(self::CAPABILITY_LEGACY)) { $role->add_cap(self::CAPABILITY); $role->remove_cap(self::CAPABILITY_LEGACY); } } } }