26 (edited by Stahn 2006-06-23 17:24)

Re: Wordpress plugin to post to PunBB

"Everytime I edit a post..."

I mean, the post should be posted (:P) when I click on [Publish]. But if I edit the post -lets say, I change the subject or correct a typo- then another topic is created on PunBB... and I don't think that's the expected behavior.

27

Re: Wordpress plugin to post to PunBB

Glad to help, Stahn (although it was actually Rickard who did the "helping", hehe).

I've also noticed that behaviour with the plugin when saving or editing posts, and it even happens if I create a page in Wordpress (the plugin creates a new forum thread for the page whenever it's saved). I suppose the plugin itself just needs a little refining.

28 (edited by inpheaux 2006-07-30 06:27)

Re: Wordpress plugin to post to PunBB

The problem with editing a post or saving a page is due to the fact that the function is being executed on the save function. scroll down to the last three lines and change:
add_action('save_post', 'ih_wp2punbb', 8);
to
add_action('publish_post', 'ih_wp2punbb');

edit: Wow, ok, I guess i should've tested this before commenting. Changing it from save_post to publish_post seems to make it still update whenever you update a post. Luckily, it doesn't post whenever you save a draft, though, because that would really suck. I'll be playing with this over the next couple days and post again when I figure out whats going on.

29 (edited by Dataclown 2006-12-14 00:33)

Re: Wordpress plugin to post to PunBB

I also have the problem that a new topic is created everytime that a post is saved in wordpress even if it was only edited. The best fix for this would probably be to find the wordpress action hook that is only executed when the a new post is created for the first time but I have not been able to find it.

My idea is to look in the punBB database for the subject of the post. If it already exists in the forum for the comments then no new topic should be created. I put the following lines in my ih_wp2punbb.php between $now = time(); and // Create the topic . 

// Create nothing if there is a topic with the same subject
$qry = "SELECT id FROM " . $db->prefix . "topics WHERE subject = \"$subject\" AND forum_id = $fid";
$rslt = $db->query($qry);

if ( $db->num_rows($rslt) )
{
    return $post_id;    
}

It would be even better to change the post in punBB so that it matches the post in wordpress since this probably was the reason to edit in the first place.

30

Re: Wordpress plugin to post to PunBB

Hi. I installed the plugin. This is my first time using punbb, so forgive me for being a newb on this all. First, I could not find out the user ID or the forum ID (so I just put 1, thinking that would be the obvious).

After configuring all of the options in the wordpress area, when I tried to make a test post, I got this error after clicking 'publish':

Warning: ih_wp2punbb(/www/protestantpub/punbb/config.php) [function.ih-wp2punbb]: failed to open stream: No such file or directory in /home/jowikine/public_html/protestantpub/wordpress/wp-content/plugins/ih_wp2punbb.php on line 23

Warning: ih_wp2punbb(/www/protestantpub/punbb/config.php) [function.ih-wp2punbb]: failed to open stream: No such file or directory in /home/jowikine/public_html/protestantpub/wordpress/wp-content/plugins/ih_wp2punbb.php on line 23

Fatal error: ih_wp2punbb() [function.require]: Failed opening required '/www/protestantpub/punbb/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/jowikine/public_html/protestantpub/wordpress/wp-content/plugins/ih_wp2punbb.php on line 23

Any help?

31

Re: Wordpress plugin to post to PunBB

Ok, my bad. The problem was that I had the wrong path put in...

Now I get this error:

Fatal error: Cannot redeclare unregister_globals() (previously declared in /home/jowikine/public_html/protestantpub/wordpress/wp-settings.php:4) in /home/jowikine/public_html/protestantpub/punbb/include/functions.php on line 1037

Which I do believe has been solved above...

32 (edited by Rantheman 2007-04-13 17:02)

Re: Wordpress plugin to post to PunBB

This is an old post but I've got updated code.
The code below will create the thread in your punbb installation but it will also update the last post status on the forum index right off the bat and update the post counts for that forum right off the bat.

Things to do today:
1.  Make it to where when you edit a post in the blog it updates the thread in the forums.
2.  When you delete a post in the blog it deletes the thread in the forum (maybe - have to think of usability - blog thread might have started a great discussion that you want to keep)
3.  When you add a comment to the blog it adds it to the forum thread as a reply.

Things for consideration:
1.  If user deletes their comment out of the forum does it delete it out of the blog?  This would require a reverse method as it integrates punbb into WP not WP into punbb.
2.  If user edits their comment in the forum does it edit in the blog?

After considering those two options and other usability items - it might be beneficial to disable manual replies and editing for that forum in punbb and make it to where all that takes place in WP.  This keeps the comments in one place.  (It also makes my work load lighter today haha)
Might get around to those considerations when I have time.

