Index: trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 104)
+++ trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 105)
@@ -31,5 +31,5 @@
 {
 	global $wpdb;
-	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
 	if (!empty($comment_type)) return ;	
 	return wp_comment_sync_to_d3forum($comment_ID,'post');
@@ -38,5 +38,5 @@
 {
 	global $wpdb;
-	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
 	if (!empty($comment_type)) return ;	
 	return wp_comment_sync_to_d3forum($comment_ID,'edit');
@@ -45,5 +45,5 @@
 {
 	global $wpdb;
-	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
 	if (!empty($comment_type)) return ;	
 	return wp_comment_sync_to_d3forum($comment_ID,'delete');
@@ -66,18 +66,18 @@
 }
 
-function onaction_comment_apobe($commentID){
+function onaction_comment_apobe($comment_ID){
 	global $wpdb;
-	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
-	$status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $commentID");
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
+	$status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $comment_ID");
 	if(is_null($status)) return;
-	//	$status = wp_get_comment_status($commentID);
+	//	$status = wp_get_comment_status($comment_ID);
 	switch($status){
 		case 'approved':
 		case 1:
-			if (empty($comment_type)) onaction_edit_comment($commentID);
+			if (empty($comment_type)) onaction_edit_comment($comment_ID);
 			break;
 		case 'unapproved':
 		case 0:
-			if (empty($comment_type)) onaction_edit_comment($commentID);
+			if (empty($comment_type)) onaction_edit_comment($comment_ID);
 			break;
 		default:
@@ -490,5 +490,5 @@
 	$forum_id = $d3f_forum_id;
 	$d3forum_dirname =$d3f_forum_dir;
-	$topic_external_link_id = $row->comment_post_ID;
+	$topic_external_link_id = $row->comment_post_ID; //There is information on WP post_ID in topic_external_link_id of D3Forum
 	$topic_title = 'Re.' . addSlashes($row->post_title);
 	$post_time = strtotime($row->comment_date);		// or	$row2['comment_date_gmt']
@@ -513,12 +513,16 @@
 		if (empty($topic_id)) return;
 	}else{
+		// Does the first comment (= topic) on the post exist?
 		$sql  =	"SELECT * FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id";
 		$row = $xoops_db->get_row($sql) ;
+		$topic_first_post_id = $row->topic_first_post_id;
 		if (empty($row)){
 			$mode = $mode = 'newtopic';
 		} else {
 			$topic_id = $row->topic_id;
-			$reply_pid = $row->topic_first_post_id; //reply_first_comment
+			
+			// if comment on same ID exists then edits comment else reply comment
 			$row = $xoops_db->get_row("SELECT * FROM $wp_d3forum_link WHERE comment_ID = $comment_ID") ;
+			
 			if (!empty($row)){
 				$mode = $mode = 'edit';
@@ -531,5 +535,5 @@
 				}
 				if ($reply_pid == 0) {
-					$reply_pid = $row->topic_first_post_id; //reply_first_comment
+					$reply_pid = $topic_first_post_id; //reply_first_comment
 				}
 			}
@@ -624,5 +628,5 @@
 			$wp_sql .=  "VALUES ";
 			$wp_sql .=    "($comment_ID, $post_id)";
-			$xoops_db->query($sql);
+			$xoops_db->query($wp_sql);
 			
 			break;
@@ -661,5 +665,5 @@
 			$wp_sql .=  "VALUES ";
 			$wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id)";		
-			$xoops_db->query($sql);
+			$xoops_db->query($wp_sql);
 
 			break;
@@ -750,5 +754,5 @@
 		$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
 		$posts = $xoops_db->query($sql);
-		if( empty($posts)) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+		if( empty($posts)) die( 'SQL ERROR '.__LINE__ ) ;
 		foreach($posts as $post){
 			wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
@@ -759,8 +763,8 @@
 
 	// delete topic
-	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" )) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" )) die( 'SQL ERROR '.__LINE__ ) ;
 
 	// delete u2t
-	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" )) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" )) die( 'SQL ERROR '.__LINE__ ) ;
 }
 
