/
wordbb.php
1395 lines (1122 loc) · 42.3 KB
/
wordbb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package WordBB
* @author Graham
* @version 0.3
*/
/*
Plugin Name: WordBB
Plugin URL: https://github.com/KeanePW/WordBB
Description: WordPress/MyBB Bridge
Author: Graham
Version: 0.3.2
Author URI: https://github.com/KeanePW
*/
require_once('functions.php');
require_once('functions_mybb.php');
require_once('template.php');
if(is_admin())
{
add_action('admin_menu', 'wordbb_plugin_options_menu');
add_action('admin_init', 'wordbb_admin_init');
add_action('admin_head', 'wordbb_admin_head');
}
if(wordbb_init())
{
// plugin hooks
if(is_admin())
{
// init
add_action('admin_menu', 'wordbb_plugin_menu');
// users
add_action('admin_head', 'wordbb_admin_users_update');
add_action('manage_users_custom_column', 'wordbb_users_custom_column', 8, 3);
add_filter('manage_users_columns', 'wordbb_users_columns');
// posts
add_action('publish_post', 'wordbb_publish_post');
add_action('future_to_publish', 'wordbb_publish_post');
add_action('delete_post', 'wordbb_delete_bridge_thread');
add_action('manage_posts_custom_column', 'wordbb_posts_custom_column', 8, 2);
add_filter('manage_posts_columns', 'wordbb_posts_columns');
if(get_option('wordbb_use_mybb_comments')=='on')
{
add_filter('get_comments_number', 'wordbb_get_comments_number');
}
}
else
{
// non-admin enqueues, actions, and filters
add_action('loop_start', 'wordbb_loop_start');
add_action('loop_end', 'wordbb_loop_end');
if(get_option('wordbb_use_mybb_comments')=='on')
{
add_action('pre_comment_on_post','wordbb_comment_on_post');
add_filter('get_comments_number', 'wordbb_get_comments_number');
add_filter('comments_array', 'wordbb_get_comments_array');
if(!isset($_POST['comment_post_ID']))
{
// override comment links only if we're not using
// the wp comment form (which uses get_comment_link()
// to redirect the user to the comments page)
add_filter('get_comment_link', 'wordbb_get_comment_link', 0, 3);
}
add_filter('comment_reply_link', 'wordbb_comment_reply_link', 8, 4);
}
}
add_action('widgets_init', 'wordbb_register_widget');
// add_action('user_register', 'wordbb_user_register');
}
function wordbb_check_config() {
global $wordbb;
$errors=array();
$wordbb_mybb_url=trim(get_option('wordbb_mybb_url'));
if(false===$wordbb_mybb_url || empty($wordbb_mybb_url))
$errors[]='MyBB URL field empty';
$wordbb_mybb_abs=trim(get_option('wordbb_mybb_abs'));
if(false===$wordbb_mybb_abs || empty($wordbb_mybb_abs))
$errors[]='MyBB root folder field empty';
if(!empty($wordbb_mybb_abs))
{
if(!file_exists($wordbb_mybb_abs))
$errors[]='MyBB root folder does not exist';
else if(!file_exists($wordbb_mybb_abs.'/global.php'))
$errors[]='MyBB global.php file not found. Check your MyBB absolute path on server!';
}
/* $wordbb_post_forum=get_option('wordbb_post_forum');
if(false===$wordbb_post_forum || empty($wordbb_post_forum))
$errors[]='Default post forum field empty';*/
if($wordbb->mybb_db_found) {
$wordbb_post_author=get_option('wordbb_post_author');
if(false===$wordbb_post_author || empty($wordbb_post_author))
$errors[]='Default post author field empty';
else
{
if(!wordbb_get_user_info_by_username($wordbb_post_author))
$errors[]='Default post author username does not exist on MyBB';
}
}
if(!empty($errors))
wordbb_set_errors('Configuration is not complete. Go to <a href="options-general.php?page=wordbb-options">WordBB Options</a>',$errors,false);
return (empty($errors));
}
function wordbb_init() {
global $wordbb, $wpdb;
$wordbb = $pdb = new stdClass();
$wordbb->errors=array();
$wordbb->bridges=array();
$wordbb->loop_started=false;
// check if meta table exists, if not create it
$wpdb->query("CREATE TABLE IF NOT EXISTS `wordbb_meta` (`type` enum('cat','post','user'),`wp_id` int(11),`mybb_id` int(11))");
$prefix=get_option('wordbb_dbprefix');
if(empty($prefix))
$prefix='mybb_';
// store mybb db table names
$wordbb->table_forums=$prefix.'forums';
$wordbb->table_forumpermissions=$prefix.'forumpermissions';
$wordbb->table_users=$prefix.'users';
$wordbb->table_posts=$prefix.'posts';
$wordbb->table_threads=$prefix.'threads';
$wordbb->table_settings=$prefix.'settings';
$wordbb->mybb_db_found=wordbb_select_mybb_db();
if($wordbb->mybb_db_found)
{
$configured=wordbb_check_config();
}
wordbb_display_errors();
if($configured)
{
// store mybb url
$wordbb->mybb_url=get_option('wordbb_mybb_url');
// store mybb inc folder path
$wordbb->mybb_inc=get_option('wordbb_mybb_abs');
// wordbb plugin url
$wordbb->plugin_url=get_bloginfo('wpurl'). "/wp-content/plugins/" . plugin_basename(dirname(__FILE__));
// wordbb js url
$wordbb->js_url=$wordbb->plugin_url.'/wordbb.js';
// plugin action file url
$wordbb->action_url=$wordbb->plugin_url.'/actions.php';
// mybb settings
$wordbb->mybbsettings=_wordbb_get_mybb_settings();
// forums array
$wordbb->forums=_wordbb_get_forums();
/*$users=_wordbb_get_users();
$wordbb->users=$users['usernames'];
$wordbb->usersinfo=$users['usersinfo'];*/
// Define the $wordbb_post_author variable if it is not set
if ( isset ( $wordbb_post_author ) == false ) {
$wordbb_post_author = "";
}
// store default author's id
$post_author=wordbb_get_user_info_by_username($wordbb_post_author);
if(!empty($post_author))
$wordbb_post_author=$post_author->uid;
// get currently logged in info (if any)
$mybbuser=$_COOKIE['mybbuser'];
if(isset($mybbuser) && !empty($mybbuser))
{
$mybbuser=explode('_',$mybbuser);
$uid=$mybbuser[0];
$key=$mybbuser[1];
$userinfo=wordbb_get_user_info($uid);
if($userinfo->loginkey==$key)
$wordbb->loggeduserinfo=$userinfo;
}
// store IDs of forums non visible by the currently logged in user
$usergroup=1;
if(!empty($wordbb->loggeduserinfo))
{
$usergroup=(int)$wordbb->loggeduserinfo->usergroup;
}
$exclude_fids=array();
$forums=$wordbb->mybbdb->get_results($wordbb->mybbdb->prepare("SELECT fid FROM {$wordbb->table_forumpermissions} WHERE gid=%d AND (canview=0 OR canviewthreads=0)",$usergroup));
foreach($forums as $forum)
{
$exclude_fids[]=$forum->fid;
}
$wordbb->exclude_fids=$exclude_fids;
// define WP pluggables functions
wordbb_pluggables();
}
return $configured;
}
function wordbb_admin_head() {
global $wordbb;
echo '
<style type="text/css">
ul.wordbb-errors {
list-style-type: disc;
padding-left: 24px;
}
</style>
';
}
function wordbb_admin_init() {
global $wordbb;
// register settings
register_setting( 'wordbb', 'wordbb_post_forum', 'intval' );
register_setting( 'wordbb', 'wordbb_post_author', '' );
register_setting( 'wordbb', 'wordbb_mybb_url' );
register_setting( 'wordbb', 'wordbb_mybb_abs', 'wordbb_mybb_abs_sanitize' );
register_setting( 'wordbb', 'wordbb_dbname' );
register_setting( 'wordbb', 'wordbb_dbuser' );
register_setting( 'wordbb', 'wordbb_dbpass' );
register_setting( 'wordbb', 'wordbb_dbhost' );
register_setting( 'wordbb', 'wordbb_dbprefix' );
register_setting( 'wordbb', 'wordbb_create_thread' );
register_setting( 'wordbb', 'wordbb_create_thread_excerpt' );
register_setting( 'wordbb', 'wordbb_create_thread_post_link' );
register_setting( 'wordbb', 'wordbb_create_thread_post_link_place' );
register_setting( 'wordbb', 'wordbb_create_thread_post_link_text' );
register_setting( 'wordbb', 'wordbb_delete_thread' );
register_setting( 'wordbb', 'wordbb_use_mybb_comments' );
register_setting( 'wordbb', 'wordbb_show_mybb_comments' );
register_setting( 'wordbb', 'wordbb_redirect_mybb' );
register_setting( 'wordbb', 'wordbb_langtoday', 'wordbb_langtoday_sanitize' );
register_setting( 'wordbb', 'wordbb_langyesterday', 'wordbb_langyesterday_sanitize' );
function wordbb_mybb_abs_sanitize($v) { return str_replace('\\', '/', $v); }
function wordbb_langtoday_sanitize($v) { if(empty($v)) $v='Today'; return $v; }
function wordbb_langyesterday_sanitize($v) { if(empty($v)) $v='Yesterday'; return $v; }
// enqueue wordbb js
wp_enqueue_script('wordbb_js',$wordbb->js_url,array('jquery'),'0.1');
}
function wordbb_register_widget()
{
// register_sidebar_widget('WordBB Latest Threads', 'wordbb_widget');
// register_widget_control('WordBB Latest Threads', 'wordbb_widget_control');
$widget_options = array('classname' => 'wordbb_widget', 'description' => "Shows latest MyBB threads/posts on your sidebar." );
wp_register_sidebar_widget('wordbb_widget','WordBB Latest Threads','wordbb_widget',$widget_options);
wp_register_widget_control('wordbb_widget','WordBB Latest Threads','wordbb_widget_control',$widget_options);
}
function wordbb_widget_control()
{
if($_POST['wordbb_widget_submit'])
{
$mode=$_POST['wordbb_widget_mode'];
$title=$_POST['wordbb_widget_title'];
if(!isset($title) || empty($title))
$title='Forums Latest '.($mode=='threads'?'Threads':'Posts');
$exclude=$_POST['wordbb_widget_exclude'];
$usernames=$_POST['wordbb_widget_usernames'];
if(!empty($exclude))
{
$exclude=explode(',',trim($exclude));
$fids=array();
for($i=0; $i<count($exclude); $i++)
{
$fid=intval(trim($exclude[$i]));
if($fid>0)
$fids[]=$fid;
}
$exclude=implode(',',$fids);
}
$count=intval($_POST['wordbb_widget_count']);
if($count<=0)
$count=10;
update_option('wordbb_widget_title',$title);
update_option('wordbb_widget_mode',$mode);
update_option('wordbb_widget_exclude',$exclude);
update_option('wordbb_widget_usernames',$usernames);
update_option('wordbb_widget_count',$count);
}
$title=get_option('wordbb_widget_title');
$mode=get_option('wordbb_widget_mode');
$exclude=get_option('wordbb_widget_exclude');
$usernames=get_option('wordbb_widget_usernames');
$count=get_option('wordbb_widget_count');
?>
<ul>
<li><?php _e('Title', 'wordbb'); ?>
<input style="width: 100%" id="wordbb_widget_title" name="wordbb_widget_title" type="text" value="<?php echo $title; ?>" />
</li>
<li><?php _e('Mode', 'wordbb'); ?>
<select style="width: 100%" id="wordbb_widget_mode" name="wordbb_widget_mode">
<option value="threads" <?php if($mode=='threads') echo 'selected' ?>>Latest threads</option>
<option value="posts" <?php if($mode=='posts') echo 'selected' ?>>Latest posts</option>
</select>
</li>
<li><?php _e('Max entry count', 'wordbb'); ?>
<input style="width: 100%" id="wordbb_widget_count" name="wordbb_widget_count" type="text" value="<?php echo $count ?>" />
</li>
<li><?php _e('Exclude forums', 'wordbb'); ?>
<span style="font-size: 6pt">(comma separated list of forum IDs)</span>
<input style="width: 100%" id="wordbb_widget_exclude" name="wordbb_widget_exclude" type="text" value="<?php echo $exclude ?>" />
</li>
<li><?php _e('Show usernames', 'wordbb'); ?>
<input id="wordbb_widget_usernames" name="wordbb_widget_usernames" type="checkbox" <?php if($usernames) : ?>checked="checked"<?php endif ?> />
</li>
</ul>
<input style="width: 100%" type="hidden" id="wordbb_widget_submit" name="wordbb_widget_submit" value="1" />
<?php
}
function wordbb_widget($args)
{
global $wordbb;
extract($args);
$mode=get_option('wordbb_widget_mode');
$title=get_option('wordbb_widget_title');
$exclude=get_option('wordbb_widget_exclude');
$usernames=get_option('wordbb_widget_usernames');
$count=get_option('wordbb_widget_count');
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
$entries=($mode=='threads')?wordbb_get_latest_threads($count,$exclude):wordbb_get_latest_posts($count,$exclude);
if(is_array($entries))
?>
<ul>
<?php
foreach($entries as $entry)
{
?>
<li>
<?php if($mode=='threads') : ?>
<a href="<?php echo $wordbb->mybb_url.'/showthread.php?tid='.$entry->tid ?>"><?php echo $entry->subject ?></a>
<?php else : ?>
<a href="<?php echo $wordbb->mybb_url.'/showthread.php?tid='.$entry->tid.'&pid='.$entry->pid.'#pid'.$entry->pid ?>"><?php echo $entry->subject ?></a>
<?php endif ?>
<?php if($usernames) : ?>
by <a href="<?php echo $wordbb->mybb_url ?>/member.php?action=profile&uid=<?php echo $entry->uid ?>"><?php echo $entry->username ?></a>
<?php endif ?>
</li>
<?php
}
?>
</ul>
<?php
echo $after_widget;
}
function wordbb_pluggables() {
if(!function_exists('wp_sanitize_redirect')) :
function wp_sanitize_redirect($location) {
$location = preg_replace('|^a-z0-9-~+_.?#=&;,/:%!|i', '', $location);
$location = wp_kses_no_null($location);
// remove %0d and %0a from location
$strip = array('%0d', '%0a');
$found = true;
while($found) {
$found = false;
foreach( (array) $strip as $val ) {
while(strpos($location, $val) !== false) {
$found = true;
$location = str_replace($val, '', $location);
}
}
}
return $location;
}
endif;
}
function wordbb_error_handling($location)
{
global $wordbb;
$errors_title=$wordbb->errors['title'];
$errors_array=$wordbb->errors['errors'];
$errors='';
if(is_array($errors_array))
{
$errors.='errors_title='.urlencode($errors_title).'&';
foreach($errors_array as $wordbb_error)
{
$errors.='errors[]='.urlencode($wordbb_error).'&';
}
$errors=rtrim($errors,'&');
}
if(!empty($errors))
$location.='&'.$errors;
return $location;
}
function wordbb_set_errors($title,$errors,$getparams=true) {
global $wordbb;
$wordbb->errors=array('title'=>$title,'errors'=>$errors);
if($getparams)
add_filter('wp_redirect',wordbb_error_handling);
}
function wordbb_display_errors() {
global $wordbb;
if(empty($wordbb->errors))
{
$wordbb->errors['title'] = isset($_GET['errors_title']) ? $_GET['errors_title'] : NULL;
$wordbb->errors['errors'] = isset($_GET['errors']) ? $_GET['errors'] : NULL;
}
// display error messages
if(isset($wordbb->errors['errors']))
{
function wordbb_error() {
global $wordbb;
$errors_message='<ul class="wordbb-errors">';
foreach($wordbb->errors['errors'] as $error)
{
$errors_message.='<li>'.$error.'</li>';
}
$errors_message.='</ul>';
echo "
<div id='wordbb-error' class='updated fade'><p><strong>".__('WordBB errors — “<em>'.$wordbb->errors['title'].'</em>” ')."</strong><br /><br />".$errors_message."</p></div>
";
}
add_action('admin_notices', 'wordbb_error');
}
}
function wordbb_do_action($action,$params)
{
global $wordbb;
$params['wordbb_mybb_abs']=get_option('wordbb_mybb_abs');
$params['_wordbbnonce']=wordbb_create_nonce($action);
// uses parse_url to validate data
// tnx to Tikitiki
$url = parse_url($wordbb->action_url);
if(!$url['host'])
{
return false;
}
if(!$url['port'])
{
$url['port'] = 80;
}
if(!$url['path'])
{
$url['path'] = "/";
}
if($url['query'])
{
$url['path'] .= "?{$url['path']}";
}
$reqbody = "";
$reqbody.= "action=".urlencode($action);
foreach($params as $key=>$val)
{
if (!empty($reqbody)) $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}
$contentlength = strlen("$reqbody\r\n"); // tnx to Rog
// tnx to mike for letting me discover the Host \r\n bug
$reqheader = "POST {$url['path']} HTTP/1.0\r\n".
"Host: {$url['host']}\r\n". "User-Agent: PostIt\r\n". "Connection: Close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";
$socket = @fsockopen($url['host'], $url['port'], $errno, $errstr);
@stream_set_timeout($socket, 10);
if(!$socket)
{
return false;
}
fputs($socket, $reqheader);
while(!feof($socket))
{
$result .= fgets($socket, 12800);
}
fclose($socket);
//separate header and content
$data = explode("\r\n\r\n", $result, 2);
//$result=array('header'=>$data[0],'body'=>$data[1]);
$result=unserialize($data[1]);
if($result===false)
$result=$data[1];
return $result;
}
function wordbb_plugin_menu() {
add_posts_page('WordBB Categories', 'WordBB Categories', 'edit_pages', 'wordbb-categories', 'wordbb_categories_page');
}
function wordbb_plugin_options_menu() {
add_options_page('WordBB Options', 'WordBB Options', 'edit_pages', 'wordbb-options', 'wordbb_options_page');
}
function wordbb_options_page() {
global $wordbb;
?>
<style type="text/css">
.form-table th {
width: 30%;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
function updatePostLink() {
$('.wordbb_create_thread_post_link_place').attr('disabled',!$('#wordbb_create_thread_post_link').attr('checked'));
}
updatePostLink();
$('#wordbb_create_thread_post_link').click(updatePostLink);
});
</script>
<div class="wrap">
<h2>WordBB Options</h2>
<p style="margin-left: 8px"><em><?php echo __('Welcome in WordBB\'s configuration panel. Here you can tweak all the plugin settings to take full advantage of the bridge.<br />'); ?></em></p>
<form method="post" action="options.php">
<h3>MyBB data</h3>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="wordbb_mybb_url">MyBB URL</label></th>
<td><input type="text" id="wordbb_mybb_url" name="wordbb_mybb_url" size="40" value="<?php echo get_option('wordbb_mybb_url'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_mybb_abs">MyBB absolute path on server</label></th>
<td><input type="text" id="wordbb_mybb_abs" name="wordbb_mybb_abs" size="40" value="<?php echo get_option('wordbb_mybb_abs'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_dbname">MyBB DB name (optional) (*)</label></th>
<td><input type="text" id="wordbb_dbname" name="wordbb_dbname" size="40" value="<?php echo get_option('wordbb_dbname'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_dbuser">MyBB DB username (optional) (*)</label></th>
<td><input type="text" id="wordbb_dbuser" name="wordbb_dbuser" size="40" value="<?php echo get_option('wordbb_dbuser'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_dbpass">MyBB DB password (optional) (*)</label></th>
<td><input type="password" id="wordbb_dbpass" name="wordbb_dbpass" size="40" value="<?php echo get_option('wordbb_dbpass'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_dbhost">MyBB DB host (optional) (*)</label></th>
<td><input type="text" id="wordbb_dbhost" name="wordbb_dbhost" size="40" value="<?php echo get_option('wordbb_dbhost'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_dbprefix">MyBB DB prefix (optional) (*)</label></th>
<td><input type="text" id="wordbb_dbprefix" name="wordbb_dbprefix" size="40" value="<?php echo get_option('wordbb_dbprefix'); ?>" /></td>
</tr>
</table>
<h3>Bridge settings and comment system</h3>
<table class="form-table" border="1">
<tr valign="top">
<th scope="row"><label for="wordbb_create_thread">Create MyBB thread on WP post publish</label></th>
<td><input type="checkbox" id="wordbb_create_thread" name="wordbb_create_thread" <?php if(get_option('wordbb_create_thread')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_create_thread_excerpt">Use post excerpt instead of full post as thread's message</label></th>
<td><input type="checkbox" id="wordbb_create_thread_excerpt" name="wordbb_create_thread_excerpt" <?php if(get_option('wordbb_create_thread_excerpt')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_create_thread_post_link">Add a link to the original WordPress post in the thread's post</label></th>
<td>
<input type="checkbox" id="wordbb_create_thread_post_link" name="wordbb_create_thread_post_link" <?php if(get_option('wordbb_create_thread_post_link')=='on') echo 'checked'; ?> />
<input type="radio" id="wordbb_create_thread_post_link_place_before" class="wordbb_create_thread_post_link_place" name="wordbb_create_thread_post_link_place" value="before" <?php if(get_option('wordbb_create_thread_post_link_place')=='before') echo 'checked'; ?> /> <label for="wordbb_create_thread_post_link_place_before">before content</label>
<input type="radio" id="wordbb_create_thread_post_link_place_after" class="wordbb_create_thread_post_link_place" name="wordbb_create_thread_post_link_place" value="after" <?php if(get_option('wordbb_create_thread_post_link_place')=='after') echo 'checked'; ?> /> <label for="wordbb_create_thread_post_link_place_after">after content</label>
<br />
<label for="wordbb_create_thread_post_link_text">Link text (<code>%title%</code> will be replaced with the post's title)</label><br />
<input type="text" id="wordbb_create_thread_post_link_text" name="wordbb_create_thread_post_link_text" size="30" value="<?php echo get_option('wordbb_create_thread_post_link_text'); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_delete_thread">Delete MyBB thread on WP post deletion</label></th>
<td><input type="checkbox" id="wordbb_delete_thread" name="wordbb_delete_thread" <?php if(get_option('wordbb_delete_thread')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_use_mybb_comments">Use MyBB as comment system (**)</label></th>
<td><input type="checkbox" id="wordbb_use_mybb_comments" name="wordbb_use_mybb_comments" <?php if(get_option('wordbb_use_mybb_comments')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_show_mybb_comments">Show MyBB posts as comments on WordPress (***)</label></th>
<td><input type="checkbox" id="wordbb_show_mybb_comments" name="wordbb_show_mybb_comments" <?php if(get_option('wordbb_show_mybb_comments')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_redirect_mybb">Redirect to MyBB thread when using WP's comment form (will redirect to WP comments if unchecked)</label></th>
<td><input type="checkbox" id="wordbb_redirect_mybb" name="wordbb_redirect_mybb" <?php if(get_option('wordbb_redirect_mybb')=='on') echo 'checked'; ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_post_forum">Default post forum</label></th>
<td>
<?php
$html=wordbb_get_array_html($wordbb->forums,'wordbb_post_forum',get_option('wordbb_post_forum'),'',array(),'fid');
if(empty($html))
echo '(mybb forums table not available)';
else
echo $html;
?>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_post_author">Default post author</label></th>
<td>
<input type="text" id="wordbb_post_author" name="wordbb_post_author" value="<?php echo get_option('wordbb_post_author') ?>" />
</td>
</tr>
</table>
<h3>Friendly last visit</h3>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="wordbb_langtoday">Language "Today" string</label></th>
<td><input type="text" id="wordbb_langtoday" name="wordbb_langtoday" size="40" value="<?php echo get_option('wordbb_langtoday'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="wordbb_langyesterday">Language "Yesterday" string</label></th>
<td><input type="text" id="wordbb_langyesterday" name="wordbb_langyesterday" size="40" value="<?php echo get_option('wordbb_langyesterday'); ?>" /></td>
</tr>
</table>
<?php
settings_fields( 'wordbb' );
?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
<p>
* Leaving the database fields empty, the plugin will assume that MyBB is installed on the same database as WordPress.
</p>
<p>
** When this option is active, comments links will redirect to the corresponding MyBB threads (if any).
</p>
<p>*** When this option is active, MyBB posts will be displayed as WordPress comments in WP blog posts.<br />
<?php if(0) : ?>
<strong>WARNING</strong>: Note that your comment form will still use WordPress' comment system, so if MyBB posts are displayed as comments on a specific post, any comment sent on that post will not be displayed when this option is active. You may want to encapsulate the comment form in your WordPress theme in an if statement using the <code>wordbb_get_thread_id()</code> function, e.g. <code>if(!wordbb_get_thread_id()) { // show WP comment form } else { // show link to MyBB thread using wordbb_thread_link() }</code>. For more details, please <a href="http://valadilene.org/wordbb/wordbb-template-changes">refer to this page</a>.
<?php endif ?>
</p>
<br />
<p style="margin-left: 8px"><em><?php echo __('WordBB - <a href="http://valadilene.org/wordbb" target="_blank">http://valadilene.org/wordbb</a> (<a href="http://valadilene.org/wordbb/wordbb-documentation" target="_blank">documentation</a>) (<a href="http://valadilene.org/wordbb/wordbb-changelog" target="_blank">changelog</a>) - <a href="http://twitter.com/monochromenight" target="_blank">Follow me</a> on Twitter!<br />If you find this plugin useful or want to motivate me in my projects, please send me some pizza money! You can use the form in the sidebar over at <a href="http://valadilene.org">http://valadilene.org/wordbb</a> or the button below. Thanks!'); ?></em></p>
<p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="7033720">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</p>
</div>
<?php
}
function wordbb_categories_page() {
global $wordbb, $wpdb;
?>
<div class="wrap">
<h2>WordBB Categories</h2>
<p style="margin-left: 8px"><em><?php echo __('Here you can link WordPress categories to MyBB forums. Every blog post published in a particular category will be duplicated in the corresponding MyBB section.<br />Do not forget to activate HTML code on the MyBB forums you link to WordPress.<br />'); ?></em></p>
<form method="post" action="<?php echo $wordbb->action_url ?>">
<div class="tablenav">
<?php
$taxonomy='category';
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 0;
if ( empty($pagenum) )
$pagenum = 1;
$tags_per_page = (int) get_user_option( 'edit_' . $taxonomy . '_per_page' );
if ( empty($tags_per_page) || $tags_per_page < 1 )
$tags_per_page = 20;
$tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); // Old filter
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil(wp_count_terms($taxonomy) / $tags_per_page),
'current' => $pagenum
));
if ( $page_links )
echo "<div class='tablenav-pages'>$page_links</div>";
?>
<br class="clear" />
</div>
<div class="clear"></div>
<?php
$args = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 'exclude' => $exclude,
'hide_empty' => false, 'include_last_update_time' => false, 'hierarchical' => true, 'pad_counts' => true);
$cats = get_categories($args);
?>
<table class="widefat fixed" cellspacing="0">
<thead>
<tr>
<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
<th scope="col" id="name" class="manage-column column-name" style="">Name</th>
<th scope="col" id="description" class="manage-column column-description" style="">Description</th>
<th scope="col" id="slug" class="manage-column column-slug" style="">Slug</th>
<th scope="col" id="mybb-forum" class="manage-column column-mybb-forum num" style="">MyBB Forum</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
<th scope="col" class="manage-column column-name" style="">Name</th>
<th scope="col" class="manage-column column-description" style="">Description</th>
<th scope="col" class="manage-column column-slug" style="">Slug</th>
<th scope="col" class="manage-column column-mybb-forum num" style="">MyBB Forum</th>
</tr>
</tfoot>
<tbody id="the-list" class="list:cat">
<?php
foreach($cats as $cat) :
$class=$class==''?' class="alternate"':'';
$bridge=wordbb_get_bridge(WORDBB_CAT,$cat->cat_ID);
if($bridge)
{
$cat_forum=$bridge->mybb_id;
}
$html=wordbb_get_array_html($wordbb->forums,"wordbb_cat_forums[$cat->cat_ID]",$cat_forum,'(default)',array(get_option('wordbb_post_forum')),'fid');
?>
<tr id="tag-<?php echo $cat->cat_ID ?>"<?php echo $class ?>>
<th scope="row" class="check-column"> <input type="checkbox" name="delete_tags[]" value="<?php echo $cat->cat_ID ?>" /></th>
<td class="name column-name"><strong><a class="row-title" href="edit-tags.php?action=edit&taxonomy=category&post_type=&tag_ID=<?php echo $cat->cat_ID ?>" title="Edit “<?php echo $cat->name ?>”"><?php echo $cat->name ?></a></strong><br /><div class="row-actions"><span class='edit'><a href="edit-tags.php?action=edit&taxonomy=category&post_type=&tag_ID=<?php echo $cat->cat_ID ?>">Edit</a> | </span><span class='inline hide-if-no-js'><a href="#" class="editinline">Quick Edit</a> | </span><span class='delete'><a class='delete-tag' href='edit-tags.php?action=delete&taxonomy=category&tag_ID=<?php echo $cat->cat_ID ?>&_wpnonce=a97b9a530a'>Delete</a></span></div><div class="hidden" id="inline_<?php echo $cat->cat_ID ?>"><div class="name"><?php echo $cat->name ?></div><div class="slug"><?php echo $cat->slug ?></div><div class="parent">0</div></div></td>
<td class="description column-description"></td>
<td class="slug column-slug"><?php echo $cat->slug ?></td>
<td class="mybb-forum column-mybb-forum"><?php echo $html ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<input type="hidden" name="action" value="save_categories" />
<?php wp_nonce_field( 'wordbb_save_categories' ) ?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
</div>
<?php
}
/*function wordbb_cat_columns($defaults) {
$defaults['wordbb_cat_forum'] = __('MyBB forum');
return $defaults;
}
function wordbb_cat_custom_column($value, $column_name, $id) {
global $wordbb;
switch( $column_name ) {
case 'wordbb_cat_forum':
{
$bridge=wordbb_get_bridge(WORDBB_CAT,$id);
if($bridge)
{
$cat_forum=$bridge->mybb_id;
}
return wordbb_get_array_html($wordbb->forums,"wordbb_cat_forums[$id]",$cat_forum,'(default)',array(get_option('wordbb_post_forum')),'fid');
}
break;
}
}*/
function wordbb_get_array_html($array,$name,$default='',$blank='',$exclude=array(),$id_text='id')
{
global $wordbb;
if(!$array)
return false;
$html='';
$html.='<select id="'.$name.'" name="'.$name.'">';
$html.='<option value="">'.$blank.'</option>';
foreach($array as $k=>$v) {
if(!empty($exclude) && is_array($exclude) && array_search($k,$exclude)!==false)
continue;
$html.='<option value="'.$k.'"';
if(!empty($default) && $k==$default) $html.=' selected="selected"';
$id=(!empty($k))?" ({$id_text} {$k})":'';
$html.='>'.$v.$id.'</option>';
}
$html.='</select>';
return $html;
}
function wordbb_filter_post_content($content)
{
// remove more tag
$content=str_replace('<!--more-->','',$content);
return $content;
}
function wordbb_get_post_teaser($post)
{
$content=$post->post_content;
$excerpt=$post->post_excerpt;
if(!empty($excerpt))
return $excerpt;
// remove more tag
$content=explode('<!--more-->',$content);
return $content[0];
}
function wordbb_publish_post($id)
{
global $wordbb;
if(get_option('wordbb_create_thread')!="on")
return;
wordbb_bridge_wp_post($id);
}
function wordbb_bridge_wp_post($id)
{
$post_bridge=wordbb_get_bridge(WORDBB_POST,$id);
$post=get_post($id);
if(get_option('wordbb_create_thread_excerpt')=="on")
{
$post_content=wordbb_get_post_teaser($post);
}
else
{
$post_content=wordbb_filter_post_content($post->post_content);
}
if(get_option('wordbb_create_thread_post_link')=="on")
{
$link_text=get_option('wordbb_create_thread_post_link_text');
if(empty($link_text))
{
$link_text=__('Read the full article on the blog.');
}
$link_text=str_replace('%title%',$post->post_title,$link_text);
$link='<a class="wordbb-full-post" href="'.get_permalink($id).'" title="'.$post->post_title.'">'.$link_text.'</a>';
if(get_option('wordbb_create_thread_post_link_place')=='before')
{
$post_content=$link.'<br />'.$post_content;
}
else
{
$post_content=$post_content.'<br />'.$link;
}
}
$categories=get_the_category($post->ID);
foreach($categories as $category)
{
// get mybb forum corresponding to wp post category
$fid=false;
$bridge=wordbb_get_bridge(WORDBB_CAT,$category->cat_ID);
if($bridge)
$fid=$bridge->mybb_id;
if(empty($fid))
{
// if a bridge was not found, use default forum
$fid=get_option('wordbb_post_forum');