GravityView  2.5
The best, easiest way to display Gravity Forms entries on your website.
class-gv-entry-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 Entry class implementation.
11  *
12  * Accessible as an array for back-compatibility.
13  */
14 class GF_Entry extends Entry implements \ArrayAccess {
15 
16  /**
17  * @var string The identifier of the backend used for this entry.
18  * @api
19  * @since 2.0
20  */
21  public static $backend = 'gravityforms';
22 
23  /**
24  * Initialization.
25  */
26  private function __construct() {
27  if ( ! class_exists( 'GFAPI' ) ) {
28  gravityview()->log->error( 'Gravity Forms plugin not active.' );
29  }
30  }
31 
32  /**
33  * Construct a \GV\Entry instance by ID.
34  *
35  * @param int|string $entry_id The internal entry ID.
36  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
37  *
38  * @api
39  * @since 2.0
40  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
41  */
42  public static function by_id( $entry_id, $form_id = 0 ) {
43  $entry = null;
44 
45  /** Always try to grab by numeric ID first. */
46  if ( is_numeric( $entry_id ) ) {
47  $entry = \GFAPI::get_entry( $entry_id );
48  }
49 
50  if ( ! $entry || is_wp_error( $entry ) ) {
51  /** Hmm, slugs? Must be. */
52  if ( apply_filters( 'gravityview_custom_entry_slug', false ) ) {
53  return self::by_slug( $entry_id, $form_id );
54  }
55 
56  return null;
57  }
58 
59  return self::from_entry( $entry );
60  }
61 
62  /**
63  * Construct a \GV\Entry instance by slug name.
64  *
65  * @param int|string $entry_slug The registered slug for the entry.
66  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
67  *
68  * @api
69  * @since 2.0
70  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
71  */
72  public static function by_slug( $entry_slug, $form_id = 0 ) {
73  global $wpdb;
74 
75  if ( version_compare( \GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
76  $entry_meta = \GFFormsModel::get_entry_meta_table_name();
77  $sql = "SELECT entry_id FROM $entry_meta";
78  } else {
79  $lead_meta = \GFFormsModel::get_lead_meta_table_name();
80  $sql = "SELECT lead_id FROM $lead_meta";
81  }
82 
83  $sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND";
84 
85 
86  if ( $form_id = apply_filters( 'gravityview/common/get_entry_id_from_slug/form_id', $form_id ) ) {
87  $sql = $wpdb->prepare( "$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id );
88  } else {
89  $sql = $wpdb->prepare( "$sql meta_value = %s", $entry_slug );
90  }
91 
92  $entry_id = $wpdb->get_var( $sql );
93 
94  if ( ! is_numeric( $entry_id ) ) {
95  return null;
96  }
97 
98  return self::by_id( $entry_id );
99  }
100 
101  /**
102  * Construct a \GV\Entry instance from a Gravity Forms entry array.
103  *
104  * @param array $entry The entry array
105  *
106  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
107  */
108  public static function from_entry( $entry ) {
109  if ( empty( $entry['id'] ) ) {
110  return null;
111  }
112 
113  $self = new self();
114  $self->entry = $entry;
115 
116  $self->ID = $self->entry['id'];
117  $self->slug = \GravityView_API::get_entry_slug( $self->ID, $self->as_entry() );
118 
119  return $self;
120  }
121 
122  /**
123  * ArrayAccess compatibility layer with a Gravity Forms entry array.
124  *
125  * @internal
126  * @deprecated
127  * @since 2.0
128  * @return bool Whether the offset exists or not.
129  */
130  public function offsetExists( $offset ) {
131  return isset( $this->entry[$offset] );
132  }
133 
134  /**
135  * ArrayAccess compatibility layer with a Gravity Forms entry array.
136  *
137  * Maps the old keys to the new data;
138  *
139  * @internal
140  * @deprecated
141  * @since 2.0
142  *
143  * @return mixed The value of the requested entry data.
144  */
145  public function offsetGet( $offset ) {
146  return $this->entry[$offset];
147  }
148 
149  /**
150  * ArrayAccess compatibility layer with a Gravity Forms entry array.
151  *
152  * @internal
153  * @deprecated
154  * @since 2.0
155  *
156  * @return void
157  */
158  public function offsetSet( $offset, $value ) {
159  gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
160  }
161 
162  /**
163  * ArrayAccess compatibility layer with a Gravity Forms entry array.
164  *
165  * @internal
166  * @deprecated
167  * @since 2.0
168  * @return void
169  */
170  public function offsetUnset( $offset ) {
171  gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
172  }
173 }
static by_slug( $entry_slug, $form_id=0)
Construct a instance by slug name.
__construct()
Initialization.
gravityview()
Definition: _stubs.php:26
static from_entry( $entry)
Construct a instance from a Gravity Forms entry array.
offsetExists( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.
offsetGet( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.
If this file is called directly, abort.
offsetSet( $offset, $value)
ArrayAccess compatibility layer with a Gravity Forms entry array.
if(empty( $created_by)) $form_id
If this file is called directly, abort.
static get_entry_slug( $id_or_string, $entry=array())
Get the entry slug for the entry.
Definition: class-api.php:497
static by_id( $entry_id, $form_id=0)
Construct a instance by ID.
$entry_slug
Definition: notes.php:30
$entry
Definition: notes.php:27
offsetUnset( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.