weblog-content /wp-content/plugins/buddypress/bp-forums/bbpress/bb-includes/functions.bb-template.php

Language PHP Lines 3528
MD5 Hash f5793dae2e6540864f6f0d5d2c8742f5 Estimated Cost $79,854 (why?)
Repository https://bitbucket.org/openfarmtech/weblog-content View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
<?php

function bb_load_template( $files, $globals = false, $action_arg = null )
{
	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,
		$del_class, $bb_alt;

	if ( $globals ) {
		foreach ( $globals as $global => $v ) {
			if ( !is_numeric($global) ) {
				$$global = $v;
			} else {
				global $$v;
			}
		}
	}

	$files = (array) $files;
	$template = false;
	$default_template = false;
	$file_used = false;
	$default_file_used = false;

	foreach ( $files as $file ) {
		do_action( 'bb_' . $file, $action_arg );
		if ( false !== $template = bb_get_template( $file, false ) ) {
			$file_used = $file;
			break;
		}
		if ( !$default_template ) {
			if ( false !== $default_template = bb_get_default_template( $file ) ) {
				$default_file_used = $file;
			}
		}
	}

	if ( !$template && $default_template ) {
		$template = $default_template;
		$file_used = $default_file_used;
	}

	$template = apply_filters( 'bb_template', $template, $file_used );
	include( $template );

	do_action( 'bb_after_' . $file_used, $action_arg );
}

function bb_get_template( $file, $default = true )
{
	global $bb;
	// Skip theme loading in "safe" mode
	if ( !isset( $bb->safemode ) || $bb->safemode !== true ) {
		if ( file_exists( bb_get_active_theme_directory() .  $file ) ) {
			return bb_get_active_theme_directory() .  $file;
		}
	}

	if ( !$default ) {
		return false;
	}

	return bb_get_default_template( $file );
}

function bb_get_default_template( $file )
{
	if ( file_exists( BB_DEFAULT_THEME_DIR . $file ) ) {
		return BB_DEFAULT_THEME_DIR . $file;
	}
}

function bb_get_header()
{
	bb_load_template( 'header.php' );
}

function bb_language_attributes( $xhtml = 0 )
{
	$output = '';
	if ( $dir = bb_get_option( 'text_direction' ) ) {
		$output = 'dir="' . $dir . '" ';
	}
	if ( $lang = bb_get_option( 'language' ) ) {
		$output .= 'xml:lang="' . $lang . '" ';
		if ( $xhtml < '1.1' ) {
			$output .= 'lang="' . $lang . '"';
		}
	}

	echo ' ' . rtrim( $output );
}

function bb_generator( $type = 'xhtml' )
{
	if ( !$type ) {
		$type = 'xhtml';
	}
	echo apply_filters( 'bb_generator', bb_get_generator( $type ) . "\n", $type );
}

function bb_get_generator( $type = 'xhtml' )
{
	if ( !$type ) {
		$type = 'xhtml';
	}
	switch ( $type ) {
		case 'html':
			$gen = '<meta name="generator" content="bbPress ' . bb_get_option( 'version' ) . '">';
			break;
		case 'xhtml':
			$gen = '<meta name="generator" content="bbPress ' . bb_get_option( 'version' ) . '" />';
			break;
		case 'atom':
			$gen = '<generator uri="http://bbpress.org/" version="' . bb_get_option( 'version' ) . '">bbPress</generator>';
			break;
		case 'rss2':
			$gen = '<generator>http://bbpress.org/?v=' . bb_get_option( 'version' ) . '</generator>';
			break;
		case 'rdf':
			$gen = '<admin:generatorAgent rdf:resource="http://bbpress.org/?v=' . bb_get_option( 'version' ) . '" />';
			break;
		case 'comment':
			$gen = '<!-- generator="bbPress/' . bb_get_option( 'version' ) . '" -->';
			break;
		case 'export':
			$gen = '<!-- generator="bbPress/' . bb_get_option( 'version' ) . '" created="'. date( 'Y-m-d H:i' ) . '"-->';
			break;
	}
	return apply_filters( 'bb_get_generator', $gen, $type );
}

function bb_stylesheet_uri( $stylesheet = '' )
{
	echo esc_html( bb_get_stylesheet_uri( $stylesheet ) );
}

function bb_get_stylesheet_uri( $stylesheet = '' )
{
	if ( 'rtl' == $stylesheet ) {
		$css_file = 'style-rtl.css';
	} else {
		$css_file = 'style.css';
	}

	$active_theme = bb_get_active_theme_directory();

	if ( file_exists( $active_theme . $css_file ) ) {
		$r = bb_get_active_theme_uri() . $css_file;
	} else {
		$r = BB_DEFAULT_THEME_URL . $css_file;
	}

	return apply_filters( 'bb_get_stylesheet_uri', $r, $stylesheet );
}

function bb_active_theme_uri()
{
	echo bb_get_active_theme_uri();
}

function bb_get_active_theme_uri()
{
	global $bb;
	// Skip theme loading in "safe" mode
	if ( isset( $bb->safemode ) && $bb->safemode === true ) {
		$active_theme_uri = BB_DEFAULT_THEME_URL;
	} elseif ( !$active_theme = bb_get_option( 'bb_active_theme' ) ) {
		$active_theme_uri = BB_DEFAULT_THEME_URL;
	} else {
		$active_theme_uri = bb_get_theme_uri( $active_theme );
	}
	return apply_filters( 'bb_get_active_theme_uri', $active_theme_uri );
}

function bb_get_theme_uri( $theme = false )
{
	global $bb;
	if ( preg_match( '/^([a-z0-9_-]+)#([a-z0-9_-]+)$/i', $theme, $_matches ) ) {
		$theme_uri = $bb->theme_locations[$_matches[1]]['url'] . $_matches[2] . '/';
	} else {
		$theme_uri = $bb->theme_locations['core']['url'];
	}
	return apply_filters( 'bb_get_theme_uri', $theme_uri, $theme );
}

function bb_get_footer()
{
	bb_load_template( 'footer.php' );
}

function bb_head()
{
	do_action('bb_head');
}

/**
 * Display the link to the Really Simple Discovery service endpoint.
 *
 * @link http://archipelago.phrasewise.com/rsd
 * @since 1.0
 */
function bb_rsd_link() {
	if (bb_get_option('enable_xmlrpc'))
		echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . bb_get_uri('xmlrpc.php', 'rsd', BB_URI_CONTEXT_LINK_OTHER + BB_URI_CONTEXT_BB_XMLRPC) . '" />' . "\n";
}

/**
 * Display the link to the pingback service endpoint.
 *
 * @since 1.0
 */
function bb_pingback_link() {
	if (bb_get_option('enable_pingback'))
		echo '<link rel="pingback" href="' . bb_get_uri('xmlrpc.php', null, BB_URI_CONTEXT_LINK_OTHER + BB_URI_CONTEXT_BB_XMLRPC) . '" />' . "\n";
}

function profile_menu() {
	global $user_id, $profile_menu, $self, $profile_page_title;
	$list  = "<ul id='profile-menu'>";
	$list .= "\n\t<li" . ( ( $self ) ? '' : ' class="current"' ) . '><a href="' . esc_attr( get_user_profile_link( $user_id ) ) . '">' . __('Profile') . '</a></li>';
	$id = bb_get_current_user_info( 'id' );
	foreach ($profile_menu as $item) {
		// 0 = name, 1 = users cap, 2 = others cap, 3 = file
		$class = '';
		if ( $item[3] == $self ) {
			$class = ' class="current"';
			$profile_page_title = $item[0];
		}
		if ( bb_can_access_tab( $item, $id, $user_id ) )
			if ( file_exists($item[3]) || is_callable($item[3]) )
				$list .= "\n\t<li$class><a href='" . esc_attr( get_profile_tab_link($user_id, $item[4]) ) . "'>{$item[0]}</a></li>";
	}
	$list .= "\n</ul>";
	echo $list;
}

function login_form() {
	if ( bb_is_user_logged_in() )
		bb_load_template( 'logged-in.php' );
	else
		bb_load_template( 'login-form.php', array('user_login', 'remember_checked', 'redirect_to', 're') );
}

function search_form( $q = '' ) {
	bb_load_template( 'search-form.php', array('q' => $q) );
}

function bb_post_template() {
	bb_load_template( 'post.php' );
}

function post_form( $args = array() ) {
	global $page, $topic, $forum;

	if ( is_string( $args ) ) {
		$args['h2'] = $args;
	}
	$defaults = array(
		'h2' => '',
		'last_page_only' => true
	);
	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	if ( isset( $forum->forum_is_category ) && $forum->forum_is_category ) {
		return;
	}

	$add = topic_pages_add();
	if ( empty( $h2 ) && false !== $h2 ) {
		if ( bb_is_topic() ) {
			$h2 = __( 'Reply' );
		} elseif ( bb_is_forum() ) {
			$h2 = __( 'New Topic in this Forum' );
		} elseif ( bb_is_tag() || bb_is_front() ) {
			$h2 = __( 'Add New Topic' );
		}
	}

	$last_page = bb_get_page_number( ( isset( $topic->topic_posts ) ? $topic->topic_posts : 0 ) + $add );

	if ( !empty( $h2 ) ) {
		if ( bb_is_topic() && ( $page != $last_page && $last_page_only ) ) {
			$h2 = '<a href="' . esc_attr( get_topic_link( 0, $last_page ) . '#postform' ) . '">' . $h2 . ' &raquo;</a>';
		}
		echo '<h2 class="post-form">' . $h2 . '</h2>' . "\n";
	}

	do_action( 'pre_post_form' );

	if (
		( bb_is_topic() && bb_current_user_can( 'write_post', $topic->topic_id ) && ( $page == $last_page || !$last_page_only ) ) ||
		( !bb_is_topic() && bb_current_user_can( 'write_topic', isset( $forum->forum_id ) ? $forum->forum_id : 0 ) )
	) {
		echo '<form class="postform post-form" id="postform" method="post" action="' . bb_get_uri( 'bb-post.php', null, BB_URI_CONTEXT_FORM_ACTION ) . '">' . "\n";
		echo '<fieldset>' . "\n";
		bb_load_template( 'post-form.php', array('h2' => $h2) );
		bb_nonce_field( bb_is_topic() ? 'create-post_' . $topic->topic_id : 'create-topic' );
		if ( bb_is_forum() ) {
			echo '<input type="hidden" name="forum_id" value="' . $forum->forum_id . '" />' . "\n";
		} elseif ( bb_is_topic() ) {
			echo '<input type="hidden" name="topic_id" value="' . $topic->topic_id . '" />' . "\n";
		}
		do_action( 'post_form' );
		echo "\n</fieldset>\n</form>\n";
	} elseif ( !bb_is_user_logged_in() ) {
		echo '<p>';
		printf(
			__('You must <a href="%s">log in</a> to post.'),
			esc_attr( bb_get_uri( 'bb-login.php', null, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_USER_FORMS ) )
		);
		echo '</p>';
	}

	do_action( 'post_post_form' );
}

function edit_form() {
	global $bb_post;
	do_action('pre_edit_form');
	echo '<form class="postform edit-form" method="post" action="' . bb_get_uri('bb-edit.php', null, BB_URI_CONTEXT_FORM_ACTION)  . '">' . "\n";
	echo '<fieldset>' . "\n";
	bb_load_template( 'edit-form.php', array('topic_title') );
	bb_nonce_field( 'edit-post_' . $bb_post->post_id );
	do_action('edit_form');
	if ($_REQUEST['view'] === 'all')
		echo "\n" . '<input type="hidden" name="view" value="all" />';
	echo "\n" . '</fieldset>' . "\n" . '</form>' . "\n";
	do_action('post_edit_form');
}

function alt_class( $key, $others = '' ) {
	echo get_alt_class( $key, $others );
}

function get_alt_class( $key, $others = '' ) {
	global $bb_alt;
	$class = '';
	if ( !isset( $bb_alt[$key] ) ) $bb_alt[$key] = -1;
	++$bb_alt[$key];
	$others = trim($others);
	if ( $others xor $bb_alt[$key] % 2 )
		$class = ' class="' . ( ($others) ? $others : 'alt' ) . '"';
	elseif ( $others && $bb_alt[$key] % 2 )
		$class = ' class="' . $others . ' alt"';
	return $class;
}

function bb_location() {
	echo apply_filters( 'bb_location', bb_get_location() );
}

function bb_get_location() { // Not for display.  Do not internationalize.
	static $file;
	static $filename;

	if ( !isset( $file ) ) {
		$path = '';
		foreach ( array( $_SERVER['SCRIPT_NAME'], $_SERVER['SCRIPT_FILENAME'], $_SERVER['PHP_SELF'] ) as $_path ) {
			if ( false !== strpos( $_path, '.php' ) ) {
				$path = $_path;
				break;
			}
		}

		$filename = bb_find_filename( $path );
		// Make $file relative to bbPress root directory
		$file = str_replace( bb_get_option( 'path' ), '', $path );
	}

	switch ( $filename ) {
		case 'index.php':
		case 'page.php':
			$location = 'front-page';
			break;
		case 'forum.php':
			$location = 'forum-page';
			break;
		case 'tags.php':
			$location = 'tag-page';
			break;
		case 'edit.php':
			$location = 'topic-edit-page';
			break;
		case 'topic.php':
			$location = 'topic-page';
			break;
		case 'rss.php':
			$location = 'feed-page';
			break;
		case 'search.php':
			$location = 'search-page';
			break;
		case 'profile.php':
			$location = 'profile-page';
			break;
		case 'favorites.php':
			$location = 'favorites-page';
			break;
		case 'view.php':
			$location = 'view-page';
			break;
		case 'statistics.php':
			$location = 'stats-page';
			break;
		case 'bb-login.php':
			$location = 'login-page';
			break;
		case 'register.php':
			$location = 'register-page';
			break;
		default:
			$location = apply_filters( 'bb_get_location', '', $file );
			break;
	}

	return $location;
}

function bb_is_front() {
	return 'front-page' == bb_get_location();
}

function bb_is_forum() {
	return 'forum-page' == bb_get_location();
}

function bb_is_tags() {
	return 'tag-page' == bb_get_location();
}

function bb_is_tag() {
	global $tag, $tag_name;
	return $tag && $tag_name && bb_is_tags();
}

function bb_is_topic_edit() {
	return 'topic-edit-page' == bb_get_location();
}

function bb_is_topic() {
	return 'topic-page' == bb_get_location();
}

function bb_is_feed() {
	return 'feed-page' == bb_get_location();
}

function bb_is_search() {
	return 'search-page' == bb_get_location();
}

function bb_is_profile() {
	return 'profile-page' == bb_get_location();
}

