1 (edited by aaronproctor 2013-10-03 04:25)

Topic: [Integration] WordPress Posts Re-posted to punBB Forum

This is a re-post of http://punbb.informer.com/forums/topic/ … -posting/, now in a more-appropriate v.1.4 forum. Thank you, Jones, for starting this much needed integration between WordPress posts and punBB auto-posts.

For members like freequest and others, the code below is to be added to your WordPress theme's function.php.

I've made some adjustments to make the code easier to edit. This was tested and works with punBB 1.4 and WordPress 3.5.1. Whenever a new post is published within WordPress, it is also auto-posted to the punBB board with a permalink back to the original URL.

You can see it in action with posts between the home page of the following link and www [dot] bobafettfanclub [dot] com/boards/forum/8/dialogue/.

Simply replace the defaults noted in the code below:

function punbb_publish_post($post_ID) {
    
    /*
    
    Based upon code by Jones
    
    Revisions on 2013-05-03 by FWD:labs / Aaron Proctor include:
    - Replace WordPress HTML content to valid BBCode
    - Sanitize database imputs with mysql_real_escape_string(trim($variable))
    - Add link within punBB re-post back to WordPress permalink
    
    */
    
    global $wpdb;
    
        /* Defaults - declare all of the following before using */
        $punbbPrefix = ""; // e.g. punbb_
        $punbbUsername = ""; // e.g. admin
        $punbbUserID = ""; // e.g. 2
        $punbbUserIP = ""; // e.g 1.2.3.4
        $punbbForumID = ""; // e.g. 8
    
    $wppost = $wpdb->get_row("SELECT ID, post_date, post_title, post_content FROM ".$wpdb->posts." WHERE ID = ".mysql_real_escape_string(trim($post_ID))." LIMIT 1");
    
    /* Check if edited or new, based on username and post date match only */
    $wppost_check = $wpdb->get_row("SELECT id, topic_id FROM ".$punbbPrefix."posts WHERE poster = '".$punbbUsername."' AND posted = ".strtotime($wppost->post_date));

    $getArticle = $wppost->post_content;

        // Convert HTML to BBCode
    
        // Images need some extra help to get to BBCode
        
            // WordPress formatted
        $getArticle = preg_replace("/<img src=([^>]+) alt=([^>]+) width=([^>]+) height=([^>]+) class=([^>]+)\>/i", '[img]$1[/img]', $getArticle);
            // By-hand formatted
        $getArticle = preg_replace("/<img src=([^>]+) \/\>/i", '[img]$1[/img]', $getArticle);
                
        // <br style="clear:both;/"> may break the input
        $getArticle = preg_replace("/<br style=([^>]+)\>/i", "\n", $getArticle);
    
        // Based upon bb2html() by Louai Munajim
        // Improvements made for list items and double quotes
        $htmlcode = array("&lt;", "&gt;",
                        "<ul>", "</ul>", 
                        "<li>", "</li>", 
                        "<strong>", "</strong>", 
                        "<u>", "</u>", 
                        "<i>", "</i>",
                        '<a href="', "</a>",
                        "<blockquote>", "</blockquote>",
                        '">',
                        '"');
        $bbcode = array("<", ">",
                        "[list]", "[/list]", 
                        "[*]", "[/*]",
                        "[b]", "[/b]", 
                        "[u]", "[/u]", 
                        "[i]", "[/i]",
                        '[url="', "[/url]",
                        "[quote]", "[/quote]",
                        '"]',
                        '');
        $getArticle = str_replace($htmlcode, $bbcode, $getArticle);
        
    if ($wppost_check) {

        /* Edited Post */
        $wpdb->query("UPDATE ".$punbbPrefix."topics SET subject = '".mysql_real_escape_string(trim($wppost->post_title))."' WHERE id = ".$wppost_check->topic_id);
        $wpdb->query("UPDATE ".$punbbPrefix."posts SET message = '".mysql_real_escape_string(trim($getArticle))."' WHERE id = ".$wppost_check->id);

    } else {
        
        /* New Post */

        /* Puts a link to the original posting in the message board post */
        $permalink = get_permalink($post_ID);
        $wp_add = "\n\nView the original post [url=$permalink]here[/url]";
        $displayArticle = $getArticle.$wp_add;
        
        $wpdb->query("INSERT INTO ".$punbbPrefix."topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES(
    '".$punbbUsername."',
    '".$wppost->post_title."',
    ".strtotime($wppost->post_date).",
    ".strtotime($wppost->post_date).",
    '".$punbbUsername."',
    '".$punbbForumID."'
    )");
        $topic_id = $wpdb->insert_id;

        $wpdb->query("INSERT INTO ".$punbbPrefix."posts (poster, poster_id, message, posted, topic_id, poster_ip) VALUES(
    '".$punbbUsername."',
    ".$punbbUserID.",
    '".mysql_real_escape_string(trim($displayArticle))."',
    ".strtotime($wppost->post_date).",
    ".$topic_id.",
    '".$punbbUserIP."'
    )");
        $post_id = $wpdb->insert_id;

        /* Add new topic needs first_post_id and last_post_id to successfully delete within punBB */
        $wpdb->query("UPDATE ".$punbbPrefix."topics SET last_post_id = ".$post_id.", first_post_id = ".$post_id." WHERE id = ".$topic_id);
        
        $wpdb->query("UPDATE ".$punbbPrefix."forums SET last_post = ".strtotime($wppost->post_date).", last_post_id = ".$post_id.", last_poster = '".$punbbUsername."' WHERE id = $punbbForumID");
        $punbb_postnumber = $wpdb->get_row("SELECT num_posts, username FROM ".$punbbPrefix."users WHERE username = '".$punbbUsername."' LIMIT 1");
        $wpdb->query("UPDATE ".$punbbPrefix."users SET num_posts = ".$punbb_postnumber->num_posts."+1 WHERE username = '".$punbbUsername."'");
    
    }

    return $post_ID; /* Optional? */

}

