root/bbsync/trunk/bbsync.php

Revision 772, 17.1 kB (checked in by fel64, 1 year ago)

fixed per-post forum bug

Line 
1 <?php
2 /*
3 Plugin Name: bbSync
4 Plugin URI:
5 Description: Complete integration with your bbPress install. <strong>It <em>needs</em> your <a href="options-general.php?page=bbsync.php">config</a>!</strong> :)
6 Version: 0.94
7 Author: fel64
8 Author URI: http://www.loinhead.net/
9 */
10
11 add_action('publish_post', 'felsyncpost', 999 );
12
13 add_action('comment_post', 'felsynccomment', 999, 2 );
14 add_action('wp_set_comment_status', 'felsynccomment', 999, 2);
15 add_action('wp_set_comment_status', 'felsynccomment', 999, 2);
16
17 add_action('admin_menu', 'bbsyncaddsubmenu');
18 add_action('edit_form_advanced', 'bbsyncpostoptions');
19
20     remove_action('publish_post', 'bbpress_setpost');
21     remove_filter('the_content', 'bbpress_add_forumlink');
22     
23 /* CORE */
24 function load_bb() {
25     global $bb, $bbdb, $bb_current_user, $page, $bb_cache,
26         $posts, $bb_post, $post_id, $topics, $topic, $topic_id,
27         $forums, $forum, $forum_id, $tags, $tag, $tag_name, $user, $user_id, $view// MAINTAIN - source is template-functions.php
28     global $bb_roles, $bb_table_prefix, $bb_locale, $bb_timestart, $timeend// INCOMPLETE
29     global $opshuns;
30     $opshuns = get_option('bbsync');
31     if( is_readable( $opshuns['bbpath'] ) ) {
32 /**/    remove_action('bb_user_has_no_caps', 'bb_give_user_default_role');  // MAINTAIN
33     require_once( $opshuns['bbpath'] ); //bb-load
34 /**/    add_action('bb_user_has_no_caps', 'bb_give_user_default_role');  // MAINTAIN
35     return true;
36     } else return false;
37 }
38
39 function felsyncpost( $post_id ) {
40     global $bbdb, $wpdb, $current_user;
41     $opshuns = get_option('bbsync');
42     $post = wp_get_single_post( $post_id );
43     
44     if( !empty( $post->post_password ) )
45         return;
46         
47     if( !load_bb() )
48         return false;
49     
50     if( !$current_user )
51         return;
52     bb_set_current_user( $current_user->ID );
53     
54     $post_title = $bbdb->escape( $post->post_title );
55     $post_text = felprepposttext( $post->post_content, get_post_meta( $post_id, 'bbsync_truncate', true ) );
56     
57     if( felwptobb( $post_id ) ) {
58         //update topic/post
59         $topic_id = felwptobb( $post_id );
60         if( bb_update_topic( $post_title, $topic_id ) )
61             $link_exists = true;
62         $reply = bb_get_first_post( $topic_id );
63         bb_update_post( $post_text, $reply->post_id, $topic_id );
64     }
65     if( !$link_exists ) {
66         felnewtopic( $post_id, $post_title, $post_text );
67     }
68 }
69
70 function felnewtopic( $post_id, $post_title, $post_text ) {
71     $opshuns = get_option('bbSync');
72     if( !( isset( $_POST['forum'] ) && $forum_id = felinterpretforum( $_POST['forum'] ) ) )
73         $forum_id = $opshuns['forum'];
74     
75     $topic_id = bb_new_topic( $post_title, $forum_id, $tags );
76     $reply_id = bb_new_post( $topic_id, $post_text );
77     if( $topic_id && $reply_id ) {
78         felmetalink( $post_id, $topic_id );
79         $r = true;
80     }
81     
82     $oldcoms = get_approved_comments( $post_id );
83     if( $oldcoms ) {
84         foreach( $oldcoms AS $oldcom ) {
85             if( $user = bb_get_user( $oldcom->comment_author ) ) {
86                 $time = strtotime( $oldcom->comment_date );
87                 $text = '<em><strong>This comment was originally posted at ' . date( 'G:i', $time ) . ' on ' . date( 'jS F Y', $time ) . ".</strong></em>\n\n" . $oldcom->comment_content;
88                 bb_set_current_user( $user->ID );
89                 bb_new_post( $topic_id, mysql_real_escape_string( $text ) );
90             }
91         }
92     }
93     return $r;
94 }
95
96 function felmetalink( $post_id, $topic_id )
97 {
98     $opshuns = get_option('bbsync');
99     load_bb();
100     bb_update_topicmeta( $topic_id, 'wp_post', $post_id );
101     if( !update_post_meta( $post_id, 'bb_topic', $topic_id ) )
102         add_post_meta( $post_id, 'bb_topic', $topic_id, true );
103     return true;
104 }
105
106 function felwptobb( $post_id ) {
107     $bb_topic = get_post_meta( $post_id, 'bb_topic', $true );
108     return $bb_topic[0];
109 }
110
111 function felgetbbreplies() {
112     global $wpdb, $post;
113     $opshuns = get_option('bbsync');
114     $topic_id = felwptobb( $post->ID );
115     if( $topic_id ) {
116         $bb_ = $opshuns['bb_'];
117         $bb_posts = $bb_ . 'posts';
118         $bbreplies = $wpdb->get_results("
119             SELECT *
120             FROM $bb_posts
121             WHERE topic_id = $topic_id AND post_status = 0
122         ");
123         array_shift( $bbreplies ); //pops first one off
124         return $bbreplies;
125     } else {
126         return false;
127     }
128 }
129
130 /* COMMENTS -> REPLIES */
131 function felsynccomment( $comment_id ) {
132     global $wpdb;
133     $opshuns = get_option('bbsync');
134     load_bb();
135     $comment = get_comment( $comment_id );
136     $topic_id = felwptobb( $comment->comment_post_ID );
137     if( ( $topic_id ) && ( $comment->comment_approved == 1 ) && ( $comment->user_id ) && bb_set_current_user( $comment->user_id ) ) {
138         //topic linked, genuine comment, actual user, bb likes user
139         bb_new_post( $topic_id, $comment->comment_content );
140         return true;
141     } else {
142         return false;
143     }
144 }
145
146 /* MIGRATION */
147 function felbbPostmigration() {
148     global $wpdb;
149     $bbPoststable = $wpdb->prefix . 'bbpress_post_posts';
150     $bbPostsoptionstable = $wpdb->prefix . 'bbpress_post_options';
151     
152     $bbPosts = $wpdb->get_results("
153         SELECT * FROM $bbPoststable
154     ");
155     
156     if( $bbPosts ) {
157         foreach( $bbPosts as $bbPost ) {
158             if( !felwptobb( $bbPost->post_id ) ) {
159                 if( !felmetalink( $bbPost->post_id, $bbPost->topic_id ) )
160                     $fail = true;
161             }
162         }
163         if( $fail ) {
164             $errorsfelled[] = $felerrors['notopicmetalink'];
165         } else {
166             $wpdb->query("
167                 DROP TABLE $bbPoststable, $bbPostsoptionstable
168             ");
169             $felmessage = $felmessages['migrated'];
170         }
171     } else {
172         $errorsfelled[] = $felerrors['nobbPost'];
173     }
174 }
175
176 function felprepposttext( $post_text, $set_truncate = 0 ) {
177     $opshuns = get_option('bbsync');
178
179     if( $opshuns['nomore'] ) {
180         $ptvers = get_extended( $post_text );
181         $post_text = $ptvers['main'];
182     } elseif( $opshuns['pageonly'] ) {
183         list( $post_text, $throwaway ) = explode('<!--nextpage-->', $post_text, 2 );
184     } elseif( ( isset( $_POST['truncate'] ) && $_POST['truncate'] === 0 ) || ( !( isset( $_POST['truncate'] ) && $_POST['truncate'] === 0 ) && $opshuns['truncate'] ) ) {
185     //if POSTed truncate is 0 XOR default truncate is 0, no modification
186     } elseif( $_POST['truncate'] ) {
187     //if POSTed truncate is not 0
188         $post_text = substr( $post_text, 0, $_POST['truncate'] ) . '...';
189         if( !add_post_meta( $post_id, 'bbsync_truncate', $_POST['truncate'], true ) )
190             update_post_meta( $post_id, 'bbsync_truncate', $_POST['truncate'] );
191     } elseif( $set_truncate ) {
192     //if no POSTed truncate and special data for this 'un
193         $post_text = substr( $post_text, 0, $set_truncate ) . '...';
194     } elseif( $opshuns['truncate'] ) {
195     //otherwise, go to default if exists
196         $post_text = substr( $post_text, 0, $opshuns['truncate'] ) . '...';
197     }
198
199     if( !empty( $opshuns['link'] ) && $post_text != $post_text ) {
200         $arrrsearches = array('%title%', '%name%');
201         $arrrreplaces = array($post->post_title, get_bloginfo('name') );
202         $linktext = str_replace($arrrsearches, $arrrreplaces, $opshuns['link'] );
203         $linkurl = get_permalink( $post->ID );
204         $linktext = "\n\n<a href='$linkurl'>$linktext</a>";
205         $post_text .= $linktext;
206     }
207
208     remove_filter('pre_post', 'encode_bad');
209     remove_filter('pre_post', 'bb_encode_bad');
210     remove_filter('pre_post', 'bb_filter_kses', 50 );
211     remove_filter('pre_post', 'allow_images_encode_bad', 9 );
212     remove_filter('pre_post', 'allow_images', 52 );
213     
214     return $post_text;
215 }
216
217
218 /* INTERFACE */
219 function bbsyncaddsubmenu() {
220     if (function_exists("add_submenu_page"))
221         add_submenu_page('options-general.php', 'bbSync', 'bbSync', 10, 'bbsync.php', 'felbbsyncinterface');
222 }
223
224 function felbbsyncinterface() {
225     if (function_exists("add_submenu_page")) {
226         $opshuns = get_option('bbsync');
227         
228         $bbsyncurl = get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=bbsync.php';
229         $felerrors = array(
230             'nobbpath' => 'I don\'t have a working path to your bb-load.php file for bbPress, and I need it to make this work!<br /> The directory of WordPress is '. dirname( getcwd() ) .', if it helps.',
231             'nobbPost' => 'I couldn\'t find the bbPress Post table, so I couldn\'t migrate it either. Sorry!',
232         );
233         $felmessages = array(
234             'options' => 'Thanks! Updated your options no problem. Should all work! :-)',
235             'migrated' => 'Coolio, your old data from bbPress Post has been migrated and the tables it used deleted. \o/',
236             'broughttospeed' => 'Yup, brought your forum to speed with old blog posts and comments. Check your forum and (hopefully) cheer!'
237         );
238         
239         if( $_POST['migrate'] == 'bbPress Post') {
240             felbbPostmigration();
241         }
242         
243         if( $_POST['settings'] == true ) {
244             //options handling
245             $allowed_opshuns = array(
246                 'bbpath',           
247                 'bburl',
248                 'bb_',
249                 'forum',
250                 'nomore',
251                 'pageonly',
252                 'truncate',
253                 'bbAPI',
254                 'ppo',
255                 'link'
256             );
257             foreach( $_POST AS $postkey => $postvalue ) {
258                 if( in_array( $postkey, $allowed_opshuns ) )
259                     $opshuns[ $postkey ] = $postvalue;
260             }
261             if( !is_int( $opshuns['truncate'] ) && $opshuns['truncate'] < 0 ) {
262                 $opshuns['truncate'] = 0;
263             }
264             if( $_POST['forum'] && $forum_temp = felinterpretforum( $_POST['forum'] ) ) {
265                 $opshuns['forum'] = $forum_temp;
266             }
267             $truefalses = array(
268                 'nomore',
269                 'pageonly',
270                 'bbAPI',
271                 'ppo'
272             );
273             foreach( $truefalses AS $kee ) {
274                 if( !$_POST[ $kee ] ) {
275                     $opshuns[ $kee ] = false;
276                 }
277             }
278             update_option( 'bbsync', $opshuns );
279         }
280             
281         if( !$opshuns['bbpath'] || !strpos( $opshuns['bbpath'], 'bb-load.php') || !is_readable( $opshuns['bbpath'] ) ) {
282             $errorsfelled[] = $felerrors['nobbpath'];
283         }
284         if( $_POST['settings'] == true && !$errorsfelled ) {
285                 $felmessage = $felmessages['options'];
286         }
287         
288         if( $_POST['migrate'] == true ) {
289             felbbPostmigration();
290         }
291         
292         if( $errorsfelled ) {
293         foreach( $errorsfelled AS $felerror ) {
294                 ?>
295            
296 <div id="message" class="error fade">
297     <p><?php echo $felerror; ?></p>
298 </div>
299
300                 <?php
301             }
302         }
303         if( $felmessage ) {
304             ?>
305        
306 <div id="message" class="updated fade">
307     <p><?php echo $felmessage; ?></p>
308 </div>
309
310             <?php
311         }
312         
313         ?>
314    
315 <div class="wrap">
316     <h2>bbSync ~ bbPress Synchronisation Options</h2>
317     <div>
318         <h3>Settings</h3>
319         <form method="POST" action="<?php echo $bbsyncurl; ?>">
320             <p><input type="text" name="bbpath" id="bbpath" size="40" value="<?php echo $opshuns['bbpath']; ?>" /> <label for="bbpath">The absolute <em>path</em> to your bbPress bb-load.php file. It could be <code>/var/www/htdocs/forums/bb-load.php</code> or similar.</label></p>
321             <p><input type="text" name="bburl" id="bburl" size="40" value="<?php echo $opshuns['bburl']; ?>" /> <label for="bburl">What's the web address for your forum? Something like <code>http://example.com/forums</code> with no trailing slash please!</label></p>
322             <ul>
323                 <li><input type="text" name="bb_" id="bb_" size="2" value="<?php echo( $opshuns['bb_'] ? $opshuns['bb_'] : 'bb_'); ?>" /> <label for="bb_">What's the prefix for your bbPress tables? It's probably <code>bb_</code>.</label></li>
324                 <li><input type="text" name="forum" id="forum" size="6" value="<?php echo $opshuns['forum']; ?>" /> <label for="forum">Which forum should the topics go in by default?</label></li>
325                 <li><input type="checkbox" name="nomore" id="nomore" <?php if( $opshuns['nomore'] ) echo 'checked="true"'; ?> /> <label for="nomore">Cut posts off at the <code>&lt;!--more--&gt;</code> tag? Totally overrides the next two options.</label></li>
326                 <li><input type="checkbox" name="pageonly" id="pageonly" <?php if( $opshuns['pageonly'] ) echo 'checked="true"'; ?> /> <label for="pageonly">Cut posts off after the <code>&lt;!--nextpage--&gt;</code> tag? Overrides the next option.</label></li>
327                 <li><input type="text" name="truncate" id="truncate" size="2" value="<?php echo $opshuns['truncate']; ?>" /> <label for="truncate">How much of your blog post do you want shown on the forums? Set it to 0 to get the full post.</label></li>
328                 <li><input type="checkbox" name="bbAPI" id="bbAPI" <?php if( $opshuns['bbAPI'] ) echo 'checked="true'; ?> /> <label for="bbAPI">Do you want me to load bbPress whenever to make things like links nicer? This could add a lot of work to your server and make it slower.</label></li>
329                 <li><input type="checkbox" name="ppo" id="ppo" <?php if( $opshuns['ppo'] ) echo 'checked="true"'; ?> /> <label for="ppo">Would you like options on a per-post basis? This will let you chose which forum to send the topic to and how much of the post to show.</label></li>
330                 <li><input type="text" name="link" id="link" value="<?php echo $opshuns['link']; ?>" /> <label for="link">Want a link back to the blog post? <code>%title%</code> and <code>%name%</code> will be replaced with the post name and blog title respectively. (Leave blank if you don't want a link.)</label></li>
331             </ul>
332             <input type="hidden" name="settings" value="true" />
333             <input type="Submit" value="Submit Info" />
334         </form>
335     </div>
336     <div>
337         <h3>Migrate from bbPress Post</h3>
338         <p>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!</p>
339         <form method="POST" action="<?php echo $bbsyncurl; ?>">
340             <input type="hidden" name="migrate" value="bbPress Post" />
341             <input type="submit" value="Migrate" />
342         </form>
343     </div>
344     <div>
345         <h3>bbSync</h3>
346         <p>This plugin should completely synchronise everything of interest between Wordpress and bbPress.</p>
347         <ul>
348             <li>It creates a new topic when you post in your blog, and links them behind the scenes.</li>
349             <li>Replies in the thread can be shown where the comments are (quite automagically). Use <code>&lt;?php bbrepliestext(); ?&gt;</code> to just link to the thread; it accepts the link text as an argument and <code>%no%</code> and <code>%replies%</code> will be replaced with the numebr of posts and the right pluralicised form of the word reply.</li>
350             <li>Comments made by the normal wp post form to integrated posts will be picked up and sent into the forum - so people can reply as they did before!</li>
351             <li>Deactivates the bbPress Post plugin. If you don't like that, take out lines 20 and 21 (<code>remove_action(...</code> and <code>remove_filter(...</code>) of bbsync.php. </li>
352             <li>Can migrate data from the bbPress Post plugin and then delete it. bbPress Post must not be active after migration!</li>
353         </ul>
354     </div>
355 </div>   
356         <?php
357     }
358 }
359
360 function bbsyncpostoptions() {
361     global $post_id;
362     $opshuns = get_option('bbsync');
363     if( $opshuns['ppo'] ) {
364         ?>
365 <p>
366     <strong>bbSync ~ </strong>
367     <?php if( !isset( $_GET['post'] ) || ( isset( $_GET['post'] ) && !felwptobb( $_GET['post'] ) ) ) { ?><label for="forum">Forum: </label><input type="text" name="forum" id="forum" size="6" value="<?php $opshuns['forum'] ?>" /><?php } ?>
368     <label for="truncate">Excerpt length: </label><input type="text" name="truncate" id="truncate" size="2" value="<?php $opshuns['truncate'] ?>" />
369 </p>
370         <?php
371     }
372 }
373
374 function felinterpretforum( $forumidname ) {
375     if( !load_bb() )
376         return false;
377     global $bbdb;
378     $opshuns = get_option('bbsync');
379     $forum = $bbdb->get_row("
380         SELECT forum_id, forum_name
381         FROM {$bbdb->forums}
382         WHERE ( forum_id = '$forumidname' OR forum_name = '$forumidname' )
383     ");
384     if( $forum ) {
385         return $forum->forum_id;
386     } else {
387         return 0;
388     }
389 }
390
391 function bbreplylink( $topic_id = 0 ) {
392     global $id;
393     $opshuns = get_option('bbsync');
394     if( !$topic_id )
395         $topic_id = felwptobb( $id );
396     if( $topic_id ) {
397         return felforumtopiclink( $topic_id );
398     }
399     return false;
400 }
401
402 function felforumtopiclink( $topic_id ) {
403     $opshuns = get_option('bbsync');
404     if( $opshuns['bbAPI'] ) {
405         load_bb();
406         $topic = get_topic( $topic_id );
407         $last_page = get_page_number( $topic->topic_posts + topic_pages_add( $topic->topic_id ) );
408         $replylink attribute_escape( get_topic_link( $topic->topic_id, $last_page ) . '#postform' );
409     } else {
410         $replylink = $opshuns['bburl'] . '/topic.php?id=' . $topic_id . '#postform';
411     }
412     return $replylink;
413 }
414
415 function bbrepliestext( $texties = 'Talk about this! %no% %replies%' ) {
416     global $id;
417     $opshuns = get_option('bbsync');
418     load_bb();
419     $topic_id = felwptobb( $id );
420     if( $topic_id ) {
421         $topic = get_topic( $topic_id );
422         $posties = $topic->topic_posts;
423         $repliesword = 'replies';
424         if( $posties == 1 )
425             $repliesword = 'reply';
426         $arrrsearches = array('%no%', '%replies%');
427         $arrrreplaces = array($topic->topic_posts, $repliesword );
428         $linktext = str_replace($arrrsearches, $arrrreplaces, $texties );
429         $linkurl = felforumtopiclink( $topic_id );
430         $linktext = "\n\n<a href='$linkurl'>$linktext</a>";
431     }
432 }
433
434 add_action('comments_array', 'replace_comments', 1, 2);
435 function replace_comments( $comments, $post_ID ) {
436     if( felwptobb( $post_ID ) && isset( $comments ) ) {
437         foreach( $comments as $comment )
438             if( $comment->comment_type !== '')
439                 $trackbacks[] = $comment;
440         
441         unset( $comments );
442         foreach( felgetbbreplies() as $reply ) {
443             $com->comment_ID = $reply->post_id;
444             $com->comment_post_ID = $post_ID;
445             $com->user_id = $reply->poster_id;
446             $com->comment_content = $reply->post_text;
447             $com->comment_date = $reply->post_time;
448             $com->comment_author_IP = $reply->poster_ip;
449             $user = get_userdata( $com->user_id );
450             $com->comment_author $user->display_name;
451             $com->comment_author_email $user->user_email;
452             $com->comment_author_url $data->user_url;
453             $comments[] = $com;
454         }
455         $comments = array_merge( (array) $comments, (array) $trackbacks );
456     }
457     return $comments;
458 }
459 ?>
Note: See TracBrowser for help on using the browser.