start_transaction();
//fetch and process wp data
$postdata = get_postdata($post_id);
$message = $postdata['Content'];
$message = ih_cleanArticle($message);
$subject = $postdata['Title'];
$now = time();
$new_pid = $db->insert_id();
// Create nothing if the post already has topicid
$tid = get_post_meta($post_id, 'topicid', TRUE);
$pid = get_post_meta($post_id, 'postid', TRUE);
if($tid) {
print_r(get_post_meta($post_id));
echo "pid: " . $pid;
$low_prio = ($db_type == 'mysql') ? 'LOW_PRIORITY ' : '';
$updatethreadquery = "UPDATE ".$db->prefix."topics SET subject='".$db->escape($subject)."', last_post_id='".$new_pid."' WHERE id=".$tid."";
$db->query($updatethreadquery) or die ("Error" . mysql_error() ." File: " . __FILE__ . " on line: " . __LINE__."");
return $post_id;
}
// Create the topic
$db->query('INSERT INTO '.$db->prefix.'topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES(\''.$db->escape($username).'\', \''.$db->escape($subject).'\', '.$now.', '.$now.', \''.$db->escape($username).'\', '.$fid.')') or error('Unable to create topic', __FILE__, __LINE__, $db->error());
$new_tid = $db->insert_id();
$ntid = $new_tid;
// Create the post ("topic post")
$messagetolink = "Discuss the article [url='www.trickyjig.com/forumwp/?p=".$post_id."'][b]posted here.[/b][/url]";
$db->query('INSERT INTO '.$db->prefix.'posts (poster, poster_id, poster_ip, message, hide_smilies, posted, topic_id) VALUES(\''.$db->escape($username).'\', '.$pun_user['id'].', \''.$_SERVER['REMOTE_ADDR'].'\', \''.$db->escape($messagetolink).'\', \''.$hide_smilies.'\', '.$now.', '.$new_tid.')') or error('Unable to create post', __FILE__, __LINE__, $db->error());
// Update the topic with last_post_id
//$db->query('UPDATE '.$db->prefix.'topics SET last_post_id='.$new_pid.' WHERE id='.$new_tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
/*not implemented yet due to function conflicts
update_search_index('post', $new_pid, $message, $subject);
update_forum($fid);*/
$low_prio = ($db_type == 'mysql') ? 'LOW_PRIORITY ' : '';
$db->query('UPDATE '.$low_prio.$db->prefix.'users SET num_posts=num_posts+1, last_post='.$now.' WHERE id='.$pun_user['id']) or error('Unable to update user', __FILE__, __LINE__, $db->error());
//$querystring = " ";
$db->query('UPDATE '.$low_prio.$db->prefix.'forums SET num_topics=num_topics+1, last_post='.$now.', last_post_id=\''.$pun_user['id'].'\', last_poster=\''.$db->escape($username).'\' WHERE id=\''.$fid.'\'') or error('Unable to update forum', __FILE__, __LINE__, $db->error());
//add custom field to wordpress post
$qry = "INSERT INTO {$wpdb->postmeta} (Post_ID, meta_key, meta_value) VALUES ({$post_id}, 'topicid','{$new_tid}');";
$wpdb->query($qry);
return $post_id;
}
/* Cleans WordPress HTML and adds BBCode.
This is from NP_PunBB at http://nupusi.com, by Radek HULAN, Bert Garcia and Rickard Andersson. I just removed some Nucleus specific stuff*/
function ih_cleanArticle($article)
{
global $CONF;
// Make sure all linebreaks are \n
$article = str_replace("\r", "", $article);
// convert links into bbCode
$article = preg_replace('/(.*?)<\/a>/', '[url=$2]$3[/url]', $article);
$article = preg_replace('//', '[img]$2[/img]', $article);
$article = preg_replace('/<%(.*?)%>/', '', $article);
// do bold, italic and underline
$article = str_replace(array('', '', '', '', '', ''), array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]'), $article);
// pre/code into bbCode
$article = str_replace('
', "[code]", $article);
$article = str_replace('', "[/code]
", $article);
$article = str_replace('', "[code]", $article);
$article = str_replace('', "[/code]
", $article);
// blockquote into bbCode
$article=str_replace('',"[quote]",$article);
$article=str_replace('
',"[/quote]
",$article);
// ending tags into line breaks
$article = str_replace('
Options updated