GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-form-gravityforms.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The Gravity Forms Form class implementation.
11  *
12  * Accessible as an array for back-compatibility.
13  */
14 class GF_Form extends Form implements \ArrayAccess {
15 
16  /**
17  * @var string The identifier of the backend used for this form.
18  * @api
19  * @since 2.0
20  */
21  public static $backend = self::BACKEND_GRAVITYFORMS;
22 
23  /**
24  * Initialization.
25  */
26  private function __construct() {
27  if ( ! class_exists( 'GFAPI' ) ) {
28  gravityview()->log->error( 'Gravity Forms plugin is not active.' );
29  }
30  }
31 
32  /**
33  * Construct a \GV\GF_Form instance by ID.
34  *
35  * @param int|string $form_id The internal form ID.
36  *
37  * @api
38  * @since 2.0
39  * @return \GV\GF_Form|null An instance of this form or null if not found.
40  */
41  public static function by_id( $form_id ) {
42 
43  static $forms = array();
44 
45  $form = isset( $forms[ $form_id ] ) ? $forms[ $form_id ] : \GFAPI::get_form( $form_id );
46 
47  $forms[ $form_id ] = $form;
48 
49  if ( ! $form ) {
50  return null;
51  }
52 
53  $self = new self();
54  $self->form = $form;
55 
56  $self->ID = intval( $self->form['id'] );
57 
58  return $self;
59  }
60 
61  /**
62  * Construct a \GV\Form instance from a Gravity Forms form array.
63  *
64  * @since 2.0.7
65  *
66  * @param array $form The form array
67  *
68  * @return \GV\GF_Form|null An instance of this form or null if not found.
69  */
70  public static function from_form( $form ) {
71  if ( empty( $form['id'] ) ) {
72  return null;
73  }
74 
75  $self = new self();
76  $self->form = $form;
77  $self->ID = $self->form['id'];
78 
79  return $self;
80  }
81 
82  /**
83  * Get all entries for this form.
84  *
85  * @api
86  * @since 2.0
87  *
88  * @return \GV\Entry_Collection The \GV\Entry_Collection
89  */
90  public function get_entries() {
91  $entries = new \GV\Entry_Collection();
92 
93  $form = &$this;
94 
95  /** Add the fetcher lazy callback. */
96  $entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) {
97  $entries = new \GV\Entry_Collection();
98 
99  $search_criteria = array();
100  $sorting = array();
101  $paging = array();
102 
103  /** Apply the filters */
104  foreach ( $filters as $filter ) {
105  $search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
106  }
107 
108  /** Apply the sorts */
109  foreach ( $sorts as $sort ) {
110  /** Gravity Forms does not have multi-sorting, so just overwrite. */
111  $sorting = array(
112  'key' => $sort->field->ID,
113  'direction' => $sort->direction,
114  'is_numeric' => $sort->mode == Entry_Sort::NUMERIC,
115  );
116  }
117 
118  /** The offset and limit */
119  if ( ! empty( $offset->limit ) ) {
120  $paging['page_size'] = $offset->limit;
121  }
122 
123  if ( ! empty( $offset->offset ) ) {
124  $paging['offset'] = $offset->offset;
125  }
126 
127  foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) {
128  $entries->add( \GV\GF_Entry::from_entry( $entry ) );
129  }
130 
131  return $entries;
132  } );
133 
134  /** Add the counter lazy callback. */
135  $entries->add_count_callback( function( $filters ) use ( $form ) {
136  $search_criteria = array();
137  $sorting = array();
138 
139  /** Apply the filters */
140  /** @type \GV\GF_Entry_Filter|\GV\Entry_Filter $filter */
141  foreach ( $filters as $filter ) {
142  $search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
143  }
144 
145  return \GFAPI::count_entries( $form->ID, $search_criteria );
146  } );
147 
148  return $entries;
149  }
150 
151  /**
152  * Get a \GV\Field by Form and Field ID for this data source.
153  *
154  * @param \GV\GF_Form $form The Gravity Form form ID.
155  * @param int $field_id The Gravity Form field ID for the $form_id.
156  *
157  * @return \GV\Field|null The requested field or null if not found.
158  */
159  public static function get_field( /** varargs */ ) {
160  $args = func_get_args();
161 
162  if ( ! is_array( $args ) || count( $args ) != 2 ) {
163  gravityview()->log->error( '{source} expects 2 arguments for ::get_field ($form, $field_id)', array( 'source' => __CLASS__ ) );
164  return null;
165  }
166 
167  /** Unwrap the arguments. */
168  list( $form, $field_id ) = $args;
169 
170  /** Wrap it up into a \GV\Field. */
171  return GF_Field::by_id( $form, $field_id );
172  }
173 
174  /**
175  * Get an array of GV Fields for this data source
176  *
177  * @return \GV\Field[]|array Empty array if no fields
178  */
179  public function get_fields() {
180  $fields = array();
181  foreach ( $this['fields'] as $field ) {
182  foreach ( empty( $field['inputs'] ) ? array( $field['id'] ) : wp_list_pluck( $field['inputs'], 'id' ) as $id ) {
183  if ( is_numeric( $id ) ) {
184  $fields[ $id ] = self::get_field( $this, $id );
185  } else {
186  $fields[ $id ] = Internal_Field::by_id( $id );
187  }
188  }
189  }
190 
191  return array_filter( $fields );
192  }
193 
194  /**
195  * Proxies.
196  *
197  * @param string $key The property to get.
198  *
199  * @return mixed
200  */
201  public function __get( $key ) {
202  switch ( $key ) {
203  case 'fields':
204  return $this->get_fields();
205  default:
206  return parent::__get( $key );
207  }
208  }
209 
210  /**
211  * ArrayAccess compatibility layer with a Gravity Forms form array.
212  *
213  * @internal
214  * @deprecated
215  * @since 2.0
216  * @return bool Whether the offset exists or not.
217  */
218  #[\ReturnTypeWillChange]
219  public function offsetExists( $offset ) {
220  return isset( $this->form[$offset] );
221  }
222 
223  /**
224  * ArrayAccess compatibility layer with a Gravity Forms form array.
225  *
226  * Maps the old keys to the new data;
227  *
228  * @internal
229  * @deprecated
230  * @since 2.0
231  *
232  * @return mixed The value of the requested form data.
233  */
234  #[\ReturnTypeWillChange]
235  public function offsetGet( $offset ) {
236  return $this->form[$offset];
237  }
238 
239  /**
240  * ArrayAccess compatibility layer with a Gravity Forms form array.
241  *
242  * @internal
243  * @deprecated
244  * @since 2.0
245  *
246  * @return void
247  */
248  #[\ReturnTypeWillChange]
249  public function offsetSet( $offset, $value ) {
250  gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
251  }
252 
253  /**
254  * ArrayAccess compatibility layer with a Gravity Forms form array.
255  *
256  * @internal
257  * @deprecated
258  * @since 2.0
259  * @return void
260  */
261  #[\ReturnTypeWillChange]
262  public function offsetUnset( $offset ) {
263  gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
264  }
265 }
offsetExists( $offset)
ArrayAccess compatibility layer with a Gravity Forms form array.
$forms
Definition: data-source.php:19
static get_field()
Get a by Form and Field ID for this data source.
__construct()
Initialization.
$entries
if(gravityview() ->plugin->is_GF_25()) $form
static from_form( $form)
Construct a instance from a Gravity Forms form array.
offsetGet( $offset)
ArrayAccess compatibility layer with a Gravity Forms form array.
If this file is called directly, abort.
offsetSet( $offset, $value)
ArrayAccess compatibility layer with a Gravity Forms form array.
static by_id( $form_id)
Construct a instance by ID.
if(empty( $created_by)) $form_id
offsetUnset( $offset)
ArrayAccess compatibility layer with a Gravity Forms form array.
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
get_entries()
Get all entries for this form.
$entry
Definition: notes.php:27
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
get_fields()
Get an array of GV Fields for this data source.