BP_REST_Document_Folder_Endpoint
BuddyPress Document Folder endpoints.
Description
Source
File: bp-document/classes/class-bp-rest-document-folder-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 | class BP_REST_Document_Folder_Endpoint extends WP_REST_Controller { /** * BP_REST_Document_Endpoint Instance. * * @var BP_REST_Document_Endpoint */ protected $document_endpoint ; /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = 'document/folder' ; $this ->document_endpoint = new BP_REST_Document_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 folder.' , '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' ), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); register_rest_route( $this -> namespace , '/' . $this ->rest_base . '/tree' , array ( 'args' => array ( 'group_id' => array ( 'description' => __( 'A unique numeric ID for the Group.' , 'buddyboss' ), 'type' => 'integer' , ), ), array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'folder_tree_items' ), 'permission_callback' => array ( $this , 'folder_tree_items_permissions_check' ), ), ) ); } /** * Retrieve document folders. * * @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/document/folder Get Folders * @apiName GetBBFolders * @apiGroup Document * @apiDescription Retrieve Folders. * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. * @apiParam {Number} [page] 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=asc,desc} [order=desc] Order sort attribute ascending or descending. * @apiParam {String=id,title,date_created,user_id,group_id,privacy} [orderby=date_created] Order by a specific parameter. * @apiParam {Number} [max] Maximum number of results to return. * @apiParam {Number} [user_id] Limit result set to items created by a specific user (ID). * @apiParam {Number} [parent] A unique numeric ID for the Folder. * @apiParam {Number} [group_id] A unique numeric ID for the Group. * @apiParam {Array=public,loggedin,friends,onlyme,grouponly} [privacy=public] Privacy of the Folder. * @apiParam {Array} [exclude] Ensure result set excludes specific IDs. * @apiParam {Array} [include] Ensure result set includes specific IDs. * @apiParam {Boolean} [count_total=true] Show total count or not. */ public function get_items( $request ) { $args = array ( 'page' => $request [ 'page' ], 'per_page' => $request [ 'per_page' ], 'sort' => strtoupper ( $request [ 'order' ] ), 'order_by' => $request [ 'orderby' ], 'count_total' => $request [ 'count_total' ], ); if ( ! empty ( $request [ 'search' ] ) ) { $args [ 'search_terms' ] = $request [ 'search' ]; } if ( ! empty ( $request [ 'max' ] ) ) { $args [ 'max' ] = $request [ 'max' ]; } if ( ! empty ( $request [ 'user_id' ] ) ) { $args [ 'user_id' ] = $request [ 'user_id' ]; } if ( isset( $request [ 'parent' ] ) && null !== $request [ 'parent' ] ) { $args [ 'parent' ] = $request [ 'parent' ]; } if ( ! empty ( $request [ 'group_id' ] ) ) { $args [ 'group_id' ] = $request [ 'group_id' ]; } if ( ! empty ( $request [ 'privacy' ] ) ) { $args [ 'privacy' ] = $request [ 'privacy' ]; } if ( ! empty ( $request [ 'exclude' ] ) ) { $args [ 'exclude' ] = $request [ 'exclude' ]; } if ( ! empty ( $request [ 'include' ] ) ) { $args [ 'in' ] = $request [ 'include' ]; } /** * 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_document_folder_get_items_query_args' , $args , $request ); $folders = $this ->assemble_response_data( $args ); $retval = array (); foreach ( $folders [ 'folders' ] as $folder ) { $retval [] = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); } $response = rest_ensure_response( $retval ); $response = bp_rest_response_add_total_headers( $response , $folders [ 'total' ], $args [ 'per_page' ] ); /** * Fires after a list of document's folder is fetched via the REST API. * * @since 0.1.0 * * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @param array $folders Fetched Folders. */ do_action( 'bp_rest_document_folder_get_items' , $folders , $response , $request ); return $response ; } /** * Checks if a given request has access to get all users. * * @param WP_REST_Request $request Full details about the request. * * @return bool * @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 folder `get_items` permissions check. * * @param bool $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_document_folder_get_items_permissions_check' , $retval , $request ); } /** * Retrieve a single Folder. * * @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/document/folder/:id Get Folder * @apiName GetBBFolder * @apiGroup Document * @apiDescription Retrieve a single folder. * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. * @apiParam {Number} id A unique numeric ID for the folder. */ public function get_item( $request ) { $id = $request [ 'id' ]; $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $id ) ) ); if ( empty ( $folders [ 'folders' ] ) ) { return new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } $retval = '' ; foreach ( $folders [ 'folders' ] as $folder ) { $retval = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); } $response = rest_ensure_response( $retval ); /** * Fires after a folder is fetched via the REST API. * * @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_document_folder_get_item' , $response , $request ); return $response ; } /** * Checks if a given request has access to get all users. * * @param WP_REST_Request $request Full details about the request. * * @return bool * @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(), ) ); } $folder = new BP_Document_Folder( $request [ 'id' ] ); if ( true === $retval && empty ( $folder ->id ) ) { $retval = new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && 'public' !== $folder ->privacy && true === $this ->bp_rest_check_folder_privacy_restriction( $folder ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, Restrict access to view this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the document folder `get_item` permissions check. * * @since 0.1.0 * * @param WP_REST_Request $request The request sent to the API. * @param bool $retval Returned value. */ return apply_filters( 'bp_rest_document_folder_get_item_permissions_check' , $retval , $request ); } /** * Create document folder. * * @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/document/folder Create Folder * @apiName CreateBBFolder * @apiGroup Document * @apiDescription Create Document Folder. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {string} title Folder Title. * @apiParam {Number} [group_id] A unique numeric ID for the Group. * @apiParam {Number} [parent] A unique numeric ID for the Parent Folder. * @apiParam {string=public,loggedin,friends,onlyme,grouponly} [privacy=public] Privacy of the Folder. */ public function create_item( $request ) { $args = array ( 'title' => wp_strip_all_tags( $request [ 'title' ] ), 'privacy' => $request [ 'privacy' ], ); if ( isset( $request [ 'group_id' ] ) && ! empty ( $request [ 'group_id' ] ) ) { $args [ 'group_id' ] = $request [ 'group_id' ]; $args [ 'privacy' ] = 'grouponly' ; } if ( isset( $request [ 'parent' ] ) && ! empty ( $request [ 'parent' ] ) ) { $args [ 'parent' ] = $request [ 'parent' ]; $parent_folder = new BP_Document_Folder( $args [ 'parent' ] ); $args [ 'privacy' ] = $parent_folder ->privacy; } /** * 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_document_folder_create_items_query_args' , $args , $request ); $folder_id = bp_folder_add( $args ); if ( is_wp_error( $folder_id ) ) { return $folder_id ; } $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $folder_id ) ) ); $folder = current( $folders [ 'folders' ] ); $fields_update = $this ->update_additional_fields_for_object( $folder , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a Document folder is created via the REST API. * * @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_document_folder_create_item' , $response , $request ); return $response ; } /** * Check if a given request has access to create a folder. * * @param WP_REST_Request $request Full data about the request. * * @return WP_Error|bool * @since 0.1.0 */ public function create_item_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to create a folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval && isset( $request [ 'group_id' ] ) && ! empty ( $request [ 'group_id' ] ) ) { if ( ! bp_is_active( 'groups' ) || groups_can_user_manage_document( bp_loggedin_user_id(), (int) $request [ 'group_id' ] ) ) { $retval = new WP_Error( 'bp_rest_invalid_permission' , __( 'You don\'t have a permission to create a folder inside this group.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } if ( true === $retval && isset( $request [ 'parent' ] ) && ! empty ( $request [ 'parent' ] ) ) { $parent_folder = new BP_Document_Folder( $request [ 'parent' ] ); if ( empty ( $parent_folder ->id ) ) { $retval = new WP_Error( 'bp_rest_invalid_parent_folder' , __( 'Invalid Parent Folder ID.' , 'buddyboss' ), array ( 'status' => 400, ) ); } elseif ( ! bp_folder_user_can_edit( $parent_folder ->id ) ) { $retval = new WP_Error( 'bp_rest_invalid_permission' , __( 'You don\'t have a permission to create a folder inside this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } /** * Filter the document folder `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_document_folder_create_items_permissions_check' , $retval , $request ); } /** * Update a folder. * * @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/document/folder/:id Update Folder * @apiName UpdateBBFolder * @apiGroup Document * @apiDescription Update a folder. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} id A unique numeric ID for the folder * @apiParam {string} [title] Folder title. * @apiParam {Number} [parent] A unique numeric ID for the parent folder. * @apiParam {Number} [group_id] A unique numeric ID for the Group. * @apiParam {string=public,loggedin,onlyme,friends,grouponly} [privacy] Privacy of the folder. */ public function update_item( $request ) { $id = $request [ 'id' ]; $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $id ) ) ); if ( empty ( $folders [ 'folders' ] ) ) { return new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } $folder = end ( $folders [ 'folders' ] ); $args = array ( 'id' => $folder ->id, 'user_id' => $folder ->user_id, 'title' => $folder ->title, 'group_id' => $folder ->group_id, 'parent' => $folder ->parent, 'privacy' => $folder ->privacy, ); if ( isset( $request [ 'title' ] ) && ! empty ( $request [ 'title' ] ) ) { $args [ 'title' ] = wp_strip_all_tags( $request [ 'title' ] ); } if ( isset( $request [ 'privacy' ] ) && ! empty ( $request [ 'privacy' ] ) ) { $args [ 'privacy' ] = $request [ 'privacy' ]; } if ( isset( $request [ 'group_id' ] ) && ! empty ( $request [ 'group_id' ] ) ) { $args [ 'group_id' ] = $request [ 'group_id' ]; $args [ 'privacy' ] = 'grouponly' ; } if ( isset( $request [ 'parent' ] ) && ! empty ( $request [ 'parent' ] ) ) { $args [ 'parent' ] = $request [ 'parent' ]; $parent_folder = new BP_Document_Folder( $args [ 'parent' ] ); $args [ 'privacy' ] = $parent_folder ->privacy; } /** * 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_document_folder_update_items_query_args' , $args , $request ); if ( isset( $request [ 'privacy' ] ) && ! empty ( $request [ 'privacy' ] ) ) { bp_document_update_privacy( $folder ->id, $request [ 'privacy' ], 'folder' ); } // Move folders. if ( (int) $args [ 'parent' ] !== (int) $folder ->parent ) { $folder_id = $folder ->id; $destination_folder_id = $args [ 'parent' ]; $group_id = $args [ 'group_id' ]; if ( (int) $folder_id > 0 ) { if ( ! bp_folder_user_can_edit( $folder_id ) ) { return new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, You don\'t have permission to move this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } if ( (int) $destination_folder_id > 0 ) { if ( ! bp_folder_user_can_edit( $destination_folder_id ) ) { return new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, You don\'t have permission to move this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } $fetch_children = bp_document_get_folder_children( $folder_id ); if ( ! empty ( $fetch_children ) ) { if ( in_array( $destination_folder_id , $fetch_children , true ) ) { return new WP_Error( 'bp_rest_invalid_move_folder' , __( 'Couldn’t move item because it\'s parent folder.' , 'buddyboss' ), array ( 'status' => 400, ) ); } } bp_document_move_folder_to_folder( $folder_id , $destination_folder_id , $group_id ); } $updated_folder_id = bp_folder_add( $args ); $status = true; if ( is_wp_error( $updated_folder_id ) ) { return $updated_folder_id ; } if ( empty ( $updated_folder_id ) ) { $status = false; } $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $updated_folder_id ) ) ); $folder = current( $folders [ 'folders' ] ); $fields_update = $this ->update_additional_fields_for_object( $folder , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); $response = new WP_REST_Response(); $response ->set_data( array ( 'updated' => $status , 'data' => $retval , ) ); /** * Fires after an document folder is updated via the REST API. * * @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_document_folder_update_item' , $response , $request ); return $response ; } /** * Check if a given request has access to update a document. * * @param WP_REST_Request $request Full details 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() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to update this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } $folder = new BP_Document_Folder( $request [ 'id' ] ); if ( true === $retval && empty ( $folder ->id ) ) { $retval = new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && ! bp_folder_user_can_edit( $folder ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to update this folder.' , 'buddyboss' ), array ( 'status' => 500, ) ); } if ( true === $retval && isset( $request [ 'group_id' ] ) && ! empty ( $request [ 'group_id' ] ) ) { if ( ! bp_is_active( 'groups' ) || groups_can_user_manage_document( bp_loggedin_user_id(), (int) $request [ 'group_id' ] ) ) { $retval = new WP_Error( 'bp_rest_invalid_permission' , __( 'You don\'t have a permission to edit a folder inside this group.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } /** * Filter the document to `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_document_update_item_permissions_check' , $retval , $request ); } /** * Delete a single Folder. * * @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/document/folder/:id Delete Folder * @apiName DeleteBBFolder * @apiGroup Document * @apiDescription Delete a single Folder. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} id A unique numeric ID for the folder. */ public function delete_item( $request ) { $id = $request [ 'id' ]; $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $id ) ) ); if ( empty ( $folders [ 'folders' ] ) ) { return new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } $previous = '' ; foreach ( $folders [ 'folders' ] as $folder ) { $previous = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); } if ( ! bp_folder_user_can_delete( $id ) ) { return WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to delete this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } $status = bp_folder_delete( array ( 'id' => $id ) ); // Build the response. $response = new WP_REST_Response(); $response ->set_data( array ( 'deleted' => $status , 'previous' => $previous , ) ); /** * Fires after a folder is deleted via the REST API. * * @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_document_folder_delete_item' , $response , $request ); return $response ; } /** * Checks if a given request has access to for the user. * * @param WP_REST_Request $request Full details about the request. * * @return 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 delete this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } $folder = new BP_Document_Folder( $request [ 'id' ] ); if ( true === $retval && empty ( $folder ->id ) ) { $retval = new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && ! bp_folder_user_can_delete( $folder ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to delete this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the document folder `delete_item` permissions check. * * @param bool $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_document_folder_delete_item_permissions_check' , $retval , $request ); } /** * Retrieve document folder tree. * * @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/document/folder/tree Folder tree * @apiName GetBBFoldersTree * @apiGroup Document * @apiDescription Retrieve Folder tree * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} [group_id] A unique numeric ID for the Group. */ public function folder_tree_items( $request ) { global $wpdb , $bp ; $group_id = $request ->get_param( 'group_id' ); $user_id = bp_loggedin_user_id(); if ( empty ( $group_id ) ) { $group_id = 0; } // phpcs:ignore $documents_folder_query = $wpdb ->prepare( "SELECT * FROM {$bp->document->table_name_folder} WHERE user_id = %d AND group_id = %d ORDER BY id DESC" , $user_id , $group_id ); // phpcs:ignore $data = $wpdb ->get_results( $documents_folder_query , ARRAY_A ); // db call ok; no-cache ok. if ( ! empty ( $data ) ) { // Build array of item references. foreach ( $data as $key => & $item ) { $items_by_reference [ $item [ 'id' ] ] = & $item ; // Children array. $items_by_reference [ $item [ 'id' ] ][ 'children' ] = array (); } } if ( ! empty ( $data ) ) { // Set items as children of the relevant parent item. foreach ( $data as $key => & $item ) { if ( $item [ 'parent' ] && isset( $items_by_reference [ $item [ 'parent' ] ] ) ) { $items_by_reference [ $item [ 'parent' ] ][ 'children' ][] = & $item ; } } } if ( ! empty ( $data ) ) { // Remove items that were added to parents elsewhere. foreach ( $data as $key => & $item ) { if ( $item [ 'parent' ] && isset( $items_by_reference [ $item [ 'parent' ] ] ) ) { unset( $data [ $key ] ); } } } if ( ! empty ( $data ) ) { $data = array_values ( $data ); } $response = rest_ensure_response( $data ); /** * Fires after a list of document's folder tree is fetched via the REST API. * * @since 0.1.0 * * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */ do_action( 'bp_rest_document_folder_tree_items' , $response , $request ); return $response ; } /** * Checks if a given request has access to view the folder tree. * * @param WP_REST_Request $request Full details about the request. * * @return bool * @since 0.1.0 */ public function folder_tree_items_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to view folder tree.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the folder tree `folder_tree_items` permissions check. * * @param bool $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_document_folder_tree_items_permissions_check' , $retval , $request ); } /** * Get document folders. * * @param array|string $args All arguments and defaults are shared with BP_Document_Folder::get(), * except for the following. * * @return array */ public function assemble_response_data( $args ) { // Fetch specific document items based on ID's. if ( isset( $args [ 'folder_ids' ] ) && ! empty ( $args [ 'folder_ids' ] ) ) { return bp_folder_get_specific( $args ); // Fetch all activity items. } else { return bp_folder_get( $args ); } } /** * Select the item schema arguments needed for the CREATABLE 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 = array (); $key = 'create' ; $args [ 'title' ] = array ( 'description' => __( 'Folder Title.' , 'buddyboss' ), 'type' => 'string' , 'required' => true, 'sanitize_callback' => 'sanitize_text_field' , 'validate_callback' => 'rest_validate_request_arg' , ); $args [ 'parent' ] = array ( 'description' => __( 'A unique numeric ID for the parent folder.' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $args [ 'group_id' ] = array ( 'description' => __( 'A unique numeric ID for the Group.' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $args [ 'privacy' ] = array ( 'description' => __( 'Privacy of the folder.' , 'buddyboss' ), 'type' => 'string' , 'enum' => array ( 'public' , 'loggedin' , 'friends' , 'onlyme' , 'grouponly' ), 'default' => 'public' , 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ); if ( WP_REST_Server::EDITABLE === $method ) { $key = 'edit' ; $args [ 'id' ] = array ( 'description' => __( 'A unique numeric ID for the folder' , 'buddyboss' ), 'type' => 'integer' , 'required' => true, 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $args [ 'title' ][ 'required' ] = false; } /** * 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_document_folder_{$key}_query_arguments" , $args , $method ); } /** * Get the document folder schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'bp_document_folder' , 'type' => 'object' , 'properties' => array (), ); $schema [ 'properties' ] = $this ->document_endpoint->get_item_schema()[ 'properties' ]; /** * Filters the document schema. * * @param array $schema The endpoint schema. */ return apply_filters( 'bp_rest_document_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 [ 'order' ] = array ( 'description' => __( 'Order sort attribute ascending or descending.' , 'buddyboss' ), 'default' => 'desc' , 'type' => 'string' , 'enum' => array ( 'asc' , 'desc' ), 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'orderby' ] = array ( 'description' => __( 'Order folders by which attribute.' , 'buddyboss' ), 'default' => 'date_created' , 'type' => 'string' , 'enum' => array ( 'id' , 'title' , 'date_created' , 'user_id' , 'group_id' , 'privacy' ), 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'max' ] = array ( 'description' => __( 'Maximum number of results to return' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'user_id' ] = array ( 'description' => __( 'Limit results to a specific user.' , 'buddyboss' ), 'default' => 0, 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'parent' ] = array ( 'description' => __( 'A unique numeric ID for the parent Folder.' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'group_id' ] = array ( 'description' => __( 'A unique numeric ID for the Group.' , 'buddyboss' ), 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'privacy' ] = array ( 'description' => __( 'Privacy of the folder.' , 'buddyboss' ), 'type' => 'array' , 'items' => array ( 'enum' => array ( 'public' , 'loggedin' , 'friends' , 'onlyme' , 'grouponly' ), 'type' => 'string' , ), 'sanitize_callback' => 'bp_rest_sanitize_string_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'include' ] = array ( 'description' => __( 'Ensure result set includes specific folder IDs.' , 'buddyboss' ), 'default' => array (), 'type' => 'array' , 'items' => array ( 'type' => 'integer' ), 'sanitize_callback' => 'wp_parse_id_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'exclude' ] = array ( 'description' => __( 'Ensure result set excludes specific folder IDs.' , 'buddyboss' ), 'default' => array (), 'type' => 'array' , 'items' => array ( 'type' => 'integer' ), 'sanitize_callback' => 'wp_parse_id_list' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'count_total' ] = array ( 'description' => __( 'Show total count or not.' , 'buddyboss' ), 'default' => true, 'type' => 'boolean' , ); /** * Filters the collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_document_folder_collection_params' , $params ); } /** * Check user access based on the privacy for the single folder. * * @param BP_Document_Folder $folder Document Folder object. * * @return bool */ protected function bp_rest_check_folder_privacy_restriction( $folder ) { return ( 'onlyme' === $folder ->privacy && bp_loggedin_user_id() !== $folder ->user_id ) || ( 'loggedin' === $folder ->privacy && empty ( bp_loggedin_user_id() ) ) || ( bp_is_active( 'groups' ) && 'grouponly' === $folder ->privacy && ! empty ( $folder ->group_id ) && 'public' !== bp_get_group_status( groups_get_group( $folder ->group_id ) ) && empty ( groups_is_user_admin( bp_loggedin_user_id(), $folder ->group_id ) ) && empty ( groups_is_user_mod( bp_loggedin_user_id(), $folder ->group_id ) ) && empty ( groups_is_user_member( bp_loggedin_user_id(), $folder ->group_id ) ) ) || ( bp_is_active( 'friends' ) && 'friends' === $folder ->privacy && ! empty ( $folder ->user_id ) && bp_loggedin_user_id() !== $folder ->user_id && 'is_friend' !== friends_check_friendship_status( $folder ->user_id, bp_loggedin_user_id() ) ); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- assemble_response_data — Get document folders.
- bp_rest_check_folder_privacy_restriction — Check user access based on the privacy for the single folder.
- create_item — Create document folder.
- create_item_permissions_check — Check if a given request has access to create a folder.
- delete_item — Delete a single Folder.
- delete_item_permissions_check — Checks if a given request has access to for the user.
- folder_tree_items — Retrieve document folder tree.
- folder_tree_items_permissions_check — Checks if a given request has access to view the folder tree.
- get_collection_params — Get the query params for collections.
- get_endpoint_args_for_item_schema — Select the item schema arguments needed for the CREATABLE methods.
- get_item — Retrieve a single Folder.
- get_item_permissions_check — Checks if a given request has access to get all users.
- get_item_schema — Get the document folder schema, conforming to JSON Schema.
- get_items — Retrieve document folders.
- get_items_permissions_check — Checks if a given request has access to get all users.
- register_routes — Register the component routes.
- update_item — Update a folder.
- update_item_permissions_check — Check if a given request has access to update a document.
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.