Any rate here's my contribution:
<?php
/*
Plugin Name: Ian's WordPress2PunBB plugin
Plugin URI: http://punbb.org/forums/viewtopic.php?id=8373
Description: Sends a new post to a comment forum in punbb
Version: 0.2
Revision Author: Randall Potter
Revision Author URL: services.randallpotter.com 
Original Author: Ian Huston
Original Author URI: http://www.ianhuston.net
*/

/*Main Wordpress post processing function*/
function ih_wp2punbb( $post_id )
{
//get wordpress options and define variables
$punbb_info = get_option("wp2punbb_info");
$username = $punbb_info[0];
$pun_user['id'] = $punbb_info[2];
$userid = $punbb_info[2];

$fid = $punbb_info[1];
define('PUN_ROOT', $punbb_info[3]);

global $wpdb;
//include punbb functions needed
require PUN_ROOT.'config.php';
require PUN_ROOT.'/include/functions.php';
require PUN_ROOT.'/include/dblayer/common_db.php';

// Start a transaction
$db->start_transaction();


//fetch and process wp data
$postdata = get_postdata($post_id);
$message = $postdata['Content'];
$message = ih_cleanArticle($message);
$subject = $postdata['Title'];
$now = time();


// 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();

// Create the post ("topic post")
$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($message).'\', \''.$hide_smilies.'\', '.$now.', '.$new_tid.')') or error('Unable to create post', __FILE__, __LINE__, $db->error());

$new_pid = $db->insert_id();