function bb_is_favorites() {
	return 'favorites-page' == bb_get_location();
}

function bb_is_view() {
	return 'view-page' == bb_get_location();
}

function bb_is_statistics() {
	return 'stats-page' == bb_get_location();
}

function bb_is_admin() {
	if ( defined('BB_IS_ADMIN') )
		return BB_IS_ADMIN;
	return false;
}

function bb_title( $args = '' ) {
	echo apply_filters( 'bb_title', bb_get_title( $args ) );
}

function bb_get_title( $args = '' ) {
	$defaults = array(
		'separator' => ' &laquo; ',
		'order' => 'normal',
		'front' => ''
	);
	$args = wp_parse_args( $args, $defaults );
	$title = array();
	
	switch ( bb_get_location() ) {
		case 'front-page':
			if ( !empty( $args['front'] ) )
				$title[] = $args['front'];
			break;
		
		case 'topic-edit-page':
		case 'topic-page':
			$title[] = get_topic_title();
			break;
		
		case 'forum-page':
			$title[] = get_forum_name();
			break;
		
		case 'tag-page':
			if ( bb_is_tag() )
				$title[] = esc_html( bb_get_tag_name() );
			
			$title[] = __('Tags');
			break;
		
		case 'profile-page':
			$title[] = get_user_display_name();
			break;
		
		case 'view-page':
			$title[] = get_view_name();
			break;
	}
	
	if ( $st = bb_get_option( 'static_title' ) )
		$title = array( $st );
	
	$title[] = bb_get_option( 'name' );
	
	if ( 'reversed' == $args['order'] )
		$title = array_reverse( $title );
	
	return apply_filters( 'bb_get_title', implode( $args['separator'], $title ) );
}

function bb_feed_head() {
	
	$feeds = array();
	
	switch (bb_get_location()) {
		case 'profile-page':
			if ( $tab = isset($_GET['tab']) ? $_GET['tab'] : bb_get_path(2) )
				if ($tab != 'favorites')
					break;
			
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; User Favorites: %2$s'), bb_get_option( 'name' ), get_user_name()),
				'href'  => get_favorites_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			break;
		
		case 'topic-page':
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; Topic: %2$s'), bb_get_option( 'name' ), get_topic_title()),
				'href'  => get_topic_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			break;
		
		case 'tag-page':
			if (bb_is_tag()) {
				$feeds[] = array(
					'title' => sprintf(__('%1$s &raquo; Tag: %2$s - Recent Posts'), bb_get_option( 'name' ), bb_get_tag_name()),
					'href'  => bb_get_tag_posts_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
				);
				$feeds[] = array(
					'title' => sprintf(__('%1$s &raquo; Tag: %2$s - Recent Topics'), bb_get_option( 'name' ), bb_get_tag_name()),
					'href'  => bb_get_tag_topics_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
				);
			}
			break;
		
		case 'forum-page':
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; Forum: %2$s - Recent Posts'), bb_get_option( 'name' ), get_forum_name()),
				'href'  => bb_get_forum_posts_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; Forum: %2$s - Recent Topics'), bb_get_option( 'name' ), get_forum_name()),
				'href'  => bb_get_forum_topics_rss_link(0, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			break;
		
		case 'front-page':
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; Recent Posts'), bb_get_option( 'name' )),
				'href'  => bb_get_posts_rss_link(BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			$feeds[] = array(
				'title' => sprintf(__('%1$s &raquo; Recent Topics'), bb_get_option( 'name' )),
				'href'  => bb_get_topics_rss_link(BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
			);
			break;
		
		case 'view-page':
			global $bb_views, $view;
			if ($bb_views[$view]['feed']) {
				$feeds[] = array(
					'title' => sprintf(__('%1$s &raquo; View: %2$s'), bb_get_option( 'name' ), get_view_name()),
					'href'  => bb_get_view_rss_link(null, BB_URI_CONTEXT_LINK_ALTERNATE_HREF + BB_URI_CONTEXT_BB_FEED)
				);
			}
			break;
	}
	
	if (count($feeds)) {
		$feed_links = array();
		foreach ($feeds as $feed) {
			$link = '<link rel="alternate" type="application/rss+xml" ';
			$link .= 'title="' . esc_attr($feed['title']) . '" ';
			$link .= 'href="' . esc_attr($feed['href']) . '" />';
			$feed_links[] = $link;
		}
		$feed_links = join("\n", $feed_links);
	} else {
		$feed_links = '';
	}
	
	echo apply_filters('bb_feed_head', $feed_links);
}

function bb_get_posts_rss_link($context = 0) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	if ( bb_get_option( 'mod_rewrite' ) )
		$link = bb_get_uri('rss/', null, $context);
	else
		$link = bb_get_uri('rss.php', null, $context);
	return apply_filters( 'bb_get_posts_rss_link', $link, $context );
}

function bb_get_topics_rss_link($context = 0) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	if ( bb_get_option( 'mod_rewrite' ) )
		$link = bb_get_uri('rss/topics', null, $context);
	else
		$link = bb_get_uri('rss.php', array('topics' => 1), $context);
	return apply_filters( 'bb_get_topics_rss_link', $link, $context );
}

function bb_view_rss_link($view = null, $context = 0) {
	echo apply_filters( 'bb_view_rss_link', bb_get_view_rss_link($view, $context), $context);
}

function bb_get_view_rss_link($view = null, $context = 0) {
	if (!$view) {
		global $view;
	}
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	if ( bb_get_option( 'mod_rewrite' ) )
		$link = bb_get_uri('rss/view/' . $view, null, $context);
	else
		$link = bb_get_uri('rss.php', array('view' => $view), $context);
	return apply_filters( 'bb_get_view_rss_link', $link, $context );
}

function bb_latest_topics_pages( $args = null )
{
	$defaults = array( 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page;
	static $bb_latest_topics_count;
	if ( !$bb_latest_topics_count) {
		global $bbdb;
		$bb_latest_topics_count = $bbdb->get_var('SELECT COUNT(`topic_id`) FROM `' . $bbdb->topics . '` WHERE `topic_open` = 1 AND `topic_status` = 0 AND `topic_sticky` != 2;');
	}
	if ( $pages = apply_filters( 'bb_latest_topics_pages', get_page_number_links( $page, $bb_latest_topics_count ), $bb_latest_topics_count ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

// FORUMS

function forum_id( $forum_id = 0 ) {
	echo apply_filters( 'forum_id', get_forum_id( $forum_id ) );
}

function get_forum_id( $forum_id = 0 ) {
	global $forum;
	$forum_id = (int) $forum_id;
	if ( $forum_id )
		$_forum = bb_get_forum( $forum_id );
	else
		$_forum =& $forum;
	return $_forum->forum_id;
}

function forum_link( $forum_id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}
	echo apply_filters('forum_link', get_forum_link( $forum_id, $page, $context ), $forum_id, $context );
}

function get_forum_link( $forum_id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}
	
	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'forum_slug';
		} else {
			$column = 'forum_id';
		}
		$page = (1 < $page) ? '/page/' . $page : '';
		$link = bb_get_uri('forum/' . $forum->$column . $page, null, $context);
	} else {
		$query = array(
			'id' => $forum->forum_id,
			'page' => (1 < $page) ? $page : false
		);
		$link = bb_get_uri('forum.php', $query, $context);
	}

	return apply_filters( 'get_forum_link', $link, $forum->forum_id, $context );
}

function forum_name( $forum_id = 0 ) {
	echo apply_filters( 'forum_name', get_forum_name( $forum_id ), $forum_id );
}

function get_forum_name( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_name', $forum->forum_name, $forum->forum_id );
}

function forum_description( $args = null ) {
	if ( is_numeric($args) )
		$args = array( 'id' => $args );
	elseif ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'before' => $args );
	$defaults = array( 'id' => 0, 'before' => ' &#8211; ', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	if ( $desc = apply_filters( 'forum_description', get_forum_description( $args['id'] ), $args['id'], $args ) )
		echo $args['before'] . $desc . $args['after'];
}

function get_forum_description( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_description', $forum->forum_desc, $forum->forum_id );
}

function get_forum_parent( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_parent', $forum->forum_parent, $forum->forum_id );
}

function get_forum_position( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_position', $forum->forum_order, $forum->forum_id );
}

function bb_get_forum_is_category( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'bb_get_forum_is_category', isset($forum->forum_is_category) ? $forum->forum_is_category : false, $forum->forum_id );
}

function forum_topics( $forum_id = 0 ) {
	echo apply_filters( 'forum_topics', get_forum_topics( $forum_id ), $forum_id );
}

function get_forum_topics( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_topics', $forum->topics, $forum->forum_id );
}

function forum_posts( $forum_id = 0 ) {
	echo apply_filters( 'forum_posts', get_forum_posts( $forum_id ), $forum_id );
}

function get_forum_posts( $forum_id = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	return apply_filters( 'get_forum_posts', $forum->posts, $forum->forum_id );
}

function forum_pages( $args = null )
{
	// Compatibility
	if ( $args && is_numeric( $args ) ) {
		$args = array( 'id' => $args );
	}
	$defaults = array( 'id' => 0, 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page;
	$forum = bb_get_forum( get_forum_id( $args['id'] ) );
	if ( $pages = apply_filters( 'forum_pages', get_page_number_links( $page, $forum->topics ), $forum->topics ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

function bb_forum_posts_rss_link( $forum_id = 0, $context = 0 ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	echo apply_filters('bb_forum_posts_rss_link', bb_get_forum_posts_rss_link( $forum_id, $context ), $context );
}

function bb_get_forum_posts_rss_link( $forum_id = 0, $context = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	
	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'forum_slug';
		} else {
			$column = 'forum_id';
		}
		$link = bb_get_uri('rss/forum/' . $forum->$column, null, $context);
	} else {
		$link = bb_get_uri('rss.php', array('forum' => $forum->forum_id), $context);
	}
	return apply_filters( 'bb_get_forum_posts_rss_link', $link, $forum->forum_id, $context );
}

function bb_forum_topics_rss_link( $forum_id = 0, $context = 0 ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	echo apply_filters('bb_forum_topics_rss_link', bb_get_forum_topics_rss_link( $forum_id, $context ), $context );
}

function bb_get_forum_topics_rss_link( $forum_id = 0, $context = 0 ) {
	$forum = bb_get_forum( get_forum_id( $forum_id ) );
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	
	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'forum_slug';
		} else {
			$column = 'forum_id';
		}
		$link = bb_get_uri('rss/forum/' . $forum->$column . '/topics', null, $context);
	} else {
		$link = bb_get_uri('rss.php', array('forum' => $forum->forum_id, 'topics' => 1), $context);
	}
	return apply_filters( 'bb_get_forum_topics_rss_link', $link, $forum->forum_id, $context );
}

function bb_get_forum_bread_crumb($args = '') {
	$defaults = array(
		'forum_id' => 0,
		'separator' => ' &raquo; ',
		'class' => null
	);
	$args = wp_parse_args($args, $defaults);
	extract($args, EXTR_SKIP);

	$trail = '';
	$trail_forum = bb_get_forum(get_forum_id($forum_id));
	if ($class) {
		$class = ' class="' . $class . '"';
	}
	$current_trail_forum_id = $trail_forum->forum_id;
	while ( $trail_forum && $trail_forum->forum_id > 0 ) {
		$crumb = $separator;
		if ($current_trail_forum_id != $trail_forum->forum_id || !bb_is_forum()) {
			$crumb .= '<a' . $class . ' href="' . get_forum_link($trail_forum->forum_id) . '">';
		} elseif ($class) {
			$crumb .= '<span' . $class . '>';
		}
		$crumb .= get_forum_name($trail_forum->forum_id);
		if ($current_trail_forum_id != $trail_forum->forum_id || !bb_is_forum()) {
			$crumb .= '</a>';
		} elseif ($class) {
			$crumb .= '</span>';
		}
		$trail = $crumb . $trail;
		$trail_forum = bb_get_forum($trail_forum->forum_parent);
	}

	return apply_filters('bb_get_forum_bread_crumb', $trail, $forum_id );
}

function bb_forum_bread_crumb( $args = '' ) {
	echo apply_filters('bb_forum_bread_crumb', bb_get_forum_bread_crumb( $args ) );
}

// Forum Loop //

function &bb_forums( $args = '' ) {
	global $bb_forums_loop;

	$default_type = 'flat';

	if ( is_numeric($args) ) {
		$args = array( 'child_of' => $args );
	} elseif ( func_num_args() > 1 ) { // bb_forums( 'ul', $args ); Deprecated
		$default_type = $args;
		$args = func_get_arg(1);
	} elseif ( $args && is_string($args) && false === strpos($args, '=') ) {
		$args = array( 'type' => $args );
	}

	// hierarchical not used here.  Sent to bb_get_forums for proper ordering.
	$args = wp_parse_args( $args, array('hierarchical' => true, 'type' => $default_type, 'walker' => 'BB_Walker_Blank') );

	$levels = array( '', '' );

	if ( in_array($args['type'], array('list', 'ul')) )
		$levels = array( '<ul>', '</ul>' );

	$forums = bb_get_forums( $args );

	if ( !class_exists($args['walker']) )
		$args['walker'] = 'BB_Walker_Blank';

	if ( $bb_forums_loop = BB_Loop::start( $forums, $args['walker'] ) ) {
		$bb_forums_loop->preserve( array('forum', 'forum_id') );
		$bb_forums_loop->walker->db_fields = array( 'id' => 'forum_id', 'parent' => 'forum_parent' );
		list($bb_forums_loop->walker->start_lvl, $bb_forums_loop->walker->end_lvl) = $levels;
		return $bb_forums_loop->elements;
	}
	$false = false;
	return $false;
}

function bb_forum() { // Returns current depth
	global $bb_forums_loop;
	if ( !is_object($bb_forums_loop) || !is_a($bb_forums_loop, 'BB_Loop') )
		return false;
	if ( !is_array($bb_forums_loop->elements) )
		return false;

	if ( $bb_forums_loop->step() ) {
		$GLOBALS['forum'] =& $bb_forums_loop->elements[key($bb_forums_loop->elements)]; // Globalize the current forum object
	} else {
		$bb_forums_loop->reinstate();
		return $bb_forums_loop = null; // All done?  Kill the object and exit the loop.
	}

	return $bb_forums_loop->walker->depth;
}

function bb_forum_pad( $pad, $offset = 0 ) {
	global $bb_forums_loop;
	if ( !is_object($bb_forums_loop) || !is_a($bb_forums_loop, 'BB_Loop') )
		return false;

	echo $bb_forums_loop->pad( $pad, $offset );
}

function bb_forum_class( $args = null ) {
	echo apply_filters( 'bb_forum_class', get_alt_class( 'forum', bb_get_forum_class_names( $args ) ), $args );
}

function bb_get_forum_class_names( $args = null ) {
	if ( is_numeric( $args ) ) { // Not used
		$args = array( 'id' => $args );
	} elseif ( $args && is_string( $args ) && false === strpos( $args, '=' ) ) {
		$args = array( 'class' => $args );
	}
	$defaults = array( 'id' => 0, 'key' => 'forum', 'class' => '', 'output' => 'string' );
	$args = wp_parse_args( $args, $defaults );

	$classes = array();
	if ( $args['class'] ) {
		$classes[] = $args['class'];
	}

	global $bb_forums_loop;
	if ( is_object( $bb_forums_loop ) && is_a( $bb_forums_loop, 'BB_Loop' ) ) {
		$classes = array_merge( $classes, $bb_forums_loop->classes( 'array' ) );
	}

	if ( $args['output'] === 'string' ) {
		$classes = join( ' ', $classes );
	}

	return apply_filters( 'bb_get_forum_class', $classes, $args );
}

// TOPICS
function topic_id( $id = 0 ) {
	echo apply_filters( 'topic_id', get_topic_id( $id ) );
}

function get_topic_id( $id = 0 ) {
	global $topic;
	$id = (int) $id;
	if ( $id )
		$_topic = get_topic( $id );
	else
		$_topic =& $topic;

	if ( empty($_topic->topic_id) )
		return 0;

	return (int) $_topic->topic_id;
}

function topic_link( $id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	echo apply_filters( 'topic_link', get_topic_link( $id, $page, $context ), $id, $page, $context );
}

function get_topic_link( $id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	$topic = get_topic( get_topic_id( $id ) );

	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}

	$args = array();

	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'topic_slug';
		} else {
			$column = 'topic_id';
		}
		$page = (1 < $page) ? '/page/' . $page : '';
		$link = bb_get_uri('topic/' . $topic->$column . $page, null, $context);
	} else {
		$page = (1 < $page) ? $page : false;
		$link = bb_get_uri('topic.php', array('id' => $topic->topic_id, 'page' => $page), $context);
	}

	return apply_filters( 'get_topic_link', $link, $topic->topic_id, $context );
}

function topic_rss_link( $id = 0, $context = 0 ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	echo apply_filters('topic_rss_link', get_topic_rss_link($id, $context), $id, $context );
}

function get_topic_rss_link( $id = 0, $context = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );

	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}

	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'topic_slug';
		} else {
			$column = 'topic_id';
		}
		$link = bb_get_uri('rss/topic/' . $topic->$column, null, $context);
	} else {
		$link = bb_get_uri('rss.php', array('topic' => $topic->topic_id), $context);
	}
	return apply_filters( 'get_topic_rss_link', $link, $topic->topic_id, $context );
}

function bb_topic_labels() {
	echo apply_filters( 'bb_topic_labels', null );
}

function topic_title( $id = 0 ) {
	echo apply_filters( 'topic_title', get_topic_title( $id ), get_topic_id( $id ) );
}

function get_topic_title( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	return apply_filters( 'get_topic_title', $topic->topic_title, $topic->topic_id );
}

function topic_page_links( $id = 0, $args = null ) {
	echo apply_filters( 'topic_page_links', get_topic_page_links( $id, $args ), get_topic_id( $id ) );
}

function get_topic_page_links( $id = 0, $args = null ) {

	$defaults = array(
		'show_all' => false,
		'end_size' => 3,
		'before' => ' - ',
		'after' => null
	);

	$args = wp_parse_args( $args, $defaults );

	$topic = get_topic( get_topic_id( $id ) );

	$uri = get_topic_link();
	if ( bb_get_option('mod_rewrite') ) {
		if ( false === $pos = strpos( $uri, '?' ) ) {
			$uri = $uri . '%_%';
		} else {
			$uri = substr_replace( $uri, '%_%', $pos, 0 );
		}
	} else {
		$uri = add_query_arg( 'page', '%_%', $uri );
	}

	$posts = $topic->topic_posts + topic_pages_add( $topic->topic_id );

	$per_page = apply_filters( 'get_topic_page_links_per_page', bb_get_option('page_topics') );

	$_links = bb_paginate_links(
		array(
			'base' => $uri,
			'format' => bb_get_option('mod_rewrite') ? '/page/%#%' : '%#%',
			'total' => ceil($posts/$per_page),
			'current' => 0,
			'show_all' => $args['show_all'],
			'end_size' => $args['end_size'],
			'type' => 'array'
		)
	);

	$links = $_links;

	if ( $links ) {
		if ( !$show_first ) {
			unset( $links[0] );
		}

		$r = '';
		if ( $args['before'] ) {
			$r .= $args['before'];
		}
		$r .= join('', $links);
		if ( $args['after'] ) {
			$r .= $args['after'];
		}
	}

	return apply_filters( 'get_topic_page_links', $r, $_links, $topic->topic_id );
}

function bb_topic_voices( $id = 0 ) {
	echo apply_filters( 'bb_topic_voices', bb_get_topic_voices( $id ), get_topic_id( $id ) );
}

function bb_get_topic_voices( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );

	if ( empty( $topic->voices_count ) ) {
		global $bbdb;

		if ( $voices = $bbdb->get_col( $bbdb->prepare( "SELECT DISTINCT poster_id FROM $bbdb->posts WHERE topic_id = %s AND post_status = '0';", $topic->topic_id ) ) ) {
			$voices = count( $voices );
			bb_update_topicmeta( $topic->topic_id, 'voices_count', $voices );
		}
	} else {
		$voices = $topic->voices_count;
	}

	return apply_filters( 'bb_get_topic_voices', $voices, $topic->topic_id );
}

function topic_posts( $id = 0 ) {
	echo apply_filters( 'topic_posts', get_topic_posts( $id ), get_topic_id( $id ) );
}

function get_topic_posts( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	return apply_filters( 'get_topic_posts', $topic->topic_posts, $topic->topic_id );
}

function get_topic_deleted_posts( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	return apply_filters( 'get_topic_deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts : 0, $topic->topic_id );
}

function topic_noreply( $title ) {
	if ( 1 == get_topic_posts() && ( bb_is_front() || bb_is_forum() ) )
		$title = "<strong>$title</strong>";
	return $title;
}

function topic_last_poster( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	echo apply_filters( 'topic_last_poster', get_topic_last_poster( $id ), $topic->topic_last_poster, $topic->topic_id ); // $topic->topic_last_poster = user ID
}

function get_topic_last_poster( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	$user_display_name = get_user_display_name($topic->topic_last_poster);
	return apply_filters( 'get_topic_last_poster', $user_display_name, $topic->topic_last_poster, $topic->topic_id ); // $topic->topic_last_poster = user ID
}

function topic_author( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	echo apply_filters( 'topic_author', get_topic_author( $id ), $topic->topic_poster, $topic->topic_id ); // $topic->topic_poster = user ID
}

function get_topic_author( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	$user_display_name = get_user_display_name($topic->topic_poster);
	return apply_filters( 'get_topic_author', $user_display_name, $topic->topic_poster, $topic->topic_id ); // $topic->topic_poster = user ID
}

// Filters expect the format to by mysql on both topic_time and get_topic_time
function topic_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );
	$time = apply_filters( 'topic_time', get_topic_time( array('format' => 'mysql') + $args), $args );
	echo _bb_time_function_return( $time, $args );
}

function get_topic_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );

	$topic = get_topic( get_topic_id( $args['id'] ) );

	$time = apply_filters( 'get_topic_time', $topic->topic_time, $args );

	return _bb_time_function_return( $time, $args );
}

function topic_start_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );
	$time = apply_filters( 'topic_start_time', get_topic_start_time( array('format' => 'mysql') + $args), $args );
	echo _bb_time_function_return( $time, $args );
}

function get_topic_start_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );

	$topic = get_topic( get_topic_id( $args['id'] ) );

	$time = apply_filters( 'get_topic_start_time', $topic->topic_start_time, $args, $topic->topic_id );

	return _bb_time_function_return( $time, $args );
}

function topic_last_post_link( $id = 0 ) {
	echo apply_filters( 'topic_last_post_link', get_topic_last_post_link( $id ), $id);
}

function get_topic_last_post_link( $id = 0 ){
	$topic = get_topic( get_topic_id( $id ) );
	$page = bb_get_page_number( $topic->topic_posts );
	return apply_filters( 'get_post_link', get_topic_link( $topic->topic_id, $page ) . "#post-$topic->topic_last_post_id", $topic->topic_last_post_id, $topic->topic_id );
}