add_action( 'publish_post', 'punbb_publish_post' );

Re: [Integration] WordPress Posts Re-posted to punBB Forum

It worked fine with the previous version of worpress, but not with 3.9.

Re: [Integration] WordPress Posts Re-posted to punBB Forum

wp has update to 3.9 sad

sorry my BAD english T___T
Have a nice day >.<
(^____^)v

4 (edited by aaronproctor 2014-04-20 04:14)

Re: [Integration] WordPress Posts Re-posted to punBB Forum

achtungbaby and kudataz, thanks for the posts!

I've just reviewed the code that's running in production now on a WordPress 3.9 install with a punBB 1.4.2 install. The same core code works well, however, I've re-written it to improve upon my earlier post:

/**
 * Name: WordPress Posts Re-posted to punBB Forum
 * Requirements: WordPress 3.9+, punBB 1.4.2+
 * Instructions: 1. Edit config to match your punBB install and preferred user. 2. Add this code to your WordPress theme's functions.php file. 3. Publish a post on WordPress and see it auto-publish to your punBB.
 * Version: 1.0
 * Author: FWD:labs, based on code by Jones at punbb.informer.com/forums/topic/23056/wordpress2punbb-posting/
 * Author URI: http://fwdlabs.com/resources/code/
 * License: GPL2
 */