// 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=19') 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(.*?)href=[\'|\"](.*?)[\'|\"]>(.*?)<\/a>/', '$3', $article);

        $article = preg_replace('/<img(.*?)src=[\'|\"](.*?)[\'|\"]>/', '[img]$2[/img]', $article);
        $article = preg_replace('/<%(.*?)%>/', '', $article);
        // do bold, italic and underline
        $article = str_replace(array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>'), array('', '', '', '', '', ''), $article);
        // pre/code into bbCode
        $article = str_replace('<pre>', "

", $article);
        $article = str_replace('</pre>', "

", $article);
        $article = str_replace('<code>', "

", $article);
        $article = str_replace('</code>', "

", $article);
        // blockquote into bbCode
        $article=str_replace('<blockquote>',"

",$article);
        $article=str_replace('</blockquote>',"

",$article);
        // ending tags into line breaks
        $article = str_replace('</p>', "\n\n", $article);
        $article = str_replace("<br />\n", "\n", $article);
        $article = str_replace('<br />', "\n", $article);
        $article = str_replace("<br>\n", "\n", $article);
        $article = str_replace('<br>', "\n", $article);
        $article = str_replace('</li>', "\n", $article);
        // lists
        $article = str_replace('<li>', "* ", $article);
        // headlines in bold
        $article = preg_replace('/<h(.*?)>(.*?)<\/h(.*?)>/', "$2\n", $article);
        // strip all other tags
        $article = trim(strip_tags($article));
        // convert < and > if entered to display code
        $article = str_replace(array('<', '>'), array('<', '>'), $article);

        return $article;
    }

//Adds a link to the relevant forum topic
function ih_add_comment($post_id)
{
    $punbb_info = get_option("wp2punbb_info");
    $forumurl = $punbb_info[4];
    $commentlink = $punbb_info[5];
    $tid = get_post_meta($post_id, 'topicid', TRUE);
    if($tid){
    //echo '| asdf<a href="'.$forumurl.'viewtopic.php?id='.$tid.'">'.$commentlink.'</a>asdf';
    echo ('<a href="'.$forumurl.'viewtopic.php?id='.$tid.'">Discuss in Forum</a> | ');
    }
}

//Wordpress options handler
function ih_add_pages()
{
add_options_page('WP2PunBB', 'WP2PunBB', 8, __FILE__, 'ih_options_page');
}
//Wordpress Options page interface
function ih_options_page()
{
if (isset($_POST['info_update'])) {
    $punbb_info[0]=$_POST['username'];
    $punbb_info[1]=$_POST['forumid'];
    $punbb_info[2]=$_POST['userid'];
    $punbb_info[3]=$_POST['forumdir'];
    $punbb_info[4]=$_POST['forumurl'];
    $punbb_info[5]=$_POST['commentlink'];

    update_option("wp2punbb_info", $punbb_info);
?><div class="updated"><p><strong>Options updated</strong></p></div><?php
    }
$punbb_info = get_option("wp2punbb_info");

?>
<div class=wrap>
  <form method="post">
    <h2>Ians WP2PunBB Plugin</h2>
     <fieldset name="set1">
    <legend>PunBB forum options</legend>
    <h3>PunBB username:<input type=text name="username" value="<?php echo $punbb_info[0];?>" /></h3>
    <h3>PunBB user ID:<input type=text name="userid" value="<?php echo $punbb_info[2];?>" /></h3>
    <h3>PunBB comment forum ID:<input type=text name="forumid" value="<?php echo $punbb_info[1];?>" /></h3>
    <h3>Comment link text:<input type=text name="commentlink" value="<?php echo $punbb_info[5];?>" /></h3>
    <h3>URL to punbb:<input type=text name="forumurl" value="<?php echo $punbb_info[4];?>" /></h3>
    <em>Include final slash e.g. http://www.example.com/forum/</em>
    <h3>Absolute path to punbb:<input type=text name="forumdir" value="<?php echo $punbb_info[3];?>" /></h3>
    <em>Include final slash e.g. /var/www/httpdocs/forum/</em>
     </fieldset>
<div class="submit">
  <input type="submit" name="info_update" value="<?php
    _e('Update options', 'Localization name')
    ?>" /></div>
  </form>
</div>
<?php
}

//Add wordpress plugin hooks
add_action('save_post', 'ih_wp2punbb', 8);
add_action('admin_menu', 'ih_add_pages');
//


forums messed with the code a little:

this is the main change:

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=19') or error('Unable to update forum', __FILE__, __LINE__, $db->error());

33 (edited by Dataclown 2007-04-13 17:34)

Re: Wordpress plugin to post to PunBB

It's great to see that someone is working on this. I'll try your changes as soon as I have a test installation running.

My code for preventing new forum posts every time the wordpress post is updated looks like this:

// Create nothing if the post already has topicid
$tid = get_post_meta($post_id, 'topicid', TRUE);
if($tid) {
    return $post_id;
}

Re: Wordpress plugin to post to PunBB

Added to your code.

The following code:
Doesn't post if the same one already exists.
Changes the title of the forum thread upon edit.
Upon insertion places a post inside the thread that links back to the original word press post.

Placing: <?php ih_add_comment($post->ID); ?>

in sinple_post.php and index.php allows you to directly link to the forum post from the word press article.


you can get the code here:

click here

Re: Wordpress plugin to post to PunBB

I've integrated the code but the message gets truncated in the forum post for some reason. Is that a limitation set somewhere? If so, I can't seem to find it.

Re: Wordpress plugin to post to PunBB

theCurtis wrote:

I've integrated the code but the message gets truncated in the forum post for some reason. Is that a limitation set somewhere? If so, I can't seem to find it.

If your posts are really long (long enough to overflow a TEXT field, which is 65535 charcaters), you might have to change the column to a MEDIUMTEXT

Re: Wordpress plugin to post to PunBB

They're not that long. They seem to be getting truncated around 200-300 characters.

38 (edited by r0nh 2007-04-18 08:04)

Re: Wordpress plugin to post to PunBB

<?php ih_add_comment($post->ID); ?>

Isn't working for me. But there isn't any meta field in my posts also. So I guess it has something to do with that?!?

Maybe it has something tot do with the Wordpress update to 2.1?! Or has anyone it working with 2.1?!

Re: Wordpress plugin to post to PunBB

Looks interesting guys, doesn't work for me, and I don't think my php skills are so great as to be of help.

Re: Wordpress plugin to post to PunBB

Ive tried this but im not getting anything.  Nothing gets posted to my forum from wordpress.  Is there a way to set this when you want it to be posted to that specific forum or every time you post its going to go in that forum??

41

Re: Wordpress plugin to post to PunBB

I install the plugin without error under Wordpress. smile
But I've got a error on Punbb. sad

An error was encountered
Error: Unable to create topic.

Fatal error: Call to a member function close() on a non-object in C:\wamp\www\wordpress\forum\include\functions.php on line 1122

Where I made a mistake. hmm

Benoît V.
My Board

42

Re: Wordpress plugin to post to PunBB

this is the error im getting..

Warning: require(config.php) [function.require]: failed to open stream: No such file or directory in /home/quaker/public_html/wp-content/plugins/ih_wp2punbb.php on line 23

Warning: require(config.php) [function.require]: failed to open stream: No such file or directory in /home/quaker/public_html/wp-content/plugins/ih_wp2punbb.php on line 23

how do i set the punbb root ?????????

Q

My stuff or my style might sux, but atleast I'm willing to help when I can.
Don't be stupid and help ! We are the stupid one's !!!

43

Re: Wordpress plugin to post to PunBB

ok that was a silly goofy on my part.. i had to go to the settings and configure the plugin. but now when i use it... it does the job but the post.php is a blank screen...

errrr

Q

My stuff or my style might sux, but atleast I'm willing to help when I can.
Don't be stupid and help ! We are the stupid one's !!!