BP_Document::get( array $args = array() )
Get document items, as specified by parameters.
Description
Parameters
- $args
-
(Optional) An array of arguments. All items are optional
Default value: array()
Return
(array) The array returned has two keys: - 'total' is the count of located documents - 'documents' is an array of the located documents
Source
File: bp-document/classes/class-bp-document.php
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 | 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 ; } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.4.0 | Introduced. |
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.