function topic_pages( $args = null )
{
	// Compatibility
	if ( $args && is_numeric( $args ) ) {
		$args = array( 'id' => $args );
	}
	$defaults = array( 'id' => 0, 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page;
	$topic = get_topic( get_topic_id( $args['id'] ) );
	$add = topic_pages_add( $topic->topic_id );
	if ( $pages = apply_filters( 'topic_pages', get_page_number_links( $page, $topic->topic_posts + $add ), $topic->topic_id ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

function topic_pages_add( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	if ( isset($_GET['view']) && 'all' == $_GET['view'] && bb_current_user_can('browse_deleted') && isset( $topic->deleted_posts ) )
		$add = $topic->deleted_posts;
	else
		$add = 0;
	return apply_filters( 'topic_pages_add', $add, isset($topic->topic_id) ? $topic->topic_id : 0 );
}

function get_page_number_links( $args ) {
	if ( 1 < func_num_args() ) {
		$_args = func_get_args();
		$args = array(
			'page' => $_args[0],
			'total' => $_args[1],
			'per_page' => isset( $_args[2] ) ? $_args[2] : '',
			'mod_rewrite' => isset( $_args[3] ) ? $_args[3] : 'use_option'
		);
	}
	$defaults = array(
		'page' => 1,
		'total' => false,
		'per_page' => '',
		'mod_rewrite' => 'use_option',
		'prev_text' => __( '&laquo; Previous' ),
		'next_text' => __( 'Next &raquo;' )
	);
	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	$add_args = array();
	$uri = rtrim( $_SERVER['REQUEST_URI'], '?&' );

	if ( $mod_rewrite === 'use_option' ) {
		$mod_rewrite = bb_get_option( 'mod_rewrite' );
	}

	if ( $mod_rewrite ) {
		$format = '/page/%#%';
		if ( 1 == $page ) {
			if ( false === $pos = strpos($uri, '?') )
				$uri = $uri . '%_%';
			else
				$uri = substr_replace($uri, '%_%', $pos, 0);
		} else {
			$uri = preg_replace('|/page/[0-9]+|', '%_%', $uri);
		}
		$uri = str_replace( '/%_%', '%_%', $uri );
	} else {
		if ( 1 == $page ) {
			if ( false === $pos = strpos($uri, '?') ) {
				$uri = $uri . '%_%';
				$format = '?page=%#%';
			} else {
				$uri = substr_replace($uri, '?%_%', $pos, 1);
				$format = 'page=%#%&';
			}
		} else {
			if ( false === strpos($uri, '?page=') ) {
				$uri = preg_replace('!&page=[0-9]+!', '%_%', $uri );
				$uri = str_replace( '&page=', '', $uri );
				$format = '&page=%#%';
			} else {
				$uri = preg_replace('!\?page=[0-9]+!', '%_%', $uri );
				$uri = str_replace( '?page=', '', $uri );
				$format = '?page=%#%';
			}
		}
	}

	if ( isset($_GET['view']) && in_array($_GET['view'], bb_get_views()) )
		$add_args['view'] = $_GET['view'];

	if ( empty( $per_page ) ) {
		$per_page = bb_get_option( 'page_topics' );
	}

	$links = bb_paginate_links( array(
		'base' => $uri,
		'format' => $format,
		'total' => ceil( $total/$per_page ),
		'current' => $page,
		'add_args' => $add_args,
		'type' => 'array',
		'mid_size' => 1,
		'prev_text' => $prev_text,
		'next_text' => $next_text
	) );

	if ($links) {
		$links = join('', $links);
	}
	return $links;
}

function bb_topic_admin( $args = '' )
{
	$parts = array(
		'delete' => bb_get_topic_delete_link( $args ),
		'close'  => bb_get_topic_close_link( $args ),
		'sticky' => bb_get_topic_sticky_link( $args ),
		'move'   => bb_get_topic_move_dropdown( $args )
	);

	echo join( "\n", apply_filters( 'bb_topic_admin', $parts ) );
}

function topic_delete_link( $args = '' ) {
	echo bb_get_topic_delete_link( $args );
}

function bb_get_topic_delete_link( $args = '' ) {
	$defaults = array( 'id' => 0, 'before' => '[', 'after' => ']', 'delete_text' => false, 'undelete_text' => false, 'redirect' => true );
	extract(wp_parse_args( $args, $defaults ), EXTR_SKIP);
	$id = (int) $id;

	$topic = get_topic( get_topic_id( $id ) );

	if ( !$topic || !bb_current_user_can( 'delete_topic', $topic->topic_id ) )
		return;

	if ( true === $redirect )
		$redirect = $_SERVER['REQUEST_URI'];

	if ( 0 == $topic->topic_status ) {
		$query   = array( 'id' => $topic->topic_id, '_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false );
		$confirm = __('Are you sure you wanna delete that?');
		$display = esc_html( $delete_text ? $delete_text : __('Delete entire topic') );
	} else {
		$query   = array('id' => $topic->topic_id, 'view' => 'all', '_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false );
		$confirm = __('Are you sure you wanna undelete that?');
		$display = esc_html( $undelete_text ? $undelete_text : __('Undelete entire topic') );
	}
	$uri = bb_get_uri('bb-admin/delete-topic.php', $query, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri = esc_url( bb_nonce_url( $uri, 'delete-topic_' . $topic->topic_id ) );
	
	return $before . '<a href="' . $uri . '" onclick="return confirm(\'' . esc_js( $confirm ) . '\');">' . $display . '</a>' . $after;
}

function topic_close_link( $args = '' ) {
	echo bb_get_topic_close_link( $args );
}

function bb_get_topic_close_link( $args = '' ) {
	$defaults = array( 'id' => 0, 'before' => '[', 'after' => ']', 'close_text' => false, 'open_text' => false, 'redirect' => true );
	extract(wp_parse_args( $args, $defaults ), EXTR_SKIP);
	$id = (int) $id;

	$topic = get_topic( get_topic_id( $id ) );

	if ( !$topic || !bb_current_user_can( 'close_topic', $topic->topic_id ) )
		return;

	if ( topic_is_open( $topic->topic_id ) )
		$display = esc_html( $close_text ? $close_text : __( 'Close topic' ) );
	else
		$display = esc_html( $open_text ? $open_text : __( 'Open topic' ) );

	if ( true === $redirect )
		$redirect = $_SERVER['REQUEST_URI'];

	$uri = bb_get_uri('bb-admin/topic-toggle.php', array( 'id' => $topic->topic_id, '_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false ), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri = esc_url( bb_nonce_url( $uri, 'close-topic_' . $topic->topic_id ) );
	
	return $before . '<a href="' . $uri . '">' . $display . '</a>' . $after;
}

function topic_sticky_link( $args = '' ) {
	echo bb_get_topic_sticky_link( $args );
}

function bb_get_topic_sticky_link( $args = '' ) {
	$defaults = array( 'id' => 0, 'before' => '[', 'after' => ']' );
	extract(wp_parse_args( $args, $defaults ), EXTR_SKIP);
	$id = (int) $id;

	$topic = get_topic( get_topic_id( $id ) );

	if ( !$topic || !bb_current_user_can( 'stick_topic', $topic->topic_id ) )
		return;

	$uri_stick = bb_get_uri('bb-admin/sticky.php', array('id' => $topic->topic_id), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri_stick = esc_url( bb_nonce_url( $uri_stick, 'stick-topic_' . $topic->topic_id ) );

	$uri_super = bb_get_uri('bb-admin/sticky.php', array('id' => $topic->topic_id, 'super' => 1), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri_super = esc_url( bb_nonce_url( $uri_super, 'stick-topic_' . $topic->topic_id ) );

	if ( topic_is_sticky( $topic->topic_id ) )
		return "$before<a href='" . $uri_stick . "'>". __('Unstick topic') ."</a>$after";
	else
		return "$before<a href='" . $uri_stick . "'>". __('Stick topic') . "</a> (<a href='" . $uri_super . "'>" . __('to front') . "</a>)$after";
}

function topic_show_all_link( $id = 0 ) {
	if ( !bb_current_user_can( 'browse_deleted' ) )
		return;
	if ( 'all' == @$_GET['view'] )
		echo "<a href='" . esc_attr( get_topic_link( $id ) ) . "'>". __('View normal posts') ."</a>";
	else
		echo "<a href='" . esc_attr( add_query_arg( 'view', 'all', get_topic_link( $id ) ) ) . "'>". __('View all posts') ."</a>";
}

function topic_posts_link( $id = 0 ) {
	echo get_topic_posts_link( $id );
}

function get_topic_posts_link( $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	$post_num = get_topic_posts( $id );
	$posts = sprintf(__ngettext( '%s post', '%s posts', $post_num ), $post_num);
	$r = '';

	if ( ( 'all' == @$_GET['view'] || bb_is_admin() ) && bb_current_user_can('browse_deleted') )
		$r .= "<a href='" . esc_attr( get_topic_link( $id ) ) . "'>$posts</a>";
	else
		$r .= $posts;

	if ( bb_current_user_can( 'browse_deleted' ) ) {
		$user_id = bb_get_current_user_info( 'id' );
		if ( isset($topic->bozos[$user_id]) && 'all' != @$_GET['view'] )
			add_filter('get_topic_deleted_posts', create_function('$a', "\$a -= {$topic->bozos[$user_id]}; return \$a;") );
		if ( $deleted = get_topic_deleted_posts( $id ) ) {
			$extra = sprintf(__('+%d more'), $deleted);
			if ( 'all' == @$_GET['view'] )
				$r .= " $extra";
			else
				$r .= " <a href='" . esc_attr( add_query_arg( 'view', 'all', get_topic_link( $id ) ) ) . "'>$extra</a>";
		}
	}

	return $r;
}

function topic_move_dropdown( $args = '' )
{
	echo bb_get_topic_move_dropdown( $args );
}

function bb_get_topic_move_dropdown( $args = '' )
{
	if ( $args && is_numeric( $args ) ) {
		$args = array( 'id' => (integer) $args );
	}

	$defaults = array( 'id' => 0, 'before' => '[', 'after' => ']' );
	extract(wp_parse_args( $args, $defaults ), EXTR_SKIP);
	$id = (int) $id;

	$topic = get_topic( get_topic_id( $id ) );
	if ( !bb_current_user_can( 'move_topic', $topic->topic_id ) )
		return;

	$dropdown = bb_get_forum_dropdown( array(
		'callback' => 'bb_current_user_can',
		'callback_args' => array('move_topic', $topic->topic_id),
		'selected' => $topic->forum_id,
		'tab' => false
	) );

	if ( !$dropdown )
		return;

	$r = $before . '<form id="topic-move" method="post" action="' . bb_get_uri( 'bb-admin/topic-move.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ) . '">' . "\n";
	$r .= '<fieldset>' . "\n";
	$r .= '<div>' . "\n";
	$r .= '<input type="hidden" name="topic_id" value="' . $topic->topic_id . '" />' . "\n";
	$r .= '<label for="forum-id">'. __( 'Move to' ) . '</label>' . "\n";
	$r .= $dropdown . "\n";
	$r .= bb_nonce_field( 'move-topic_' . $topic->topic_id, '_wpnonce', true , false );
	$r .= '<input type="submit" name="Submit" value="' . __( 'Move' ) . '" />' . "\n";
	$r .= '</div>' . "\n";
	$r .= '</fieldset>' . "\n";
	$r .= '</form>' . $after;

	return $r;
}

function topic_class( $class = '', $key = 'topic', $id = 0 ) {
	$topic = get_topic( get_topic_id( $id ) );
	$class = $class ? explode(' ', $class ) : array();
	if ( '1' === $topic->topic_status && bb_current_user_can( 'browse_deleted' ) )
		$class[] = 'deleted';
	elseif ( 1 < $topic->topic_status && bb_current_user_can( 'browse_deleted' ) )
		$class[] = 'bozo';
	if ( '0' === $topic->topic_open )
		$class[] = 'closed';
	if ( 1 == $topic->topic_sticky && ( bb_is_forum() || bb_is_view() ) )
		$class[] = 'sticky';
	elseif ( 2 == $topic->topic_sticky && ( bb_is_front() || bb_is_forum() ) )
		$class[] = 'sticky super-sticky';
	$class = apply_filters( 'topic_class', $class, $topic->topic_id );
	$class = join(' ', $class);
	alt_class( $key, $class );
}

/**
 * bb_get_new_topic_link() - Get the link to the form for a new topic
 *
 * @since 1.0
 * @param mixed The arguments for this function.
 * @return string The link to the new topic form
 */
function bb_get_new_topic_link( $args = null ) {
	$defaults = array( 'text' => __('Add New &raquo;'), 'forum' => 0, 'tag' => '' );
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'text' => $args );

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	if ( $forum && $forum = bb_get_forum( $forum ) )
		$url = get_forum_link( $forum->forum_id ) . '#postform';
	elseif ( $tag && $tag = bb_get_tag( $tag ) )
		$url = bb_get_tag_link( $tag->tag ) . '#postform';
	elseif ( bb_is_forum() ) {
		global $forum;
		$url = get_forum_link( $forum->forum_id ) . '#postform';
	} elseif ( bb_is_tag() ) {
		global $tag;
		$url = bb_get_tag_link( $tag ) . '#postform';
	} elseif ( bb_is_topic() )
		$url = get_forum_link() . '#postform';
	elseif ( bb_is_front() )
		$url = bb_get_uri(null, array('new' => 1));

	if ( !bb_is_user_logged_in() )
		$url = bb_get_uri('bb-login.php', array('re' => $url), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_USER_FORMS);
	elseif ( bb_is_forum() || bb_is_topic() ) {
		if ( !bb_current_user_can( 'write_topic', get_forum_id() ) )
			return;
	} else {
		if ( !bb_current_user_can( 'write_topics' ) )
			return;
	}

	if ( $url = esc_attr( apply_filters( 'new_topic_url', $url, $args ) ) )
		return '<a href="' . $url . '" class="new-topic">' . $text . '</a>' . "\n";
}

function bb_new_topic_link( $args = null ) {
	echo bb_get_new_topic_link($args);
}

function bb_new_topic_forum_dropdown( $args = '' ) {
	if ( !is_array( $args ) ) {
		$args = array(
			'callback' => 'bb_current_user_can',
			'callback_args' => array( 'write_topic' )
		);
	}
	if ( !isset( $args['callback'] ) && !isset( $args['callback_args'] ) ) {
		$args['callback']      = 'bb_current_user_can';
		$args['callback_args'] = array( 'write_topic' );
	}
	bb_forum_dropdown( $args );
}

function bb_topic_search_form( $args = null, $query_obj = null ) {
	global $bb_query_form;

	if ( $query_obj && is_a($query_obj, 'BB_Query_Form') ); // [sic]
	else
		$query_obj =& $bb_query_form;

	$query_obj->form( $args );
}

/**
 * bb_topic_pagecount() - Print the total page count for a topic
 *
 * @since 0.9
 * @param int $topic_id The topic id of the topic being queried
 * @return void
 */
function bb_topic_pagecount( $topic_id = 0 ) {
	echo bb_get_topic_pagecount( $topic_id );
}

/**
 * bb_get_topic_pagecount() - Get the total page count for a topic
 *
 * @since 0.9
 * @param int $topic_id The topic id of the topic being queried
 * @return int The total number of pages in the topic
 */
function bb_get_topic_pagecount( $topic_id = 0 ) {
	$topic = get_topic( get_topic_id( $topic_id ) );
	return bb_get_page_number( $topic->topic_posts + topic_pages_add() );
}

/**
 * bb_is_topic_lastpage() - Report whether the current page is the last page of a given topic
 *
 * @since 0.9
 * @param int $topic_id The topic id of the topic being queried
 * @return boolean True if called on the last page of a topic, otherwise false
 */
function bb_is_topic_lastpage( $topic_id = 0 ) {
	global $page;
	return ( $page == bb_get_topic_pagecount( $topic_id ) );
}

// POSTS

function post_id( $post_id = 0 ) {
	echo get_post_id( $post_id );
}

function get_post_id( $post_id = 0 ) {
	global $bb_post;
	$post_id = (int) $post_id;
	if ( $post_id )
		$post = bb_get_post( $post_id );
	else
		$post =& $bb_post;
	return $post->post_id;
}

function post_link( $post_id = 0 ) {
	echo apply_filters( 'post_link', get_post_link( $post_id ), get_post_id( $post_id ) );
}

function get_post_link( $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	$page = bb_get_page_number( $bb_post->post_position );
	return apply_filters( 'get_post_link', get_topic_link( $bb_post->topic_id, $page ) . "#post-$bb_post->post_id", $bb_post->post_id );
}

function post_anchor_link( $force_full = false ) {
	if ( defined('DOING_AJAX') || $force_full )
		post_link();
	else
		echo '#post-' . get_post_id();
}

function post_position( $post_id = 0 ) {
	echo apply_filters( 'post_position', get_post_position( $post_id ), get_post_id( $post_id ) );
}

function get_post_position( $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	return apply_filters( 'get_post_position', $bb_post->post_position, $bb_post->post_id );
}

function post_position_link( $topic_id = 0, $position = 1 ) {
	echo apply_filters( 'post_position_link', get_post_position_link( $topic_id, $position ), get_topic_id( $topic_id ), (integer) $position );
}

function get_post_position_link( $topic_id = 0, $position = 1 ) {
	$position = (integer) $position;
	$bb_topic = get_topic( get_topic_id( $topic_id ) );
	if ( $bb_topic->topic_posts < $position ) {
		return;
	}
	$page = bb_get_page_number( $position );
	return apply_filters( 'get_post_position_link', get_topic_link( $bb_post->topic_id, $page ) . "#position-$position", $bb_topic->topic_id, $position );
}

function bb_post_meta( $key, $post_id = 0 ) {
	echo bb_get_post_meta( $key, $post_id );
}

function bb_get_post_meta( $key, $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	if ( isset($bb_post->$key) )
		return $bb_post->$key;
}


function post_author( $post_id = 0 ) {
	echo apply_filters('post_author', get_post_author( $post_id ), $post_id );
}

function get_post_author( $post_id = 0 ) {
	if ( $user = bb_get_user( get_post_author_id( $post_id ) ) )
		return apply_filters( 'get_post_author', $user->display_name, $user->ID, $post_id );
	elseif ( $title = bb_get_post_meta( 'pingback_title' ) )
		return apply_filters( 'bb_get_pingback_title', $title, $post_id );
	else
		return apply_filters( 'get_post_author', __('Anonymous'), 0, $post_id );
}

function post_author_link( $post_id = 0 ) {
	if ( $link = get_user_link( get_post_author_id( $post_id ) ) ) {
		echo '<a href="' . esc_attr( $link ) . '">' . get_post_author( $post_id ) . '</a>';
	} elseif ( $link = bb_get_post_meta( 'pingback_uri' )) {
		echo '<a href="' . esc_attr( $link ) . '">' . get_post_author( $post_id ) . '</a>';
	} else {
		post_author( $post_id );
	}
}

function post_author_avatar( $size = '48', $default = '', $post_id = 0 ) {
	if ( ! bb_get_option('avatars_show') )
		return false;
	
	$author_id = get_post_author_id( $post_id );
	echo bb_get_avatar( $author_id, $size, $default );
}

function post_author_avatar_link( $size = '48', $default = '', $post_id = 0 ) {
	if ( ! bb_get_option('avatars_show') )
		return false;
	
	$author_id = get_post_author_id( $post_id );
	if ( $link = get_user_link( $author_id ) ) {
		echo '<a href="' . esc_attr( $link ) . '">' . bb_get_avatar( $author_id, $size, $default ) . '</a>';
	} else {
		echo bb_get_avatar( $author_id, $size, $default );
	}
}

function post_text( $post_id = 0 ) {
	echo apply_filters( 'post_text', get_post_text( $post_id ), get_post_id( $post_id ) );
}

function get_post_text( $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	return apply_filters( 'get_post_text', $bb_post->post_text, $bb_post->post_id );
}

function bb_post_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );
	$time = apply_filters( 'bb_post_time', bb_get_post_time( array('format' => 'mysql') + $args ), $args );
	echo _bb_time_function_return( $time, $args );
}

function bb_get_post_time( $args = '' ) {
	$args = _bb_parse_time_function_args( $args );

	$bb_post = bb_get_post( get_post_id( $args['id'] ) );

	$time = apply_filters( 'bb_get_post_time', $bb_post->post_time, $args );

	return _bb_time_function_return( $time, $args );
}

function post_ip( $post_id = 0 ) {
	if ( bb_current_user_can( 'view_by_ip' ) )
		echo apply_filters( 'post_ip', get_post_ip( $post_id ), get_post_id( $post_id ) );
}

function get_post_ip( $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	return apply_filters( 'get_post_ip', $bb_post->poster_ip, $bb_post->post_id );
}

function bb_post_admin( $args = null )
{
	$defaults = array(
		'post_id' => 0,
		'before' => '',
		'after' => '',
		'before_each' => '',
		'after_each' => "\n",
		'each' => array(
			'ip' => array(
				'post_id' => 0
			),
			'edit' => array(
				'post_id' => 0
			),
			'delete' => array(
				'post_id' => 0
			),
			'undelete' => array(
				'post_id' => 0
			)
		)
	);
	if ( isset( $args['each'] ) ) {
		$each_args = $args['each'];
		$_each_args = $defaults['each'];
		foreach ( $each_args as $_part_name => $_part_args ) {
			if ( !isset( $defaults['each'][$_part_name] ) ) {
				continue;
			}
			$_each_args[$_part_name] = wp_parse_args( $_part_args, $defaults['each'][$_part_name] );
		}
	}
	$args = wp_parse_args( $args, $defaults );
	if ( isset( $_each_args ) ) {
		$args['each'] = $_each_args;
	}

	$parts = array();
	if ( is_array( $args['each'] ) && count( $args['each'] ) ) {
		foreach ( $args['each'] as $_part_name => $_part_args ) {
			if ( $args['post_id'] && !$_part_args['post_id'] ) {
				$_part_args['post_id'] = $args['post_id'];
			}
			if ( $args['before_each'] && !$_part_args['before'] ) {
				$_part_args['before'] = $args['before_each'];
			}
			if ( $args['after_each'] && !$_part_args['after'] ) {
				$_part_args['after'] = $args['after_each'];
			}
			$_part_function = 'bb_get_post_' . $_part_name . '_link';
			$parts[$_part_name] = $_part_function( $_part_args );
		}

		// For the benefit of filters, mark the final part
		if ( !isset( $args['last_each'] ) ) {
			$args['last_each'] = $_part_args;
		}
	}

	$parts = apply_filters( 'bb_post_admin', $parts, $args );

	if ( !count( $parts ) ) {
		return;
	}

	echo $args['before'] . join( '', $parts ) . $args['after'];
}

function post_ip_link( $args = null )
{
	echo bb_get_post_ip_link( $args );
}

function bb_get_post_ip_link( $args = null )
{
	if ( !bb_current_user_can( 'view_by_ip' ) ) {
		return;
	}

	$defaults = array(
		'post_id' => 0,
		'before' => '',
		'after' => '',
		'text' => '%s'
	);
	if ( is_numeric( $args ) ) {
		$args = array( 'post_id' => $args );
	}
	$args = wp_parse_args( $args, $defaults );

	$bb_post = bb_get_post( get_post_id( $args['post_id'] ) );

	$uri = bb_get_uri( 'bb-admin/posts.php', array( 'poster_ip' => get_post_ip( $bb_post->post_id ) ), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN );

	// Make sure that the last tag in $before gets a class (if it's there)
	if ( preg_match( '/.*(<[^>]+>)[^<]*/', $args['before'], $_node ) ) {
		if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) {
			$args['before'] = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-ip-link ' . $_class[2] . $_class[1], $args['before'] );
		} else {
			$args['before'] = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-ip-link"$3$4>$5', $args['before'], 1 );
		}
	}

	$link = $args['before'] . '<a class="post-ip-link" href="' . esc_attr( $uri ) . '">' . esc_html( sprintf( $args['text'], get_post_ip( $bb_post->post_id ) ) ) . '</a>' . $args['after'];
	return apply_filters( 'post_ip_link', $link, $bb_post->post_id, $args );
}

function post_edit_link( $args = null )
{
	echo bb_get_post_edit_link( $args );
}

function bb_get_post_edit_link( $args = null )
{
	$defaults = array(
		'post_id' => 0,
		'before' => '',
		'after' => '',
		'text' => __( 'Edit' )
	);
	if ( is_numeric( $args ) ) {
		$args = array( 'post_id' => $args );
	}
	$args = wp_parse_args( $args, $defaults );

	$bb_post = bb_get_post( get_post_id( $args['post_id'] ) );

	if ( bb_current_user_can( 'edit_post', $bb_post->post_id ) ) {
		$uri = bb_get_uri( 'edit.php', array( 'id' => $bb_post->post_id ) );

		// Make sure that the last tag in $before gets a class (if it's there)
		if ( preg_match( '/.*(<[^>]+>)[^<]*/', $args['before'], $_node ) ) {
			if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) {
				$args['before'] = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-edit-link ' . $_class[2] . $_class[1], $args['before'] );
			} else {
				$args['before'] = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-edit-link"$3$4>$5', $args['before'], 1 );
			}
		}

		$r = $args['before'] . '<a class="post-edit-link" href="' . esc_attr( apply_filters( 'post_edit_uri', $uri, $bb_post->post_id, $args ) ) . '">' . esc_html( $args['text'] ) . '</a>' . $args['after'];
		return apply_filters( 'bb_get_post_edit_link', $r, $bb_post->post_id, $args );
	}
}

function post_del_class( $post_id = 0 )
{
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	$classes = array();
	if ( bb_get_post_meta( 'pingback_uri', $post_id ) ) {
		$classes[] = 'pingback';
	}
	if ( $bb_post->post_status == 1 ) {
		$classes[] = 'deleted';
	} elseif ( $bb_post->post_status != 0 ) {
		$classes[] = 'post-status-' . $bb_post->post_status;
	}
	if ( count( $classes ) ) {
		$classes = join( ' ', $classes );
	} else {
		$classes = '';
	}
	return apply_filters( 'post_del_class', $classes, $bb_post->post_id, $bb_post );
}

function post_delete_link( $args = null )
{
	echo bb_get_post_delete_link( $args );
}

function bb_get_post_delete_link( $args = null )
{
	$defaults = array(
		'post_id' => 0,
		'before' => '',
		'after' => '',
		'text' => __( 'Delete' ),
		'redirect' => true
	);
	if ( is_numeric( $args ) || is_object( $args ) ) {
		$args = array( 'post_id' => $args );
	}
	if ( isset( $args['delete_text'] ) && ( !isset( $args['text'] ) || !$args['text'] ) ) {
		$args['text'] = $args['delete_text'];
	}

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	$bb_post = bb_get_post( get_post_id( $post_id ) );
	//if ( bb_is_first( $bb_post->post_id ) ) {
	//	$topic = get_topic( $bb_post->topic_id );
	//	if ( 2 > $topic->topic_posts ) {
			// Should delete the whole topic
	//		return;
	//	}
	//}
	
	if ( !bb_current_user_can( 'delete_post', $bb_post->post_id ) ) {
		return;
	}

	if ( true === $redirect ) {
		$redirect = $_SERVER['REQUEST_URI'];
	}

	$uri = bb_get_uri('bb-admin/delete-post.php', array(
		'id' => $bb_post->post_id,
		'status' => 1,
		'_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false
	), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri = esc_url( bb_nonce_url( $uri, 'delete-post_' . $bb_post->post_id ) );

	if ( ( bb_is_admin() || isset( $_GET['view'] ) && 'all' == $_GET['view'] ) ) {
		$ajax_class = 'dim:thread:post-' . $bb_post->post_id . ':deleted:FF3333:FFFF33:action=delete-post&amp;status=1';
	} else {
		$ajax_class = 'delete:thread:post-' . $bb_post->post_id . '::status=1';
	}

	$text = esc_html( $text );

	// Make sure that the last tag in $before gets a class (if it's there)
	if ( preg_match( '/.*(<[^>]+>)[^<]*/', $before, $_node ) ) {
		if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) {
			$before = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-delete-link ' . $_class[2] . $_class[1], $before );
		} else {
			$before = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-delete-link"$3$4>$5', $before, 1 );
		}
	}

	$r = $before . '<a href="' . $uri . '" class="' . $ajax_class . ' post-delete-link">' . $text . '</a>' . $after;
	$r = apply_filters( 'post_delete_link', $r, $bb_post->post_status, $bb_post->post_id, $args );
	return $r;
}

function bb_post_undelete_link( $args = null )
{
	echo bb_get_post_undelete_link( $args );
}

function bb_get_post_undelete_link( $args = null )
{
	$defaults = array(
		'post_id' => 0,
		'before' => '',
		'after' => '',
		'text' => __( 'Undelete' ),
		'redirect' => true
	);
	if ( is_numeric( $args ) || is_object( $args ) ) {
		$args = array( 'post_id' => $args );
	}

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	$bb_post = bb_get_post( get_post_id( $post_id ) );

	if ( !bb_current_user_can( 'delete_post', $bb_post->post_id ) ) {
		return;
	}

	if ( true === $redirect ) {
		$redirect = $_SERVER['REQUEST_URI'];
	}

	$uri = bb_get_uri('bb-admin/delete-post.php', array(
		'id' => $bb_post->post_id,
		'status' => 0,
		'view' => 'all',
		'_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false
	), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN);
	$uri = esc_url( bb_nonce_url( $uri, 'delete-post_' . $bb_post->post_id ) );

	$ajax_class = 'dim:thread:post-' . $bb_post->post_id . ':deleted:FF3333:FFFF33:action=delete-post&amp;status=0';

	$text = esc_html( $text );

	// Make sure that the last tag in $before gets a class (if it's there)
	if ( preg_match( '/.*(<[^>]+>)[^<]*/', $before, $_node ) ) {
		if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) {
			$before = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-undelete-link ' . $_class[2] . $_class[1], $before );
		} else {
			$before = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-undelete-link"$3$4>$5', $before, 1 );
		}
	}

	$r = $before . '<a href="' . $uri . '" class="' . $ajax_class . ' post-undelete-link">' . $text . '</a>' . $after;
	$r = apply_filters( 'post_undelete_link', $r, $bb_post->post_status, $bb_post->post_id, $args );
	return $r;
}

function post_author_id( $post_id = 0 ) {
	echo apply_filters( 'post_author_id', get_post_author_id( $post_id ), get_post_id( $post_id ) );
}

function get_post_author_id( $post_id = 0 ) {
	$bb_post = bb_get_post( get_post_id( $post_id ) );
	return apply_filters( 'get_post_author_id', (int) $bb_post->poster_id, get_post_id( $post_id ) );
}

function post_author_title( $post_id = 0 ) {
	echo apply_filters( 'post_author_title', get_post_author_title( $post_id ), get_post_id( $post_id ) );
}

function get_post_author_title( $post_id = 0 ) {
	return get_user_title( get_post_author_id( $post_id ) );
}

function post_author_title_link( $post_id = 0 ) {
	echo apply_filters( 'post_author_title_link', get_post_author_title_link( $post_id ), get_post_id( $post_id ) );
}

function get_post_author_title_link( $post_id = 0 ) {
	$title = get_post_author_title( $post_id );
	if ( false === $title ) {
		if ( bb_get_post_meta( 'pingback_uri', $post_id ) )
			$r = __('PingBack');
		else
			$r = __('Unregistered'); // This should never happen
	} else
		$r = '<a href="' . esc_attr( get_user_profile_link( get_post_author_id( $post_id ) ) ) . '">' . $title . '</a>';

	return apply_filters( 'get_post_author_title_link', $r, get_post_id( $post_id ) );
}

function post_author_type( $post_id = 0 ) {
	$id = get_post_author_id( $post_id );
	$type = get_user_type( $id );
	if ( false === $type ) {
		if ( bb_get_post_meta( 'pingback_uri', $post_id ) )
			$r = __('PingBack');
		else
			$r = __('Unregistered'); // This should never happen
	} else
		$r = '<a href="' . esc_attr( get_user_profile_link( $id ) ) . '">' . $type . '</a>';

	echo apply_filters( 'post_author_type', $r, $post_id );
}

function allowed_markup( $args = '' ) {
	echo apply_filters( 'allowed_markup', get_allowed_markup( $args ) );
}

// format=list or array( 'format' => 'list' )
function get_allowed_markup( $args = '' ) {
	$args = wp_parse_args( $args, array('format' => 'flat') );
	extract($args, EXTR_SKIP);

	$tags = bb_allowed_tags();
	unset($tags['pre'], $tags['br']);
	$tags = array_keys($tags);

	switch ( $format ) :
	case 'array' :
		$r = $tags;
		break;
	case 'list' :
		$r = "<ul class='allowed-markup'>\n\t<li>";
		$r .= join("</li>\n\t<li>", $tags);
		$r .= "</li>\n</ul>\n";
		break;
	default :
		$r = join(' ', $tags);
		break;
	endswitch;
	return apply_filters( 'get_allowed_markup', $r, $format );
}

// USERS
function bb_get_user_id( $id = 0 ) {
	global $user;
	if ( is_object($id) && isset($id->ID) )
		return (int) $id->ID;
	elseif ( !$id )
		return $user->ID;

	$_user = bb_get_user( (int) $id );
	return isset($_user->ID) ? $_user->ID : 0;
}

function user_profile_link( $id = 0 , $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}
	echo apply_filters( 'user_profile_link', get_user_profile_link( $id ), bb_get_user_id( $id ), $context );
}

function get_user_profile_link( $id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	$user = bb_get_user( bb_get_user_id( $id ) );
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}
	
	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'user_nicename';
		} else {
			$column = 'ID';
		}
		$page = (1 < $page) ? '/page/' . $page : '';
		$r = bb_get_uri('profile/' . $user->$column . $page, null, $context);
	} else {
		$query = array(
			'id' => $user->ID,
			'page' => (1 < $page) ? $page : false
		);
		$r = bb_get_uri('profile.php', $query, $context);
	}
	return apply_filters( 'get_user_profile_link', $r, $user->ID, $context );
}

function user_delete_button() {
	global $user;
	if ( bb_current_user_can( 'edit_users' ) && bb_get_current_user_info( 'id' ) != (int) $user->ID )
		echo apply_filters( 'user_delete_button', get_user_delete_button() );
}

function get_user_delete_button() {
	$r  = '<input type="submit" class="delete" name="delete-user" value="' . __('Delete User &raquo;') . '" ';
	$r .= 'onclick="return confirm(\'' . esc_js(__('Are you sure you want to delete this user?')) . '\')" />';
	return apply_filters( 'get_user_delete_button', $r);
}

function profile_tab_link( $id = 0, $tab, $page = 1 ) {
	echo apply_filters( 'profile_tab_link', get_profile_tab_link( $id, $tab ) );
}

function get_profile_tab_link( $id = 0, $tab, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	$user = bb_get_user( bb_get_user_id( $id ) );

	$tab = bb_sanitize_with_dashes($tab);

	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}

	if ( $tab === 'edit' && !( $context & BB_URI_CONTEXT_BB_USER_FORMS ) ) {
		$context += BB_URI_CONTEXT_BB_USER_FORMS;
	}

	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'user_nicename';
		} else {
			$column = 'ID';
		}
		$page = (1 < $page) ? '/page/' . $page : '';
		$r = bb_get_uri('profile/' . $user->$column . '/' . $tab . $page, null, $context);
	} else {
		$query = array(
			'id' => $user->ID,
			'tab' => $tab,
			'page' => (1 < $page) ? $page : false
		);
		$r = bb_get_uri('profile.php', $query, $context);
	}
	return apply_filters( 'get_profile_tab_link', $r, $user->ID, $context );
}