@@ -777,16 +781,16 @@
 
 	$sql = "SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" ;
-	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT topic in sync topic" ) ;
-	list( $forum_id ) = $results ;
+	if( ! $forum_id = $xoops_db->get_var($sql) ) die( "ERROR SELECT topic in sync topic" ) ;
+
 
 	// get first_post_id
 	$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0" ;
-	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT first_post in sync topic" ) ;
-	list( $first_post_id ) = $results ;
+	if( ! $first_post_id = $xoops_db->get_var($sql) ) die( "ERROR SELECT first_post in sync topic" ) ;
 
 	// get last_post_id and total_posts
-	$sql = "SELECT MAX(post_id),COUNT(post_id) FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
-	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT last_post in sync topic" ) ;
-	list( $last_post_id , $total_posts ) = $results ;
+	$sql = "SELECT MAX(post_id) as last_post_id,COUNT(post_id) as total_posts FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
+	if( ! $row = $xoops_db->get_row($sql) ) die( "ERROR SELECT last_post in sync topic" ) ;
+	$last_post_id = $row->last_post_id;
+	$total_posts = $row->total_posts;
 
 	if( empty( $total_posts ) ) {
@@ -797,21 +801,30 @@
 
 		// update redundant columns in topics table
-		list( $first_post_time , $first_uid , $first_subject , $unique_path ) = $xoops_db->get_results( "SELECT post_time,uid,subject,unique_path FROM ".$d3forum_prefix."posts WHERE post_id=$first_post_id" ) ;
-		list( $last_post_time , $last_uid ) = $xoops_db->get_results( "SELECT post_time,uid FROM ".$d3forum_prefix."posts WHERE post_id=$last_post_id" )  ;
-
+		$row = $xoops_db->get_row( "SELECT post_time,uid,subject,unique_path FROM ".$d3forum_prefix."posts WHERE post_id=$first_post_id" ) ;
+		$first_post_time = $row->post_time;
+		$first_uid = $row->uid;
+		$first_subject = $row->subject;
+		$unique_path = $row->unique_path;
+		$row = $xoops_db->get_row( "SELECT post_time,uid FROM ".$d3forum_prefix."posts WHERE post_id=$last_post_id" ) ;
+		$last_post_time = $row->post_time;
+		$last_uid = $row->uid;
 		// sync topic_title same as first post's subject if specified
 		$topictitle4set = $sync_topic_title ? "topic_title='".addslashes($first_subject)."'," : "" ;
 
-		if( ! $$xoops_db->query( "UPDATE ".$d3forum_prefix."topics SET {$topictitle4set} topic_posts_count=$total_posts, topic_first_uid=$first_uid, topic_first_post_id=$first_post_id, topic_first_post_time=$first_post_time, topic_last_uid=$last_uid, topic_last_post_id=$last_post_id, topic_last_post_time=$last_post_time WHERE topic_id=$topic_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+		if( ! $xoops_db->query( "UPDATE ".$d3forum_prefix."topics SET {$topictitle4set} topic_posts_count=$total_posts, topic_first_uid=$first_uid, topic_first_post_id=$first_post_id, topic_first_post_time=$first_post_time, topic_last_uid=$last_uid, topic_last_post_id=$last_post_id, topic_last_post_time=$last_post_time WHERE topic_id=$topic_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
 
 		// rebuild tree informations
-		$tree_array = wp_d3forum_maketree_recursive( $db->prefix($d3forum_dirname."_posts") , intval( $first_post_id ) , 'post_id' , array() , 0 , empty( $unique_path ) ? '.1' : $unique_path ) ;
-		if( ! empty( $tree_array ) ) foreach( $tree_array as $key => $val ) {
-			$xoops_db->query( "UPDATE ".$d3forum_prefix."posts SET depth_in_tree=".$val['depth'].", order_in_tree=".($key+1).", unique_path='".addslashes($val['unique_path'])."' WHERE post_id=".$val['post_id'] ) ;
-		}
-	}
-
-	if( $sync_also_forum ) return wp_d3forum_sync_forum( $d3forum_dirname , $forum_id ) ;
-	else return true ;
+		$tree_array = wp_d3forum_maketree_recursive( $d3forum_prefix."posts" , intval( $first_post_id ) , 'post_id' , array() , 0 , empty( $unique_path ) ? '.1' : $unique_path ) ;
+		if( ! empty( $tree_array ) ) {
+			foreach( $tree_array as $key => $val ) {
+				$xoops_db->query( "UPDATE ".$d3forum_prefix."posts SET depth_in_tree=".$val['depth'].", order_in_tree=".($key+1).", unique_path='".addslashes($val['unique_path'])."' WHERE post_id=".$val['post_id'] ) ;
+			}
+		}
+	}
+
+	if( $sync_also_forum ) 
+		return wp_d3forum_sync_forum( $d3forum_dirname , $forum_id ) ;
+	else 
+		return true ;
 }
 
