BP_Document
Database interaction class for the BuddyBoss document component.
Description
Instance methods are available for creating/editing an document, static methods for querying document.
Source
File: bp-document/classes/class-bp-document.php
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 | class BP_Document { /** Properties ************************************************************/ /** * ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $id ; /** * Blog ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $blog_id ; /** * Attachment ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $attachment_id ; /** * User ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $user_id ; /** * Title of the document item. * * @since BuddyBoss 1.4.0 * @var string */ var $title ; /** * Folder ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $folder_id ; /** * Group ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $group_id ; /** * Activity ID of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $activity_id ; /** * Privacy of the document item. * * @since BuddyBoss 1.4.0 * @var string */ var $privacy ; /** * Menu order of the document item. * * @since BuddyBoss 1.4.0 * @var int */ var $menu_order ; /** * Upload date of the document item. * * @since BuddyBoss 1.4.0 * @var string */ var $date_created ; /** * Update date of the document item. * * @since BuddyBoss 1.4.0 * @var string */ var $date_modified ; /** * Error holder. * * @since BuddyBoss 1.4.0 * @var WP_Error */ public $errors ; /** * Error type to return. Either 'bool' or 'wp_error'. * * @since BuddyBoss 1.4.0 * @var string */ public $error_type = 'bool' ; static $per_pdf_secs = 20; // Seconds to allow for regenerating the preview of each PDF in the "Regen. PDF Previews" administraion tool. static $min_time_limit = 300; // Minimum seconds to set max execution time to on doing regeneration. static $poll_interval = 1; // How often to poll for progress info in seconds. static $timing_dec_places = 1; // Number of decimal places to show on time (in seconds) taken. /** * Constructor method. * * @param int|bool $id Optional. The ID of a specific activity item. * * @since BuddyBoss 1.4.0 */ function __construct( $id = false ) { // Instantiate errors object. $this ->errors = new WP_Error(); if ( ! empty ( $id ) ) { $this ->id = (int) $id ; $this ->populate(); } } /** * Called on 'wp_image_editors' action. * Adds Ghostscript `BP_GOPP_Image_Editor_GS` class to head of image editors list. */ static function bp_document_wp_image_editors( $image_editors ) { if ( ! in_array( 'BP_GOPP_Image_Editor_GS' , $image_editors , true ) ) { self::bp_document_load_gopp_image_editor_gs(); array_unshift ( $image_editors , 'BP_GOPP_Image_Editor_GS' ); } return $image_editors ; } /** * Helper to load BP_GOPP_Image_Editor_GS class. */ static function bp_document_load_gopp_image_editor_gs() { if ( ! class_exists ( 'BP_GOPP_Image_Editor_GS' ) ) { if ( ! class_exists ( 'WP_Image_Editor' ) ) { require ABSPATH . WPINC . '/class-wp-image-editor.php' ; } require trailingslashit( dirname( __FILE__ ) ) . '/class-bp-gopp-image-editor-gs.php' ; } } /** * Populate the object with data about the specific document item. * * @since BuddyBoss 1.4.0 */ public function populate() { global $wpdb ; $row = wp_cache_get( $this ->id, 'bp_document' ); if ( false === $row ) { $bp = buddypress(); $row = $wpdb ->get_row( $wpdb ->prepare( "SELECT * FROM {$bp->document->table_name} WHERE id = %d" , $this ->id ) ); // db call ok; no-cache ok; wp_cache_set( $this ->id, $row , 'bp_document' ); } if ( empty ( $row ) ) { $this ->id = 0; return ; } $this ->id = (int) $row ->id; $this ->blog_id = (int) $row ->blog_id; $this ->attachment_id = (int) $row ->attachment_id; $this ->user_id = (int) $row ->user_id; $this ->title = $row ->title; $this ->folder_id = (int) $row ->folder_id; $this ->group_id = (int) $row ->group_id; $this ->activity_id = (int) $row ->activity_id; $this ->privacy = $row ->privacy; $this ->menu_order = (int) $row ->menu_order; $this ->date_created = $row ->date_created; $this ->date_modified = $row ->date_modified; } /** * Remove all temp directory. * * @param $dir * * @since BuddyBoss 1.4.0 */ public static function bp_document_remove_temp_directory( $dir ) { if ( is_dir ( $dir ) ) { $objects = scandir( $dir ); foreach ( $objects as $object ) { if ( $object != '.' && $object != '..' ) { if ( filetype ( $dir . '/' . $object ) == 'dir' ) { self::bp_document_remove_temp_directory( $dir . '/' . $object ); } else { unlink( $dir . '/' . $object ); } } } reset( $objects ); rmdir ( $dir ); } } /** * Get document items, as specified by parameters. * * @param array $args { * An array of arguments. All items are optional. * * @type int $page Which page of results to fetch. Using page=1 without per_page will result * in no pagination. Default: 1. * @type int|bool $per_page Number of results per page. Default: 20. * @type int|bool $max Maximum number of results to return. Default: false (unlimited). * @type string $fields Document fields to return. Pass 'ids' to get only the document IDs. * 'all' returns full document objects. * @type string $sort ASC or DESC. Default: 'DESC'. * @type string $order_by Column to order results by. * @type array $exclude Array of document IDs to exclude. Default: false. * @type string $search_terms Limit results by a search term. Default: false. * @type string|bool $count_total If true, an additional DB query is run to count the total document items * for the query. Default: false. * } * @return array The array returned has two keys: * - 'total' is the count of located documents * - 'documents' is an array of the located documents * @since BuddyBoss 1.4.0 */ public static function get( $args = array () ) { global $wpdb ; $bp = buddypress(); $r = wp_parse_args( $args , array ( 'scope' => '' , // Scope - Groups, friends etc. 'page' => 1, // The current page. 'per_page' => 20, // Document items per page. 'max' => false, // Max number of items to return. 'fields' => 'all' , // Fields to include. 'sort' => 'DESC' , // ASC or DESC. 'order_by' => 'date_created' , // Column to order by. 'exclude' => false, // Array of ids to exclude. 'in' => false, // Array of ids to limit query by (IN). 'search_terms' => false, // Terms to search by. 'privacy' => false, // public, loggedin, onlyme, friends, grouponly, message. 'count_total' => false, // Whether or not to use count_total. 'folder_id' => false, 'folder' => true, 'user_directory' => true, 'meta_query' => false, // Filter by document meta. ) ); // Select conditions. $select_sql = 'SELECT DISTINCT d.id' ; $from_sql = " FROM {$bp->document->table_name} d" ; $join_sql = '' ; // Where conditions. $where_conditions = array (); if ( ! empty ( $r [ 'scope' ] ) ) { $scope_query = self::get_scope_query_sql( $r [ 'scope' ], $r ); // Override some arguments if needed. if ( ! empty ( $scope_query [ 'override' ] ) ) { $r = array_replace_recursive( $r , $scope_query [ 'override' ] ); } } // Searching. if ( $r [ 'search_terms' ] ) { $search_terms_like = '%' . bp_esc_like( $r [ 'search_terms' ] ) . '%' ; $where_conditions [ 'search_sql' ] = $wpdb ->prepare( 'd.title LIKE %s' , $search_terms_like ); /** * Filters whether or not to include users for search parameters. * * @param bool $value Whether or not to include user search. Default false. * * @since BuddyBoss 1.4.0 */ if ( apply_filters( 'bp_document_get_include_user_search' , false ) ) { $user_search = get_user_by( 'slug' , $r [ 'search_terms' ] ); if ( false !== $user_search ) { $user_id = $user_search ->ID; $where_conditions [ 'search_sql' ] .= $wpdb ->prepare( ' OR d.user_id = %d' , $user_id ); } } } // Sorting. $sort = $r [ 'sort' ]; if ( $sort !== 'ASC' && $sort !== 'DESC' ) { $sort = 'DESC' ; } switch ( $r [ 'order_by' ] ) { case 'id' : case 'user_id' : case 'blog_id' : case 'attachment_id' : case 'title' : case 'folder' : case 'folder_id' : case 'activity_id' : case 'group_id' : case 'menu_order' : break ; default : $r [ 'order_by' ] = 'date_created' ; break ; } $order_by = 'd.' . $r [ 'order_by' ]; // Exclude specified items. if ( ! empty ( $r [ 'exclude' ] ) ) { $exclude = implode( ',' , wp_parse_id_list( $r [ 'exclude' ] ) ); $where_conditions [ 'exclude' ] = "d.id NOT IN ({$exclude})" ; } // The specific ids to which you want to limit the query. if ( ! empty ( $r [ 'in' ] ) ) { $in = implode( ',' , wp_parse_id_list( $r [ 'in' ] ) ); $where_conditions [ 'in' ] = "d.id IN ({$in})" ; // we want to disable limit query when include document ids. $r [ 'page' ] = false; $r [ 'per_page' ] = false; } if ( ! empty ( $r [ 'activity_id' ] ) ) { $where_conditions [ 'activity' ] = "d.activity_id = {$r['activity_id']}" ; } // existing-document check to query document which has no folders assigned. if ( ! empty ( $r [ 'folder_id' ] ) && 'existing-document' !== $r [ 'folder_id' ] ) { $where_conditions [ 'folder' ] = "d.folder_id = {$r['folder_id']}" ; } elseif ( ! empty ( $r [ 'folder_id' ] ) && 'existing-document' === $r [ 'folder_id' ] ) { $where_conditions [ 'folder' ] = 'd.folder_id = 0' ; } if ( ! empty ( $r [ 'user_id' ] ) ) { $where_conditions [ 'user' ] = "d.user_id = {$r['user_id']}" ; } if ( ! empty ( $r [ 'group_id' ] ) ) { $where_conditions [ 'user' ] = "d.group_id = {$r['group_id']}" ; } if ( ! empty ( $r [ 'privacy' ] ) ) { $privacy = "'" . implode( "', '" , $r ['privacy '] ) . "' "; $where_conditions [ 'privacy' ] = "d.privacy IN ({$privacy})" ; } // Process meta_query into SQL. $meta_query_sql = self::get_meta_query_sql( $r [ 'meta_query' ] ); if ( ! empty ( $meta_query_sql [ 'join' ] ) ) { $join_sql .= $meta_query_sql [ 'join' ]; } if ( ! empty ( $meta_query_sql [ 'where' ] ) ) { $where_conditions [] = $meta_query_sql [ 'where' ]; } /** * Filters the MySQL WHERE conditions for the Document items get method. * * @param array $where_conditions Current conditions for MySQL WHERE statement. * @param array $r Parsed arguments passed into method. * @param string $select_sql Current SELECT MySQL statement at point of execution. * @param string $from_sql Current FROM MySQL statement at point of execution. * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. * * @since BuddyBoss 1.4.0 */ $where_conditions = apply_filters( 'bp_document_get_where_conditions' , $where_conditions , $r , $select_sql , $from_sql , $join_sql ); if ( empty ( $where_conditions ) ) { $where_conditions [ '2' ] = '2' ; } // Join the where conditions together. if ( ! empty ( $scope_query [ 'sql' ] ) ) { $where_sql = 'WHERE ( ' . join( ' AND ' , $where_conditions ) . ' ) OR ( ' . $scope_query [ 'sql' ] . ' )' ; } else { $where_sql = 'WHERE ' . join( ' AND ' , $where_conditions ); } /** * Filter the MySQL JOIN clause for the main document query. * * @param string $join_sql JOIN clause. * @param array $r Method parameters. * @param string $select_sql Current SELECT MySQL statement. * @param string $from_sql Current FROM MySQL statement. * @param string $where_sql Current WHERE MySQL statement. * * @since BuddyBoss 1.4.0 */ $join_sql = apply_filters( 'bp_document_get_join_sql' , $join_sql , $r , $select_sql , $from_sql , $where_sql ); // Sanitize page and per_page parameters. $page = absint( $r [ 'page' ] ); $per_page = absint( $r [ 'per_page' ] ); $retval = array ( 'documents' => null, 'total' => null, 'has_more_items' => null, ); // Query first for document IDs. $document_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, d.id {$sort}" ; if ( ! empty ( $per_page ) && ! empty ( $page ) ) { // We query for $per_page + 1 items in order to // populate the has_more_items flag. $document_ids_sql .= $wpdb ->prepare( ' LIMIT %d, %d' , absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); } /** * Filters the paged document MySQL statement. * * @param string $document_ids_sql MySQL statement used to query for Document IDs. * @param array $r Array of arguments passed into method. * * @since BuddyBoss 1.4.0 */ $document_ids_sql = apply_filters( 'bp_document_paged_activities_sql' , $document_ids_sql , $r ); $cache_group = 'bp_document' ; $cached = bp_core_get_incremented_cache( $document_ids_sql , $cache_group ); if ( false === $cached ) { $document_ids = $wpdb ->get_col( $document_ids_sql ); bp_core_set_incremented_cache( $document_ids_sql , $cache_group , $document_ids ); } else { $document_ids = $cached ; } $retval [ 'has_more_items' ] = ! empty ( $per_page ) && count ( $document_ids ) > $per_page ; // If we've fetched more than the $per_page value, we // can discard the extra now. if ( ! empty ( $per_page ) && count ( $document_ids ) === $per_page + 1 ) { array_pop ( $document_ids ); } if ( 'ids' === $r [ 'fields' ] ) { $documents = array_map ( 'intval' , $document_ids ); } else { $documents = self::get_document_data( $document_ids ); } if ( 'ids' !== $r [ 'fields' ] ) { // Get the fullnames of users so we don't have to query in the loop. // $documents = self::append_user_fullnames( $documents ); // Pre-fetch data associated with document users and other objects. $documents = self::prefetch_object_data( $documents ); } $retval [ 'documents' ] = $documents ; // If $max is set, only return up to the max results. if ( ! empty ( $r [ 'count_total' ] ) ) { /** * Filters the total document MySQL statement. * * @param string $value MySQL statement used to query for total documents. * @param string $where_sql MySQL WHERE statement portion. * @param string $sort Sort direction for query. * * @since BuddyBoss 1.4.0 */ $total_documents_sql = apply_filters( 'bp_document_total_documents_sql' , "SELECT count(DISTINCT d.id) FROM {$bp->document->table_name} d {$join_sql} {$where_sql}" , $where_sql , $sort ); $cached = bp_core_get_incremented_cache( $total_documents_sql , $cache_group ); if ( false === $cached ) { $total_documents = $wpdb ->get_var( $total_documents_sql ); bp_core_set_incremented_cache( $total_documents_sql , $cache_group , $total_documents ); } else { $total_documents = $cached ; } if ( ! empty ( $r [ 'max' ] ) ) { if ( (int) $total_documents > (int) $r [ 'max' ] ) { $total_documents = $r [ 'max' ]; } } $retval [ 'total' ] = $total_documents ; } return $retval ; } /** Static Methods ***************************************************/ /** * Get the SQL for the 'scope' param in BP_Document::get(). * A scope is a predetermined set of document arguments. This method is used * to grab these document arguments and override any existing args if needed. * Can handle multiple scopes. * * @param mixed $scope The document scope. Accepts string or array of scopes. * @param array $r Current activity arguments. Same as those of BP_document::get(), * but merged with defaults. * * @return false|array 'sql' WHERE SQL string and 'override' document args. * @since BuddyBoss 1.4.0 */ public static function get_scope_query_sql( $scope = false, $r = array () ) { // Define arrays for future use. $query_args = array (); $override = array (); $retval = array (); // Check for array of scopes. if ( is_array ( $scope ) ) { $scopes = $scope ; // Explode a comma separated string of scopes. } elseif ( is_string ( $scope ) ) { $scopes = explode ( ',' , $scope ); } // Bail if no scope passed. if ( empty ( $scopes ) ) { return false; } // Helper to easily grab the 'user_id'. if ( ! empty ( $r [ 'filter' ][ 'user_id' ] ) ) { $r [ 'user_id' ] = $r [ 'filter' ][ 'user_id' ]; } // Parse each scope; yes! we handle multiples! foreach ( $scopes as $scope ) { $scope_args = array (); /** * Plugins can hook here to set their document arguments for custom scopes. * This is a dynamic filter based on the document scope. eg: * - 'bp_document_set_groups_scope_args' * - 'bp_document_set_friends_scope_args' * To see how this filter is used, plugin devs should check out: * - bp_groups_filter_document_scope() - used for 'groups' scope * - bp_friends_filter_document_scope() - used for 'friends' scope * * @param array { * Document query clauses. * * @type array { * Document arguments for your custom scope. * See {@link BP_Document_Query::_construct()} for more details. * } * @type array $override Optional. Override existing document arguments passed by $r. * } * } * * @param array $r Current activity arguments passed in BP_Document::get(). * * @since BuddyBoss 1.4.0 */ $scope_args = apply_filters( "bp_document_set_{$scope}_scope_args" , array (), $r ); if ( ! empty ( $scope_args ) ) { // Merge override properties from other scopes // this might be a problem... if ( ! empty ( $scope_args [ 'override' ] ) ) { $override = array_merge ( $override , $scope_args [ 'override' ] ); unset( $scope_args [ 'override' ] ); } // Save scope args. if ( ! empty ( $scope_args ) ) { $query_args [] = $scope_args ; } } } if ( ! empty ( $query_args ) ) { // Set relation to OR. $query_args [ 'relation' ] = 'OR' ; $query = new BP_Document_Query( $query_args ); $sql = $query ->get_sql(); if ( ! empty ( $sql ) ) { $retval [ 'sql' ] = $sql ; } } if ( ! empty ( $override ) ) { $retval [ 'override' ] = $override ; } return $retval ; } /** * Convert document IDs to document objects, as expected in template loop. * * @param array $document_ids Array of document IDs. * * @return array * @since BuddyBoss 1.4.0 */ protected static function get_document_data( $document_ids = array () ) { global $wpdb ; // Bail if no document ID's passed. if ( empty ( $document_ids ) ) { return array (); } // Get BuddyPress. $bp = buddypress(); $documents = array (); $uncached_ids = bp_get_non_cached_ids( $document_ids , 'bp_document' ); // Prime caches as necessary. if ( ! empty ( $uncached_ids ) ) { // Format the document ID's for use in the query below. $uncached_ids_sql = implode( ',' , wp_parse_id_list( $uncached_ids ) ); // Fetch data from document table, preserving order. $queried_adata = $wpdb ->get_results( "SELECT * FROM {$bp->document->table_name} WHERE id IN ({$uncached_ids_sql})" ); // db call ok; no-cache ok; // Put that data into the placeholders created earlier, // and add it to the cache. foreach ( ( array ) $queried_adata as $adata ) { wp_cache_set( $adata ->id, $adata , 'bp_document' ); } } // Now fetch data from the cache. foreach ( $document_ids as $document_id ) { // Integer casting. $document = wp_cache_get( $document_id , 'bp_document' ); if ( ! empty ( $document ) ) { $document ->id = (int) $document ->id; $document ->blog_id = (int) $document ->blog_id; $document ->user_id = (int) $document ->user_id; $document ->attachment_id = (int) $document ->attachment_id; $document ->folder_id = (int) $document ->folder_id; $document ->activity_id = (int) $document ->activity_id; $document ->group_id = (int) $document ->group_id; $document ->menu_order = (int) $document ->menu_order; $document ->parent = (int) $document ->folder_id; } $group_name = '' ; $visibility = '' ; if ( $document ->group_id > 0 ) { if ( bp_is_active( 'groups' ) ) { $group = groups_get_group( $document ->group_id ); $group_name = bp_get_group_name( $group ); $status = bp_get_group_status( $group ); if ( 'hidden' === $status || 'private' === $status ) { $visibility = esc_html__( 'Group Members' , 'buddyboss' ); } else { $visibility = ucfirst( $status ); } } else { $visibility = '' ; } } else { $document_privacy = bp_document_get_visibility_levels(); if ( 'friends' === $document ->privacy && bp_loggedin_user_id() !== (int) $document ->user_id ) { $visibility = esc_html__( 'Connections' , 'buddyboss' ); } elseif ( 'message' === $document ->privacy ) { $visibility = esc_html__( 'Message' , 'buddyboss' ); } elseif ( 'forums' === $document ->privacy ) { $visibility = esc_html__( 'Forums' , 'buddyboss' ); } else { $visibility = ( isset( $document_privacy [ $document ->privacy ] ) ) ? $document_privacy [ $document ->privacy ] : '' ; } } // fetch attachment data. $attachment_data = new stdClass(); $attachment_data ->full = '' ; $attachment_data ->thumb = '' ; $attachment_data ->activity_thumb = '' ; $attachment_data ->meta = wp_get_attachment_metadata( $document ->attachment_id ); $document ->attachment_data = $attachment_data ; $document ->group_name = $group_name ; $document ->visibility = $visibility ; $documents [] = $document ; } // Then fetch user data. $user_query = new BP_User_Query( array ( 'user_ids' => wp_list_pluck( $documents , 'user_id' ), 'populate_extras' => false, ) ); // Associated located user data with document items. foreach ( $documents as $a_index => $a_item ) { $a_user_id = intval ( $a_item ->user_id ); $a_user = isset( $user_query ->results[ $a_user_id ] ) ? $user_query ->results[ $a_user_id ] : '' ; if ( ! empty ( $a_user ) ) { $documents [ $a_index ]->user_email = $a_user ->user_email; $documents [ $a_index ]->user_nicename = $a_user ->user_nicename; $documents [ $a_index ]->user_login = $a_user ->user_login; $documents [ $a_index ]->display_name = $a_user ->display_name; } } return $documents ; } /** * Pre-fetch data for objects associated with document items. * Document items are associated with users, and often with other * BuddyPress data objects. Here, we pre-fetch data about these * associated objects, so that inline lookups - done primarily when * building action strings - do not result in excess database queries. * * @param array $documents Array of document. * * @return array $documents Array of document. * @since BuddyBoss 1.4.0 */ protected static function prefetch_object_data( $documents ) { /** * Filters inside prefetch_object_data method to aid in pre-fetching object data associated with document item. * * @param array $documents Array of document. * * @since BuddyBoss 1.4.0 */ return apply_filters( 'bp_document_prefetch_object_data' , $documents ); } /** * @param array $args * * @return null[] */ public static function documents( $args = array () ) { global $wpdb ; $bp = buddypress(); $r = wp_parse_args( $args , array ( 'scope' => '' , // Scope - Groups, friends etc. 'page' => 1, // The current page. 'per_page' => 20, // Document items per page. 'max' => false, // Max number of items to return. 'fields' => 'all' , // Fields to include. 'sort' => 'DESC' , // ASC or DESC. 'order_by' => 'date_created' , // Column to order by. 'exclude' => false, // Array of ids to exclude. 'in' => false, // Array of ids to limit query by (IN). 'search_terms' => false, // Terms to search by. 'privacy' => false, // public, loggedin, onlyme, friends, grouponly, message. 'count_total' => false, // Whether or not to use count_total. 'user_directory' => true, 'folder_id' => 0, 'meta_query_document' => false, 'meta_query_folder' => false, ) ); // Select conditions. $select_sql_document = 'SELECT DISTINCT d.*' ; $select_sql_folder = 'SELECT DISTINCT f.*' ; $from_sql_document = " FROM {$bp->document->table_name} d, {$bp->document->table_name_meta} dm WHERE ( d.id = dm.document_id ) " ; $from_sql_folder = " FROM {$bp->document->table_name_folder} f WHERE id != '0' " ; $join_sql_document = '' ; $join_sql_folder = '' ; // Where conditions. $where_conditions_document = array (); $where_conditions_folder = array (); if ( ! empty ( $r [ 'scope' ] ) ) { $scope_query_document = self::get_scope_document_query_sql( $r [ 'scope' ], $r ); $scope_query_folder = self::get_scope_folder_query_sql( $r [ 'scope' ], $r ); // Override some arguments if needed. if ( ! empty ( $scope_query_document [ 'override' ] ) ) { $r = array_replace_recursive( $r , $scope_query_document [ 'override' ] ); } // Override some arguments if needed. if ( ! empty ( $scope_query_folder [ 'override' ] ) ) { $r = array_replace_recursive( $r , $scope_query_folder [ 'override' ] ); } } // Searching. if ( $r [ 'search_terms' ] ) { $search_terms_like = '%' . bp_esc_like( $r [ 'search_terms' ] ) . '%' ; $where_conditions_document [ 'search_sql' ] = $wpdb ->prepare( '( d.title LIKE %s' , $search_terms_like ); $where_conditions_folder [ 'search_sql' ] = $wpdb ->prepare( 'f.title LIKE %s' , $search_terms_like ); $where_conditions_document [ 'search_sql' ] .= $wpdb ->prepare( ' OR dm.meta_key = "extension" AND dm.meta_value LIKE %s ' , $search_terms_like ); $where_conditions_document [ 'search_sql' ] .= $wpdb ->prepare( ' OR dm.meta_key = "file_name" AND dm.meta_value LIKE %s )' , $search_terms_like ); /** * Filters whether or not to include users for search parameters. * * @param bool $value Whether or not to include user search. Default false. * * @since BuddyBoss 1.4.0 */ if ( apply_filters( 'bp_document_get_include_user_search' , false ) ) { $user_search = get_user_by( 'slug' , $r [ 'search_terms' ] ); if ( false !== $user_search ) { $user_id = $user_search ->ID; $where_conditions_document [ 'search_sql' ] .= $wpdb ->prepare( ' OR d.user_id = %d' , $user_id ); $where_conditions_folder [ 'search_sql' ] .= $wpdb ->prepare( ' OR f.user_id = %d' , $user_id ); } } } // Sorting. $sort = $r [ 'sort' ]; if ( 'ASC' !== $sort && 'DESC' !== $sort ) { $sort = 'ASC' ; } switch ( $r [ 'order_by' ] ) { case 'id' : case 'user_id' : case 'blog_id' : case 'attachment_id' : case 'title' : case 'folder_id' : case 'activity_id' : case 'privacy' : case 'group_id' : case 'menu_order' : case 'visibility' : case 'date_modified' : case 'date_created' : break ; default : $r [ 'order_by' ] = 'title' ; break ; } $order_by_document = 'd.' . $r [ 'order_by' ]; $order_by_folder = 'f.' . $r [ 'order_by' ]; // Exclude specified items. if ( ! empty ( $r [ 'exclude' ] ) ) { $exclude = implode( ',' , wp_parse_id_list( $r [ 'exclude' ] ) ); $where_conditions_document [ 'exclude' ] = "d.id NOT IN ({$exclude})" ; $where_conditions_folder [ 'exclude' ] = "f.id NOT IN ({$exclude})" ; } // The specific ids to which you want to limit the query. if ( ! empty ( $r [ 'in' ] ) ) { $in = implode( ',' , wp_parse_id_list( $r [ 'in' ] ) ); $where_conditions_document [ 'in' ] = "d.id IN ({$in})" ; $where_conditions_folder [ 'in' ] = "f.id IN ({$in})" ; } if ( ! empty ( $r [ 'activity_id' ] ) ) { $where_conditions_document [ 'activity' ] = "d.activity_id = {$r['activity_id']}" ; } if ( ! empty ( $r [ 'user_id' ] ) ) { $where_conditions_document [ 'user' ] = "d.user_id = {$r['user_id']}" ; $where_conditions_folder [ 'user' ] = "f.user_id = {$r['user_id']}" ; } if ( ! empty ( $r [ 'group_id' ] ) ) { $where_conditions_document [ 'user' ] = "d.group_id = {$r['group_id']}" ; $where_conditions_folder [ 'user' ] = "f.group_id = {$r['group_id']}" ; } if ( ! empty ( $r [ 'privacy' ] ) ) { $privacy = "'" . implode( "', '" , $r ['privacy '] ) . "' "; $where_conditions_document [ 'privacy' ] = "d.privacy IN ({$privacy})" ; $where_conditions_folder [ 'privacy' ] = "f.privacy IN ({$privacy})" ; } // Process meta_query into SQL. $meta_query_sql_document = self::get_meta_query_sql( $r [ 'meta_query_document' ] ); $meta_query_sql_folder = self::get_document_folder_meta_query_sql( $r [ 'meta_query_folder' ] ); if ( ! empty ( $meta_query_sql_document [ 'join' ] ) ) { $join_sql_document .= $meta_query_sql_document [ 'join' ]; } if ( ! empty ( $meta_query_sql_folder [ 'join' ] ) ) { $join_sql_folder .= $meta_query_sql_folder [ 'join' ]; } if ( ! empty ( $meta_query_sql_document [ 'where' ] ) ) { $where_conditions_document [] = $meta_query_sql_document [ 'where' ]; } if ( ! empty ( $meta_query_sql_folder [ 'where' ] ) ) { $where_conditions_folder [] = $meta_query_sql_folder [ 'where' ]; } /** * Filters the MySQL WHERE conditions for the Document items get method. * * @param array $where_conditions Current conditions for MySQL WHERE statement. * @param array $r Parsed arguments passed into method. * @param string $select_sql Current SELECT MySQL statement at point of execution. * @param string $from_sql Current FROM MySQL statement at point of execution. * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. * * @since BuddyBoss 1.4.0 */ $where_conditions_document = apply_filters( 'bp_document_get_where_conditions_document' , $where_conditions_document , $r , $select_sql_document , $from_sql_document , $join_sql_document ); $where_conditions_folder = apply_filters( 'bp_document_get_where_conditions_folder' , $where_conditions_folder , $r , $select_sql_folder , $from_sql_folder , $join_sql_folder ); // Join the where conditions together for document. if ( ! empty ( $scope_query_document [ 'sql' ] ) ) { $where_sql_document = 'AND ' . ( ! empty ( $where_conditions_document ) ? '( ' . join( ' AND ' , $where_conditions_document ) . ' ) AND ' : '' ) . ' ( ' . $scope_query_document [ 'sql' ] . ' )' ; } else { $where_sql_document = ( ! empty ( $where_conditions_document ) ? 'AND ' . join( ' AND ' , $where_conditions_document ) : '' ); } // Join the where conditions together for folder. if ( ! empty ( $scope_query_folder [ 'sql' ] ) ) { $where_sql_folder = 'AND ' . ( ! empty ( $where_conditions_folder ) ? '( ' . join( ' AND ' , $where_conditions_folder ) . ' ) AND ' : '' ) . ' ( ' . $scope_query_folder [ 'sql' ] . ' )' ; } else { $where_sql_folder = ( ! empty ( $where_conditions_folder ) ? 'AND ' . join( ' AND ' , $where_conditions_folder ) : '' ); } /** * Filter the MySQL JOIN clause for the main document query. * * @param string $join_sql JOIN clause. * @param array $r Method parameters. * @param string $select_sql Current SELECT MySQL statement. * @param string $from_sql Current FROM MySQL statement. * @param string $where_sql Current WHERE MySQL statement. * * @since BuddyBoss 1.4.0 */ $join_sql_folder = apply_filters( 'bp_document_get_join_sql_folder' , $join_sql_folder , $r , $select_sql_folder , $from_sql_folder , $where_sql_folder ); $join_sql_document = apply_filters( 'bp_document_get_join_sql_document' , $join_sql_document , $r , $select_sql_document , $from_sql_document , $where_sql_document ); $retval = array ( 'documents' => null, 'total' => null, 'has_more_items' => null, ); // Query first for document IDs. $document_ids_sql_folder = "{$select_sql_folder} {$from_sql_folder} {$join_sql_folder} {$where_sql_folder} ORDER BY {$order_by_folder} {$sort}" ; $document_ids_sql_document = "{$select_sql_document} {$from_sql_document} {$join_sql_document} {$where_sql_document} ORDER BY {$order_by_document} {$sort}" ; /** * Filters the paged document MySQL statement. * * @param string $document_ids_sql MySQL statement used to query for Document IDs. * @param array $r Array of arguments passed into method. * * @since BuddyBoss 1.4.0 */ $document_ids_sql_folder = apply_filters( 'bp_document_paged_activities_sql_folder' , $document_ids_sql_folder , $r ); $document_ids_sql_document = apply_filters( 'bp_document_paged_activities_sql_document' , $document_ids_sql_document , $r ); $cache_group = 'bp_document' ; $cached_folder = bp_core_get_incremented_cache( $document_ids_sql_folder , $cache_group ); $cached_document = bp_core_get_incremented_cache( $document_ids_sql_document , $cache_group ); if ( false === $cached_folder ) { $document_ids_folder = $wpdb ->get_col( $document_ids_sql_folder ); // db call ok; no-cache ok; bp_core_set_incremented_cache( $document_ids_sql_folder , $cache_group , $document_ids_folder ); } else { $document_ids_folder = $cached_folder ; } if ( false === $cached_document ) { $document_ids_document = $wpdb ->get_col( $document_ids_sql_document ); // db call ok; no-cache ok; bp_core_set_incremented_cache( $document_ids_sql_document , $cache_group , $document_ids_document ); } else { $document_ids_document = $cached_document ; } if ( 'ids' === $r [ 'fields' ] ) { $documents_folder = array_map ( 'intval' , $document_ids_folder ); $documents_document = array_map ( 'intval' , $document_ids_document ); $documents = array_merge ( $documents_folder , $documents_document ); } else { $documents_document = self::get_document_data( $document_ids_document ); $documents_folder = self::get_folder_data( $document_ids_folder ); $documents = array_merge ( $documents_folder , $documents_document ); } if ( 'ids' !== $r [ 'fields' ] ) { // Get the fullnames of users so we don't have to query in the loop. // $documents = self::append_user_fullnames( $documents ); // Pre-fetch data associated with document users and other objects. $documents = self::prefetch_object_data( $documents ); } $direction = 'SORT_' . $sort ; if ( 'privacy' === $r [ 'order_by' ] ) { $r [ 'order_by' ] = 'visibility' ; } elseif ( 'group_id' === $r [ 'order_by' ] ) { $r [ 'order_by' ] = 'group_name' ; } $documents = self::array_msort( $documents , array ( $r [ 'order_by' ] => $direction ) ); $retval [ 'has_more_items' ] = ! empty ( $r [ 'per_page' ] ) && isset( $r [ 'per_page' ] ) && count ( $documents ) > $r [ 'per_page' ]; if ( isset( $r [ 'per_page' ] ) && isset( $r [ 'page' ] ) && ! empty ( $r [ 'per_page' ] ) && ! empty ( $r [ 'page' ] ) && $retval [ 'has_more_items' ] ) { $total = count ( $documents ); $current_page = $r [ 'page' ]; $item_per_page = $r [ 'per_page' ]; $start = ( $current_page - 1 ) * $item_per_page ; $documents = array_slice ( $documents , $start , $item_per_page ); $retval [ 'has_more_items' ] = $total > ( $current_page * $item_per_page ); $retval [ 'documents' ] = $documents ; } else { $retval [ 'documents' ] = $documents ; } $retval [ 'total' ] = count ( $retval [ 'documents' ] ); return $retval ; } /** * Get the SQL for the 'scope' param in BP_Document::get(). * A scope is a predetermined set of document arguments. This method is used * to grab these document arguments and override any existing args if needed. * Can handle multiple scopes. * * @param mixed $scope The document scope. Accepts string or array of scopes. * @param array $r Current activity arguments. Same as those of BP_document::get(), * but merged with defaults. * * @return false|array 'sql' WHERE SQL string and 'override' document args. * @since BuddyBoss 1.4.0 */ public static function get_scope_document_query_sql( $scope = false, $r = array () ) { // Define arrays for future use. $query_args = array (); $override = array (); $retval = array (); // Check for array of scopes. if ( is_array ( $scope ) ) { $scopes = $scope ; // Explode a comma separated string of scopes. } elseif ( is_string ( $scope ) ) { $scopes = explode ( ',' , $scope ); } // Bail if no scope passed. if ( empty ( $scopes ) ) { return false; } // Helper to easily grab the 'user_id'. if ( ! empty ( $r [ 'filter' ][ 'user_id' ] ) ) { $r [ 'user_id' ] = $r [ 'filter' ][ 'user_id' ]; } // Parse each scope; yes! we handle multiples! foreach ( $scopes as $scope ) { $scope_args = array (); /** * Plugins can hook here to set their document arguments for custom scopes. * This is a dynamic filter based on the document scope. eg: * - 'bp_document_set_groups_scope_args' * - 'bp_document_set_friends_scope_args' * To see how this filter is used, plugin devs should check out: * - bp_groups_filter_document_scope() - used for 'groups' scope * - bp_friends_filter_document_scope() - used for 'friends' scope * * @param array { * Document query clauses. * * @type array { * Document arguments for your custom scope. * See {@link BP_Document_Query::_construct()} for more details. * } * @type array $override Optional. Override existing document arguments passed by $r. * } * } * * @param array $r Current activity arguments passed in BP_Document::get(). * * @since BuddyBoss 1.4.0 */ $scope_args = apply_filters( "bp_document_set_document_{$scope}_scope_args" , array (), $r ); if ( ! empty ( $scope_args ) ) { // Merge override properties from other scopes // this might be a problem... if ( ! empty ( $scope_args [ 'override' ] ) ) { $override = array_merge ( $override , $scope_args [ 'override' ] ); unset( $scope_args [ 'override' ] ); } // Save scope args. if ( ! empty ( $scope_args ) ) { $query_args [] = $scope_args ; } } } if ( ! empty ( $query_args ) ) { // Set relation to OR. $query_args [ 'relation' ] = 'OR' ; $query = new BP_Document_Query( $query_args ); $sql = $query ->get_sql_document(); if ( ! empty ( $sql ) ) { $retval [ 'sql' ] = $sql ; } } if ( ! empty ( $override ) ) { $retval [ 'override' ] = $override ; } return $retval ; } /** * Get the SQL for the 'scope' param in BP_Document::get(). * A scope is a predetermined set of document arguments. This method is used * to grab these document arguments and override any existing args if needed. * Can handle multiple scopes. * * @param mixed $scope The document scope. Accepts string or array of scopes. * @param array $r Current activity arguments. Same as those of BP_document::get(), * but merged with defaults. * * @return false|array 'sql' WHERE SQL string and 'override' document args. * @since BuddyBoss 1.4.0 */ public static function get_scope_folder_query_sql( $scope = false, $r = array () ) { // Define arrays for future use. $query_args = array (); $override = array (); $retval = array (); // Check for array of scopes. if ( is_array ( $scope ) ) { $scopes = $scope ; // Explode a comma separated string of scopes. } elseif ( is_string ( $scope ) ) { $scopes = explode ( ',' , $scope ); } // Bail if no scope passed. if ( empty ( $scopes ) ) { return false; } // Helper to easily grab the 'user_id'. if ( ! empty ( $r [ 'filter' ][ 'user_id' ] ) ) { $r [ 'user_id' ] = $r [ 'filter' ][ 'user_id' ]; } // Parse each scope; yes! we handle multiples! foreach ( $scopes as $scope ) { $scope_args = array (); /** * Plugins can hook here to set their document arguments for custom scopes. * This is a dynamic filter based on the document scope. eg: * - 'bp_document_set_groups_scope_args' * - 'bp_document_set_friends_scope_args' * To see how this filter is used, plugin devs should check out: * - bp_groups_filter_document_scope() - used for 'groups' scope * - bp_friends_filter_document_scope() - used for 'friends' scope * * @param array { * Document query clauses. * * @type array { * Document arguments for your custom scope. * See {@link BP_Document_Query::_construct()} for more details. * } * @type array $override Optional. Override existing document arguments passed by $r. * } * } * * @param array $r Current activity arguments passed in BP_Document::get(). * * @since BuddyBoss 1.4.0 */ $scope_args = apply_filters( "bp_document_set_folder_{$scope}_scope_args" , array (), $r ); if ( ! empty ( $scope_args ) ) { // Merge override properties from other scopes // this might be a problem... if ( ! empty ( $scope_args [ 'override' ] ) ) { $override = array_merge ( $override , $scope_args [ 'override' ] ); unset( $scope_args [ 'override' ] ); } // Save scope args. if ( ! empty ( $scope_args ) ) { $query_args [] = $scope_args ; } } } if ( ! empty ( $query_args ) ) { // Set relation to OR. $query_args [ 'relation' ] = 'OR' ; $query = new BP_Document_Query( $query_args ); $sql = $query ->get_sql_folder(); if ( ! empty ( $sql ) ) { $retval [ 'sql' ] = $sql ; } } if ( ! empty ( $override ) ) { $retval [ 'override' ] = $override ; } return $retval ; } /** * Convert document IDs to document objects, as expected in template loop. * * @param array $folder_ids Array of document IDs. * * @return array * @since BuddyBoss 1.4.0 */ protected static function get_folder_data( $folder_ids = array () ) { global $wpdb ; // Bail if no document ID's passed. if ( empty ( $folder_ids ) ) { return array (); } // Get BuddyPress. $bp = buddypress(); $documents = array (); $uncached_ids = bp_get_non_cached_ids( $folder_ids , 'bp_document_folder' ); // Prime caches as necessary. if ( ! empty ( $uncached_ids ) ) { // Format the document ID's for use in the query below. $uncached_ids_sql = implode( ',' , wp_parse_id_list( $uncached_ids ) ); // Fetch data from document table, preserving order. $queried_adata = $wpdb ->get_results( "SELECT * FROM {$bp->document->table_name_folder} WHERE id IN ({$uncached_ids_sql})" ); // db call ok; no-cache ok; // Put that data into the placeholders created earlier, // and add it to the cache. foreach ( ( array ) $queried_adata as $adata ) { wp_cache_set( $adata ->id, $adata , 'bp_document_folder' ); } } // Now fetch data from the cache. foreach ( $folder_ids as $document_id ) { // Integer casting. $document = wp_cache_get( $document_id , 'bp_document_folder' ); if ( ! empty ( $document ) ) { $document ->id = (int) $document ->id; $document ->user_id = (int) $document ->user_id; $document ->group_id = (int) $document ->group_id; $document ->date_created = $document ->date_created; $document ->title = $document ->title; $document ->privacy = $document ->privacy; $document ->parent = $document ->parent; $document ->folder_id = (int) $document ->id; if ( (int) $document ->group_id > 0 ) { $document ->folder = 'group' ; if ( bp_is_active( 'groups' ) ) { $group = groups_get_group( array ( 'group_id' => $document ->group_id ) ); $document ->link = bp_get_group_permalink( $group ) . bp_get_document_slug() . '/folder/' . (int) $document ->id; } $document ->link = '' ; } else { $document ->folder = 'profile' ; $document ->link = bp_core_get_user_domain( (int) $document ->user_id ) . bp_get_document_slug() . '/folder/' . (int) $document ->id; } } $group_name = '' ; $visibility = '' ; if ( $document ->group_id > 0 ) { if ( bp_is_active( 'groups' ) ) { $group = groups_get_group( $document ->group_id ); $group_name = bp_get_group_name( $group ); $status = bp_get_group_status( $group ); if ( 'hidden' === $status || 'private' === $status ) { $visibility = esc_html__( 'Group Members' , 'buddyboss' ); } else { $visibility = ucfirst( $status ); } } else { $visibility = '' ; } } else { $document_privacy = bp_document_get_visibility_levels(); if ( 'friends' === $document ->privacy && bp_loggedin_user_id() !== (int) $document ->user_id ) { $visibility = esc_html__( 'Connections' , 'buddyboss' ); } else { $visibility = (isset( $document_privacy [ $document ->privacy ] ) ) ? $document_privacy [ $document ->privacy ] : $document ->privacy ; } } $document ->group_name = $group_name ; $document ->visibility = $visibility ; $documents [] = $document ; } // Then fetch user data. $user_query = new BP_User_Query( array ( 'user_ids' => wp_list_pluck( $documents , 'user_id' ), 'populate_extras' => false, ) ); // Associated located user data with document items. foreach ( $documents as $a_index => $a_item ) { $a_user_id = intval ( $a_item ->user_id ); $a_user = isset( $user_query ->results[ $a_user_id ] ) ? $user_query ->results[ $a_user_id ] : '' ; if ( ! empty ( $a_user ) ) { $documents [ $a_index ]->user_email = $a_user ->user_email; $documents [ $a_index ]->user_nicename = $a_user ->user_nicename; $documents [ $a_index ]->user_login = $a_user ->user_login; $documents [ $a_index ]->display_name = $a_user ->display_name; } } return $documents ; } /** * Sort data based on order. * * @param $array * @param $cols * * @return array|mixed * * @since BuddyBoss 1.4.0 */ public static function array_msort( $array , $cols ) { $array = json_decode( json_encode( $array ), true ); $colarr = array (); foreach ( $cols as $col => $order ) { $colarr [ $col ] = array (); foreach ( $array as $k => $row ) { $colarr [ $col ][ '_' . $k ] = strtolower ( $row [ $col ] ); } } $eval = 'array_multisort(' ; foreach ( $cols as $col => $order ) { $eval .= '$colarr[\'' . $col . '\'],' . $order . ',' ; } $eval = substr ( $eval , 0, - 1 ) . ');' ; eval ( $eval ); $ret = array (); foreach ( $colarr as $col => $arr ) { foreach ( $arr as $k => $v ) { $k = substr ( $k , 1 ); if ( ! isset( $ret [ $k ] ) ) { $ret [ $k ] = $array [ $k ]; } $ret [ $k ][ $col ] = $array [ $k ][ $col ]; } } if ( ! empty ( $ret ) ) { $i = 0; $arr = array (); foreach ( $ret as $k => $v ) { $ret [ $i ] = (object) $v ; $arr [ $i ] = (object) $v ; $i ++; } } return $arr ; } /** * Sort by column. * * @param $array * @param $column * @param int $direction * * @since BuddyBoss 1.4.0 */ public static function array_sort_by_column( $array , $column , $direction = SORT_DESC ) { $new_array = json_decode( json_encode( $array ), true ); if ( 'date_created' === $column ) { $mapping_arr = array_map ( 'strtotime' , array_column( $new_array , $column ) ); array_multisort ( $mapping_arr , $direction ); } else { $reference_array = array (); foreach ( $array as $key => $row ) { $reference_array [ $key ] = $row [ $column ]; } array_multisort ( $reference_array , $direction , $array ); } } /** * Create SQL IN clause for filter queries. * * @param string $field The database field. * @param array|bool $items The values for the IN clause, or false when none are found. * * @return string|false * @see BP_Document::get_filter_sql() * @since BuddyBoss 1.4.0 */ public static function get_in_operator_sql( $field , $items ) { global $wpdb ; // Split items at the comma. if ( ! is_array ( $items ) ) { $items = explode ( ',' , $items ); } // Array of prepared integers or quoted strings. $items_prepared = array (); // Clean up and format each item. foreach ( $items as $item ) { // Clean up the string. $item = trim( $item ); // Pass everything through prepare for security and to safely quote strings. $items_prepared [] = ( is_numeric ( $item ) ) ? $wpdb ->prepare( '%d' , $item ) : $wpdb ->prepare( '%s' , $item ); } // Build IN operator sql syntax. if ( count ( $items_prepared ) ) { return sprintf( '%s IN ( %s )' , trim( $field ), implode( ',' , $items_prepared ) ); } else { return false; } } /** * Delete document items from the database. * To delete a specific document item, pass an 'id' parameter. * Otherwise use the filters. * * @param array $args { * @int $id Optional. The ID of a specific item to delete. * @int $blog_id Optional. The blog ID to filter by. * @int $attachment_id Optional. The attachment ID to filter by. * @int $user_id Optional. The user ID to filter by. * @string $title Optional. The title to filter by. * @int $folder_id Optional. The folder ID to filter by. * @int $activity_id Optional. The activity ID to filter by. * @int $group_id Optional. The group ID to filter by. * @string $privacy Optional. The privacy to filter by. * @string $date_created Optional. The date to filter by. * } * @param bool $from Context of deletion from. ex. attachment, activity etc. * * @return array|bool An array of deleted document IDs on success, false on failure. * @since BuddyBoss 1.4.0 */ public static function delete ( $args = array (), $from = false ) { global $wpdb ; $bp = buddypress(); $r = wp_parse_args( $args , array ( 'id' => false, 'blog_id' => false, 'attachment_id' => false, 'user_id' => false, 'title' => false, 'folder_id' => false, 'activity_id' => false, 'group_id' => false, 'privacy' => false, 'date_created' => false, ) ); // Setup empty array from where query arguments. $where_args = array (); // ID. if ( ! empty ( $r [ 'id' ] ) ) { $where_args [] = $wpdb ->prepare( 'id = %d' , $r [ 'id' ] ); } // blog ID. if ( ! empty ( $r [ 'blog_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'blog_id = %d' , $r [ 'blog_id' ] ); } // attachment ID. if ( ! empty ( $r [ 'attachment_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'attachment_id = %d' , $r [ 'attachment_id' ] ); } // User ID. if ( ! empty ( $r [ 'user_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'user_id = %d' , $r [ 'user_id' ] ); } // title. if ( ! empty ( $r [ 'title' ] ) ) { $where_args [] = $wpdb ->prepare( 'title = %s' , $r [ 'title' ] ); } // folder ID. if ( ! empty ( $r [ 'folder_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'folder_id = %d' , $r [ 'folder_id' ] ); } // activity ID. if ( ! empty ( $r [ 'activity_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'activity_id = %d' , $r [ 'activity_id' ] ); } // group ID. if ( ! empty ( $r [ 'group_id' ] ) ) { $where_args [] = $wpdb ->prepare( 'group_id = %d' , $r [ 'group_id' ] ); } // privacy. if ( ! empty ( $r [ 'privacy' ] ) ) { $where_args [] = $wpdb ->prepare( 'privacy = %s' , $r [ 'privacy' ] ); } // Date created. if ( ! empty ( $r [ 'date_created' ] ) ) { $where_args [] = $wpdb ->prepare( 'date_created = %s' , $r [ 'date_created' ] ); } // Bail if no where arguments. if ( empty ( $where_args ) ) { return false; } // Join the where arguments for querying. $where_sql = 'WHERE ' . join( ' AND ' , $where_args ); // Fetch all document being deleted so we can perform more actions. $documents = $wpdb ->get_results( "SELECT * FROM {$bp->document->table_name} {$where_sql}" ); // db call ok; no-cache ok; /** * Action to allow intercepting document items to be deleted. * * @param array $documents Array of document. * @param array $r Array of parsed arguments. * * @since BuddyBoss 1.4.0 */ do_action_ref_array( 'bp_document_before_delete' , array ( $documents , $r ) ); // Attempt to delete document from the database. $deleted = $wpdb ->query( "DELETE FROM {$bp->document->table_name} {$where_sql}" ); // db call ok; no-cache ok; // Bail if nothing was deleted. if ( empty ( $deleted ) ) { return false; } /** * Action to allow intercepting document items just deleted. * * @param array $documents Array of document. * @param array $r Array of parsed arguments. * * @since BuddyBoss 1.4.0 */ do_action_ref_array( 'bp_document_after_delete' , array ( $documents , $r ) ); // Pluck the document IDs out of the $documents array. $document_ids = wp_parse_id_list( wp_list_pluck( $documents , 'id' ) ); $activity_ids = wp_parse_id_list( wp_list_pluck( $documents , 'activity_id' ) ); $attachment_ids = wp_parse_id_list( wp_list_pluck( $documents , 'attachment_id' ) ); // Delete preview attachment. foreach ( $document_ids as $document_delete ) { $preview_id = bp_document_get_meta( $document_delete , 'preview_attachment_id' , true ); if ( $preview_id ) { wp_delete_attachment( $preview_id , true ); } } // if ( ! empty( $document_ids ) ) { // // Loop through attachment ids and attempt to delete. // foreach ( $document_ids as $document ) { // $preview_attachment_id = bp_document_get_meta( $document, 'preview_attachment_id', true ); // if ( $preview_attachment_id ) { // wp_delete_attachment( $preview_attachment_id, true ); // } // } // } // Delete meta. if ( ! empty ( $document_ids ) ) { // Delete all document meta entries for document items. self::delete_document_meta_entries( wp_list_pluck( $documents , 'id' ) ); } // Handle accompanying attachments and meta deletion. if ( ! empty ( $attachment_ids ) ) { // Loop through attachment ids and attempt to delete. foreach ( $attachment_ids as $attachment_id ) { if ( bp_is_active( 'activity' ) ) { $parent_activity_id = get_post_meta( $attachment_id , 'bp_document_parent_activity_id' , true ); if ( ! empty ( $parent_activity_id ) ) { $activity_document_ids = bp_activity_get_meta( $parent_activity_id , 'bp_document_ids' , true ); if ( ! empty ( $activity_document_ids ) ) { $activity_document_ids = explode ( ',' , $activity_document_ids ); $activity_document_ids = array_diff ( $activity_document_ids , $document_ids ); if ( ! empty ( $activity_document_ids ) ) { $activity_document_ids = implode( ',' , $activity_document_ids ); bp_activity_update_meta( $parent_activity_id , 'bp_document_ids' , $activity_document_ids ); } else { $activity_ids [] = $parent_activity_id ; } } } } if ( empty ( $from ) ) { wp_delete_attachment( $attachment_id , true ); } } } // delete related activity. if ( ! empty ( $activity_ids ) && bp_is_active( 'activity' ) ) { foreach ( $activity_ids as $activity_id ) { $activity = new BP_Activity_Activity( (int) $activity_id ); // Check access. if ( bp_activity_user_can_delete( $activity ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_before_action_delete_activity' , $activity ->id, $activity ->user_id ); // Deleting an activity comment. if ( 'activity_comment' === $activity ->type ) { if ( bp_activity_delete_comment( $activity ->item_id, $activity ->id ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity' , $activity ->id, $activity ->user_id ); } // Deleting an activity. } else { if ( bp_activity_delete( array ( 'id' => $activity ->id, 'user_id' => $activity ->user_id, ) ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity' , $activity ->id, $activity ->user_id ); } } } } } return $document_ids ; } /** * Delete the meta entries associated with a set of document items. * * @since BuddyBoss 1.4.0 * * @param array $document_ids Document IDs whose meta should be deleted. * @return bool True on success. */ public static function delete_document_meta_entries( $document_ids = array () ) { $document_ids = wp_parse_id_list( $document_ids ); foreach ( $document_ids as $document_id ) { bp_document_delete_meta( $document_id ); } return true; } /** * Count total document for the given user. * * @param int $user_id * * @return array|bool|int * @since BuddyBoss 1.4.0 */ public static function total_document_count( $user_id = 0 ) { global $bp , $wpdb ; $privacy = array ( 'public' ); if ( is_user_logged_in() ) { $privacy [] = 'loggedin' ; if ( bp_is_active( 'friends' ) ) { $is_friend = friends_check_friendship( get_current_user_id(), $user_id ); if ( $is_friend ) { $privacy [] = 'friends' ; } } } $privacy = "'" . implode( "', '" , $privacy ) . "'" ; $total_count_document = (int) $wpdb ->get_var( "SELECT COUNT(*) FROM {$bp->document->table_name} WHERE user_id = {$user_id} AND privacy IN ({$privacy}) AND folder_id = 0" ); // db call ok; no-cache ok; $total_count_folder = (int) $wpdb ->get_var( "SELECT COUNT(*) FROM {$bp->document->table_name_folder} WHERE user_id = {$user_id} AND privacy IN ({$privacy}) AND parent = 0" ); // db call ok; no-cache ok; $total_count = $total_count_folder + $total_count_document ; return $total_count ; } /** * Count total document for the given group. * * @param int $group_id * * @return array|bool|int * @since BuddyBoss 1.4.0 */ public static function total_group_document_count( $group_id = 0 ) { global $bp , $wpdb ; $total_count_document = (int) $wpdb ->get_var( "SELECT COUNT(*) FROM {$bp->document->table_name} WHERE group_id = {$group_id} AND folder_id = 0" ); // db call ok; no-cache ok; $total_count_folder = (int) $wpdb ->get_var( "SELECT COUNT(*) FROM {$bp->document->table_name_folder} WHERE group_id = {$group_id} AND parent = 0" ); // db call ok; no-cache ok; $total_count = $total_count_folder + $total_count_document ; return $total_count ; } /** * Get all document ids for the folder. * * @param bool $folder_id * * @return array|bool * @since BuddyBoss 1.4.0 */ public static function get_folder_document_ids( $folder_id = false ) { global $bp , $wpdb ; if ( ! $folder_id ) { return false; } $folder_document_sql = $wpdb ->prepare( "SELECT DISTINCT d.id FROM {$bp->document->table_name} d WHERE d.folder_id = %d" , $folder_id ); $cached = bp_core_get_incremented_cache( $folder_document_sql , 'bp_document' ); if ( false === $cached ) { $document_ids = $wpdb ->get_col( $folder_document_sql ); // db call ok; no-cache ok; bp_core_set_incremented_cache( $folder_document_sql , 'bp_document' , $document_ids ); } else { $document_ids = $cached ; } return ( array ) $document_ids ; } /** * Get document id for the activity. * * @param bool $activity_id * * @return array|bool * @since BuddyBoss 1.1.6 */ public static function get_activity_document_id( $activity_id = false ) { global $bp , $wpdb ; if ( ! $activity_id ) { return false; } $activity_document_id = (int) $wpdb ->get_var( "SELECT DISTINCT d.id FROM {$bp->document->table_name} d WHERE d.activity_id = {$activity_id}" ); // db call ok; no-cache ok; return $activity_document_id ; } /** * Append xProfile fullnames to an document array. * * @param array $documents Documents array. * * @return array * @since BuddyBoss 1.4.0 */ protected static function append_user_fullnames( $documents ) { if ( bp_is_active( 'xprofile' ) && ! empty ( $documents ) ) { $document_user_ids = wp_list_pluck( $documents , 'user_id' ); if ( ! empty ( $document_user_ids ) ) { $fullnames = bp_core_get_user_displaynames( $document_user_ids ); if ( ! empty ( $fullnames ) ) { foreach ( ( array ) $documents as $i => $document ) { if ( ! empty ( $fullnames [ $document ->user_id ] ) ) { $documents [ $i ]->user_fullname = $fullnames [ $document ->user_id ]; } } } } } return $documents ; } /** * Save the document item to the database. * * @return WP_Error|bool True on success. * @since BuddyBoss 1.4.0 */ public function save() { global $wpdb ; $bp = buddypress(); $this ->id = apply_filters_ref_array( 'bp_document_id_before_save' , array ( $this ->id, & $this ) ); $this ->blog_id = apply_filters_ref_array( 'bp_document_blog_id_before_save' , array ( $this ->blog_id, & $this ) ); $this ->attachment_id = apply_filters_ref_array( 'bp_document_attachment_id_before_save' , array ( $this ->attachment_id, & $this ) ); $this ->user_id = apply_filters_ref_array( 'bp_document_user_id_before_save' , array ( $this ->user_id, & $this ) ); $this ->title = apply_filters_ref_array( 'bp_document_title_before_save' , array ( $this ->title, & $this ) ); $this ->folder_id = apply_filters_ref_array( 'bp_document_folder_id_before_save' , array ( $this ->folder_id, & $this ) ); $this ->activity_id = apply_filters_ref_array( 'bp_document_activity_id_before_save' , array ( $this ->activity_id, & $this ) ); $this ->group_id = apply_filters_ref_array( 'bp_document_group_id_before_save' , array ( $this ->group_id, & $this ) ); $this ->privacy = apply_filters_ref_array( 'bp_document_privacy_before_save' , array ( $this ->privacy, & $this ) ); $this ->menu_order = apply_filters_ref_array( 'bp_document_menu_order_before_save' , array ( $this ->menu_order, & $this ) ); $this ->date_created = apply_filters_ref_array( 'bp_document_date_created_before_save' , array ( $this ->date_created, & $this ) ); $this ->date_modified = apply_filters_ref_array( 'bp_document_date_modified_before_save' , array ( $this ->date_modified, & $this ) ); /** * Fires before the current document item gets saved. * Please use this hook to filter the properties above. Each part will be passed in. * * @param BP_Document $this Current instance of the document item being saved. Passed by reference. * * @since BuddyBoss 1.4.0 */ do_action_ref_array( 'bp_document_before_save' , array ( & $this ) ); if ( 'wp_error' === $this ->error_type && $this ->errors->get_error_code() ) { return $this ->errors; } if ( empty ( $this ->attachment_id ) // || empty( $this->activity_id ) //todo: when forums document is saving, it should have activity id assigned if settings enabled need to check this ) { if ( 'bool' === $this ->error_type ) { return false; } else { if ( empty ( $this ->activity_id ) ) { $this ->errors->add( 'bp_document_missing_activity' ); } else { $this ->errors->add( 'bp_document_missing_attachment' ); } return $this ->errors; } } // Generate PDF file preview image. $attachment_id = $this ->attachment_id; $pdf_preview = false; $is_pdf = false; $is_preview_generated = get_post_meta( $attachment_id , 'document_preview_generated' , true ); $preview_attachment_id = (int) get_post_meta( $attachment_id , 'document_preview_attachment_id' , true ); if ( empty ( $is_preview_generated ) ) { $extension = bp_document_extension( $attachment_id ); $preview_attachment_id = 0; $file = get_attached_file( $attachment_id ); $upload_dir = wp_upload_dir(); if ( 'pdf' === $extension ) { $is_pdf = true; $output_file = wp_get_attachment_image_url( $attachment_id , 'full' ); $output_file_src = bp_document_scaled_image_path( $attachment_id ); if ( '' !== $output_file && '' !== basename ( $output_file ) && strstr ( $output_file , 'bb_documents/' ) ) { add_filter( 'upload_dir' , 'bp_document_upload_dir_script' ); $upload_dir = $upload_dir [ 'basedir' ]; // Create temp folder. $upload_dir = $upload_dir . '/preview-image-folder-' . time(); $preview_folder = $upload_dir ; // If folder not exists then create. if ( ! is_dir ( $upload_dir ) ) { // Create temp folder. wp_mkdir_p( $upload_dir ); chmod ( $upload_dir , 0777 ); // Create given main parent folder. $preview_folder = $upload_dir ; wp_mkdir_p( $preview_folder ); $file_name = basename ( $output_file ); $extension_pos = strrpos ( $file_name , '.' ); // find position of the last dot, so where the extension starts $thumb = substr ( $file_name , 0, $extension_pos ) . '_thumb' . substr ( $file_name , $extension_pos ); copy ( $output_file_src , $preview_folder . '/' . $thumb ); } $files = scandir( $preview_folder ); $firstFile = $preview_folder . '/' . $files [2]; bp_document_chmod_r( $preview_folder ); $image_data = file_get_contents ( $firstFile ); $filename = basename ( $output_file ); $upload_dir = wp_upload_dir(); if ( wp_mkdir_p( $upload_dir [ 'path' ] ) ) { $file = $upload_dir [ 'path' ] . '/' . $filename ; } else { $file = $upload_dir [ 'basedir' ] . '/' . $filename ; } file_put_contents ( $file , $image_data ); $wp_filetype = wp_check_filetype( $filename , null ); $attachment = array ( 'post_mime_type' => $wp_filetype [ 'type' ], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '' , 'post_status' => 'inherit' , ); $preview_attachment_id = wp_insert_attachment( $attachment , $file ); if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin' . '/includes/image.php' ; require_once ABSPATH . 'wp-admin' . '/includes/file.php' ; require_once ABSPATH . 'wp-admin' . '/includes/media.php' ; } $attach_data = wp_generate_attachment_metadata( $preview_attachment_id , $file ); wp_update_attachment_metadata( $preview_attachment_id , $attach_data ); update_post_meta( $attachment_id , 'document_preview_generated' , 'yes' ); update_post_meta( $attachment_id , 'document_preview_attachment_id' , $preview_attachment_id ); $pdf_preview = true; BP_Document::bp_document_remove_temp_directory( $preview_folder ); remove_filter( 'upload_dir' , 'bp_document_upload_dir_script' ); } } else if ( 'css' === $extension || 'txt' === $extension || 'js' === $extension || 'html' === $extension || 'htm' === $extension || 'csv' === $extension ) { $absolute_path = get_attached_file( $attachment_id ); if ( '' !== $absolute_path && '' !== basename ( $absolute_path ) && strstr ( $absolute_path , 'bb_documents/' ) ) { $upload_dir = $upload_dir [ 'basedir' ]; // Create temp folder. $upload_dir = $upload_dir . '/preview-image-folder-' . time(); $preview_folder = $upload_dir ; // If folder not exists then create. if ( ! is_dir ( $upload_dir ) ) { // Create temp folder. wp_mkdir_p( $upload_dir ); chmod ( $upload_dir , 0777 ); // Create given main parent folder. $preview_folder = $upload_dir ; wp_mkdir_p( $preview_folder ); $file_name = basename ( $absolute_path ); $extension_pos = strrpos ( $file_name , '.' ); // find position of the last dot, so where the extension starts $thumb = substr ( $file_name , 0, $extension_pos ) . '_thumb' . substr ( $file_name , $extension_pos ); copy ( $absolute_path , $preview_folder . '/' . $thumb ); } $files = scandir( $preview_folder ); $firstFile = $preview_folder . '/' . $files [2]; bp_document_chmod_r( $preview_folder ); $image_data = file_get_contents ( $firstFile ); $words = 10000; $mirror_text = strlen ( $image_data ) > $words ? substr ( $image_data ,0, $words ). '...' : $image_data ; update_post_meta( $attachment_id , 'document_preview_mirror_text' , $mirror_text ); BP_Document::bp_document_remove_temp_directory( $preview_folder ); } } } // If we have an existing ID, update the document item, otherwise insert it. if ( ! empty ( $this ->id ) ) { $q = $wpdb ->prepare( "UPDATE {$bp->document->table_name} SET blog_id = %d, attachment_id = %d, user_id = %d, title = %s, folder_id = %d, activity_id = %d, group_id = %d, privacy = %s, menu_order = %d, date_modified = %s WHERE id = %d" , $this ->blog_id, $this ->attachment_id, $this ->user_id, $this ->title, $this ->folder_id, $this ->activity_id, $this ->group_id, $this ->privacy, $this ->menu_order, $this ->date_modified, $this ->id ); } else { $q = $wpdb ->prepare( "INSERT INTO {$bp->document->table_name} ( blog_id, attachment_id, user_id, title, folder_id, activity_id, group_id, privacy, menu_order, date_created, date_modified ) VALUES ( %d, %d, %d, %s, %d, %d, %d, %s, %d, %s, %s )" , $this ->blog_id, $this ->attachment_id, $this ->user_id, $this ->title, $this ->folder_id, $this ->activity_id, $this ->group_id, $this ->privacy, $this ->menu_order, $this ->date_created, $this ->date_modified ); } if ( false === $wpdb ->query( $q ) ) { return false; } // If this is a new document item, set the $id property. if ( empty ( $this ->id ) ) { $this ->id = $wpdb ->insert_id; } if ( $preview_attachment_id ) { bp_document_update_meta( $this ->id, 'preview_attachment_id' , $preview_attachment_id ); } if ( ! $pdf_preview && $is_pdf ) { add_filter( 'wp_image_editors' , array ( $this , 'bp_document_wp_image_editors' ) ); self::bp_document_pdf_previews( array ( $this ->attachment_id ), true, $this ->id ); remove_filter( 'wp_image_editors' , array ( $this , 'bp_document_wp_image_editors' ) ); } // Update folder modified date. $folder = (int) $this ->folder_id; if ( $folder > 0 ) { bp_document_update_folder_modified_date( $folder ); } /** * Fires after an document item has been saved to the database. * * @param BP_Document $this Current instance of document item being saved. Passed by reference. * * @since BuddyBoss 1.4.0 */ do_action_ref_array( 'bp_document_after_save' , array ( & $this ) ); return true; } /** * Helper to set the max_execution_time. */ static function bp_document_set_time_limit( $time_limit ) { $max_execution_time = ini_get ( 'max_execution_time' ); if ( $max_execution_time && $time_limit > $max_execution_time ) { return @ set_time_limit( $time_limit ); } return null; } /** * Does the actual PDF preview regenerate. */ static function bp_document_pdf_previews( $ids , $check_mime_type = false, $document_id ) { $cnt = $num_updates = $num_fails = $time = 0; $preview_attachment_id = bp_document_get_meta( $document_id , 'preview_attachment_id' , true ); if ( $ids && ! $preview_attachment_id ) { $time = microtime( true ); $cnt = count ( $ids ); self::bp_document_set_time_limit( max( $cnt * self:: $per_pdf_secs , self:: $min_time_limit ) ); foreach ( $ids as $idx => $id ) { if ( $check_mime_type && 'application/pdf' !== get_post_mime_type( $id ) ) { continue ; } $file = get_attached_file( $id ); if ( false === $file || '' === $file ) { $num_fails ++; } else { // Get current metadata if any. $old_value = get_metadata( 'post' , $id , '_wp_attachment_metadata' ); if ( $old_value && ( ! is_array ( $old_value ) || 1 !== count ( $old_value ) ) ) { $old_value = null; } // Remove old intermediate thumbnails if any. if ( $old_value && ! empty ( $old_value [0][ 'sizes' ] ) && is_array ( $old_value [0][ 'sizes' ] ) ) { $dirname = dirname( $file ) . '/' ; foreach ( $old_value [0][ 'sizes' ] as $sizeinfo ) { // Check whether pre WP 4.7.3 lacking PDF marker and if so don't delete so as not to break links to thumbnails in content. if ( false !== strpos ( $sizeinfo [ 'file' ], '-pdf' ) ) { @ unlink( $dirname . $sizeinfo [ 'file' ] ); } } } if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin' . '/includes/image.php' ; require_once ABSPATH . 'wp-admin' . '/includes/file.php' ; require_once ABSPATH . 'wp-admin' . '/includes/media.php' ; } // Generate new intermediate thumbnails. $meta = wp_generate_attachment_metadata( $id , $file ); if ( ! $meta ) { $num_fails ++; } else { // wp_update_attachment_metadata() returns false if nothing to update so check first. if ( ( $old_value && $old_value [0] === $meta ) || false !== wp_update_attachment_metadata( $id , $meta ) ) { $num_updates ++; } else { $num_fails ++; } } if ( $meta ) { $upload_dir = wp_upload_dir(); $preview_folder = '' ; $output_file = wp_get_attachment_image_url( $id , 'full' ); $output_file_src = bp_document_scaled_image_path( $id ); if ( '' !== $output_file && '' !== basename ( $output_file ) && strstr ( $output_file , 'bb_documents/' ) ) { add_filter( 'upload_dir' , 'bp_document_upload_dir_script' ); $upload_dir = $upload_dir [ 'basedir' ]; // Create temp folder. $upload_dir = $upload_dir . '/preview-image-folder-' . time(); // If folder not exists then create. if ( ! is_dir ( $upload_dir ) ) { // Create temp folder. wp_mkdir_p( $upload_dir ); chmod ( $upload_dir , 0777 ); // Create given main parent folder. $preview_folder = $upload_dir ; wp_mkdir_p( $preview_folder ); $file_name = basename ( $output_file ); $extension_pos = strrpos ( $file_name , '.' ); // find position of the last dot, so where the extension starts $thumb = substr ( $file_name , 0, $extension_pos ) . '_thumb' . substr ( $file_name , $extension_pos ); copy ( $output_file_src , $preview_folder . '/' . $thumb ); } $files = scandir( $preview_folder ); $firstFile = $preview_folder . '/' . $files [2]; bp_document_chmod_r( $preview_folder ); $image_data = file_get_contents ( $firstFile ); $filename = basename ( $output_file ); $upload_dir = wp_upload_dir(); if ( wp_mkdir_p( $upload_dir [ 'path' ] ) ) { $file = $upload_dir [ 'path' ] . '/' . $filename ; } else { $file = $upload_dir [ 'basedir' ] . '/' . $filename ; } file_put_contents ( $file , $image_data ); $wp_filetype = wp_check_filetype( $filename , null ); $attachment = array ( 'post_mime_type' => $wp_filetype [ 'type' ], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '' , 'post_status' => 'inherit' , ); $preview_attachment_id = wp_insert_attachment( $attachment , $file ); if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin' . '/includes/image.php' ; require_once ABSPATH . 'wp-admin' . '/includes/file.php' ; require_once ABSPATH . 'wp-admin' . '/includes/media.php' ; } $attach_data = wp_generate_attachment_metadata( $preview_attachment_id , $file ); wp_update_attachment_metadata( $preview_attachment_id , $attach_data ); update_post_meta( $id , 'document_preview_generated' , 'yes' ); update_post_meta( $id , 'document_preview_attachment_id' , $preview_attachment_id ); bp_document_update_meta( $document_id , 'preview_attachment_id' , $preview_attachment_id ); BP_Document::bp_document_remove_temp_directory( $preview_folder ); remove_filter( 'upload_dir' , 'bp_document_upload_dir_script' ); } } } } $time = round ( microtime( true ) - $time , self:: $timing_dec_places ); } return array ( $cnt , $num_updates , $num_fails , $time ); } /** * Get the SQL for the 'meta_query' param in BP_Document::get(). * * We use WP_Meta_Query to do the heavy lifting of parsing the * meta_query array and creating the necessary SQL clauses. However, * since BP_Document::get() builds its SQL differently than * WP_Query, we have to alter the return value (stripping the leading * AND keyword from the 'where' clause). * * @since BuddyPress 1.8.0 * * @param array $meta_query An array of meta_query filters. See the * documentation for WP_Meta_Query for details. * @return array $sql_array 'join' and 'where' clauses. */ public static function get_meta_query_sql( $meta_query = array () ) { global $wpdb ; $sql_array = array ( 'join' => '' , 'where' => '' , ); if ( ! empty ( $meta_query ) ) { $document_meta_query = new WP_Meta_Query( $meta_query ); // WP_Meta_Query expects the table name at // $wpdb->document_meta. $wpdb ->documentmeta = buddypress()->document->table_name_meta; $meta_sql = $document_meta_query ->get_sql( 'document' , 'd' , 'id' ); // Strip the leading AND - BP handles it in get(). $sql_array [ 'where' ] = preg_replace( '/^\sAND/' , '' , $meta_sql [ 'where' ] ); $sql_array [ 'join' ] = $meta_sql [ 'join' ]; } return $sql_array ; } /** * Get the SQL for the 'meta_query' param in BP_Document::get(). * * We use WP_Meta_Query to do the heavy lifting of parsing the * meta_query array and creating the necessary SQL clauses. However, * since BP_Document::get() builds its SQL differently than * WP_Query, we have to alter the return value (stripping the leading * AND keyword from the 'where' clause). * * @since BuddyPress 1.8.0 * * @param array $meta_query An array of meta_query filters. See the * documentation for WP_Meta_Query for details. * @return array $sql_array 'join' and 'where' clauses. */ public static function get_document_folder_meta_query_sql( $meta_query = array () ) { global $wpdb ; $sql_array = array ( 'join' => '' , 'where' => '' , ); if ( ! empty ( $meta_query ) ) { $document_meta_query = new WP_Meta_Query( $meta_query ); // WP_Meta_Query expects the table name at // $wpdb->document_meta. $wpdb ->documentmeta = buddypress()->document->table_name_folder_meta; $meta_sql = $document_meta_query ->get_sql( 'document_folder' , 'f' , 'id' ); // Strip the leading AND - BP handles it in get(). $sql_array [ 'where' ] = preg_replace( '/^\sAND/' , '' , $meta_sql [ 'where' ] ); $sql_array [ 'join' ] = $meta_sql [ 'join' ]; } return $sql_array ; } /** * Get document attachment id for the activity. * * @param integer $activity_id Activity ID * * @return integer|bool * @since BuddyBoss 1.4.0 */ public static function get_activity_attachment_id( $activity_id = 0 ) { global $bp , $wpdb ; if ( empty ( $activity_id ) ) { return false; } return (int) $wpdb ->get_var( "SELECT DISTINCT d.attachment_id FROM {$bp->document->table_name} d WHERE d.activity_id = {$activity_id}" ); } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.4.0 | Introduced. |
Methods
- __construct — Constructor method.
- append_user_fullnames — Append xProfile fullnames to an document array.
- array_msort — Sort data based on order.
- array_sort_by_column — Sort by column.
- bp_document_load_gopp_image_editor_gs — Helper to load BP_GOPP_Image_Editor_GS class.
- bp_document_pdf_previews — Does the actual PDF preview regenerate.
- bp_document_remove_temp_directory — Remove all temp directory.
- bp_document_set_time_limit — Helper to set the max_execution_time.
- bp_document_wp_image_editors — Called on 'wp_image_editors' action.
- delete — Delete document items from the database.
- delete_document_meta_entries — Delete the meta entries associated with a set of document items.
- documents
- get — Get document items, as specified by parameters.
- get_activity_attachment_id — Get document attachment id for the activity.
- get_activity_document_id — Get document id for the activity.
- get_document_data — Convert document IDs to document objects, as expected in template loop.
- get_document_folder_meta_query_sql — Get the SQL for the 'meta_query' param in BP_Document::get().
- get_folder_data — Convert document IDs to document objects, as expected in template loop.
- get_folder_document_ids — Get all document ids for the folder.
- get_in_operator_sql — Create SQL IN clause for filter queries.
- get_meta_query_sql — Get the SQL for the 'meta_query' param in BP_Document::get().
- get_scope_document_query_sql — Get the SQL for the 'scope' param in BP_Document::get().
- get_scope_folder_query_sql — Get the SQL for the 'scope' param in BP_Document::get().
- get_scope_query_sql — Get the SQL for the 'scope' param in BP_Document::get().
- populate — Populate the object with data about the specific document item.
- prefetch_object_data — Pre-fetch data for objects associated with document items.
- save — Save the document item to the database.
- total_document_count — Count total document for the given user.
- total_group_document_count — Count total document for the given group.
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.