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

Description

Parameters

$args

(array) (Optional)

Default value: array()

Return

(null[])

Source

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

	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.