function punbb_publish_post($post_ID) {
        
    global $wpdb;
    
    // Config
    $punbbPrefix = ""; // Example: punbb_
    $punbbUsername = ""; // Example: Forum Admin
    $punbbUserID = ""; // Example: 2
    $punbbUserIP = ""; // Example: 1.2.3.4
    $punbbForumID = ""; // Example: 8
    
    // Exit gracefully if config unchanged
    if ($punbbPrefix == "" || $punbbUsername == "" || $punbbUserID == "" || $punbbUserIP == "" || $punbbForumID == "") {
        return false;
    }
    
    // Select WordPress post based on the provided $post_ID
    $wppost = $wpdb->get_row("SELECT ID, post_date, post_title, post_content FROM ".$wpdb->posts." WHERE ID = ".mysql_real_escape_string(trim($post_ID))." LIMIT 1");
        
    $getArticle = $wppost->post_content;

    // Convert HTML to BBCode
    
        // Images need some extra help to get to BBCode
        
        // If WordPress formatted the image
        // $getArticle = preg_replace("/<img src=([^>]+) alt=([^>]+) width=([^>]+) height=([^>]+) class=([^>]+)\>/i", '[img]$1[/img]', $getArticle);

        // If a content editor formatted the image by hand
        // $getArticle = preg_replace("/<img src=([^>]+) \/\>/i", '[img]$1[/img]', $getArticle);

        $getArticle = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].*?>#i', '[img]$1[/img]', $getArticle);    
                
        // Remove <br style="clear:both;/">, which may break the input
        $getArticle = preg_replace("/<br style=([^>]+)\>/i", "\n", $getArticle);
        
        // Remove [caption] content
        // Source: stackoverflow.com/a/16988328/1527619
        $getArticle = preg_replace('#\s*\[caption[^]]*\].*?\[/caption\]\s*#is', '', $getArticle);
    
        // Convert HTML to punBB'd BBCODE
        // Improvements made for list items and double quotes
        // Based upon bb2html() by Louai Munajim
        $htmlcode = array("&lt;", "&gt;",
            "<ul>", "</ul>", 
            "<li>", "</li>", 
            "<strong>", "</strong>", 
            "<u>", "</u>", 
            "<i>", "</i>",
            "<em>", "</em>",
            '<a href="', "</a>",
            "<blockquote>", "</blockquote>",
            '">',
            '"');
        $bbcode = array("<", ">",
            "[list]", "[/list]", 
            "[*]", "[/*]",
            "[b]", "[/b]", 
            "[u]", "[/u]", 
            "[i]", "[/i]",
            "[i]", "[/i]",
            '[url="', "[/url]",
            "[quote]", "[/quote]",
            '"]',
            '');

    $getArticle = str_replace($htmlcode, $bbcode, $getArticle);
                
    // Check punBB if we're editing a topic or adding a new topic, simply based on the username and post date matching
    $wppost_check = $wpdb->get_row("SELECT id, topic_id FROM ".$punbbPrefix."posts WHERE poster = '".$punbbUsername."' AND posted = ".strtotime($wppost->post_date));
    
    // punBB found a match, so we're editing an existing topic
    if ($wppost_check) {

        // Update the title of the punBB topic with any changes made within WordPress
         $wpdb->query("UPDATE ".$punbbPrefix."topics SET subject = '".mysql_real_escape_string(trim($wppost->post_title))."' WHERE id = ".$wppost_check->topic_id);
        
        // Update the content of the punBB post with any changes made within WordPress
        $wpdb->query("UPDATE ".$punbbPrefix."posts SET message = '".mysql_real_escape_string(trim($getArticle))."' WHERE id = ".$wppost_check->id);

    // punBB did not find a match, so we're adding a new topic
    } else {
        
        // Add a permalink to the original post in WordPress to the punBB message board re-post
        $permalink = get_permalink($post_ID);
        $wp_add = "\n\nView the original post [url=$permalink]here[/url]";
        $displayArticle = $getArticle.$wp_add;
        
        // Modify the title of the punBB version, perhaps with a prefix like [NEWS]
        $displayTopicTitle = $wppost->post_title;
        $displayTopicTitle = "[NEWS] ".$displayTopicTitle;
        
        // Create the punBB topic
        $wpdb->query("INSERT INTO ".$punbbPrefix."topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES(
            '".$punbbUsername."',
            '".$displayTopicTitle."',
            ".strtotime($wppost->post_date).",
            ".strtotime($wppost->post_date).",
            '".$punbbUsername."',
            '".$punbbForumID."'
            )");
        $topic_id = $wpdb->insert_id;

        // Create the punBB post in the new topic
        $wpdb->query("INSERT INTO ".$punbbPrefix."posts (poster, poster_id, message, posted, topic_id, poster_ip) VALUES(
            '".$punbbUsername."',
            ".$punbbUserID.",
            '".mysql_real_escape_string(trim($displayArticle))."',
            ".strtotime($wppost->post_date).",
            ".$topic_id.",
            '".$punbbUserIP."'
            )");
        $post_id = $wpdb->insert_id;

        // New topic in punBB needs first_post_id and last_post_id to successfully set
        $wpdb->query("UPDATE ".$punbbPrefix."topics SET last_post_id = ".$post_id.", first_post_id = ".$post_id." WHERE id = ".$topic_id);
        
        // Update the forums table with last post, last post ID, and last poster, which emulates how posts are done within punBB
        $wpdb->query("UPDATE ".$punbbPrefix."forums SET last_post = ".strtotime($wppost->post_date).", last_post_id = ".$post_id.", last_poster = '".$punbbUsername."' WHERE id = $punbbForumID");
        
        // Get the number of posts of the user
        $punbb_postnumber = $wpdb->get_row("SELECT num_posts FROM ".$punbbPrefix."users WHERE username = '".$punbbUsername."' LIMIT 1");
        
        // Add one to the number of their posts
        $wpdb->query("UPDATE ".$punbbPrefix."users SET num_posts = ".$punbb_postnumber->num_posts."+1 WHERE username = '".$punbbUsername."'");
        
    }

    return $post_ID;

}