function user_link( $id = 0 ) {
	echo apply_filters( 'user_link', get_user_link( $id ), $id );
}

function get_user_link( $id = 0 ) {
	if ( $user = bb_get_user( bb_get_user_id( $id ) ) )
		return apply_filters( 'get_user_link', $user->user_url, $user->ID );
}

function full_user_link( $id = 0 ) {
	echo get_full_user_link( $id );
}

function get_full_user_link( $id = 0 ) {
	if ( get_user_link( $id ) )
		$r = '<a href="' . esc_attr( get_user_link( $id ) ) . '">' . get_user_display_name( $id ) . '</a>';
	else
		$r = get_user_display_name( $id );
	return $r;
}

function user_type_label( $type ) {
	echo apply_filters( 'user_type_label', get_user_type_label( $type ), $type );
}

function get_user_type_label( $type ) {
	global $wp_roles;
	if ( $wp_roles->is_role( $type ) )
		return apply_filters( 'get_user_type_label', $wp_roles->role_names[$type], $type );
}

function user_type( $id = 0 ) {
	echo apply_filters( 'user_type', get_user_type( $id ), $id );
}

function get_user_type( $id = 0 ) {
	if ( $user = bb_get_user( bb_get_user_id( $id ) ) ) :
		@$caps = array_keys($user->capabilities);
		if ( !$caps )
			$caps[] = 'inactive';

		$type = get_user_type_label( $caps[0] ); //Just support one role for now.
	else :
		$type = false;
	endif;
	return apply_filters( 'get_user_type', $type, $user->ID );
}

