It needs your config! :)
Version: 0.94
Author: fel64
Author URI: http://www.loinhead.net/
*/
add_action('publish_post', 'felsyncpost', 999 );
add_action('comment_post', 'felsynccomment', 999, 2 );
add_action('wp_set_comment_status', 'felsynccomment', 999, 2);
add_action('wp_set_comment_status', 'felsynccomment', 999, 2);
add_action('admin_menu', 'bbsyncaddsubmenu');
add_action('edit_form_advanced', 'bbsyncpostoptions');
remove_action('publish_post', 'bbpress_setpost');
remove_filter('the_content', 'bbpress_add_forumlink');
/* CORE */
function load_bb() {
global $bb, $bbdb, $bb_current_user, $page, $bb_cache,
$posts, $bb_post, $post_id, $topics, $topic, $topic_id,
$forums, $forum, $forum_id, $tags, $tag, $tag_name, $user, $user_id, $view; // MAINTAIN - source is template-functions.php
global $bb_roles, $bb_table_prefix, $bb_locale, $bb_timestart, $timeend; // INCOMPLETE
global $opshuns;
$opshuns = get_option('bbsync');
if( is_readable( $opshuns['bbpath'] ) ) {
/**/ remove_action('bb_user_has_no_caps', 'bb_give_user_default_role'); // MAINTAIN
require_once( $opshuns['bbpath'] ); //bb-load
/**/ add_action('bb_user_has_no_caps', 'bb_give_user_default_role'); // MAINTAIN
return true;
} else return false;
}
function felsyncpost( $post_id ) {
global $bbdb, $wpdb, $current_user;
$opshuns = get_option('bbsync');
$post = wp_get_single_post( $post_id );
if( !empty( $post->post_password ) )
return;
if( !load_bb() )
return false;
if( !$current_user )
return;
bb_set_current_user( $current_user->ID );
$post_title = $bbdb->escape( $post->post_title );
$post_text = felprepposttext( $post->post_content, get_post_meta( $post_id, 'bbsync_truncate', true ) );
if( felwptobb( $post_id ) ) {
//update topic/post
$topic_id = felwptobb( $post_id );
if( bb_update_topic( $post_title, $topic_id ) )
$link_exists = true;
$reply = bb_get_first_post( $topic_id );
bb_update_post( $post_text, $reply->post_id, $topic_id );
}
if( !$link_exists ) {
felnewtopic( $post_id, $post_title, $post_text );
}
}
function felnewtopic( $post_id, $post_title, $post_text ) {
$opshuns = get_option('bbSync');
if( !( isset( $_POST['forum'] ) && $forum_id = felinterpretforum( $_POST['forum'] ) ) )
$forum_id = $opshuns['forum'];
$topic_id = bb_new_topic( $post_title, $forum_id, $tags );
$reply_id = bb_new_post( $topic_id, $post_text );
if( $topic_id && $reply_id ) {
felmetalink( $post_id, $topic_id );
$r = true;
}
$oldcoms = get_approved_comments( $post_id );
if( $oldcoms ) {
foreach( $oldcoms AS $oldcom ) {
if( $user = bb_get_user( $oldcom->comment_author ) ) {
$time = strtotime( $oldcom->comment_date );
$text = 'This comment was originally posted at ' . date( 'G:i', $time ) . ' on ' . date( 'jS F Y', $time ) . ".\n\n" . $oldcom->comment_content;
bb_set_current_user( $user->ID );
bb_new_post( $topic_id, mysql_real_escape_string( $text ) );
}
}
}
return $r;
}
function felmetalink( $post_id, $topic_id )
{
$opshuns = get_option('bbsync');
load_bb();
bb_update_topicmeta( $topic_id, 'wp_post', $post_id );
if( !update_post_meta( $post_id, 'bb_topic', $topic_id ) )
add_post_meta( $post_id, 'bb_topic', $topic_id, true );
return true;
}
function felwptobb( $post_id ) {
$bb_topic = get_post_meta( $post_id, 'bb_topic', $true );
return $bb_topic[0];
}
function felgetbbreplies() {
global $wpdb, $post;
$opshuns = get_option('bbsync');
$topic_id = felwptobb( $post->ID );
if( $topic_id ) {
$bb_ = $opshuns['bb_'];
$bb_posts = $bb_ . 'posts';
$bbreplies = $wpdb->get_results("
SELECT *
FROM $bb_posts
WHERE topic_id = $topic_id AND post_status = 0
");
array_shift( $bbreplies ); //pops first one off
return $bbreplies;
} else {
return false;
}
}
/* COMMENTS -> REPLIES */
function felsynccomment( $comment_id ) {
global $wpdb;
$opshuns = get_option('bbsync');
load_bb();
$comment = get_comment( $comment_id );
$topic_id = felwptobb( $comment->comment_post_ID );
if( ( $topic_id ) && ( $comment->comment_approved == 1 ) && ( $comment->user_id ) && bb_set_current_user( $comment->user_id ) ) {
//topic linked, genuine comment, actual user, bb likes user
bb_new_post( $topic_id, $comment->comment_content );
return true;
} else {
return false;
}
}
/* MIGRATION */
function felbbPostmigration() {
global $wpdb;
$bbPoststable = $wpdb->prefix . 'bbpress_post_posts';
$bbPostsoptionstable = $wpdb->prefix . 'bbpress_post_options';
$bbPosts = $wpdb->get_results("
SELECT * FROM $bbPoststable
");
if( $bbPosts ) {
foreach( $bbPosts as $bbPost ) {
if( !felwptobb( $bbPost->post_id ) ) {
if( !felmetalink( $bbPost->post_id, $bbPost->topic_id ) )
$fail = true;
}
}
if( $fail ) {
$errorsfelled[] = $felerrors['notopicmetalink'];
} else {
$wpdb->query("
DROP TABLE $bbPoststable, $bbPostsoptionstable
");
$felmessage = $felmessages['migrated'];
}
} else {
$errorsfelled[] = $felerrors['nobbPost'];
}
}
function felprepposttext( $post_text, $set_truncate = 0 ) {
$opshuns = get_option('bbsync');
if( $opshuns['nomore'] ) {
$ptvers = get_extended( $post_text );
$post_text = $ptvers['main'];
} elseif( $opshuns['pageonly'] ) {
list( $post_text, $throwaway ) = explode('', $post_text, 2 );
} elseif( ( isset( $_POST['truncate'] ) && $_POST['truncate'] === 0 ) || ( !( isset( $_POST['truncate'] ) && $_POST['truncate'] === 0 ) && $opshuns['truncate'] ) ) {
//if POSTed truncate is 0 XOR default truncate is 0, no modification
} elseif( $_POST['truncate'] ) {
//if POSTed truncate is not 0
$post_text = substr( $post_text, 0, $_POST['truncate'] ) . '...';
if( !add_post_meta( $post_id, 'bbsync_truncate', $_POST['truncate'], true ) )
update_post_meta( $post_id, 'bbsync_truncate', $_POST['truncate'] );
} elseif( $set_truncate ) {
//if no POSTed truncate and special data for this 'un
$post_text = substr( $post_text, 0, $set_truncate ) . '...';
} elseif( $opshuns['truncate'] ) {
//otherwise, go to default if exists
$post_text = substr( $post_text, 0, $opshuns['truncate'] ) . '...';
}
if( !empty( $opshuns['link'] ) && $post_text != $post_text ) {
$arrrsearches = array('%title%', '%name%');
$arrrreplaces = array($post->post_title, get_bloginfo('name') );
$linktext = str_replace($arrrsearches, $arrrreplaces, $opshuns['link'] );
$linkurl = get_permalink( $post->ID );
$linktext = "\n\n$linktext";
$post_text .= $linktext;
}
remove_filter('pre_post', 'encode_bad');
remove_filter('pre_post', 'bb_encode_bad');
remove_filter('pre_post', 'bb_filter_kses', 50 );
remove_filter('pre_post', 'allow_images_encode_bad', 9 );
remove_filter('pre_post', 'allow_images', 52 );
return $post_text;
}
/* INTERFACE */
function bbsyncaddsubmenu() {
if (function_exists("add_submenu_page"))
add_submenu_page('options-general.php', 'bbSync', 'bbSync', 10, 'bbsync.php', 'felbbsyncinterface');
}
function felbbsyncinterface() {
if (function_exists("add_submenu_page")) {
$opshuns = get_option('bbsync');
$bbsyncurl = get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=bbsync.php';
$felerrors = array(
'nobbpath' => 'I don\'t have a working path to your bb-load.php file for bbPress, and I need it to make this work!
The directory of WordPress is '. dirname( getcwd() ) .', if it helps.',
'nobbPost' => 'I couldn\'t find the bbPress Post table, so I couldn\'t migrate it either. Sorry!',
);
$felmessages = array(
'options' => 'Thanks! Updated your options no problem. Should all work! :-)',
'migrated' => 'Coolio, your old data from bbPress Post has been migrated and the tables it used deleted. \o/',
'broughttospeed' => 'Yup, brought your forum to speed with old blog posts and comments. Check your forum and (hopefully) cheer!'
);
if( $_POST['migrate'] == 'bbPress Post') {
felbbPostmigration();
}
if( $_POST['settings'] == true ) {
//options handling
$allowed_opshuns = array(
'bbpath',
'bburl',
'bb_',
'forum',
'nomore',
'pageonly',
'truncate',
'bbAPI',
'ppo',
'link'
);
foreach( $_POST AS $postkey => $postvalue ) {
if( in_array( $postkey, $allowed_opshuns ) )
$opshuns[ $postkey ] = $postvalue;
}
if( !is_int( $opshuns['truncate'] ) && $opshuns['truncate'] < 0 ) {
$opshuns['truncate'] = 0;
}
if( $_POST['forum'] && $forum_temp = felinterpretforum( $_POST['forum'] ) ) {
$opshuns['forum'] = $forum_temp;
}
$truefalses = array(
'nomore',
'pageonly',
'bbAPI',
'ppo'
);
foreach( $truefalses AS $kee ) {
if( !$_POST[ $kee ] ) {
$opshuns[ $kee ] = false;
}
}
update_option( 'bbsync', $opshuns );
}
if( !$opshuns['bbpath'] || !strpos( $opshuns['bbpath'], 'bb-load.php') || !is_readable( $opshuns['bbpath'] ) ) {
$errorsfelled[] = $felerrors['nobbpath'];
}
if( $_POST['settings'] == true && !$errorsfelled ) {
$felmessage = $felmessages['options'];
}
if( $_POST['migrate'] == true ) {
felbbPostmigration();
}
if( $errorsfelled ) {
foreach( $errorsfelled AS $felerror ) {
?>
This will get all the old blog post -> topic links, transfer them into the new system, and finally kill the unnecessary tables. It's a permanent change. Backup your databases first!
This plugin should completely synchronise everything of interest between Wordpress and bbPress.
<?php bbrepliestext(); ?> to just link to the thread; it accepts the link text as an argument and %no% and %replies% will be replaced with the numebr of posts and the right pluralicised form of the word reply.remove_action(... and remove_filter(...) of bbsync.php. bbSync ~
get_row(" SELECT forum_id, forum_name FROM {$bbdb->forums} WHERE ( forum_id = '$forumidname' OR forum_name = '$forumidname' ) "); if( $forum ) { return $forum->forum_id; } else { return 0; } } function bbreplylink( $topic_id = 0 ) { global $id; $opshuns = get_option('bbsync'); if( !$topic_id ) $topic_id = felwptobb( $id ); if( $topic_id ) { return felforumtopiclink( $topic_id ); } return false; } function felforumtopiclink( $topic_id ) { $opshuns = get_option('bbsync'); if( $opshuns['bbAPI'] ) { load_bb(); $topic = get_topic( $topic_id ); $last_page = get_page_number( $topic->topic_posts + topic_pages_add( $topic->topic_id ) ); $replylink = attribute_escape( get_topic_link( $topic->topic_id, $last_page ) . '#postform' ); } else { $replylink = $opshuns['bburl'] . '/topic.php?id=' . $topic_id . '#postform'; } return $replylink; } function bbrepliestext( $texties = 'Talk about this! %no% %replies%' ) { global $id; $opshuns = get_option('bbsync'); load_bb(); $topic_id = felwptobb( $id ); if( $topic_id ) { $topic = get_topic( $topic_id ); $posties = $topic->topic_posts; $repliesword = 'replies'; if( $posties == 1 ) $repliesword = 'reply'; $arrrsearches = array('%no%', '%replies%'); $arrrreplaces = array($topic->topic_posts, $repliesword ); $linktext = str_replace($arrrsearches, $arrrreplaces, $texties ); $linkurl = felforumtopiclink( $topic_id ); $linktext = "\n\n$linktext"; } } add_action('comments_array', 'replace_comments', 1, 2); function replace_comments( $comments, $post_ID ) { if( felwptobb( $post_ID ) && isset( $comments ) ) { foreach( $comments as $comment ) if( $comment->comment_type !== '') $trackbacks[] = $comment; unset( $comments ); foreach( felgetbbreplies() as $reply ) { $com->comment_ID = $reply->post_id; $com->comment_post_ID = $post_ID; $com->user_id = $reply->poster_id; $com->comment_content = $reply->post_text; $com->comment_date = $reply->post_time; $com->comment_author_IP = $reply->poster_ip; $user = get_userdata( $com->user_id ); $com->comment_author = $user->display_name; $com->comment_author_email = $user->user_email; $com->comment_author_url = $data->user_url; $comments[] = $com; } $comments = array_merge( (array) $comments, (array) $trackbacks ); } return $comments; } ?>