add_action( 'publish_post', 'punbb_publish_post' );

You can see this in action at these links, refreshed today.

punBB:
bobafettfanclub.com/boards/topic/4168/news-black-series-boba-fett-reissued-in-wave-4/

WordPress:
bobafettfanclub.com/news/collectibles/black-series-boba-fett-re-issued-in-wave-4/

5 (edited by kudataz 2015-02-11 04:11)

Re: [Integration] WordPress Posts Re-posted to punBB Forum

aaronproctor wrote:

achtungbaby and kudataz, thanks for the posts!

I've just reviewed the code that's running in production now on a WordPress 3.9 install with a punBB 1.4.2 install. The same core code works well, however, I've re-written it to improve upon my earlier post:

punBB:
bobafettfanclub.com/boards/topic/4168/news-black-series-boba-fett-reissued-in-wave-4/

WordPress:
bobafettfanclub.com/news/collectibles/black-series-boba-fett-re-issued-in-wave-4/

WP now is 4.1 is still work?

i have question about config in this line..
    // Config
    $punbbPrefix = ""; // Example: punbb_                                 
    $punbbUsername = ""; // Example: Forum Admin        <-- random username or real username registered in punbb database?
    $punbbUserID = ""; // Example: 2                               <-- random id or real id integrated with username database?
    $punbbUserIP = ""; // Example: 1.2.3.4                      <-- random IP or real IP my PC ?
    $punbbForumID = ""; // Example: 8                           <--  whats forum id you mean? maybe like this //punbb.informer.com/forums/forum/82/punbb-14-additions/ use id 82 ?

Thanks again in advanced big_smile

sorry my BAD english T___T
Have a nice day >.<
(^____^)v

Re: [Integration] WordPress Posts Re-posted to punBB Forum

Hi kudataz, thanks for the question!

We're on WordPress 4.1 with this code and it's still working.

Related, see http://fwdlabs.com/resources/code/wordp … nbb-forum/ for more up-to-date code releases.

Regarding your questions on the config:

* $punbbUsername should be a real username unless it's intentionally to appear as a bot aka non-user (which isn't the intention here)

* $punbbUserID should match the real username or may create a problem, since it wasn't built to run as a bot / non-user

* $punbbUserIP can be spoofed and is a required variable that punBB wants to store, generally for security purposes

* $punbbForumID should be the forum ID known within punBB so this auto-post shows up in the right spot

Let me know if you have any other questions or concerns. Thanks again!