function get_user_name( $id = 0 ) {
	$user = bb_get_user( bb_get_user_id( $id ) );
	return apply_filters( 'get_user_name', $user->user_login, $user->ID );
}

function get_user_display_name( $id = 0 ) {
	$user = bb_get_user( bb_get_user_id( $id ) );
	return apply_filters( 'get_user_display_name', $user->display_name, $user->ID );
}

function user_title( $id = 0 ) {
	echo apply_filters( 'user_title', get_user_title( $id ), bb_get_user_id( $id ) );
}

function get_user_title( $id = 0 ) {
	$user = bb_get_user( bb_get_user_id( $id ) );
	return empty( $user->title ) ? get_user_type( $id ) : apply_filters( 'get_user_title', $user->title, $user->ID );
}

function profile_pages( $args = null )
{
	$defaults = array( 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page, $user;
	$add = apply_filters( 'profile_pages_add', $add );
	if ( $pages = apply_filters( 'profile_pages', get_page_number_links( $page, $user->topics_replied + $add ), $user->user_id ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

function bb_profile_data( $id = 0 ) {
	if ( !$user = bb_get_user( bb_get_user_id( $id ) ) )
		return;

	$reg_time = bb_gmtstrtotime( $user->user_registered );
	$profile_info_keys = bb_get_profile_info_keys();
	echo "<dl id='userinfo'>\n";
	echo "\t<dt>" . __('Member Since') . "</dt>\n";
	echo "\t<dd>" . bb_datetime_format_i18n($reg_time, 'date') . ' (' . bb_since($reg_time) . ")</dd>\n";
	if ( is_array( $profile_info_keys ) ) {
		foreach ( $profile_info_keys as $key => $label ) {
			if ( in_array($key, array('first_name', 'last_name', 'display_name')) || !isset($user->$key) )
				continue;
			$val = 'user_url' == $key ? get_user_link( $user->ID ) : $user->$key;
			if (
				( 'user_email' != $key || ( 'user_email' == $key && bb_current_user_can( 'edit_users' ) ) )
				&& $val
				&& 'http://' != $val
			) {
				echo "\t<dt>{$label[1]}</dt>\n";
				$val = make_clickable( $val );
				$attributes = array();
				if (isset($label[2]) && !empty($label[2]))
					if (preg_match("#^<a#i", $val))
						$val = preg_replace("#^<a#i", '<a class="' . esc_attr($label[2]) . '"', $val);
					else
						$val = '<span class="' . esc_attr($label[2]) . '">' . $val . '</span>';
				
				echo "\t<dd>" . $val . "</dd>\n";
			}
		}
	}
	echo "</dl>\n";
}

function bb_profile_base_content() {
	global $self;
	if ( !is_callable( $self ) )
		return; // should never happen
	call_user_func( $self );
}

function bb_profile_data_form( $id = 0 ) {
	global $errors;
	if ( !$user = bb_get_user( bb_get_user_id( $id ) ) )
		return;

	if ( !bb_current_user_can( 'edit_user', $user->ID ) )
		return;

	$error_codes = $errors->get_error_codes();
	$profile_info_keys = bb_get_profile_info_keys();
	$required = false;
?>
<table id="userinfo">
<?php
	if ( is_array($profile_info_keys) ) :
		$bb_current_id = bb_get_current_user_info( 'id' );
		foreach ( $profile_info_keys as $key => $label ) :
			if ( $label[0] ) {
				$class = 'form-field form-required required';
				$required = true;
			} else {
				$class = 'form-field';
			}
			$title = esc_attr( $label[1] );

			$name = esc_attr( $key );
			$type = isset($label[2]) ? esc_attr( $label[2] ) : 'text';
			if ( !in_array( $type, array( 'checkbox', 'file', 'hidden', 'image', 'password', 'radio', 'text' ) ) ) {
				$type = 'text';
			}

			$checked = false;
			if ( in_array( $key, $error_codes ) ) {
				$class .= ' form-invalid error';
				$data = $errors->get_error_data( $key );
				if ( 'checkbox' == $type ) {
					if ( isset($data['data']) )
						$checked = $data['data'];
					else
						$checked = $_POST[$key];
					$value = $label[3];
					$checked = $checked == $value;
				} else {
					if ( isset($data['data']) )
						$value = $data['data'];
					else
						$value = $_POST[$key];
				}

				$message = esc_html( $errors->get_error_message( $key ) );
				$message = "<em>$message</em>";
			} else {
				if ( 'checkbox' == $type ) {
					$checked = $user->$key == $label[3] || $label[4] == $label[3];
					$value = $label[3];
				} else {
					$value = isset($user->$key) ? $user->$key : '';
				}
				$message = '';
			}

			$checked = $checked ? ' checked="checked"' : '';
			$value = esc_attr( $value );

?>

<tr class="<?php echo $class; ?>">
	<th scope="row">
		<label for="<?php echo $name; ?>"><?php echo $title; ?></label>
		<?php echo $message; ?>
	</th>
	<td>
<?php
			if ($key == 'display_name') {
?>
		<select name="display_name" id="display_name">
<?php
				$public_display = array();
				$public_display['display_displayname'] = $user->display_name;
				//$public_display['display_nickname'] = $user->nickname;
				$public_display['display_username'] = $user->user_login;
				if ( isset($user->first_name) ) {
					$public_display['display_firstname'] = $user->first_name;
					if ( isset($user->last_name) ) {
						$public_display['display_firstlast'] = $user->first_name.' '.$user->last_name;
						$public_display['display_lastfirst'] = $user->last_name.' '.$user->first_name;
					}
				}
				if ( isset($user->last_name) )
					$public_display['display_lastname'] = $user->last_name;
				
				$public_display = array_unique(array_filter(array_map('trim', $public_display)));
				
				foreach($public_display as $id => $item) {
?>
			<option id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $item ); ?>"><?php echo esc_html( $item ); ?></option>
<?php
				}
?>
		</select>
<?php
			} else {
?>
		<?php if ( 'checkbox' == $type && isset($label[5]) ) echo '<label for="' . $name . '">'; ?>
		<input name="<?php echo $name; ?>" id="<?php echo $name; ?>" type="<?php echo $type; ?>"<?php echo $checked; ?> value="<?php echo $value; ?>" />
		<?php if ( 'checkbox' == $type && isset($label[5]) ) echo esc_html( $label[5] ) . '</label>'; ?>
<?php
			}
?>
	</td>
</tr>

<?php endforeach; endif; // $profile_info_keys; $profile_info_keys ?>

</table>

<?php bb_nonce_field( 'edit-profile_' . $user->ID ); if ( $required ) : ?>

<p class="required-message"><?php _e('These items are <span class="required">required</span>.') ?></p>

<?php
	endif;
	do_action( 'extra_profile_info', $user->ID );
}

function bb_profile_admin_form( $id = 0 ) {
	global $wp_roles, $errors;
	if ( !$user = bb_get_user( bb_get_user_id( $id ) ) )
		return;

	if ( !bb_current_user_can( 'edit_user', $user->ID ) )
		return;

	$error_codes = $errors->get_error_codes();
	$bb_current_id = bb_get_current_user_info( 'id' );

	$profile_admin_keys = bb_get_profile_admin_keys();
	$assignable_caps = bb_get_assignable_caps();
	$required = false;

	$roles = $wp_roles->role_names;
	$can_keep_gate = bb_current_user_can( 'keep_gate' );

	// Keymasters can't demote themselves
	if ( ( $bb_current_id == $user->ID && $can_keep_gate ) || ( isset( $user->capabilities ) && is_array( $user->capabilities ) && array_key_exists('keymaster', $user->capabilities) && !$can_keep_gate ) ) {
		$roles = array( 'keymaster' => $roles['keymaster'] );
	} elseif ( !$can_keep_gate ) { // only keymasters can promote others to keymaster status
		unset($roles['keymaster']);
	}

	$selected = array( 'inactive' => ' selected="selected"' );
?>
<table id="admininfo">
<tr class='form-field<?php if ( in_array( 'role', $error_codes ) ) echo ' form-invalid error'; ?>'>
	<th scope="row">
		<label for="admininfo_role"><?php _e('User Type'); ?></label>
		<?php if ( in_array( 'role', $error_codes ) ) echo '<em>' . $errors->get_error_message( 'role' ) . '</em>'; ?>
	</th>
	<td>
		<select id="admininfo_role" name="role">
<?php
	foreach( $roles as $r => $n ) {
		if ( isset( $user->capabilities ) && is_array( $user->capabilities ) && array_key_exists( $r, $user->capabilities ) ) {
			$selected['inactive'] = '';
			$selected[$r] = ' selected="selected"';
		} elseif ( $r !== 'inactive' ) {
			$selected[$r] = '';
		}
?>
			<option value="<?php echo $r; ?>"<?php echo $selected[$r]; ?>><?php echo $n; ?></option>
<?php
	}
?>
		</select>
	</td>
</tr>
<?php
	if (count($assignable_caps)) :
?>
<tr class="extra-caps-row">
	<th scope="row"><?php _e('Allow this user to'); ?></th>
	<td>
<?php
	foreach( $assignable_caps as $cap => $label ) :
		$name = esc_attr( $cap );
		$checked = '';
		if ( isset( $user->capabilities ) && is_array( $user->capabilities ) && array_key_exists( $cap, $user->capabilities ) ) {
			$checked = ' checked="checked"';
		}
		$label = esc_html( $label );
?>

		<label><input name="<?php echo $name; ?>" value="1" type="checkbox"<?php echo $checked; ?> /> <?php echo $label; ?></label><br />

<?php endforeach; ?>

	</td>
</tr>

<?php
	endif;
	
	if ( is_array($profile_admin_keys) ) :
		foreach ( $profile_admin_keys as $key => $label ) :
			if ( $label[0] ) {
				$class = 'form-field form-required required';
				$required = true;
			} else {
				$class = 'form-field';
			}
			$title = esc_attr( $label[1] );

			$name = esc_attr( $key );
			$type = isset($label[2]) ? esc_attr( $label[2] ) : 'text';

			$checked = false;
			if ( in_array( $key, $error_codes ) ) {
				$class .= ' form-invalid error';
				$data = $errors->get_error_data( $key );
				if ( 'checkbox' == $type ) {
					if ( isset($data['data']) )
						$checked = $data['data'];
					else
						$checked = $_POST[$key];
					$value = $label[3];
					$checked = $checked == $value;
				} else {
					if ( isset($data['data']) )
						$value = $data['data'];
					else
						$value = $_POST[$key];
				}

				$message = esc_html( $errors->get_error_message( $key ) );
				$message = "<em>$message</em>";
			} else {
				if ( 'checkbox' == $type ) {
					$checked = $user->$key == $label[3] || $label[4] == $label[3];
					$value = $label[3];
				} else {
					$value = isset($user->$key) ? $user->$key : '';
				}
				$message = '';
			}

			$checked = $checked ? ' checked="checked"' : '';
			$value = esc_attr( $value );

?>

<tr class="<?php echo $class; ?>">
	<th scope="row">
		<label for="<?php echo $name; ?>"><?php echo $title ?></label>
		<?php echo $message; ?>
	</th>
	<td>
		<?php if ( 'checkbox' == $type && isset($label[5]) ) echo "<label for='$name'>"; ?>
		<input name="<?php echo $name; ?>" id="<?php echo $name; ?>" type="<?php echo $type; ?>"<?php echo $checked; ?> value="<?php echo $value; ?>" />
		<?php if ( 'checkbox' == $type && isset($label[5]) ) echo esc_html( $label[5] ) . "</label>"; ?>
	</td>
</tr>

<?php endforeach; endif; // $profile_admin_keys; $profile_admin_keys ?>

</table>

<?php if ( $required ) : ?>
<p class="required-message"><?php _e('These items are <span class="required">required</span>.') ?></p>

<?php endif; ?>
<p><?php _e('Inactive users can login and look around but not do anything. Blocked users just see a simple error message when they visit the site.'); ?></p>
<p><?php _e('<strong>Note</strong>: Blocking a user does <em>not</em> block any IP addresses.'); ?></p>
<?php
}

function bb_profile_password_form( $id = 0 ) {
	global $errors;
	if ( !$user = bb_get_user( bb_get_user_id( $id ) ) )
		return;

	if ( !bb_current_user_can( 'change_user_password', $user->ID ) )
		return;

	$class = 'form-field';

	if ( $message = $errors->get_error_message( 'pass' ) ) {
		$class .= ' form-invalid error';
		$message = '<em>' . esc_html( $message ) . '</em>';
	}
?>

<table>
<tr class="<?php echo $class; ?>">
	<th scope="row" rowspan="2">
		<label for="pass1"><?php _e('New password'); ?></label>
		<?php echo $message; ?>
	</th>
	<td>
		<input name="pass1" type="password" id="pass1" autocomplete="off" />
	</td>
</tr>
<tr class="<?php echo $class; ?>">
	<td>
		<input name="pass2" type="password" id="pass2" autocomplete="off" />
	</td>
</tr>
<tr class="pass-strength">
	<th scope="row"><?php _e('Password Strength'); ?></th>
	<td>
		<input type="hidden" name="user_login" id="user_login" value="<?php echo $user->user_login; ?>" />
		<noscript>
			<?php _e('Disabled (requires JavaScript)'); ?>
		</noscript>
		<script type="text/javascript" charset="utf-8">
			if (typeof jQuery != 'undefined') {
				document.writeln('<div id="pass-strength-result">' + pwsL10n.short + '</div>');
			} else {
				document.writeln('<?php echo str_replace("'", "\'", __('Disabled (requires jQuery)')); ?>')
			}
		</script>
	</td>
</tr>
</table>

<p><?php _e('Hint: Use upper and lower case characters, numbers and symbols like !"?$%^&amp;( in your password.'); ?></p>

<?php

}

function bb_logout_link( $args = '' ) {
	echo apply_filters( 'bb_logout_link', bb_get_logout_link( $args ), $args );
}

function bb_get_logout_link( $args = '' ) {
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'text' => $args );

	$defaults = array('text' => __('Log Out'), 'before' => '', 'after' => '', 'redirect' => '');
	$args = wp_parse_args( $args, $defaults );
	extract($args, EXTR_SKIP);

	$query = array( 'logout' => 1 );
	if ( $redirect ) {
		$query['re'] = $redirect;
	}

	$uri = esc_attr( bb_get_uri('bb-login.php', $query, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_USER_FORMS) );

	return apply_filters( 'bb_get_logout_link', $before . '<a href="' . $uri . '">' . $text . '</a>' . $after, $args );
}

function bb_admin_link( $args = '' ) {
	if ( !bb_current_user_can( 'moderate' ) )
		return;
	echo apply_filters( 'bb_admin_link', bb_get_admin_link( $args ), $args );
}

function bb_get_admin_link( $args = '' ) {
	if ( !bb_current_user_can( 'moderate' ) )
		return;
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'text' => $args );

	$defaults = array('text' => __('Admin'), 'before' => '', 'after' => '');
	$args = wp_parse_args( $args, $defaults );
	extract($args, EXTR_SKIP);

	$uri = esc_attr( bb_get_uri('bb-admin/', null, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN) );

	return apply_filters( 'bb_get_admin_link', $before . '<a href="' . $uri . '">' . $text . '</a>' . $after, $args );
}

function bb_profile_link( $args = '' ) {
	echo apply_filters( 'bb_profile_link', bb_get_profile_link( $args ), $args );
}

function bb_get_profile_link( $args = '' ) {
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'text' => $args );
	elseif ( is_numeric($args) )
		$args = array( 'id' => $args );

	$defaults = array( 'text' => __('View your profile'), 'before' => '', 'after' => '', 'id' => false );
	$args = wp_parse_args( $args, $defaults );
	extract($args, EXTR_SKIP);

	$id = (int) $id;
	if ( !$id )
		$id = bb_get_current_user_info( 'id' );

	return apply_filters( 'bb_get_profile_link', "$before<a href='" . esc_attr( get_user_profile_link( $id ) ) . "'>$text</a>$after", $args );
}

function bb_current_user_info( $key = '' ) {
	if ( !$key )
		return;
	echo apply_filters( 'bb_current_user_info', bb_get_current_user_info( $key ), $key );
}
	

function bb_get_current_user_info( $key = '' ) {
	if ( !is_string($key) )
		return;
	if ( !$user = bb_get_current_user() ) // Not globalized
		return false;

	switch ( $key ) :
	case '' :
		return $user;
		break;
	case 'id' :
	case 'ID' :
		return (int) $user->ID;
		break;
	case 'name' :
		return get_user_display_name( $user->ID );
		break;
	case 'login' :
	case 'user_login' :
		return get_user_name( $user->ID );
		break;
	case 'email' :
	case 'user_email' :
		return bb_get_user_email( $user->ID );
		break;
	case 'url' :
	case 'uri' :
	case 'user_url' :
		return get_user_link( $user->ID );
		break;
	endswitch;
}

function bb_get_user_email( $id ) {
	if ( !$user = bb_get_user( bb_get_user_id( $id ) ) )
		return false;

	return apply_filters( 'bb_get_user_email', $user->user_email, $id );
}

//TAGS
function topic_tags()
{
	global $tags, $tag, $topic_tag_cache, $user_tags, $other_tags, $topic;
	if ( is_array( $tags ) || bb_current_user_can( 'edit_tag_by_on', bb_get_current_user_info( 'id' ), $topic->topic_id ) ) {
		bb_load_template( 'topic-tags.php', array('user_tags', 'other_tags', 'public_tags') );
	}
}

function bb_tag_page_link()
{
	echo bb_get_tag_page_link();
}

function bb_get_tag_page_link( $context = BB_URI_CONTEXT_A_HREF )
{
	if ( bb_get_option( 'mod_rewrite' ) ) {
		$r = bb_get_uri( 'tags/', null, $context );
	} else {
		$r = bb_get_uri( 'tags.php', null, $context );
	}
	return apply_filters( 'bb_get_tag_page_link', $r, $context );
}

function bb_tag_link( $tag_id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF )
{
	echo apply_filters( 'bb_tag_link', bb_get_tag_link( $tag_id, $page, $context ), $tag_id, $page, $context );
}

function bb_get_tag_link( $tag_id = 0, $page = 1, $context = BB_URI_CONTEXT_A_HREF )
{
	global $tag;

	if ( $tag_id ) {
		if ( is_object( $tag_id ) ) {
			$_tag = $tag_id;
		} else {
			$_tag = bb_get_tag( $tag_id );
		}
	} else {
		$_tag =& $tag;
	}

	if ( !is_object( $_tag ) ) {
		return '';
	}

	if ( !$context || !is_integer( $context ) ) {
		$context = BB_URI_CONTEXT_A_HREF;
	}

	if ( bb_get_option( 'mod_rewrite' ) ) {
		$page = (1 < $page) ? '/page/' . $page : '';
		$r = bb_get_uri( 'tags/' . $_tag->tag . $page, null, $context );
	} else {
		$query = array(
			'tag' => $_tag->tag,
			'page' => ( 1 < $page ) ? $page : false
		);
		$r = bb_get_uri( 'tags.php', $query, $context );
	}

	return apply_filters( 'bb_get_tag_link', $r, $_tag->tag, $page, $context );
}

function bb_tag_link_base()
{
	echo bb_get_tag_link_base();
}

function bb_get_tag_link_base()
{
	return bb_get_tag_page_link() . ( bb_get_option( 'mod_rewrite' ) ? '' : '?tag=' );
}

function bb_tag_name( $tag_id = 0 )
{
	echo esc_html( bb_get_tag_name( $tag_id ) );
}

function bb_get_tag_name( $tag_id = 0 ) {
	global $tag;

	if ( $tag_id ) {
		if ( is_object( $tag_id ) ) {
			$_tag = $tag_id;
		} else {
			$_tag = bb_get_tag( $tag_id );
		}
	} else {
		$_tag =& $tag;
	}

	if ( !is_object( $_tag ) ) {
		return '';
	}

	return $_tag->raw_tag;
}

function bb_tag_posts_rss_link( $tag_id = 0, $context = 0 )
{
	if ( !$context || !is_integer( $context ) ) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}

	echo apply_filters( 'tag_posts_rss_link', bb_get_tag_posts_rss_link( $tagid, $context ), $tag_id, $context );
}

function bb_get_tag_posts_rss_link( $tag_id = 0, $context = 0 )
{
	global $tag;

	if ( $tag_id ) {
		if ( is_object( $tag_id ) ) {
			$_tag = $tag_id;
		} else {
			$_tag = bb_get_tag( $tag_id );
		}
	} else {
		$_tag =& $tag;
	}

	if ( !is_object( $_tag ) ) {
		return '';
	}

	if ( !$context || !is_integer( $context ) ) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}

	if ( bb_get_option( 'mod_rewrite' ) ) {
		$link = bb_get_uri( 'rss/tags/' . $_tag->tag, null, $context );
	} else {
		$link = bb_get_uri( 'rss.php', array( 'tag' => $_tag->tag ), $context );
	}

	return apply_filters( 'get_tag_posts_rss_link', $link, $tag_id, $context );
}

function bb_tag_topics_rss_link( $tag_id = 0, $context = 0 )
{
	if ( !$context || !is_integer( $context ) ) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}

	echo apply_filters( 'tag_topics_rss_link', bb_get_tag_topics_rss_link( $tag_id, $context ), $tag_id, $context );
}

function bb_get_tag_topics_rss_link( $tag_id = 0, $context = 0 )
{
	global $tag;

	if ( $tag_id ) {
		if ( is_object( $tag_id ) ) {
			$_tag = $tag_id;
		} else {
			$_tag = bb_get_tag( $tag_id );
		}
	} else {
		$_tag =& $tag;
	}

	if ( !is_object( $_tag ) ) {
		return '';
	}

	if ( !$context || !is_integer( $context ) ) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}

	if ( bb_get_option( 'mod_rewrite' ) ) {
		$link = bb_get_uri( 'rss/tags/' . $_tag->tag . '/topics', null, $context );
	} else {
		$link = bb_get_uri( 'rss.php', array('tag' => $_tag->tag, 'topics' => 1 ), $context );
	}

	return apply_filters( 'get_tag_topics_rss_link', $link, $tag_id, $context );
}

function bb_list_tags( $args = null )
{
	$defaults = array(
		'tags' => false,
		'format' => 'list',
		'topic' => 0,
		'list_id' => 'tags-list'
	);

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	if ( !$topic = get_topic( get_topic_id( $topic ) ) ) {
		return false;
	}

	if ( !is_array( $tags ) ) {
		$tags = bb_get_topic_tags( $topic->topic_id );
	}

	if ( !$tags ) {
		return false;
	}

	$list_id = esc_attr( $list_id );

	$r = '';
	switch ( strtolower( $format ) ) {
		case 'table' :
			break;

		case 'list' :
		default :
			$args['format'] = 'list';
			$r .= '<ul id="' . $list_id . '" class="tags-list list:tag">' . "\n";
			foreach ( $tags as $tag ) {
				$r .= _bb_list_tag_item( $tag, $args );
			}
			$r .= '</ul>';
			break;
	}

	echo $r;
}

function _bb_list_tag_item( $tag, $args )
{
	$url = esc_url( bb_get_tag_link( $tag ) );
	$name = esc_html( bb_get_tag_name( $tag ) );
	if ( 'list' == $args['format'] ) {
		$id = 'tag-' . $tag->tag_id . '_' . $tag->user_id;
		return "\t" . '<li id="' . $id . '"' . get_alt_class( 'topic-tags' ) . '><a href="' . $url . '" rel="tag">' . $name . '</a> ' . bb_get_tag_remove_link( array( 'tag' => $tag, 'list_id' => $args['list_id'] ) ) . '</li>' . "\n";
	}
}
	
function tag_form( $args = null )
{
	$defaults = array( 'topic' => 0, 'submit' => __('Add &raquo;'), 'list_id' => 'tags-list' );
	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	if ( !$topic = get_topic( get_topic_id( $topic ) ) ) {
		return false;
	}

	if ( !bb_current_user_can( 'edit_tag_by_on', bb_get_current_user_info( 'id' ), $topic->topic_id ) ) {
		return false;
	}

	global $page;
?>

<form id="tag-form" method="post" action="<?php bb_uri('tag-add.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN); ?>" class="add:<?php echo esc_attr( $list_id ); ?>:">
	<p>
		<input name="tag" type="text" id="tag" />
		<input type="hidden" name="id" value="<?php echo $topic->topic_id; ?>" />
		<input type="hidden" name="page" value="<?php echo $page; ?>" />
		<?php bb_nonce_field( 'add-tag_' . $topic->topic_id ); ?>
		<input type="submit" name="submit" id="tagformsub" value="<?php echo esc_attr( $submit ); ?>" />
	</p>
</form>

<?php
}

function manage_tags_forms()
{
	global $tag;
	if ( !bb_current_user_can( 'manage_tags' ) ) {
		return false;
	}

	$form  = '<ul id="manage-tags">' . "\n";
	$form .= '<li id="tag-rename">' . __('Rename tag:') . "\n\t";
	$form .= '<form method="post" action="' . bb_get_uri( 'bb-admin/tag-rename.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ) . '"><div>' . "\n\t";
	$form .= '<input type="text" name="tag" size="10" maxlength="30" />' . "\n\t";
	$form .= '<input type="hidden" name="id" value="' . $tag->tag_id . '" />' . "\n\t";
	$form .= "<input type='submit' name='Submit' value='" . __('Rename') . "' />\n\t";
	echo $form;
	bb_nonce_field( 'rename-tag_' . $tag->tag_id );
	echo "\n\t</div></form>\n  </li>\n ";
	$form  = "<li id='tag-merge'>" . __('Merge this tag into:') . "\n\t";
	$form .= "<form method='post' action='" . bb_get_uri('bb-admin/tag-merge.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN) . "'><div>\n\t";
	$form .= "<input type='text' name='tag' size='10' maxlength='30' />\n\t";
	$form .= "<input type='hidden' name='id' value='$tag->tag_id' />\n\t";
	$form .= "<input type='submit' name='Submit' value='" . __('Merge') . "' ";
	$form .= 'onclick="return confirm(\'' . esc_js( sprintf(__('Are you sure you want to merge the "%s" tag into the tag you specified? This is permanent and cannot be undone.'), $tag->raw_tag) ) . "');\" />\n\t";
	echo $form;
	bb_nonce_field( 'merge-tag_' . $tag->tag_id );
	echo "\n\t</div></form>\n  </li>\n ";
	$form  = "<li id='tag-destroy'>" . __('Destroy tag:') . "\n\t";
	$form .= "<form method='post' action='" . bb_get_uri('bb-admin/tag-destroy.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN) . "'><div>\n\t";
	$form .= "<input type='hidden' name='id' value='$tag->tag_id' />\n\t";
	$form .= "<input type='submit' name='Submit' value='" . __('Destroy') . "' ";
	$form .= 'onclick="return confirm(\'' . esc_js( sprintf(__('Are you sure you want to destroy the "%s" tag? This is permanent and cannot be undone.'), $tag->raw_tag) ) . "');\" />\n\t";
	echo $form;
	bb_nonce_field( 'destroy-tag_' . $tag->tag_id );
	echo "\n\t</div></form>\n  </li>\n</ul>";
}

function bb_tag_remove_link( $args = null ) {
	echo bb_get_tag_remove_link( $args );
}

function bb_get_tag_remove_link( $args = null ) {
	if ( is_scalar($args) || is_object( $args ) )
		$args = array( 'tag' => $args );
	$defaults = array( 'tag' => 0, 'topic' => 0, 'list_id' => 'tags-list' );
	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	if ( is_object( $tag ) && isset( $tag->tag_id ) ); // [sic]
	elseif ( !$tag = bb_get_tag( bb_get_tag_id( $tag ) ) )
		return false;
	if ( !$topic = get_topic( get_topic_id( $topic ) ) )
		return false;
	if ( !bb_current_user_can( 'edit_tag_by_on', $tag->user_id, $topic->topic_id ) )
		return false;
	$url = bb_get_uri('tag-remove.php', array('tag' => $tag->tag_id, 'user' => $tag->user_id, 'topic' => $topic->topic_id) );
	$url = esc_url( bb_nonce_url( $url, 'remove-tag_' . $tag->tag_id . '|' . $topic->topic_id) );
	$title = esc_attr__( 'Remove this tag' );
	$list_id = esc_attr( $list_id );
	return "[<a href='$url' class='delete:$list_id:tag-{$tag->tag_id}_{$tag->user_id}' title='$title'>&times;</a>]";
}

function bb_tag_heat_map( $args = '' ) {
	$defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'limit' => 40, 'format' => 'flat' );
	$args = wp_parse_args( $args, $defaults );

	if ( 1 < $fn = func_num_args() ) : // For back compat
		$args['smallest'] = func_get_arg(0);
		$args['largest']  = func_get_arg(1);
		$args['unit']     = 2 < $fn ? func_get_arg(2) : $unit;
		$args['limit']    = 3 < $fn ? func_get_arg(3) : $limit;
	endif;

	extract($args, EXTR_SKIP);

	$tags = bb_get_top_tags( array( 'number' => $limit ) );

	if ( empty($tags) )
		return;

	$r = bb_get_tag_heat_map( $tags, $args );
	echo apply_filters( 'tag_heat_map', $r, $args );
}

function bb_get_tag_heat_map( $tags, $args = '' ) {
	$defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'limit' => 45, 'format' => 'flat' );
	$args = wp_parse_args( $args, $defaults );
	extract($args, EXTR_SKIP);

	if ( !$tags )
		return;

	foreach ( (array) $tags as $tag ) {
		$counts{$tag->raw_tag} = $tag->tag_count;
		$taglinks{$tag->raw_tag} = bb_get_tag_link( $tag );
	}

	$min_count = min($counts);
	$spread = max($counts) - $min_count;
	if ( $spread <= 0 )
		$spread = 1;
	$fontspread = $largest - $smallest;
	if ( $fontspread <= 0 )
		$fontspread = 1;
	$fontstep = $fontspread / $spread;

	do_action_ref_array( 'sort_tag_heat_map', array(&$counts) );

	$a = array();

	foreach ( $counts as $tag => $count ) {
		$taglink = esc_attr($taglinks{$tag});
		$tag = str_replace(' ', '&nbsp;', esc_html( $tag ));
		$fontsize = round( $smallest + ( ( $count - $min_count ) * $fontstep ), 1 );
		$a[] = "<a href='$taglink' title='" . esc_attr( sprintf( __('%d topics'), $count ) ) . "' rel='tag' style='font-size:$fontsize$unit;'>$tag</a>";
	}

	switch ( $format ) :
	case 'array' :
		$r =& $a;
		break;
	case 'list' :
		$r = "<ul class='bb-tag-heat-map'>\n\t<li>";
		$r .= join("</li>\n\t<li>", $a);
		$r .= "</li>\n</ul>\n";
		break;
	default :
		$r = join("\n", $a);
		break;
	endswitch;

	return apply_filters( 'bb_get_tag_heat_map', $r, $tags, $args );
}

function bb_sort_tag_heat_map( &$tag_counts ) {
	uksort($tag_counts, 'strnatcasecmp');
}

function tag_pages( $args = null )
{
	$defaults = array( 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page, $tagged_topic_count;
	if ( $pages = apply_filters( 'tag_pages', get_page_number_links( $page, $tagged_topic_count ) ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

function bb_forum_dropdown( $args = '' ) {
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'callback' => $args );
	if ( 1 < func_num_args() )
		$args['callback_args'] = func_get_arg(1);
	echo bb_get_forum_dropdown( $args );
}

function bb_get_forum_dropdown( $args = '' ) {
	$defaults = array( 'callback' => false, 'callback_args' => false, 'id' => 'forum_id', 'none' => false, 'selected' => false, 'tab' => false, 'hierarchical' => 1, 'depth' => 0, 'child_of' => 0, 'disable_categories' => 1, 'options_only' => false );
	if ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array( 'callback' => $args );
	if ( 1 < func_num_args() )
		$args['callback_args'] = func_get_arg(1);

	$args = wp_parse_args( $args, $defaults );

	extract($args, EXTR_SKIP);

	if ( !bb_forums( $args ) )
		return;

	global $forum_id, $forum;
	$old_global = $forum;

	$name = esc_attr( $id );
	$id = str_replace( '_', '-', $name );
	$tab = (int) $tab;

	if ( $none && 1 == $none )
		$none = __('- None -');

	$r = '';
	if ( !$options_only ) {
		if ( $tab ) {
			$tab = ' tabindex="' . $tab . '"';
		} else {
			$tab = '';
		}
		$r .= '<select name="' . $name . '" id="' . $id . '"' . $tab . '">' . "\n";
	}
	if ( $none )
		$r .= "\n" . '<option value="0">' . $none . '</option>' . "\n";

	$no_option_selected = true;
	$options = array();
	while ( $depth = bb_forum() ) :
		global $forum; // Globals + References = Pain
		$pad_left = str_repeat( '&nbsp;&nbsp;&nbsp;', $depth - 1 );
		if ( $disable_categories && isset($forum->forum_is_category) && $forum->forum_is_category ) {
			$options[] = array(
				'value' => 0,
				'display' => $pad_left . $forum->forum_name,
				'disabled' => true,
				'selected' => false
			);
			continue;
		}
		$_selected = false;
		if ( (!$selected && $forum_id == $forum->forum_id) || $selected == $forum->forum_id ) {
			$_selected = true;
			$no_option_selected = false;
		}
		$options[] = array(
			'value' => $forum->forum_id,
			'display' => $pad_left . $forum->forum_name,
			'disabled' => false,
			'selected' => $_selected
		);
	endwhile;

	if ( 1 === count( $options ) && !$none ) {
		foreach ( $options as $option_index => $option_value ) {
			if ( $option_value['disabled'] ) {
				return;
			}
			return '<input type="hidden" name="' . $name . '" id="' . $id . '" value="' . esc_attr( $option_value['value'] ) . '" /><span>' . esc_html( $option_value['display'] ) . '</span>';
		}
	}

	foreach ($options as $option_index => $option_value) {
		if (!$none && !$selected && $no_option_selected && !$option_value['disabled']) {
			$option_value['selected'] = true;
			$no_option_selected = false;
		}
		$option_disabled = $option_value['disabled'] ? ' disabled="disabled"' : '';
		$option_selected = $option_value['selected'] ? ' selected="selected"' : '';
		$r .= "\n" . '<option value="' . esc_attr( $option_value['value'] ) . '"' . $option_disabled . $option_selected . '>' . esc_html( $option_value['display'] ) . '</option>' . "\n";
	}
	
	$forum = $old_global;
	if ( !$options_only )
		$r .= '</select>' . "\n";

	return $r;
}

//FAVORITES
function favorites_link( $user_id = 0 ) {
	echo apply_filters( 'favorites_link', get_favorites_link( $user_id ) );
}

function get_favorites_link( $user_id = 0 ) {
	if ( !$user_id )
		$user_id = bb_get_current_user_info( 'id' );
	return apply_filters( 'get_favorites_link', get_profile_tab_link($user_id, 'favorites'), $user_id );
}

function user_favorites_link($add = array(), $rem = array(), $user_id = 0) {
	global $topic, $bb_current_user;
	if ( empty($add) || !is_array($add) )
		$add = array('mid' => __('Add this topic to your favorites'), 'post' => __(' (%?%)'));
	if ( empty($rem) || !is_array($rem) )
		$rem = array( 'pre' => __('This topic is one of your %favorites% ['), 'mid' => __('&times;'), 'post' => __(']'));
	if ( $user_id ) :
		if ( !bb_current_user_can( 'edit_favorites_of', (int) $user_id ) )
			return false;
		if ( !$user = bb_get_user( bb_get_user_id( $user_id ) ) ) :
			return false;
		endif;
	else :
		if ( !bb_current_user_can('edit_favorites') )
			return false;
		$user =& $bb_current_user->data;
	endif;

        $url = esc_url( get_favorites_link( $user_id ) );
	if ( $is_fav = is_user_favorite( $user->ID, $topic->topic_id ) ) :
		$rem = preg_replace('|%(.+)%|', "<a href='$url'>$1</a>", $rem);
		$favs = array('fav' => '0', 'topic_id' => $topic->topic_id);
		$pre  = ( is_array($rem) && isset($rem['pre'])  ) ? $rem['pre']  : '';
		$mid  = ( is_array($rem) && isset($rem['mid'])  ) ? $rem['mid']  : ( is_string($rem) ? $rem : '' );
		$post = ( is_array($rem) && isset($rem['post']) ) ? $rem['post'] : '';
	elseif ( false === $is_fav ) :
		$add = preg_replace('|%(.+)%|', "<a href='$url'>$1</a>", $add);
		$favs = array('fav' => '1', 'topic_id' => $topic->topic_id);
		$pre  = ( is_array($add) && isset($add['pre'])  ) ? $add['pre']  : '';
		$mid  = ( is_array($add) && isset($add['mid'])  ) ? $add['mid']  : ( is_string($add) ? $add : '' );
		$post = ( is_array($add) && isset($add['post']) ) ? $add['post'] : '';
	endif;

	$url = esc_url(  bb_nonce_url( add_query_arg( $favs, get_favorites_link( $user_id ) ), 'toggle-favorite_' . $topic->topic_id ) );

	if (  !is_null($is_fav) )
		echo "<span id='favorite-$topic->topic_id'>$pre<a href='$url' class='dim:favorite-toggle:favorite-$topic->topic_id:is-favorite'>$mid</a>$post</span>";
}

function favorites_rss_link( $id = 0, $context = 0 ) {
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	echo apply_filters('favorites_rss_link', get_favorites_rss_link( $id, $context ), $context, $id);
}

function get_favorites_rss_link( $id = 0, $context = 0 ) {
	$user = bb_get_user( bb_get_user_id( $id ) );
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_FEED;
	}
	
	$rewrite = bb_get_option( 'mod_rewrite' );
	if ( $rewrite ) {
		if ( $rewrite === 'slugs' ) {
			$column = 'user_nicename';
		} else {
			$column = 'ID';
		}
		$link = bb_get_uri('rss/profile/' . $user->$column, null, $context);
	} else {
		$link = bb_get_uri('rss.php', array('profile' => $user->ID), $context);
	}
	return apply_filters( 'get_favorites_rss_link', $link, $user->ID, $context );
}

function favorites_pages( $args = null )
{
	$defaults = array( 'before' => '', 'after' => '' );
	$args = wp_parse_args( $args, $defaults );

	global $page, $user, $favorites_total;
	if ( $pages = apply_filters( 'favorites_pages', get_page_number_links( $page, $favorites_total ), $user->user_id ) ) {
		echo $args['before'] . $pages . $args['after'];
	}
}

//VIEWS
function view_name( $view = '' ) { // Filtration should be done at bb_register_view()
	echo get_view_name( $view );
}

function get_view_name( $_view = '' ) {
	global $view, $bb_views;
	if ( $_view )
		$v = bb_slug_sanitize($_view);
	else
		$v =& $view;

	if ( isset($bb_views[$v]) )
		return $bb_views[$v]['title'];
}

function view_pages() {
	global $page, $view_count;
	echo apply_filters( 'view_pages', get_page_number_links( $page, $view_count ) );
}

function view_link( $_view = false, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	echo get_view_link( $_view, $page, $context );
}

function get_view_link( $_view = false, $page = 1, $context = BB_URI_CONTEXT_A_HREF ) {
	global $view, $bb_views;
	if ( $_view )
		$v = bb_slug_sanitize($_view);
	else
		$v =& $view;
	
	if (!$context || !is_integer($context)) {
		$context = BB_URI_CONTEXT_A_HREF;
	}
	
	if ( !array_key_exists($v, $bb_views) )
		return bb_get_uri(null, null, $context);
	if ( bb_get_option('mod_rewrite') ) {
		$page = ( 1 < $page ) ? '/page/' . $page : '';
		$link = bb_get_uri('view/' . $v . $page, null, $context);
	} else {
		$query = array(
			'view' => $v,
			'page' => ( 1 < $page ) ? $page : false,
		);
		$link = bb_get_uri('view.php', $query, $context);
	}

	return apply_filters( 'get_view_link', $link, $v, $page, $context );
}

function _bb_parse_time_function_args( $args ) {
	if ( is_numeric($args) )
		$args = array('id' => $args);
	elseif ( $args && is_string($args) && false === strpos($args, '=') )
		$args = array('format' => $args);

	$defaults = array( 'id' => 0, 'format' => 'since', 'more' => 0, 'localize' => true );
	return wp_parse_args( $args, $defaults );
}

function _bb_time_function_return( $time, $args ) {
	$time = bb_gmtstrtotime( $time );

	switch ( $format = $args['format'] ) :
	case 'since' :
		return bb_since( $time, $args['more'] );
		break;
	case 'timestamp' :
		$format = 'U';
		break;
	case 'mysql' :
		$format = 'Y-m-d H:i:s';
		break;
	endswitch;

	if ( $args['localize'] ) {
		return bb_gmdate_i18n( $format, $time );
	} else {
		return gmdate( $format, $time );
	}
}

function bb_template_scripts() {
	if ( bb_is_topic() && bb_is_user_logged_in() )
		wp_enqueue_script( 'topic' );
	elseif ( bb_is_profile() && bb_is_user_logged_in() ) {
		global $self;
		if ($self == 'profile-edit.php') {
			wp_enqueue_script( 'profile-edit' );
		}
	}
}
Back to Top