@@ -828,5 +841,7 @@
 	$new_post_ids = array() ;
 	$max_count_of_last_level = 0 ;
-	while( list( $new_post_id , $new_unique_path ) = $db->fetchRow( $result ) ) {
+	foreach($result as $row){
+		$new_post_id = $row->post_id;
+		$new_unique_path = $row->unique_path;
 		$new_post_ids[ intval( $new_post_id ) ] = $new_unique_path ;
 		if( ! empty( $new_unique_path ) ) {
@@ -857,5 +872,7 @@
 		return $parray ;
 	}
-	while( list( $new_cat_id , $new_cat_title ) = $results ) {
+	foreach($result as $row){
+		$new_cat_id = $row->cat_id;
+		$new_cat_title = $row->cat_title;
 		$parray = wp_d3forum_makecattree_recursive( $tablename , $new_cat_id , $order , $parray , $depth + 1 , $new_cat_title ) ;
 	}
@@ -873,12 +890,14 @@
 
 	$sql = "SELECT cat_id FROM ".$d3forum_prefix."forums WHERE forum_id=$forum_id" ;
-	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync forum" ) ;
-	list( $cat_id ) = $results ;
-
-	$sql = "SELECT MAX(topic_last_post_id),MAX(topic_last_post_time),COUNT(topic_id),SUM(topic_posts_count) FROM ".$d3forum_prefix."topics WHERE forum_id=$forum_id" ;
-	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT topics in sync forum" ) ;
-	list( $last_post_id , $last_post_time , $topics_count , $posts_count ) = $results ;
-
-	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."forums SET forum_topics_count=".intval($topics_count).",forum_posts_count=".intval($posts_count).", forum_last_post_id=".intval($last_post_id).", forum_last_post_time=".intval($last_post_time)." WHERE forum_id=$forum_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+	if( ! $cat_id = $xoops_db->get_var( $sql ) ) die( "ERROR SELECT forum in sync forum" ) ;
+
+	$sql = "SELECT MAX(topic_last_post_id) as last_post_id ,MAX(topic_last_post_time) as last_post_time ,COUNT(topic_id) as topics_count,SUM(topic_posts_count) as posts_count FROM ".$d3forum_prefix."topics WHERE forum_id=$forum_id" ;
+	if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT topics in sync forum" ) ;
+	$last_post_id = $row->last_post_id;
+	$last_post_time = $row->last_post_time;
+	$topics_count = $row->topics_count;
+	$posts_count = $row->posts_count;
+
+	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."forums SET forum_topics_count=".intval($topics_count).",forum_posts_count=".intval($posts_count).", forum_last_post_id=".intval($last_post_id).", forum_last_post_time=".intval($last_post_time)." WHERE forum_id=$forum_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
 
 	if( $sync_also_category ) return wp_d3forum_sync_category( $d3forum_dirname , $cat_id ) ;
@@ -902,18 +921,24 @@
 
 	// topics/posts information belonging this category directly
-	$sql = "SELECT MAX(forum_last_post_id),MAX(forum_last_post_time),SUM(forum_topics_count),SUM(forum_posts_count) FROM ".$d3forum_prefix."forums WHERE cat_id=$cat_id" ;
-	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
-	list( $last_post_id , $last_post_time , $topics_count , $posts_count ) = $results ;
+	$sql = "SELECT MAX(forum_last_post_id) as last_post_id,MAX(forum_last_post_time) as last_post_time,SUM(forum_topics_count) as topics_count,SUM(forum_posts_count) as posts_count FROM ".$d3forum_prefix."forums WHERE cat_id=$cat_id" ;
+	if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
+	$last_post_id = $row->last_post_id ;
+	$last_post_time = $row->last_post_time ;
+	$topics_count = $row->topics_count ;
+	$posts_count = $row->posts_count ;
 
 	// topics/posts information belonging this category and/or subcategories
-	$sql = "SELECT MAX(forum_last_post_id),MAX(forum_last_post_time),SUM(forum_topics_count),SUM(forum_posts_count) FROM ".$d3forum_prefix."forums WHERE cat_id IN (".implode(",",$children).")" ;
-	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
-	list( $last_post_id_in_tree , $last_post_time_in_tree , $topics_count_in_tree , $posts_count_in_tree ) = $results ;
+	$sql = "SELECT MAX(forum_last_post_id) as last_post_id_in_tree,MAX(forum_last_post_time) as last_post_time_in_tree,SUM(forum_topics_count) as topics_count_in_tree,SUM(forum_posts_count) as posts_count_in_tree FROM ".$d3forum_prefix."forums WHERE cat_id IN (".implode(",",$children).")" ;
+	if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
+	$last_post_id_in_tree = $row->last_post_id_in_tree ;
+	$last_post_time_in_tree = $row->last_post_time_in_tree ;
+	$topics_count_in_tree = $row->topics_count_in_tree ;
+	$posts_count_in_tree = $row->posts_count_in_tree ;
 
 	// update query
-	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."categories SET cat_topics_count=".intval($topics_count).",cat_posts_count=".intval($posts_count).", cat_last_post_id=".intval($last_post_id).", cat_last_post_time=".intval($last_post_time).",cat_topics_count_in_tree=".intval($topics_count_in_tree).",cat_posts_count_in_tree=".intval($posts_count_in_tree).", cat_last_post_id_in_tree=".intval($last_post_id_in_tree).", cat_last_post_time_in_tree=".intval($last_post_time_in_tree)." WHERE cat_id=$cat_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."categories SET cat_topics_count=".intval($topics_count).",cat_posts_count=".intval($posts_count).", cat_last_post_id=".intval($last_post_id).", cat_last_post_time=".intval($last_post_time).",cat_topics_count_in_tree=".intval($topics_count_in_tree).",cat_posts_count_in_tree=".intval($posts_count_in_tree).", cat_last_post_id_in_tree=".intval($last_post_id_in_tree).", cat_last_post_time_in_tree=".intval($last_post_time_in_tree)." WHERE cat_id=$cat_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
 
 	// do sync parents
-	list( $pid ) = $xoops_db->get_results( "SELECT pid FROM ".$d3forum_prefix."categories WHERE cat_id=$cat_id" )  ;
+	$pid = $xoops_db->get_var( "SELECT pid FROM ".$d3forum_prefix."categories WHERE cat_id=$cat_id" )  ;
 	if( $pid != $cat_id && $pid > 0 ) {
 		wp_d3forum_sync_category( $d3forum_dirname , $pid ) ;
@@ -941,7 +966,6 @@
 
 	// check the latest data in history
-	$results = $xoops_db->get_results( "SELECT data FROM ".$d3forum_prefix."post_histories WHERE post_id=$post_id ORDER BY history_time DESC" ) ;
-	if( !empty( $results ) ) {
-		list( $old_data_serialized ) = $results ;
+	$old_data_serialized = $xoops_db->get_var( "SELECT data FROM ".$d3forum_prefix."post_histories WHERE post_id=$post_id ORDER BY history_time DESC" ) ;
+	if( !empty( $old_data_serialized ) ) {
 		$old_data = unserialize( $old_data_serialized ) ;
 		if( $old_data == $data ) return ;
