run_migrations( $installed_ver ); update_option( 'metaforge_db_version', self::DB_VERSION ); } } private function run_migrations( $current_version ) { // Migration to 0.3.0 - AI Memory Table if ( version_compare( $current_version, '0.3.0', '<' ) ) { $this->migrate_0_3_0_ai_memory(); } if ( version_compare( $current_version, '0.4.0', '<' ) ) { $this->migrate_0_4_0_cpt_architecture(); } // Clear field group caches and ensure capabilities on 1.0.0 upgrade if ( version_compare( $current_version, '1.0.0', '<' ) ) { if ( function_exists( 'metaforge_sync_capabilities' ) ) { metaforge_sync_capabilities(); } MetaForge_Loader::clear_cache(); } } private function migrate_0_4_0_cpt_architecture() { $legacy_groups = get_option( 'metaforge_field_groups' ); if ( empty( $legacy_groups ) || ! is_array( $legacy_groups ) ) { return; } // Ensure post type is registered for wp_insert_post if ( ! post_type_exists( 'metaforge_group' ) ) { metaforge_register_core_cpts(); } foreach ( $legacy_groups as $index => $group ) { $title = ! empty( $group['group_name'] ) ? sanitize_text_field( $group['group_name'] ) : 'Group ' . ( $index + 1 ); $post_id = wp_insert_post( array( 'post_title' => $title, 'post_type' => 'metaforge_group', 'post_status' => 'publish', 'menu_order' => $index, ) ); if ( $post_id && ! is_wp_error( $post_id ) ) { // Save the exact array structure directly as meta update_post_meta( $post_id, '_metaforge_group_config', $group ); } } // Delete the old option to free up autoloaded memory delete_option( 'metaforge_field_groups' ); // Clear transient MetaForge_Loader::clear_cache(); } private function migrate_0_3_0_ai_memory() { global $wpdb; $table_name = $wpdb->prefix . 'metaforge_ai_memory'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, context_group varchar(100) NOT NULL DEFAULT '', prompt text NOT NULL, response_json longtext NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY user_id (user_id), KEY context_group (context_group) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } }