BP_REST_Topics_Endpoint
Topics endpoints.
Description
Source
File: bp-forums/classes/class-bp-rest-topics-endpoint.php
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 | class BP_REST_Topics_Endpoint extends WP_REST_Controller { /** * BP_REST_Forums_Endpoint Instance. * * @var BP_REST_Forums_Endpoint */ protected $forum_endpoint ; /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = 'topics' ; $this ->forum_endpoint = new BP_REST_Forums_Endpoint(); } /** * Register the component routes. * * @since 0.1.0 */ public function register_routes() { register_rest_route( $this -> namespace , '/' . $this ->rest_base, array ( array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_items' ), 'permission_callback' => array ( $this , 'get_items_permissions_check' ), 'args' => $this ->get_collection_params(), ), array ( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array ( $this , 'create_item' ), 'permission_callback' => array ( $this , 'create_item_permissions_check' ), 'args' => $this ->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); register_rest_route( $this -> namespace , '/' . $this ->rest_base . '/(?P<id>[\d]+)' , array ( 'args' => array ( 'id' => array ( 'description' => __( 'A unique numeric ID for the topic.' , 'buddyboss' ), 'type' => 'integer' , 'required' => true, ), ), array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_item' ), 'permission_callback' => array ( $this , 'get_item_permissions_check' ), ), array ( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array ( $this , 'update_item' ), 'permission_callback' => array ( $this , 'update_item_permissions_check' ), 'args' => $this ->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array ( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array ( $this , 'delete_item' ), 'permission_callback' => array ( $this , 'delete_item_permissions_check' ), 'args' => $this ->get_endpoint_args_for_item_schema( WP_REST_Server::DELETABLE ), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); } /** * Retrieve Topics. * * @param WP_REST_Request $request Full details about the request. * - from bbp_has_topics(). * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/topics Topics * @apiName GetBBPTopics * @apiGroup Forum Topics * @apiDescription Retrieve topics * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. * @apiParam {Number} [page=1] Current page of the collection. * @apiParam {Number} [per_page=10] Maximum number of items to be returned in result set. * @apiParam {String} [search] Limit results to those matching a string. * @apiParam {String} [author] Author ID, or comma-separated list of IDs. * @apiParam {Array} [author_exclude] An array of author IDs not to query from. * @apiParam {Array} [exclude] An array of topic IDs not to retrieve. * @apiParam {Array} [include] An array of topic IDs to retrieve. * @apiParam {Number} [offset] The number of topics to offset before retrieval. * @apiParam {String=asc,desc} [order=asc] Designates ascending or descending order of topics. * @apiParam {Array=meta_value,date,ID,author,title,modified,parent,rand,popular,activity} [orderby] Sort retrieved topics by parameter. * @apiParam {Array=publish,private,hidden} [status=publish private] Limit result set to topic assigned a specific status. * @apiParam {Number} [parent] Forum ID to retrieve all the topics. * @apiParam {Boolean} [subscriptions] Retrieve subscribed topics by user. * @apiParam {Boolean} [favorites] Retrieve favorite topics by the current user. * @apiParam {String} [tag] Search topic with specific tag. * @apiParam {String=all} [view] If current user can and is viewing all topics. */ public function get_items( $request ) { global $wpdb ; $args = array ( 'post_parent' => ( ! empty ( $request [ 'parent' ] ) ? $request [ 'parent' ] : '' ), 'orderby' => ( ! empty ( $request [ 'orderby' ] ) ? $request [ 'orderby' ] : 'meta_value' ), 'order' => ( ! empty ( $request [ 'order' ] ) ? $request [ 'order' ] : 'desc' ), 'paged' => ( ! empty ( $request [ 'page' ] ) ? $request [ 'page' ] : '' ), 'posts_per_page' => ( ! empty ( $request [ 'per_page' ] ) ? $request [ 'per_page' ] : bbp_get_topics_per_page() ), ); if ( ! empty ( $request [ 'status' ] ) ) { $args [ 'post_status' ] = implode( ' ' , $request [ 'status' ] ); } if ( ! empty ( $request [ 'search' ] ) ) { $args [ 's' ] = $this ->bbp_sanitize_search_request( $request [ 'search' ] ); } if ( ! empty ( $request [ 'author' ] ) ) { $args [ 'author' ] = $request [ 'author' ]; } if ( ! empty ( $request [ 'author_exclude' ] ) ) { $args [ 'author__not_in' ] = $request [ 'author_exclude' ]; } if ( ! empty ( $request [ 'exclude' ] ) ) { $args [ 'post__not_in' ] = $request [ 'exclude' ]; } if ( ! empty ( $request [ 'include' ] ) ) { $args [ 'post__in' ] = $request [ 'include' ]; } if ( ! empty ( $request [ 'offset' ] ) ) { $args [ 'offset' ] = $request [ 'offset' ]; } $default_show_stickies = false; if ( ! empty ( $args [ 'post_parent' ] ) && 'forum' === get_post_type( $args [ 'post_parent' ] ) && empty ( $request [ 'search' ] ) ) { $default_show_stickies = true; } if ( ! empty ( $args [ 'orderby' ] ) && is_array ( $args [ 'orderby' ] ) ) { if ( in_array( 'popular' , $args [ 'orderby' ], true ) ) { $args [ 'orderby' ] = 'meta_value_num' ; $args [ 'meta_key' ] = '_bbp_reply_count' ; // phpcs:ignore } elseif ( in_array( 'activity' , $args [ 'orderby' ], true ) ) { $args [ 'orderby' ] = 'meta_value' ; $args [ 'meta_key' ] = '_bbp_last_active_time' ; // phpcs:ignore } } if ( is_array ( $args [ 'orderby' ] ) ) { $args [ 'orderby' ] = implode( ' ' , $args [ 'orderby' ] ); } /** * Filter the query arguments for the request. * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ $args = apply_filters( 'bp_rest_topics_get_items_query_args' , $args , $request ); $default = array ( 'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics. 'show_stickies' => $default_show_stickies , // Ignore sticky topics? 'max_num_pages' => false, // Maximum number of pages to show. // Conditionally prime the cache for related posts. 'update_post_family_cache' => true, ); if ( ! empty ( $args [ 'post_parent' ] ) ) { // phpcs:ignore $default [ 'meta_key' ] = '_bbp_last_active_time' ; } // What are the default allowed statuses (based on user caps). if ( bbp_get_view_all( 'edit_others_topics' ) ) { // Default view=all statuses. $post_statuses = array_keys ( bbp_get_topic_statuses() ); // Add support for private status. if ( current_user_can( 'read_private_topics' ) ) { $post_statuses [] = bbp_get_private_status_id(); } // Join post statuses together. $default [ 'post_status' ] = $post_statuses ; // Lean on the 'perm' query var value of 'readable' to provide statuses. } else { $default [ 'perm' ] = 'readable' ; } $tag = sanitize_title( $request ->get_param( 'tag' ) ); if ( bbp_allow_topic_tags() && ! empty ( $tag ) ) { $default [ 'term' ] = bbp_get_topic_tag_slug( $tag ); $default [ 'taxonomy' ] = bbp_get_topic_tag_tax_id(); } $bbp_t = bbp_parse_args( $args , $default , 'has_topics' ); if ( isset( $request [ 'subscriptions' ] ) && ! empty ( $request [ 'subscriptions' ] ) ) { $user_id = (int) ( ( isset( $args [ 'author' ] ) && ! empty ( $args [ 'author' ] ) ) ? $args [ 'author' ] : bbp_get_current_user_id() ); $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); if ( ! empty ( $subscriptions ) ) { $bbp_t [ 'post__in' ] = $subscriptions ; if ( isset( $args [ 'author' ] ) ) { unset( $bbp_t [ 'author' ] ); } } else { $bbp_t = array (); } } elseif ( isset( $request [ 'favorites' ] ) && ! empty ( $request [ 'favorites' ] ) ) { $user_id = (int) ( ( isset( $args [ 'author' ] ) && ! empty ( $args [ 'author' ] ) ) ? $args [ 'author' ] : bbp_get_current_user_id() ); $favorites = bbp_get_user_favorites_topic_ids( $user_id ); if ( ! empty ( $favorites ) ) { $bbp_t [ 'post__in' ] = $favorites ; if ( isset( $args [ 'author' ] ) ) { unset( $bbp_t [ 'author' ] ); } } else { $bbp_t = array (); } } // Run the query. $topics_query = new WP_Query( $bbp_t ); /** Stickies */ // Put sticky posts at the top of the posts array. if ( ! empty ( $bbp_t [ 'show_stickies' ] ) && $bbp_t [ 'paged' ] <= 1 ) { // Strip the super stickies from topic query. // bp-forums/groups.php L791. if ( ! empty ( $bbp_t [ 'post_parent' ] ) && 'forum' === get_post_type( $bbp_t [ 'post_parent' ] ) ) { $group_ids = bbp_get_forum_group_ids( $bbp_t [ 'post_parent' ] ); if ( ! empty ( $group_ids ) ) { add_filter( 'bbp_get_super_stickies' , array ( $this , 'no_super_stickies' ), 10, 1 ); } } // Get super stickies and stickies in this forum. $stickies = bbp_get_super_stickies(); // Strip the super stickies from topic query. if ( ! empty ( $bbp_t [ 'post_parent' ] ) && 'forum' === get_post_type( $bbp_t [ 'post_parent' ] ) ) { $group_ids = bbp_get_forum_group_ids( $bbp_t [ 'post_parent' ] ); if ( ! empty ( $group_ids ) ) { remove_filter( 'bbp_get_super_stickies' , array ( $this , 'no_super_stickies' ), 10, 1 ); } } // Get stickies for current forum. if ( ! empty ( $bbp_t [ 'post_parent' ] ) ) { $stickies = array_merge ( $stickies , bbp_get_stickies( $bbp_t [ 'post_parent' ] ) ); } // Remove any duplicate stickies. $stickies = array_unique ( $stickies ); // We have stickies. if ( is_array ( $stickies ) && ! empty ( $stickies ) ) { // Start the offset at -1 so first sticky is at correct 0 offset. $sticky_offset = - 1; // Loop over topics and relocate stickies to the front. foreach ( $stickies as $sticky_index => $sticky_id ) { // Get the post offset from the posts array. $post_offsets = wp_filter_object_list( $topics_query ->posts, array ( 'ID' => $sticky_id ), 'OR' , 'ID' ); // Continue if no post offsets. if ( empty ( $post_offsets ) ) { continue ; } // Loop over posts in current query and splice them into position. foreach ( array_keys ( $post_offsets ) as $post_offset ) { $sticky_offset ++; $sticky = $topics_query ->posts[ $post_offset ]; // Remove sticky from current position. array_splice ( $topics_query ->posts, $post_offset , 1 ); // Move to front, after other stickies. array_splice ( $topics_query ->posts, $sticky_offset , 0, array ( $sticky ) ); // Cleanup. unset( $stickies [ $sticky_index ] ); unset( $sticky ); } // Cleanup. unset( $post_offsets ); } // Cleanup. unset( $sticky_offset ); // If any posts have been excluded specifically, Ignore those that are sticky. if ( ! empty ( $stickies ) && ! empty ( $bbp_t [ 'post__not_in' ] ) ) { $stickies = array_diff ( $stickies , $bbp_t [ 'post__not_in' ] ); } // Fetch sticky posts that weren't in the query results. if ( ! empty ( $stickies ) ) { // Query to use in get_posts to get sticky posts. $sticky_query = array ( 'post_type' => bbp_get_topic_post_type(), 'post_parent' => 'any' , 'meta_key' => '_bbp_last_active_time' , // phpcs:ignore 'orderby' => 'meta_value' , 'order' => 'DESC' , 'include' => $stickies , ); // Cleanup. unset( $stickies ); // Conditionally exclude private/hidden forum ID's. $exclude_forum_ids = bbp_exclude_forum_ids( 'array' ); if ( ! empty ( $exclude_forum_ids ) ) { $sticky_query [ 'post_parent__not_in' ] = $exclude_forum_ids ; } // What are the default allowed statuses (based on user caps). if ( bbp_get_view_all( 'edit_others_topics' ) ) { $sticky_query [ 'post_status' ] = $bbp_t [ 'post_status' ]; // Lean on the 'perm' query var value of 'readable' to provide statuses. } else { $sticky_query [ 'post_status' ] = $bbp_t [ 'perm' ]; } // Get all stickies. $sticky_posts = get_posts( $sticky_query ); if ( ! empty ( $sticky_posts ) ) { // Get a count of the visible stickies. $sticky_count = count ( $sticky_posts ); // Merge the stickies topics with the query topics. $topics_query ->posts = array_merge ( $sticky_posts , $topics_query ->posts ); // Adjust loop and counts for new sticky positions. $topics_query ->found_posts = (int) $topics_query ->found_posts + (int) $sticky_count ; $topics_query ->post_count = (int) $topics_query ->post_count + (int) $sticky_count ; // Cleanup. unset( $sticky_posts ); } } } } // If no limit to posts per page, set it to the current post_count. if ( - 1 === $bbp_t [ 'posts_per_page' ] ) { $topics_query ->posts_per_page = $topics_query ->post_count; } /** --Stickies */ $topics = ( ! empty ( $topics_query ->posts ) ? $topics_query ->posts : array () ); $retval = array (); foreach ( $topics as $topic ) { $retval [] = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); } $response = rest_ensure_response( $retval ); $response = bp_rest_response_add_total_headers( $response , $topics_query ->found_posts, $args [ 'posts_per_page' ] ); /** * Fires after a list of topics is fetched via the REST API. * * @param array $topics Fetched Topics. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topics_get_items' , $topics , $response , $request ); return $response ; } /** * Check if a given request has access to list topics. * * @param WP_REST_Request $request Full data about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function get_items_permissions_check( $request ) { $retval = true; if ( function_exists( 'bp_enable_private_network' ) && true !== bp_enable_private_network() && ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, Restrict access to only logged-in members.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the topics `get_items` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topics_get_items_permissions_check' , $retval , $request ); } /** * Retrieve a single topic. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/topics/:id Topic * @apiName GetBBPTopic * @apiGroup Forum Topics * @apiDescription Retrieve a single topic. * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. * @apiParam {Number} id A unique numeric ID for the topic. */ public function get_item( $request ) { $topic = get_post( $request [ 'id' ] ); $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a list of topic is fetched via the REST API. * * @param array $topic Fetched topic. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topic_get_item' , $topic , $response , $request ); return $response ; } /** * Check if a given request has access to list topic. * * @param WP_REST_Request $request Full data about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function get_item_permissions_check( $request ) { $retval = true; if ( function_exists( 'bp_enable_private_network' ) && true !== bp_enable_private_network() && ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, Restrict access to only logged-in members.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } $topic = get_post( $request [ 'id' ] ); if ( true === $retval && empty ( $topic ->ID ) ) { $retval = new WP_Error( 'bp_rest_topic_invalid_id' , __( 'Invalid topic ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && ( ! isset( $topic ->post_type ) || 'topic' !== $topic ->post_type ) ) { $retval = new WP_Error( 'bp_rest_topic_invalid_id' , __( 'Invalid topic ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( isset( $topic ->post_type ) ) { $post_type = get_post_type_object( $topic ->post_type ); if ( true === $retval && ! current_user_can( $post_type ->cap->read_post, $topic ->ID ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to access this topic.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } /** * Filter the topic `get_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_get_item_permissions_check' , $retval , $request ); } /** * Create a topic. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {POST} /wp-json/buddyboss/v1/topics Create Topic * @apiName CreateBBPTopic * @apiGroup Forum Topics * @apiDescription Create a topic. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {String} title The title of the topic. * @apiParam {String} content The content of the topic. * @apiParam {Number} parent ID of the parent Forum. * @apiParam {String=publish,closed,spam,trash,pending} [status=publish] The current status of the topic. * @apiParam {String=stick,super,unstick} [sticky=unstick] Whether the topic is sticky or not. * @apiParam {Number} [group] ID of the forum's group. * @apiParam {String} [topic_tags] Topic's tags with comma separated. * @apiParam {Array} [bbp_media] Media specific IDs when Media component is enable. * @apiParam {Array} [bbp_media_gif] Save gif data into topic when Media component is enable. param(url,mp4) */ public function create_item( $request ) { $request ->set_param( 'context' , 'edit' ); $topic = $this ->prepare_topic_for_database( $request ); // Define local variable(s). $forum_id = 0; $topic_author = 0; $anonymous_data = 0; $topic_title = '' ; $topic_content = '' ; $terms = array ( bbp_get_topic_tag_tax_id() => array () ); /** Topic Author */ if ( bbp_is_anonymous() ) { $anonymous_args = array ( 'bbp_anonymous_name' => ! empty ( $request [ 'anonymous_name' ] ) ? sanitize_text_field( $request [ 'anonymous_name' ] ) : '' , 'bbp_anonymous_email' => ! empty ( $request [ 'anonymous_email' ] ) ? sanitize_email( $request [ 'anonymous_email' ] ) : '' , 'bbp_anonymous_website' => ! empty ( $request [ 'anonymous_website' ] ) ? sanitize_text_field( $request [ 'anonymous_website' ] ) : '' , ); // Filter anonymous data. $anonymous_data = bbp_filter_anonymous_post_data( $anonymous_args ); // Anonymous data checks out, so set cookies, etc... if ( ! empty ( $anonymous_data ) && is_array ( $anonymous_data ) ) { bbp_set_current_anonymous_user_data( $anonymous_data ); } // User is logged in. } else { // User cannot create topics. if ( ! current_user_can( 'publish_topics' ) ) { return new WP_Error( 'bp_rest_bbp_topic_permissions' , __( 'Sorry, You do not have permission to create new discussions.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } // Topic author is current user. $topic_author = bbp_get_current_user_id(); } // Remove kses filters from title and content for capable users and if the nonce is verified. if ( current_user_can( 'unfiltered_html' ) ) { remove_filter( 'bbp_new_topic_pre_title' , 'wp_filter_kses' ); remove_filter( 'bbp_new_topic_pre_content' , 'bbp_encode_bad' , 10 ); } /** Discussion Title */ if ( ! empty ( $topic ->bbp_topic_title ) ) { $topic_title = esc_attr( wp_strip_all_tags( $topic ->bbp_topic_title ) ); } // Filter and sanitize. $topic_title = apply_filters( 'bbp_new_topic_pre_title' , $topic_title ); // No topic title. if ( empty ( $topic_title ) ) { return new WP_Error( 'bp_rest_bbp_topic_title' , __( 'Sorry, Your discussion needs a subject.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Content */ if ( ! empty ( $topic ->bbp_topic_content ) ) { $topic_content = $topic ->bbp_topic_content; } // Filter and sanitize. $topic_content = apply_filters( 'bbp_new_topic_pre_content' , $topic_content ); // No topic content. if ( empty ( $topic_content ) && empty ( $request [ 'bbp_media' ] ) && empty ( $request [ 'bbp_media_gif' ] ) ) { return new WP_Error( 'bp_rest_bbp_topic_content' , __( 'Sorry, Your discussion cannot be empty.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Forum */ // Error check the POST'ed topic id. if ( isset( $topic ->bbp_forum_id ) ) { // Empty Forum id was passed. if ( empty ( $topic ->bbp_forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID is missing.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum id is not a number. } elseif ( ! is_numeric ( $topic ->bbp_forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID must be a number.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum id might be valid. } else { // Get the forum id. $posted_forum_id = intval ( $topic ->bbp_forum_id ); // Forum id is empty. if ( 0 === $posted_forum_id ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID is missing.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum id is a negative number. } elseif ( 0 > $posted_forum_id ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID cannot be a negative number.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum does not exist. } elseif ( ! bbp_get_forum( $posted_forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum does not exist.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Use the POST'ed forum id. } else { $forum_id = $posted_forum_id ; } } } // Forum exists. if ( ! empty ( $forum_id ) ) { // Forum is a category. if ( bbp_is_forum_category( $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_category' , __( 'Sorry, This forum is a category. No discussions can be created in this forum.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum is not a category. } else { // Forum is closed and user cannot access. if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum' , $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_closed' , __( 'Sorry, This forum has been closed to new discussions.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** * Added logic for group forum * Current user is part of that group or not. * We need to check manually because bbpress updating that caps only on group forum page and * in API those conditional tag will not work. */ $is_member = false; $group_ids = array (); if ( function_exists( 'bbp_get_forum_group_ids' ) ) { $group_ids = bbp_get_forum_group_ids( $forum_id ); if ( ! empty ( $group_ids ) ) { foreach ( $group_ids as $group_id ) { if ( groups_is_user_member( $topic_author , $group_id ) ) { $is_member = true; break ; } } } } // Forum is private and user cannot access. if ( bbp_is_forum_private( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_private_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_closed' , __( 'Sorry, This forum is private and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } // Forum is hidden and user cannot access. } elseif ( bbp_is_forum_hidden( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_closed' , __( 'Sorry, This forum is hidden and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } } } } /** Topic Flooding */ if ( ! bbp_check_for_flood( $anonymous_data , $topic_author ) ) { return new WP_Error( 'bp_rest_bbp_topic_flood' , __( 'Slow down; you move too fast.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Duplicate */ if ( ! bbp_check_for_duplicate( array ( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author , 'post_content' => $topic_content , 'anonymous_data' => $anonymous_data , ) ) ) { return new WP_Error( 'bp_rest_bbp_topic_duplicate' , __( 'Duplicate discussion detected; it looks as though you\'ve already said that!' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Blacklist */ if ( ! bbp_check_for_blacklist( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { return new WP_Error( 'bp_rest_bbp_topic_blacklist' , __( 'Sorry, Your discussion cannot be created at this time.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Status */ // Maybe put into moderation. if ( ! bbp_check_for_moderation( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { $topic_status = bbp_get_pending_status_id(); // Check a whitelist of possible topic status ID's. } elseif ( ! empty ( $topic ->bbp_topic_status ) && in_array( $topic ->bbp_topic_status, array_keys ( bbp_get_topic_statuses() ), true ) ) { $topic_status = $topic ->bbp_topic_status; // Default to published if nothing else. } else { $topic_status = bbp_get_public_status_id(); } /** Topic Tags */ if ( bbp_allow_topic_tags() && ! empty ( $topic ->bbp_topic_tags ) ) { // Escape tag input. $terms = esc_attr( wp_strip_all_tags( $topic ->bbp_topic_tags ) ); // Explode by comma. if ( strstr ( $terms , ',' ) ) { $terms = explode ( ',' , $terms ); } // Add topic tag ID as main key. $terms = array ( bbp_get_topic_tag_tax_id() => $terms ); } /** Additional Actions (Before Save) */ do_action( 'bbp_new_topic_pre_extras' , $forum_id ); // Bail if errors. if ( bbp_has_errors() ) { return new WP_Error( 'bp_rest_bbp_topic_unknown' , __( 'Unknown error.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** No Errors */ // Add the content of the form to $topic_data as an array. // Just in time manipulation of topic data before being created. $topic_data = apply_filters( 'bbp_new_topic_pre_insert' , array ( 'post_author' => $topic_author , 'post_title' => $topic_title , 'post_content' => $topic_content , 'post_status' => $topic_status , 'post_parent' => $forum_id , 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms , 'comment_status' => 'closed' , ) ); // Insert topic. $topic_id = wp_insert_post( $topic_data ); if ( empty ( $topic_id ) || is_wp_error( $topic_id ) ) { $append_error = ( ( is_wp_error( $topic_id ) && $topic_id ->get_error_message() ) ? __( 'The following problem(s) have been found with your topic: ' , 'buddyboss' ) . $topic_id ->get_error_message() : __( 'We are facing a problem to creating a topic.' , 'buddyboss' ) ); return new WP_Error( 'bp_rest_bbp_topic_error' , $append_error , array ( 'status' => 400, ) ); } /** Trash Check */ // If the forum is trash, or the topic_status is switched to. // trash, trash it properly. if ( ( bbp_get_trash_status_id() === get_post_field( 'post_status' , $forum_id ) ) || ( bbp_get_trash_status_id() === $topic_data [ 'post_status' ] ) ) { // Trash the reply. wp_trash_post( $topic_id ); } /** Spam Check */ // If reply or topic are spam, officially spam this reply. if ( bbp_get_spam_status_id() === $topic_data [ 'post_status' ] ) { add_post_meta( $topic_id , '_bbp_spam_meta_status' , bbp_get_public_status_id() ); } /** Update counts, etc... */ do_action( 'bbp_new_topic' , $topic_id , $forum_id , $anonymous_data , $topic_author ); /** Stickies */ // Sticky check after 'bbp_new_topic' action so forum ID meta is set. if ( ! empty ( $topic ->bbp_stick_topic ) && in_array( $topic ->bbp_stick_topic, array ( 'stick' , 'super' , 'unstick' , ), true ) ) { // What's the caps? if ( current_user_can( 'moderate' ) ) { // What's the haps? switch ( $topic ->bbp_stick_topic ) { // Sticky in this forum. case 'stick' : bbp_stick_topic( $topic_id ); break ; // Super sticky in all forums. case 'super' : bbp_stick_topic( $topic_id , true ); break ; // We can avoid this as it is a new topic. case 'unstick' : default : break ; } } } // Handle Subscription Checkbox. if ( bbp_is_subscriptions_active() ) { $author_id = bbp_get_user_id( 0, true, true ); // Check if subscribed. $subscribed = bbp_is_user_subscribed( $author_id , $topic_id ); // Subscribed and unsubscribing. if ( true === $subscribed && empty ( $topic ->bbp_topic_subscription ) ) { bbp_remove_user_subscription( $author_id , $topic_id ); // Not subscribed and subscribing. } elseif ( false === $subscribed && ! empty ( $topic ->bbp_topic_subscription ) ) { bbp_add_user_subscription( $author_id , $topic_id ); } } /** Additional Actions (After Save) */ do_action( 'bbp_new_topic_post_extras' , $topic_id ); $topic = get_post( $topic_id ); $fields_update = $this ->update_additional_fields_for_object( $topic , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a topic is created and fetched via the REST API. * * @param array $topic Created topic. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topic_create_item' , $topic , $response , $request ); return $response ; } /** * Check if a given request has access to create a topic. * * @param WP_REST_Request $request Full data about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function create_item_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() && ! bbp_allow_anonymous() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to create a topic.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the topic `create_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_create_item_permissions_check' , $retval , $request ); } /** * Update/Edit a topic. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {PATCH} /wp-json/buddyboss/v1/topics/:id Update Topic * @apiName UpdateBBPTopic * @apiGroup Forum Topics * @apiDescription Update a topic. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} id A unique numeric ID for the topic. * @apiParam {String} title The title of the topic. * @apiParam {String} content The content of the topic. * @apiParam {Number} parent ID of the parent Forum. * @apiParam {String=publish,closed,spam,trash,pending} [status=publish] The current status of the topic. * @apiParam {String=stick,super,unstick} [sticky=unstick] Whether the topic is sticky or not. * @apiParam {Number} [group] ID of the forum's group. * @apiParam {String} [topic_tags] Topic's tags with comma separated. * @apiParam {String} [reason_editing] Reason for editing a topic. * @apiParam {Boolean} [log] Keep a log of topic edit. * @apiParam {Array} [bbp_media] Media specific IDs when Media component is enable. * @apiParam {Array} [bbp_media_gif] Save gif data into topic when Media component is enable. param(url,mp4) */ public function update_item( $request ) { $request ->set_param( 'context' , 'edit' ); $topic_new = $this ->prepare_topic_for_database( $request ); // Define local variable(s). $revisions_removed = false; $topic = 0; $topic_id = 0; $topic_author = 0; $forum_id = 0; $topic_title = '' ; $topic_content = '' ; $topic_edit_reason = '' ; $anonymous_data = array (); // Topic id was not passed. if ( empty ( $topic_new ->bbp_topic_id ) ) { new WP_Error( 'bp_rest_topic_invalid_id' , __( 'Invalid topic ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); // Topic id was passed. } elseif ( is_numeric ( $topic_new ->bbp_topic_id ) ) { $topic_id = (int) $topic_new ->bbp_topic_id; $topic = bbp_get_topic( $topic_id ); } // Topic does not exist. if ( empty ( $topic ) ) { new WP_Error( 'bp_rest_bbp_edit_topic_not_found' , __( 'Sorry, The discussion you want to edit was not found.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // Topic exists. // Check users ability to create new topic. if ( ! bbp_is_topic_anonymous( $topic_id ) ) { // User cannot edit this topic. if ( ! current_user_can( 'edit_topic' , $topic_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_permissions' , __( 'Sorry, You do not have permission to edit that discussion.' , 'buddyboss' ), array ( 'status' => 403, ) ); } // Set topic author. $topic_author = bbp_get_topic_author_id( $topic_id ); // It is an anonymous post. } else { $anonymous_args = array ( 'bbp_anonymous_name' => ! empty ( $request [ 'anonymous_name' ] ) ? sanitize_text_field( $request [ 'anonymous_name' ] ) : '' , 'bbp_anonymous_email' => ! empty ( $request [ 'anonymous_email' ] ) ? sanitize_email( $request [ 'anonymous_email' ] ) : '' , 'bbp_anonymous_website' => ! empty ( $request [ 'anonymous_website' ] ) ? sanitize_text_field( $request [ 'anonymous_website' ] ) : '' , ); // Filter anonymous data. $anonymous_data = bbp_filter_anonymous_post_data( $anonymous_args ); } // Remove kses filters from title and content for capable users. if ( current_user_can( 'unfiltered_html' ) ) { remove_filter( 'bbp_edit_topic_pre_title' , 'wp_filter_kses' ); remove_filter( 'bbp_edit_topic_pre_content' , 'bbp_encode_bad' , 10 ); remove_filter( 'bbp_edit_topic_pre_content' , 'bbp_filter_kses' , 30 ); } /** Topic Forum */ // Forum id was not passed. if ( empty ( $topic_new ->bbp_forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID is missing.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum id was passed. } elseif ( is_numeric ( $topic_new ->bbp_forum_id ) ) { $forum_id = (int) $topic_new ->bbp_forum_id; } // Current forum this topic is in. $current_forum_id = bbp_get_topic_forum_id( $topic_id ); // Forum exists. if ( ! empty ( $forum_id ) && ( $forum_id !== $current_forum_id ) ) { // Forum is a category. if ( bbp_is_forum_category( $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_category' , __( 'Sorry, This forum is a category. No discussions can be created in this forum.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum is not a category. } else { // Forum is closed and user cannot access. if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum' , $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_closed' , __( 'Sorry, This forum has been closed to new discussions.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** * Added logic for group forum * Current user is part of that group or not. * We need to check manually because bbpress updating that caps only on group forum page and * in API those conditional tag will not work. */ $is_member = false; $group_ids = array (); if ( function_exists( 'bbp_get_forum_group_ids' ) ) { $group_ids = bbp_get_forum_group_ids( $forum_id ); if ( ! empty ( $group_ids ) ) { foreach ( $group_ids as $group_id ) { if ( groups_is_user_member( $topic_author , $group_id ) ) { $is_member = true; break ; } } } } // Forum is private and user cannot access. if ( bbp_is_forum_private( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_private_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_private' , __( 'Sorry, This forum is private and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } // Forum is hidden and user cannot access. } elseif ( bbp_is_forum_hidden( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_hidden' , __( 'Sorry, This forum is hidden and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } } } } /** Discussion Title */ if ( ! empty ( $topic_new ->bbp_topic_title ) ) { $topic_title = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_title ) ); } // Filter and sanitize. $topic_title = apply_filters( 'bbp_edit_topic_pre_title' , $topic_title , $topic_id ); // No topic title. if ( empty ( $topic_title ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_title' , __( 'Sorry, Your discussion needs a title.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Content */ if ( ! empty ( $topic_new ->bbp_topic_content ) ) { $topic_content = $topic_new ->bbp_topic_content; } // Filter and sanitize. $topic_content = apply_filters( 'bbp_edit_topic_pre_content' , $topic_content , $topic_id ); // No topic content. if ( empty ( $topic_content ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_content' , __( 'Sorry, Your discussion cannot be empty.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Blacklist */ if ( ! bbp_check_for_blacklist( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { return new WP_Error( 'bp_rest_bbp_topic_blacklist' , __( 'Sorry, Your discussion cannot be edited at this time.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Status */ // Maybe put into moderation. if ( ! bbp_check_for_moderation( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { // Set post status to pending if public or closed. if ( in_array( $topic ->post_status, array ( bbp_get_public_status_id(), bbp_get_closed_status_id(), ), true ) ) { $topic_status = bbp_get_pending_status_id(); } // Check a whitelist of possible topic status ID's. } elseif ( ! empty ( $topic_new ->bbp_topic_status ) && in_array( $topic_new ->bbp_topic_status, array_keys ( bbp_get_topic_statuses() ), true ) ) { $topic_status = $topic_new ->bbp_topic_status; // Use existing post_status. } else { $topic_status = $topic ->post_status; } /** Topic Tags */ // Either replace terms. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty ( $topic_new ->bbp_topic_tags ) ) { // Escape tag input. $terms = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_tags ) ); // Explode by comma. if ( strstr ( $terms , ',' ) ) { $terms = explode ( ',' , $terms ); } // Add topic tag ID as main key. $terms = array ( bbp_get_topic_tag_tax_id() => $terms ); // ...or remove them. } elseif ( isset( $topic_new ->bbp_topic_tags ) ) { $terms = array ( bbp_get_topic_tag_tax_id() => array () ); // Existing terms. } else { $terms = array ( bbp_get_topic_tag_tax_id() => explode ( ',' , bbp_get_topic_tag_names( $topic_id , ',' ) ) ); } /** Additional Actions (Before Save) */ do_action( 'bbp_edit_topic_pre_extras' , $topic_id ); // Add the content of the form to $topic_data as an array. // Just in time manipulation of topic data before being edited. $topic_data = apply_filters( 'bbp_edit_topic_pre_insert' , array ( 'ID' => $topic_id , 'post_title' => $topic_title , 'post_content' => $topic_content , 'post_status' => $topic_status , 'post_parent' => $forum_id , 'post_author' => $topic_author , 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms , ) ); // Toggle revisions to avoid duplicates. if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) { $revisions_removed = true; remove_post_type_support( bbp_get_topic_post_type(), 'revisions' ); } // Insert topic. $topic_id = wp_update_post( $topic_data ); // Toggle revisions back on. if ( true === $revisions_removed ) { $revisions_removed = false; add_post_type_support( bbp_get_topic_post_type(), 'revisions' ); } if ( empty ( $topic_id ) || is_wp_error( $topic_id ) ) { $append_error = ( ( is_wp_error( $topic_id ) && $topic_id ->get_error_message() ) ? __( 'The following problem(s) have been found with your topic: ' , 'buddyboss' ) . $topic_id ->get_error_message() . __( 'Please try again.' , 'buddyboss' ) : __( 'We are facing a problem to update a topic.' , 'buddyboss' ) ); return new WP_Error( 'bp_rest_bbp_topic_error' , $append_error , array ( 'status' => 400, ) ); } // Update counts, etc... do_action( 'bbp_edit_topic' , $topic_id , $forum_id , $anonymous_data , $topic_author , true /* Is edit */ ); /** Revisions */ // Revision Reason. if ( ! empty ( $topic_new ->bbp_topic_edit_reason ) ) { $topic_edit_reason = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_edit_reason ) ); } // Update revision log. if ( ! empty ( $topic_new ->bbp_log_topic_edit ) && ( true === $topic_new ->bbp_log_topic_edit ) ) { $revision_id = wp_save_post_revision( $topic_id ); if ( ! empty ( $revision_id ) ) { bbp_update_topic_revision_log( array ( 'topic_id' => $topic_id , 'revision_id' => $revision_id , 'author_id' => bbp_get_current_user_id(), 'reason' => $topic_edit_reason , ) ); } } /** Move Topic */ // If the new forum id is not equal to the old forum id, run the. // bbp_move_topic action and pass the topic's forum id as the. // first arg and topic id as the second to update counts. if ( $forum_id !== $topic ->post_parent ) { bbp_move_topic_handler( $topic_id , $topic ->post_parent, $forum_id ); } /** Stickies */ if ( ! empty ( $topic_new ->bbp_stick_topic ) && in_array( $topic_new ->bbp_stick_topic, array_keys ( bbp_get_topic_types() ), true ) ) { // What's the caps? if ( current_user_can( 'moderate' ) ) { // What's the haps? switch ( $topic_new ->bbp_stick_topic ) { // Sticky in forum. case 'stick' : bbp_stick_topic( $topic_id ); break ; // Sticky in all forums. case 'super' : bbp_stick_topic( $topic_id , true ); break ; // Normal. case 'unstick' : default : bbp_unstick_topic( $topic_id ); break ; } } } // Handle Subscription Checkbox. if ( bbp_is_subscriptions_active() ) { $author_id = bbp_get_user_id( 0, true, true ); // Check if subscribed. $subscribed = bbp_is_user_subscribed( $author_id , $topic_id ); // Subscribed and unsubscribing. if ( true === $subscribed && empty ( $topic_new ->bbp_topic_subscription ) ) { bbp_remove_user_subscription( $author_id , $topic_id ); // Not subscribed and subscribing. } elseif ( false === $subscribed && ! empty ( $topic_new ->bbp_topic_subscription ) ) { bbp_add_user_subscription( $author_id , $topic_id ); } } /** Additional Actions (After Save) */ do_action( 'bbp_edit_topic_post_extras' , $topic_id ); $topic = get_post( $topic_id ); $fields_update = $this ->update_additional_fields_for_object( $topic , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a topic is updated and fetched via the REST API. * * @param array $topic Updated topic. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topic_update_item' , $topic , $response , $request ); return $response ; } /** * Check if a given request has access to update a topic. * * @param WP_REST_Request $request Full data about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function update_item_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() && ! bbp_allow_anonymous() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to update a topic.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval ) { $retval = $this ->get_item_permissions_check( $request ); } if ( true === $retval ) { $topic = get_post( $request [ 'id' ] ); if ( bbp_get_user_id( 0, true, true ) !== $topic ->post_author && ! current_user_can( 'delete_topic' , $request [ 'id' ] ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to update this topic.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } /** * Filter the topic `update_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_update_item_permissions_check' , $retval , $request ); } /** * Delete a topic. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {DELETE} /wp-json/buddyboss/v1/topics/:id Trash/Delete Topic * @apiName DeleteBBPTopic * @apiGroup Forum Topics * @apiDescription Trash OR Delete a topic. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} id A unique numeric ID for the topic. */ public function delete_item( $request ) { $topic = get_post( $request [ 'id' ] ); $previous = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); $success = wp_delete_post( $topic ->ID ); // Build the response. $response = new WP_REST_Response(); $response ->set_data( array ( 'deleted' => ( ! empty ( $success ) && ! is_wp_error( $success ) ? true : $success ), 'previous' => $previous , ) ); /** * Fires after a topic is deleted via the REST API. * * @param array $topic Deleted/Trashed topic. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topic_delete_item' , $topic , $response , $request ); return $response ; } /** * Check if a given request has access to delete a topic. * * @param WP_REST_Request $request Full details about the request. * * @return WP_Error|bool * @since 0.1.0 */ public function delete_item_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to perform this action.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval ) { $retval = $this ->get_item_permissions_check( $request ); } if ( true === $retval && ! current_user_can( 'delete_topic' , $request [ 'id' ] ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to delete this topic.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the topic `delete_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_delete_item_permissions_check' , $retval , $request ); } /** * Edit some arguments for the endpoint's CREATABLE, EDITABLE and DELETABLE methods. * * @param string $method Optional. HTTP method of the request. * * @return array Endpoint arguments. * @since 0.1.0 */ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { $args = WP_REST_Controller::get_endpoint_args_for_item_schema( $method ); $key = 'create_item' ; if ( WP_REST_Server::DELETABLE === $method ) { $key = 'delete_item' ; $args = array ( 'id' => array ( 'description' => __( 'A unique numeric ID for the topic.' , 'buddyboss' ), 'type' => 'integer' , 'required' => true, ), ); } elseif ( WP_REST_Server::EDITABLE === $method || WP_REST_Server::CREATABLE === $method ) { $unset_keys = array ( 'date' , 'date_gmt' , 'password' , 'slug' , 'link' , 'author' , 'total_reply_count' , 'last_reply_id' , 'last_active_author' , 'last_active_time' , 'is_closed' , 'classes' , 'voice_count' , 'forum_id' , 'is_topic_anonymous' , 'anonymous_author_data' , 'action_states' , 'current_user_permissions' , 'revisions' , ); if ( ! empty ( $unset_keys ) ) { foreach ( $unset_keys as $k ) { if ( array_key_exists ( $k , $args ) ) { unset( $args [ $k ] ); } } } $args [ 'title' ][ 'type' ] = 'string' ; $args [ 'title' ][ 'required' ] = true; $args [ 'content' ][ 'type' ] = 'string' ; $args [ 'content' ][ 'required' ] = true; $args [ 'status' ][ 'default' ] = 'publish' ; $args [ 'status' ][ 'enum' ] = array_keys ( bbp_get_topic_statuses() ); $args [ 'sticky' ][ 'type' ] = 'string' ; $args [ 'sticky' ][ 'enum' ] = array ( 'stick' , 'super' , 'unstick' ); $args [ 'parent' ][ 'description' ] = __( 'ID of the parent Forum.' , 'buddyboss' ); $args [ 'parent' ][ 'required' ] = true; $args [ 'group' ][ 'type' ] = 'integer' ; $args [ 'group' ][ 'description' ] = __( 'ID of the forum\'s group' , 'buddyboss' ); $params [ 'subscribe' ] = array ( 'description' => __( 'whether user subscribe topic or no' , 'buddyboss' ), 'type' => 'boolean' , 'sanitize_callback' => 'rest_sanitize_boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); } if ( WP_REST_Server::EDITABLE === $method ) { $key = 'update_item' ; $args [ 'reason_editing' ] = array ( 'description' => __( 'Reason for editing a topic.' , 'buddyboss' ), 'type' => 'string' , 'validate_callback' => 'rest_validate_request_arg' , ); $args [ 'log' ] = array ( 'description' => __( 'Keep a log of topic edit.' , 'buddyboss' ), 'type' => 'boolean' , 'default' => true, 'sanitize_callback' => 'rest_sanitize_boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'subscribe' ] = array ( 'description' => __( 'whether user subscribe topic or no' , 'buddyboss' ), 'type' => 'boolean' , 'sanitize_callback' => 'rest_sanitize_boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); } /** * Filters the method query arguments. * * @param array $args Query arguments. * @param string $method HTTP method of the request. * * @since 0.1.0 */ return apply_filters( "bp_rest_topic_{$key}_query_arguments" , $args , $method ); } /** * Prepares component data for return as an object. * * @param array $topic The component and its values. * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response * @since 0.1.0 */ public function prepare_item_for_response( $topic , $request ) { $context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ; // Base fields for every post. $data = array ( 'id' => $topic ->ID, 'date' => $this ->forum_endpoint->prepare_date_response( $topic ->post_date_gmt, $topic ->post_date ), 'date_gmt' => $this ->forum_endpoint->prepare_date_response( $topic ->post_date_gmt ), 'guid' => array ( 'rendered' => esc_url( bbp_get_topic_permalink( $topic ->ID ) ), 'raw' => $topic ->guid, ), 'modified' => $this ->forum_endpoint->prepare_date_response( $topic ->post_modified_gmt, $topic ->post_modified ), 'modified_gmt' => $this ->forum_endpoint->prepare_date_response( $topic ->post_modified_gmt ), 'password' => $topic ->post_password, 'slug' => $topic ->post_name, 'status' => $topic ->post_status, 'link' => bbp_get_topic_permalink( $topic ->ID ), 'author' => (int) $topic ->post_author, 'parent' => (int) $topic ->post_parent, 'sticky' => bbp_is_topic_sticky( $topic ->ID ), 'total_reply_count' => ( bbp_show_lead_topic() ? bbp_get_topic_reply_count( $topic ->ID ) : bbp_get_topic_post_count( $topic ->ID ) ), 'last_reply_id' => bbp_get_topic_last_reply_id( $topic ->ID ), 'last_active_author' => bbp_get_topic_last_active_id( $topic ->ID ), 'last_active_time' => $this ->forum_endpoint->bbp_rest_get_topic_last_active_time( $topic ->ID ), 'is_closed' => bbp_is_topic_closed( $topic ->ID ), 'voice_count' => (int) get_post_meta( $topic ->ID, '_bbp_voice_count' , true ), 'forum_id' => (int) bbp_get_topic_forum_id( $topic ->ID ), 'is_topic_anonymous' => (int) bbp_is_topic_anonymous( $topic ->ID ), 'anonymous_author_data' => ( bbp_is_topic_anonymous( $topic ->ID ) ? array ( 'name' => bbp_get_topic_author_display_name( $topic ->ID ), 'email' => bbp_get_topic_author_email( $topic ->ID ), 'website' => bbp_get_topic_author_url( $topic ->ID ), 'avatar' => get_avatar_url( bbp_get_topic_author_email( $topic ->ID ) ), ) : false ), 'classes' => bbp_get_topic_class( $topic ->ID ), ); $data [ 'title' ] = array ( 'raw' => $topic ->post_title, 'rendered' => bbp_get_topic_title( $topic ->ID ), ); /* Prepare content */ if ( ! empty ( $topic ->post_password ) ) { $this ->forum_endpoint->prepare_password_response( $topic ->post_password ); } $data [ 'short_content' ] = wp_trim_excerpt( $topic ->post_content ); $content = apply_filters( 'the_content' , $topic ->post_content ); $data [ 'content' ] = array ( 'raw' => $topic ->post_content, 'rendered' => $content , ); // Don't leave our cookie lying around: https://github.com/WP-API/WP-API/issues/1055. if ( ! empty ( $topic ->post_password ) ) { $_COOKIE [ 'wp-postpass_' . COOKIEHASH ] = '' ; } /* -- Prepare content */ $data [ 'group' ] = ( ( function_exists( 'bbp_is_forum_group_forum' ) && bbp_get_topic_forum_id( $topic ->ID ) && bbp_is_forum_group_forum( bbp_get_topic_forum_id( $topic ->ID ) ) && function_exists( 'groups_get_group' ) ) ? ( ! empty ( bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic ->ID ) ) ) ? groups_get_group( current( bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic ->ID ) ) ) ) : '' ) : '' ); // Setup subscribe/unsubscribe state. $data [ 'action_states' ] = $this ->get_topic_action_states( $topic ->ID ); $data [ 'topic_tags' ] = $this ->get_topic_tags( $topic ->ID ); // current user permission. $data [ 'current_user_permissions' ] = $this ->get_topic_current_user_permissions( $topic ->ID ); // Revisions. $data [ 'revisions' ] = $this ->get_topic_revisions( $topic ->ID ); $data = $this ->add_additional_fields_to_object( $data , $request ); $data = $this ->filter_response_by_context( $data , $context ); // @todo add prepare_links $response = rest_ensure_response( $data ); $response ->add_links( $this ->prepare_links( $topic ) ); /** * Filter a component value returned from the API. * * @param WP_REST_Response $response The Response data. * @param WP_REST_Request $request Request used to generate the response. * @param array $component The component and its values. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_prepare_value' , $response , $request , $topic ); } /** * Get the forums schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'topics' , 'type' => 'object' , 'properties' => array ( 'id' => array ( 'description' => __( 'Unique identifier for the topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'date' => array ( 'description' => __( 'The date the object was published, in the site\'s timezone.' , 'buddyboss' ), 'type' => 'string' , 'format' => 'date-time' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'date_gmt' => array ( 'description' => __( 'The date the object was published, as GMT.' , 'buddyboss' ), 'type' => 'string' , 'format' => 'date-time' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'guid' => array ( 'description' => __( 'The url identifier for the topic.' , 'buddyboss' ), 'type' => 'object' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, 'properties' => array ( 'raw' => array ( 'description' => __( 'GUID for the topic, as it exists in the database.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'rendered' => array ( 'description' => __( 'GUID for the topic, transformed for display.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'modified' => array ( 'description' => __( 'The date for topic was last modified, in the site\'s timezone.' , 'buddyboss' ), 'type' => 'string' , 'format' => 'date-time' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'modified_gmt' => array ( 'description' => __( 'The date for topic was last modified, as GMT.' , 'buddyboss' ), 'type' => 'string' , 'format' => 'date-time' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'password' => array ( 'description' => __( 'A password to protect access to the post.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'edit' ), ), 'slug' => array ( 'description' => __( 'An alphanumeric unique identifier for the topic.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'arg_options' => array ( 'sanitize_callback' => 'sanitize_title' , ), ), 'status' => array ( 'description' => __( 'The current status of the topic.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'link' => array ( 'description' => __( 'The permalink to this topic on the site.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'format' => 'uri' , ), 'author' => array ( 'description' => __( 'The ID for the author of the topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'parent' => array ( 'description' => __( 'ID of the parent topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'sticky' => array ( 'description' => __( 'Whether the topic is sticky or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'total_reply_count' => array ( 'description' => __( 'Total replies count in the topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'last_reply_id' => array ( 'description' => __( 'Recently posted reply id into the topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'last_active_author' => array ( 'description' => __( 'Last updated the user\'s ID in topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'last_active_time' => array ( 'description' => __( 'Last updated time for the topic.' , 'buddyboss' ), 'type' => 'date-time' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'is_closed' => array ( 'description' => __( 'Whether the topic is closed or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'voice_count' => array ( 'description' => __( 'Voice count of the topic' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'forum_id' => array ( 'description' => __( 'Forum ID for the topic.' , 'buddyboss' ), 'type' => 'integer' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'is_topic_anonymous' => array ( 'description' => __( 'Whether the post is by an anonymous user or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'anonymous_author_data' => array ( 'description' => __( 'An anonymous users data.' , 'buddyboss' ), 'type' => 'object' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'properties' => array ( 'name' => array ( 'description' => __( 'Name of the anonymous user.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'email' => array ( 'description' => __( 'Email address of the anonymous user.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'website' => array ( 'description' => __( 'Website of the anonymous user.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'avatar' => array ( 'description' => __( 'Avatar url of the anonymous user.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'classes' => array ( 'description' => __( 'Classes lists for the topic.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'title' => array ( 'description' => __( 'The title of the topic.' , 'buddyboss' ), 'context' => array ( 'embed' , 'view' , 'edit' ), 'type' => 'object' , 'properties' => array ( 'raw' => array ( 'description' => __( 'Content for the title of the topic, as it exists in the database.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'rendered' => array ( 'description' => __( 'The title of the topic, transformed for display.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'short_content' => array ( 'description' => __( 'Short Content of the topic.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'content' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'The content of the topic.' , 'buddyboss' ), 'type' => 'object' , 'properties' => array ( 'raw' => array ( 'description' => __( 'Content for the topic, as it exists in the database.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'rendered' => array ( 'description' => __( 'HTML content for the topic, transformed for display.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'group' => array ( 'description' => __( 'Topic forum\'s group.' , 'buddyboss' ), 'context' => array ( 'embed' , 'view' , 'edit' ), 'type' => 'object' , ), 'action_states' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'Available actions with current user for topic.' , 'buddyboss' ), 'type' => 'object' , 'properties' => array ( 'subscribed' => array ( 'description' => __( 'Check whether the current user is subscribed or not in the topic.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'favorited' => array ( 'description' => __( 'Check whether the topic is favorited or not for the user.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'open' => array ( 'description' => __( 'Check whether the topic is open or not for the user.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'sticky' => array ( 'description' => __( 'Check whether the topic is sticky or not for the user.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'super_sticky' => array ( 'description' => __( 'Check whether the topic is super sticky or not for the user.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'spam' => array ( 'description' => __( 'Check whether the topic status is spam or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'trash' => array ( 'description' => __( 'Check whether the topic status is trash or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'topic_tags' => array ( 'description' => __( 'Topic\'s tags' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'current_user_permissions' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'Current user\'s permission with the topic.' , 'buddyboss' ), 'type' => 'object' , 'properties' => array ( 'show_replies' => array ( 'description' => __( 'Whether shows the replies for the current user or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'edit' => array ( 'description' => __( 'Whether the current user can edit the topic or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'moderate' => array ( 'description' => __( 'Whether the current user is moderator or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'reply' => array ( 'description' => __( 'Whether the current user can reply on topic or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), 'trash' => array ( 'description' => __( 'Whether the current user can trash a topic or not.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), ), ), ), 'revisions' => array ( 'description' => __( 'Revisions for topic.' , 'buddyboss' ), 'context' => array ( 'embed' , 'view' , 'edit' ), 'type' => 'object' , ), ), ); /** * Filters the topic schema. * * @param string $schema The endpoint schema. */ return apply_filters( 'bp_rest_topic_schema' , $this ->add_additional_fields_schema( $schema ) ); } /** * Get the query params for collections. * * @return array * @since 0.1.0 */ public function get_collection_params() { $params = parent::get_collection_params(); $params [ 'context' ][ 'default' ] = 'view' ; $params [ 'per_page' ][ 'default' ] = bbp_get_topics_per_page(); $params [ 'author' ] = array ( 'description' => __( 'Author ID, or comma-separated list of IDs.' , 'buddyboss' ), 'default' => '' , 'type' => 'string' , 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'author_exclude' ] = array ( 'description' => __( 'An array of author IDs not to query from.' , 'buddyboss' ), 'type' => 'array' , 'items' => array ( 'type' => 'integer' ), 'sanitize_callback' => 'wp_parse_id_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'exclude' ] = array ( 'description' => __( 'An array of topic IDs not to retrieve.' , 'buddyboss' ), 'type' => 'array' , 'items' => array ( 'type' => 'integer' ), 'sanitize_callback' => 'wp_parse_id_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'include' ] = array ( 'description' => __( 'An array of topic IDs to retrieve.' , 'buddyboss' ), 'type' => 'array' , 'items' => array ( 'type' => 'integer' ), 'sanitize_callback' => 'wp_parse_id_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'offset' ] = array ( 'description' => __( 'The number of topics to offset before retrieval.' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'order' ] = array ( 'description' => __( 'Designates ascending or descending order of topics.' , 'buddyboss' ), 'default' => 'asc' , 'type' => 'string' , 'enum' => array ( 'asc' , 'desc' ), 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'orderby' ] = array ( 'description' => __( 'Sort retrieved topics by parameter.' , 'buddyboss' ), 'type' => 'array' , 'items' => array ( 'type' => 'string' , 'enum' => array ( 'meta_value' , 'date' , 'ID' , 'author' , 'title' , 'modified' , 'parent' , 'rand' , 'popular' , 'activity' , ), ), 'sanitize_callback' => 'bp_rest_sanitize_string_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'status' ] = array ( 'description' => __( 'Limit result set to topic assigned a specific status.' , 'buddyboss' ), 'default' => array ( 'publish' , 'private' ), 'type' => 'array' , 'items' => array ( 'type' => 'string' , 'enum' => array ( 'publish' , 'private' , 'hidden' ), ), 'sanitize_callback' => 'bp_rest_sanitize_string_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'parent' ] = array ( 'description' => __( 'Forum ID to retrieve all the topics.' , 'buddyboss' ), 'default' => '0' , 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'subscriptions' ] = array ( 'description' => __( 'Retrieve subscribed topics by user.' , 'buddyboss' ), 'default' => false, 'type' => 'boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'favorites' ] = array ( 'description' => __( 'Retrieve favorite topics by the current user.' , 'buddyboss' ), 'default' => false, 'type' => 'boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'tag' ] = array ( 'description' => __( 'Search topic with specific tag.' , 'buddyboss' ), 'type' => 'string' , 'validate_callback' => 'rest_validate_request_arg' , ); /** * Filters the collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_topics_collection_params' , $params ); } /** * Prepare links for the request. * * @param WP_Post $post Post object. * * @return array * @since 0.1.0 */ protected function prepare_links( $post ) { $base = sprintf( '/%s/%s/' , $this -> namespace , $this ->rest_base ); // Entity meta. $links = array ( 'self' => array ( 'href' => rest_url( $base . $post ->ID ), ), 'collection' => array ( 'href' => rest_url( $base ), ), 'user' => array ( 'href' => rest_url( bp_rest_get_user_url( $post ->post_author ) ), 'embeddable' => true, ), ); $form_id = (int) bbp_get_topic_forum_id( $post ->ID ); if ( ! empty ( $form_id ) ) { $form_base = sprintf( '/%s/%s/' , $this ->forum_endpoint-> namespace , $this ->forum_endpoint->rest_base ); $links [ 'forum' ] = array ( 'href' => rest_url( $form_base . $form_id ), 'embeddable' => true, ); } /** * Filter links prepared for the REST response. * * @param array $links The prepared links of the REST response. * @param WP_Post $post Post object. * * @since 0.1.0 */ return apply_filters( 'bp_rest_topic_prepare_links' , $links , $post ); } /** * Get topic actions state based on current user. * * @param int $topic_id ID of the topic. * * @return array|void */ public function get_topic_action_states( $topic_id ) { if ( empty ( $topic_id ) ) { return ; } $topic_id = (int) $topic_id ; $user_id = bbp_get_user_id( 0, true, true ); $state = array ( 'subscribed' => '' , 'favorited' => '' , 'open' => bbp_is_topic_open( $topic_id ), 'sticky' => bbp_is_topic_sticky( $topic_id ), 'super_sticky' => bbp_is_topic_super_sticky( $topic_id ), 'spam' => bbp_is_topic_spam( $topic_id ), 'trash' => bbp_is_topic_trash( $topic_id ), ); if ( bbp_is_favorites_active() && current_user_can( 'edit_user' , $user_id ) ) { $state [ 'favorited' ] = bbp_is_user_favorite( $user_id , $topic_id ); } if ( bbp_is_subscriptions_active() && current_user_can( 'edit_user' , $user_id ) ) { $state [ 'subscribed' ] = bbp_is_user_subscribed( $user_id , $topic_id ); } return $state ; } /** * Topic permissions for the current user. * * @param int $topic_id ID of the topic. * * @return array|void */ public function get_topic_current_user_permissions( $topic_id ) { if ( empty ( $topic_id ) ) { return ; } $topic = bbp_get_topic( bbp_get_topic_id( (int) $topic_id ) ); $form_id = bbp_get_topic_forum_id( $topic_id ); return array ( 'show_replies' => $this ->forum_endpoint->can_access_content( $form_id ), 'edit' => ( current_user_can( 'moderate' ) || ( ! empty ( $topic ) && current_user_can( 'edit_topic' , $topic ->ID ) && ! bbp_past_edit_lock( $topic ->post_date_gmt ) ) ), 'moderate' => ! empty ( $topic ) && current_user_can( 'moderate' , $topic_id ), 'reply' => ! empty ( $topic ) && bbp_current_user_can_publish_replies() && $this ->forum_endpoint->can_access_content( $form_id , true ), 'trash' => ! empty ( $topic ) && current_user_can( 'delete_topic' , $topic ->ID ), ); } /** * Get Topic Tags. * * @param int $topic_id ID of the topic. * * @return mixed|void */ public function get_topic_tags( $topic_id ) { if ( empty ( $topic_id ) ) { return ; } // Topic is spammed so display pre-spam terms. if ( bbp_is_topic_spam( $topic_id ) ) { // Get pre-spam terms. $new_terms = get_post_meta( $topic_id , '_bbp_spam_topic_tags' , true ); // If terms exist, explode them and compile the return value. if ( empty ( $new_terms ) ) { $new_terms = '' ; } // Topic is not spam so get real terms. } else { $terms = array_filter ( ( array ) get_the_terms( $topic_id , bbp_get_topic_tag_tax_id() ) ); // Loop through them. foreach ( $terms as $term ) { $new_terms [] = $term ->name; } } // Set the return value. $topic_tags = ( ! empty ( $new_terms ) ) ? implode( ', ' , $new_terms ) : '' ; return apply_filters( 'bbp_get_form_topic_tags' , esc_attr( $topic_tags ) ); } /** * Get revisions for topic. * from: bbp_get_topic_revision_log() * * @param int $topic_id ID of the topic. * * @return bool|void */ public function get_topic_revisions( $topic_id = 0 ) { // Create necessary variables. $topic_id = bbp_get_topic_id( $topic_id ); $revision_log = bbp_get_topic_raw_revision_log( $topic_id ); if ( empty ( $topic_id ) || empty ( $revision_log ) || ! is_array ( $revision_log ) ) { return false; } $revisions = bbp_get_topic_revisions( $topic_id ); if ( empty ( $revisions ) ) { return false; } $retval = array (); // Loop through revisions. foreach ( ( array ) $revisions as $revision ) { if ( empty ( $revision_log [ $revision ->ID ] ) ) { $author_id = $revision ->post_author; $reason = '' ; } else { $author_id = $revision_log [ $revision ->ID ][ 'author' ]; $reason = $revision_log [ $revision ->ID ][ 'reason' ]; } $retval [] = array ( 'time' => esc_html( bbp_get_time_since( bbp_convert_date( $revision ->post_modified ) ) ), 'author_id' => bbp_get_topic_author_id( $topic_id ), 'author_name' => bbp_get_topic_author_display_name( $revision ->ID ), 'avatar' => ( ! empty ( bbp_get_topic_author_id( $topic_id ) ) ? get_avatar_url( bbp_get_topic_author_id( $topic_id ), 300 ) : '' ), 'reason' => $reason , ); } return apply_filters( 'bp_rest_bbp_get_topic_revision_log' , $retval , $topic_id ); } /** * Prepare a single topic for create or update * * @param WP_REST_Request $request Request object. * * @return object $topic User object. */ protected function prepare_topic_for_database( $request ) { $topic = new stdClass(); if ( isset( $request [ 'id' ] ) ) { $topic ->bbp_topic_id = $request [ 'id' ]; } if ( isset( $request [ 'parent' ] ) ) { $topic ->bbp_forum_id = $request [ 'parent' ]; } if ( isset( $request [ 'group' ] ) ) { $topic ->bbp_group_id = $request [ 'group' ]; } if ( isset( $request [ 'title' ] ) ) { $topic ->bbp_topic_title = $request [ 'title' ]; } if ( isset( $request [ 'status' ] ) ) { $topic ->bbp_topic_status = $request [ 'status' ]; } if ( isset( $request [ 'topic_tags' ] ) ) { $topic ->bbp_topic_tags = $request [ 'topic_tags' ]; } if ( isset( $request [ 'content' ] ) ) { $topic ->bbp_topic_content = $request [ 'content' ]; } if ( isset( $request [ 'sticky' ] ) ) { $topic ->bbp_stick_topic = $request [ 'sticky' ]; } if ( isset( $request [ 'reason_editing' ] ) ) { $topic ->bbp_topic_edit_reason = $request [ 'reason_editing' ]; } if ( isset( $request [ 'log' ] ) ) { $topic ->bbp_log_topic_edit = $request [ 'log' ]; } if ( isset( $request [ 'subscribe' ] ) && ( true === $request [ 'subscribe' ] ) ) { $topic ->bbp_topic_subscription = true; } elseif ( isset( $request [ 'subscribe' ] ) && ( false === $request [ 'subscribe' ] ) ) { $topic ->bbp_topic_subscription = false; } /** * Filter topic data before inserting user via REST API * * @param object $topic Topic object. * @param WP_REST_Request $request Request object. */ return apply_filters( 'bp_rest_topic_object' , $topic , $request ); } /** * Strip super stickies from the topic query * * @param array $super the super sticky post ID's. * * @return array (empty) */ public function no_super_stickies( $super = array () ) { $super = array (); return $super ; } /** * Sanitize a query argument used to pass some search terms. * Accepts a single parameter to be used for forums, topics, or replies. * - from bbp_sanitize_search_request(); * * @since 2.6.0 bbPress (r6903) * * @param string $terms Search Term. * * @return mixed */ public function bbp_sanitize_search_request( $terms ) { // Maybe implode if an array. if ( is_array ( $terms ) ) { $terms = implode( ' ' , $terms ); } // Sanitize. $retval = sanitize_title( trim( $terms ) ); // Filter & return. return apply_filters( 'bbp_sanitize_search_request' , $retval , $query_arg ); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- bbp_sanitize_search_request — Sanitize a query argument used to pass some search terms.
- create_item — Create a topic.
- create_item_permissions_check — Check if a given request has access to create a topic.
- delete_item — Delete a topic.
- delete_item_permissions_check — Check if a given request has access to delete a topic.
- get_collection_params — Get the query params for collections.
- get_endpoint_args_for_item_schema — Edit some arguments for the endpoint's CREATABLE, EDITABLE and DELETABLE methods.
- get_item — Retrieve a single topic.
- get_item_permissions_check — Check if a given request has access to list topic.
- get_item_schema — Get the forums schema, conforming to JSON Schema.
- get_items — Retrieve Topics.
- get_items_permissions_check — Check if a given request has access to list topics.
- get_topic_action_states — Get topic actions state based on current user.
- get_topic_current_user_permissions — Topic permissions for the current user.
- get_topic_revisions — Get revisions for topic.
- get_topic_tags — Get Topic Tags.
- no_super_stickies — Strip super stickies from the topic query
- prepare_item_for_response — Prepares component data for return as an object.
- prepare_links — Prepare links for the request.
- prepare_topic_for_database — Prepare a single topic for create or update
- register_routes — Register the component routes.
- update_item — Update/Edit a topic.
- update_item_permissions_check — Check if a given request has access to update a topic.
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.