5 if ( ! defined(
'GRAVITYVIEW_DIR' ) ) {
23 private $callbacks = array();
28 public $filters = array();
33 public $sorts = array();
48 public $current_page = 1;
53 private $fetched = -1;
66 $this->fetched = max( 0, $this->fetched );
67 gravityview()->log->error(
'Entry_Collections can only contain objects of type \GV\Entry.' );
71 $this->fetched = max( 1, $this->fetched + 1 );
85 public function get( $entry_id, $backend =
'gravityforms' ) {
86 foreach ( $this->all() as
$entry ) {
87 if (
$entry->ID == $entry_id ) {
106 foreach ( $this->callbacks as $callback ) {
107 if ( $callback[0] !=
'count' ) {
111 $total += $callback[1]( $this->filters );
115 $total = parent::count();
118 return $total - $this->offset;
130 if ( $this->fetched >= 0 || parent::count() ) {
131 return parent::all();
133 return $this->fetch()->all();
149 foreach ( $this->all() as
$entry ) {
150 $entry = $entry->as_entry();
151 $result[] = Utils::get( $entry, $key, null );
166 if ( $this->fetched >= 0 || parent::count() ) {
167 return parent::last();
169 return $this->fetch()->last();
181 if ( $this->fetched >= 0 || parent::count() ) {
182 return parent::first();
184 return $this->fetch()->first();
196 if ( $this->fetched >= 0 ) {
203 $offset = new \GV\Entry_Offset();
204 $offset->limit = $this->limit;
205 $offset->offset = ( $this->limit * ( $this->current_page - 1 ) ) + $this->offset;
208 foreach ( $this->callbacks as
$i => $callback ) {
209 if ( $callback[0] !=
'fetch' ) {
213 $this->merge( $callback[1]( $this->filters, $this->sorts, $offset ) );
216 $this->fetched = parent::count();
234 $collection = clone $this;
235 $collection->clear();
237 array_push( $collection->filters, $filter );
252 public function sort( $sort ) {
253 $collection = clone $this;
254 $collection->clear();
256 array_push( $collection->sorts, $sort );
272 $collection = clone $this;
273 $collection->clear();
274 $collection->limit = $limit;
293 $collection = clone $this;
294 $collection->clear();
295 $collection->offset = $offset;
306 public function page( $page ) {
307 $collection = clone $this;
308 $collection->clear();
309 $collection->current_page = $page;
334 $this->add_callback(
'fetch', $callback );
354 $this->add_callback(
'count', $callback );
365 if ( ! is_callable( $callback ) ) {
369 $this->callbacks []= array( $type, $callback );
all()
Get the entries as an array.
add_callback( $type, $callback)
Add a callback for lazy loading/counting.
offset( $offset)
Add an $offset to these entries.
If this file is called directly, abort.
pluck( $key)
Pluck by key.
add( $entry)
Add an to this collection.
last()
Get the last in this collection.
page( $page)
Set the current page.
total()
Count the total number of objects that are possible to get.
filter(\GV\Entry_Filter $filter)
Apply a filter to the current collection.
If this file is called directly, abort.
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
limit( $limit)
Limit the fetch to a specified window.
If this file is called directly, abort.
add_fetch_callback( $callback)
Defer fetching of data to the provided callable.
fetch()
Hydrate this collection now.
first()
Get the first in this collection.
add_count_callback( $callback)
Defer counting of data to the provided callable.