BP_Document::documents( array $args = array() )

Description

Parameters

$args

(Optional)

Default value: array()

Return

(null[])

Source

File: bp-document/classes/class-bp-document.php

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
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;
}

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.