create_database(); $self->migrate_legacy_notes(); $self->save_option(); } public function create_database() { Database::create_initial_custom_table(); } /** * One-time, idempotent migration of the legacy per-course note blobs * (user meta `academy_{course_id}lesson_note_{user_id}`) into the new * `academy_notes` table as course-level text notes. Non-destructive: the * original user meta is left in place. */ public function migrate_legacy_notes() { if ( get_option( 'academy_notes_migrated' ) ) { return; } global $wpdb; $table = Database::get_table_name(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery $rows = $wpdb->get_results( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key REGEXP '^academy_[0-9]+lesson_note_[0-9]+$' AND meta_value <> ''" ); if ( ! empty( $rows ) ) { $now = current_time( 'mysql' ); foreach ( $rows as $row ) { if ( ! preg_match( '/^academy_([0-9]+)lesson_note_([0-9]+)$/', $row->meta_key, $m ) ) { continue; } $content = (string) $row->meta_value; if ( '' === trim( wp_strip_all_tags( $content ) ) ) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->insert( $table, array( 'user_id' => (int) $row->user_id, 'course_id' => (int) $m[1], 'topic_id' => 0, 'topic_type' => '', 'note_type' => 'text', 'video_time' => null, 'content' => $content, 'created_at' => $now, 'updated_at' => $now, ), array( '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s' ) ); } } update_option( 'academy_notes_migrated', 1 ); } public function save_option() { if ( get_option( ACADEMY_NOTES_VERSION_NAME ) !== ACADEMY_NOTES_VERSION ) { update_option( ACADEMY_NOTES_VERSION_NAME, ACADEMY_NOTES_VERSION ); } // The "My Notes" dashboard page adds a rewrite rule — flush so its URL // resolves right after activation. \Academy\Helper::flush_rewrite_rules(); } }